Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC

Commits:

6 changed files:

Changes:

  • compiler/GHC/Tc/Gen/App.hs
    ... ... @@ -458,45 +458,9 @@ checkResultTy :: HsExpr GhcRn
    458 458
                                 --   expose foralls, but maybe not /deeply/ instantiated
    
    459 459
                   -> ExpRhoType -- Expected type; this is deeply skolemised
    
    460 460
                   -> TcM HsWrapper
    
    461
    -checkResultTy rn_expr (tc_fun,_) _ app_res_rho (Infer inf_res)
    
    462
    -  = do { ds_flag <- getDeepSubsumptionFlag_DataConHead tc_fun
    
    463
    -         -- Why the "DataConHead" bit?  See (IIR5) in
    
    464
    -         -- Note [Instantiation of InferResult] in GHC.Tc.Utils.Unify.
    
    465
    -       ; fillInferResult ds_flag (exprCtOrigin rn_expr) app_res_rho inf_res }
    
    466
    -
    
    467
    -checkResultTy rn_expr (tc_fun, fun_loc) inst_args app_res_rho (Check res_ty)
    
    468
    --- Unify with expected type from the context
    
    469
    --- See Note [Unify with expected type before typechecking arguments]
    
    470
    ---
    
    471
    --- Match up app_res_rho: the result type of rn_expr
    
    472
    ---     with res_ty:  the expected result type
    
    461
    +checkResultTy rn_expr (tc_fun, fun_loc) inst_args app_res_rho res_ty
    
    473 462
      = perhaps_add_res_ty_ctxt $
    
    474
    -   do { ds_flag <- getDeepSubsumptionFlag_DataConHead tc_fun
    
    475
    -      ; traceTc "checkResultTy {" $
    
    476
    -          vcat [ text "tc_fun:" <+> ppr tc_fun
    
    477
    -               , text "app_res_rho:" <+> ppr app_res_rho
    
    478
    -               , text "res_ty:"  <+> ppr res_ty
    
    479
    -               , text "ds_flag:" <+> ppr ds_flag ]
    
    480
    -      ; case ds_flag of
    
    481
    -          Shallow -> -- No deep subsumption
    
    482
    -             -- app_res_rho and res_ty are both rho-types,
    
    483
    -             -- so with simple subsumption we can just unify them
    
    484
    -             -- No need to zonk; the unifier does that
    
    485
    -             do { co <- unifyExprType rn_expr app_res_rho res_ty
    
    486
    -                ; traceTc "checkResultTy 1 }" (ppr co)
    
    487
    -                ; return (mkWpCastN co) }
    
    488
    -
    
    489
    -          Deep ds_reason ->   -- Deep subsumption
    
    490
    -             -- Even though both app_res_rho and res_ty are rho-types,
    
    491
    -             -- they may have nested polymorphism, so if deep subsumption
    
    492
    -             -- is on we must call tcSubType.
    
    493
    -             do { wrap <- tcSubTypeDS tc_fun ds_reason rn_expr app_res_rho res_ty
    
    494
    -                ; traceTc "checkResultTy 2 }" $
    
    495
    -                   vcat [ text "app_res_rho:" <+> ppr app_res_rho
    
    496
    -                        , text "res_ty:" <+> ppr res_ty
    
    497
    -                        , text "wrap:" <+> ppr wrap
    
    498
    -                        ]
    
    499
    -                ; return wrap } }
    
    463
    +   tcSubTypeApp rn_expr tc_fun app_res_rho res_ty
    
    500 464
       where
    
    501 465
         -- perhaps_add_res_ty_ctxt: Inside an expansion, the addFunResCtxt stuff is
    
    502 466
         -- more confusing than helpful because the function at the head isn't in
    
    ... ... @@ -506,7 +470,7 @@ checkResultTy rn_expr (tc_fun, fun_loc) inst_args app_res_rho (Check res_ty)
    506 470
           | isGeneratedSrcSpan fun_loc
    
    507 471
           = thing_inside
    
    508 472
           | otherwise
    
    509
    -      = addFunResCtxt tc_fun inst_args app_res_rho (mkCheckExpType res_ty) $
    
    473
    +      = addFunResCtxt tc_fun inst_args app_res_rho res_ty $
    
    510 474
             thing_inside
    
    511 475
     
    
    512 476
     ----------------
    

  • compiler/GHC/Tc/Gen/Head.hs
    ... ... @@ -791,10 +791,9 @@ nonBidirectionalErr = TcRnPatSynNotBidirectional
    791 791
     
    
    792 792
     {- Note [Typechecking data constructors]
    
    793 793
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    794
    -As per Note [Polymorphisation of linear fields] in
    
    795
    -GHC.Core.Multiplicity, when we use a data constructor as a term, we want to
    
    796
    -consider its field to have polymorphic multiplicities. That is,
    
    797
    -Note [Data constructors are linear by default] says:
    
    794
    +As per Note [Polymorphisation of linear fields] in GHC.Core.Multiplicity, when
    
    795
    +we use a data constructor as a term, we want to consider its field to have
    
    796
    +polymorphic multiplicities. Note [Data constructors are linear by default] says:
    
    798 797
     
    
    799 798
         Just :: a. a %1 -> Maybe a
    
    800 799
     
    

  • compiler/GHC/Tc/Utils/Unify.hs
    ... ... @@ -11,7 +11,7 @@
    11 11
     module GHC.Tc.Utils.Unify (
    
    12 12
       -- Full-blown subsumption
    
    13 13
       tcWrapResult, tcWrapResultO, tcWrapResultMono,
    
    14
    -  tcSubType, tcSubTypeSigma, tcSubTypePat, tcSubTypeDS, tcSubTypeHoleFit,
    
    14
    +  tcSubType, tcSubTypeSigma, tcSubTypePat, tcSubTypeApp, tcSubTypeHoleFit,
    
    15 15
       addSubTypeCtxt,
    
    16 16
       tcSubTypeAmbiguity, tcSubMult,
    
    17 17
       checkConstraints, checkTvConstraints,
    
    ... ... @@ -1488,24 +1488,62 @@ tcSubTypePat _ _ (Infer inf_res) ty_expected
    1488 1488
     
    
    1489 1489
     ---------------
    
    1490 1490
     
    
    1491
    --- | A subtype check that performs deep subsumption.
    
    1492
    --- See also 'tcSubTypeMono', for when no instantiation is required.
    
    1493
    -tcSubTypeDS :: HsExpr GhcTc -- ^ App head (for error messages only)
    
    1494
    -            -> DeepSubsumptionDepth
    
    1495
    -            -> HsExpr GhcRn
    
    1496
    -            -> TcRhoType   -- ^ Actual type; a rho-type, not a sigma-type
    
    1497
    -            -> TcRhoType   -- ^ Expected type
    
    1498
    -                           -- DeepSubsumption <=> when checking, this type
    
    1499
    -                           --                     is deeply skolemised
    
    1500
    -            -> TcM HsWrapper
    
    1501
    --- Only one call site, in GHC.Tc.Gen.App.checkResultTy
    
    1502
    -tcSubTypeDS tc_fun ds_depth rn_expr act_rho exp_rho
    
    1503
    -  = do { wrap <- tc_sub_type_deep (Just tc_fun, Top) ds_depth
    
    1504
    -                   (unifyExprType rn_expr)
    
    1505
    -                   (exprCtOrigin rn_expr)
    
    1506
    -                   GenSigCtxt act_rho exp_rho
    
    1491
    +-- | Connect up the inferred type of an application with the expected type.
    
    1492
    +-- This is usually just a unification, but with deep subsumption there is more to do.
    
    1493
    +tcSubTypeApp :: HsExpr GhcRn
    
    1494
    +             -> HsExpr GhcTc  -- Head
    
    1495
    +             -> TcRhoType     -- Inferred type of the application; zonked to
    
    1496
    +                              --   expose foralls, but maybe not /deeply/ instantiated
    
    1497
    +             -> ExpRhoType    -- Expected type; this is deeply skolemised
    
    1498
    +             -> TcM HsWrapper
    
    1499
    +tcSubTypeApp rn_expr tc_fun app_res_rho (Infer inf_res)
    
    1500
    +  = do { ds_flag <- getDeepSubsumptionFlag_DataConHead tc_fun
    
    1501
    +         -- Why the "DataConHead" bit?  See (IIR5) in
    
    1502
    +         -- Note [Instantiation of InferResult] in GHC.Tc.Utils.Unify.
    
    1503
    +       ; fillInferResult ds_flag (exprCtOrigin rn_expr) app_res_rho inf_res }
    
    1504
    +
    
    1505
    +tcSubTypeApp rn_expr tc_fun app_res_rho (Check exp_rho)
    
    1506
    + = do { ds_flag <- getDeepSubsumptionFlag_DataConHead tc_fun
    
    1507
    +      ; traceTc "tcSubTypeApp {" $
    
    1508
    +          vcat [ text "tc_fun:" <+> ppr tc_fun
    
    1509
    +               , text "app_res_rho:" <+> ppr app_res_rho
    
    1510
    +               , text "exp_rho:"  <+> ppr exp_rho
    
    1511
    +               , text "ds_flag:" <+> ppr ds_flag ]
    
    1512
    +      ; wrap <- case ds_flag of
    
    1513
    +          Shallow -- No deep subsumption
    
    1514
    +            -- app_res_rho and res_ty are both rho-types,
    
    1515
    +            -- so with simple subsumption we can just unify them
    
    1516
    +            -- No need to zonk; the unifier does that
    
    1517
    +            -> do { co <- unifyExprType rn_expr app_res_rho exp_rho
    
    1518
    +                  ; return (mkWpCastN co) }
    
    1519
    +
    
    1520
    +          Deep ds_depth -- Deep subsumption is ON
    
    1521
    +            -- Even though both app_res_rho and res_ty are rho-types,
    
    1522
    +            -- they may have nested polymorphism, so if deep subsumption
    
    1523
    +            -- is on we must call tcSubType.
    
    1524
    +            -> tc_sub_type_deep (Just tc_fun, Top) ds_depth
    
    1525
    +                                (unifyExprType rn_expr)
    
    1526
    +                                (exprCtOrigin rn_expr)
    
    1527
    +                                GenSigCtxt app_res_rho exp_rho
    
    1528
    +
    
    1529
    +       ; traceTc "tcSubTypeApp }" $
    
    1530
    +          vcat [ text "tc_fun:" <+> ppr tc_fun
    
    1531
    +               , text "wrap:" <+> ppr wrap ]
    
    1532
    +
    
    1507 1533
            ; return (mkWpSubType wrap) }
    
    1508 1534
     
    
    1535
    +-- | Variant of 'getDeepSubsumptionFlag' which enables a top-level subsumption
    
    1536
    +-- in order to implement the plan of Note [Typechecking data constructors].
    
    1537
    +getDeepSubsumptionFlag_DataConHead :: HsExpr GhcTc -> TcM DeepSubsumptionFlag
    
    1538
    +getDeepSubsumptionFlag_DataConHead app_head
    
    1539
    +  = do { user_ds <- xoptM LangExt.DeepSubsumption
    
    1540
    +       ; return $ if | user_ds
    
    1541
    +                     -> Deep DeepSub
    
    1542
    +                     | XExpr (ConLikeTc (RealDataCon {})) <- app_head
    
    1543
    +                     -> Deep TopSub
    
    1544
    +                     | otherwise
    
    1545
    +                     -> Shallow  }
    
    1546
    +
    
    1509 1547
     ---------------
    
    1510 1548
     
    
    1511 1549
     -- | Checks that the 'actual' type is more polymorphic than the 'expected' type.
    
    ... ... @@ -2104,18 +2142,6 @@ getDeepSubsumptionFlag =
    2104 2142
            else return Shallow
    
    2105 2143
          }
    
    2106 2144
     
    
    2107
    --- | Variant of 'getDeepSubsumptionFlag' which enables a top-level subsumption
    
    2108
    --- in order to implement the plan of Note [Typechecking data constructors].
    
    2109
    -getDeepSubsumptionFlag_DataConHead :: HsExpr GhcTc -> TcM DeepSubsumptionFlag
    
    2110
    -getDeepSubsumptionFlag_DataConHead app_head
    
    2111
    -  = do { user_ds <- xoptM LangExt.DeepSubsumption
    
    2112
    -       ; return $ if | user_ds
    
    2113
    -                     -> Deep DeepSub
    
    2114
    -                     | XExpr (ConLikeTc (RealDataCon {})) <- app_head
    
    2115
    -                     -> Deep TopSub
    
    2116
    -                     | otherwise
    
    2117
    -                     -> Shallow  }
    
    2118
    -
    
    2119 2145
     
    
    2120 2146
     -- | 'tc_sub_type_deep' is where the actual work happens for deep subsumption.
    
    2121 2147
     --
    
    ... ... @@ -2132,11 +2158,7 @@ tc_sub_type_deep :: HasDebugCallStack
    2132 2158
                      -> TcRhoType      -- ^ Expected; deeply skolemised
    
    2133 2159
                      -> TcM HsWrapper
    
    2134 2160
     tc_sub_type_deep fun_pos@(tc_fun, pos) ds_depth unify inst_orig ctxt ty_actual ty_expected
    
    2135
    -  = assertPpr (isDeeplySkolemised ty_expected)
    
    2136
    -     (vcat [ text "tc_sub_type_deep: expected type is not a deep rho type"
    
    2137
    -           , text "ty_expected:" <+> ppr ty_expected
    
    2138
    -           , text "ty_actual:" <+> ppr ty_actual
    
    2139
    -           ]) $
    
    2161
    +  = assert_precondition $
    
    2140 2162
         do { traceTc "tc_sub_type_deep" $
    
    2141 2163
              vcat [ text "ty_actual   =" <+> ppr ty_actual
    
    2142 2164
                   , text "ty_expected =" <+> ppr ty_expected ]
    
    ... ... @@ -2250,6 +2272,27 @@ tc_sub_type_deep fun_pos@(tc_fun, pos) ds_depth unify inst_orig ctxt ty_actual t
    2250 2272
           where
    
    2251 2273
             given_orig = GivenOrigin (SigSkol GenSigCtxt exp_arg [])
    
    2252 2274
     
    
    2275
    +    -- Assertion check.
    
    2276
    +    -- If DeepSubsumption is on (ds_depth = Deep DeepSub) then `exp_rho`
    
    2277
    +    --    should already be deeply skolemised; the assertion checks this
    
    2278
    +    -- But if DeepSubsumption is NOT on, but there is a data constructor at the
    
    2279
    +    --    head, we must still call `tc_sub_type_deep` (for the multiplicity arrows)
    
    2280
    +    --    Hence ds_flag = Deep TopSub, but `exp_rho` will only be /top-level/ skolemised
    
    2281
    +    --    So we can only check for top-level skolemisation (`isRhoTy`)
    
    2282
    +    -- Example of the latter (see #27210), with -XNoDeepSubsumption
    
    2283
    +    --     foo :: forall a. a -> forall b. b -> (a,b)
    
    2284
    +    --     foo = (,)
    
    2285
    +    -- We will only shallowly-skolemise the expected type
    
    2286
    +    assert_precondition = assertPpr ty_expected_is_ok $
    
    2287
    +                          vcat [ text "tc_sub_type_deep: expected type is not a deep rho type"
    
    2288
    +                               , text "ty_expected:" <+> ppr ty_expected
    
    2289
    +                               , text "ty_actual:" <+> ppr ty_actual ]
    
    2290
    +    ty_expected_is_ok
    
    2291
    +      = case ds_depth of
    
    2292
    +          TopSub  -> True
    
    2293
    +          DeepSub -> isDeeplySkolemised ty_expected
    
    2294
    +
    
    2295
    +
    
    2253 2296
     -- | Whether to do deep subsumption when recurring inside arguments.
    
    2254 2297
     recurInArgumentDSFlag :: DeepSubsumptionDepth -> DeepSubsumptionFlag
    
    2255 2298
     recurInArgumentDSFlag = \case
    
    ... ... @@ -5145,5 +5188,3 @@ lookupCycleBreakerVar cbv (IS { inert_cycle_breakers = cbvs_stack })
    5145 5188
       = tyfam_app
    
    5146 5189
       | otherwise
    
    5147 5190
       = pprPanic "lookupCycleBreakerVar found an unbound cycle breaker" (ppr cbv $$ ppr cbvs_stack)
    5148
    -
    
    5149
    ---------------------------------------------------------------------------------

  • testsuite/tests/typecheck/should_fail/T27210.hs
    1
    +{-# LANGUAGE RankNTypes, ExistentialQuantification #-}
    
    2
    +
    
    3
    +-- NB: No deep subsumption
    
    4
    +
    
    5
    +module T27210 where
    
    6
    +
    
    7
    +data Parser a
    
    8
    +  = forall x. BindP (Parser x) (x -> Parser a)
    
    9
    +
    
    10
    +oneM :: Parser a -> ( forall x. (a -> Parser x) -> Parser x )
    
    11
    +oneM = BindP

  • testsuite/tests/typecheck/should_fail/T27210.stderr
    1
    +T27210.hs:11:8: error: [GHC-83865]
    
    2
    +    • Couldn't match expected type: forall x.
    
    3
    +                                    (a -> Parser x) -> Parser x
    
    4
    +                  with actual type: (a -> Parser a0) -> Parser a0
    
    5
    +    • In the expression: BindP
    
    6
    +      In an equation for ‘oneM’: oneM = BindP
    
    7
    +    • Relevant bindings include
    
    8
    +        oneM :: Parser a -> forall x. (a -> Parser x) -> Parser x
    
    9
    +          (bound at T27210.hs:11:1)

  • testsuite/tests/typecheck/should_fail/all.T
    ... ... @@ -758,3 +758,4 @@ test('T23162d', normal, compile, [''])
    758 758
     test('T26823', normal, compile_fail, [''])
    
    759 759
     test('T26861', normal, compile_fail, [''])
    
    760 760
     test('T26862', normal, compile_fail, [''])
    
    761
    +test('T27210', normal, compile_fail, [''])