Apoorv Ingle pushed to branch wip/ani/no-ds-flag-cache at Glasgow Haskell Compiler / GHC

Commits:

12 changed files:

Changes:

  • compiler/GHC/Tc/Gen/App.hs
    ... ... @@ -11,7 +11,6 @@
    11 11
     
    
    12 12
     module GHC.Tc.Gen.App
    
    13 13
            ( tcApp
    
    14
    -       , tcExprSigma
    
    15 14
            , tcExprPrag ) where
    
    16 15
     
    
    17 16
     import {-# SOURCE #-} GHC.Tc.Gen.Expr( tcPolyExpr )
    
    ... ... @@ -165,34 +164,6 @@ Note [Instantiation variables are short lived]
    165 164
     -}
    
    166 165
     
    
    167 166
     
    
    168
    -{- *********************************************************************
    
    169
    -*                                                                      *
    
    170
    -              tcInferSigma
    
    171
    -*                                                                      *
    
    172
    -********************************************************************* -}
    
    173
    -
    
    174
    --- Very similar to tcApp, but returns a sigma (uninstantiated) type
    
    175
    --- CAUTION: Any changes to tcApp should be reflected here
    
    176
    --- cf. T19167. the head is an expanded expression applied to a type
    
    177
    --- Caution: Currently we assume that the expression is compiler generated/expanded
    
    178
    --- Because that is what T19167 test case expects.
    
    179
    --- This function should go away after MR!15778 lands
    
    180
    -tcExprSigma :: Bool -> CtOrigin -> HsExpr GhcRn -> TcM (HsExpr GhcTc, TcSigmaType)
    
    181
    -tcExprSigma inst fun_orig rn_expr
    
    182
    -  = do { (fun@(rn_fun,fun_lspan), rn_args) <- splitHsApps rn_expr
    
    183
    -       ; do_ql <- wantQuickLook rn_fun
    
    184
    -       ; (tc_fun, fun_sigma) <- tcInferAppHead fun
    
    185
    -       ; inGenCode <- inGeneratedCode
    
    186
    -       ; traceTc "tcExprSigma" (vcat [ text "rn_expr:" <+> ppr rn_expr
    
    187
    -                                     , text "tc_fun" <+> ppr tc_fun
    
    188
    -                                     , text "inGeneratedCode:" <+> ppr inGenCode])
    
    189
    -       ; (inst_args, app_res_sigma) <- tcInstFun do_ql inst (fun_orig, rn_fun, fun_lspan)
    
    190
    -                                           tc_fun fun_sigma rn_args
    
    191
    -       ; tc_args <- tcValArgs do_ql (rn_fun, fun_lspan) inst_args
    
    192
    -       ; let tc_expr = rebuildHsApps (tc_fun, fun_lspan) tc_args
    
    193
    -       ; return (tc_expr, app_res_sigma) }
    
    194
    -
    
    195
    -
    
    196 167
     {- *********************************************************************
    
    197 168
     *                                                                      *
    
    198 169
                   Typechecking n-ary applications
    
    ... ... @@ -488,8 +459,8 @@ checkResultTy :: HsExpr GhcRn
    488 459
                                 --   expose foralls, but maybe not /deeply/ instantiated
    
    489 460
                   -> ExpRhoType -- Expected type; this is deeply skolemised
    
    490 461
                   -> TcM HsWrapper
    
    491
    -checkResultTy rn_expr (tc_fun, _) _ app_res_rho (Infer inf_res)
    
    492
    -  = do { ds_flag <- getDeepSubsumptionFlag_DataConHead tc_fun
    
    462
    +checkResultTy rn_expr _ _ app_res_rho (Infer inf_res)
    
    463
    +  = do { ds_flag <- getDeepSubsumptionFlag
    
    493 464
            ; fillInferResult ds_flag (exprCtOrigin rn_expr) app_res_rho inf_res }
    
    494 465
     
    
    495 466
     
    
    ... ... @@ -539,7 +510,10 @@ checkResultTy rn_expr (tc_fun, fun_loc) inst_args app_res_rho (Check res_ty)
    539 510
             thing_inside
    
    540 511
     
    
    541 512
     ----------------
    
    542
    -tcValArgs :: QLFlag -> (HsExpr GhcRn, SrcSpan) -> [HsExprArg 'TcpInst] -> TcM [HsExprArg 'TcpTc]
    
    513
    +tcValArgs :: QLFlag
    
    514
    +          -> (HsExpr GhcRn, SrcSpan) -- Head of the application chain (used only for error message generation)
    
    515
    +          -> [HsExprArg 'TcpInst]
    
    516
    +          -> TcM [HsExprArg 'TcpTc]
    
    543 517
     -- Importantly, tcValArgs works left-to-right, so that by the time we
    
    544 518
     -- encounter an argument, we have monomorphised all the instantiation
    
    545 519
     -- variables that its type contains.  All that is left to do is an ordinary
    
    ... ... @@ -553,7 +527,7 @@ tcValArgs do_ql (fun, fun_lspan) args = go do_ql 0 args
    553 527
              ; return (arg' : args') }
    
    554 528
           where
    
    555 529
         -- increment position if the argument is user written type or value argument
    
    556
    -        pos' | EValArg{} <- arg
    
    530
    +        !pos' | EValArg{} <- arg
    
    557 531
                  = pos + 1
    
    558 532
                  | EValArgQL{} <- arg
    
    559 533
                  = pos + 1
    
    ... ... @@ -564,8 +538,11 @@ tcValArgs do_ql (fun, fun_lspan) args = go do_ql 0 args
    564 538
                  = pos
    
    565 539
     
    
    566 540
     
    
    567
    -tcValArg :: QLFlag -> Int -> (HsExpr GhcRn, SrcSpan) -> HsExprArg 'TcpInst    -- Actual argument
    
    568
    -         -> TcM (HsExprArg 'TcpTc)          -- Resulting argument
    
    541
    +tcValArg :: QLFlag                  -- ^ Are we typechecking with Quick Look turned on?
    
    542
    +         -> Int                     -- ^ Argument position (used only for error message generation)
    
    543
    +         -> (HsExpr GhcRn, SrcSpan) -- ^ Head of the application chain (used only for error message generation)
    
    544
    +         -> HsExprArg 'TcpInst      -- ^ Actual argument
    
    545
    +         -> TcM (HsExprArg 'TcpTc)  -- ^ Resulting argument
    
    569 546
     tcValArg _     _ _ (EPrag l p)         = return (EPrag l (tcExprPrag p))
    
    570 547
     tcValArg _     _ _ (ETypeArg l hty ty) = return (ETypeArg l hty ty)
    
    571 548
     tcValArg do_ql _ _ (EWrap (EHsWrap w)) = do { whenQL do_ql $ qlMonoHsWrapper w
    
    ... ... @@ -630,7 +607,8 @@ tcValArg _ pos (fun, fun_lspan) (EValArgQL {
    630 607
                                            , text "app_lspan" <+> ppr lspan
    
    631 608
                                            , text "head_lspan" <+> ppr fun_lspan
    
    632 609
                                            , text "tc_head" <+> ppr tc_head])
    
    633
    -       ; ds_flag <- getDeepSubsumptionFlag_DataConHead (fst tc_head)
    
    610
    +       ; ds_flag <- getDeepSubsumptionFlag
    
    611
    +         -- NB: whether to do deep /skolemisation/ is independent of data constructors
    
    634 612
            ; (wrap, arg')
    
    635 613
                 <- tcScalingUsage mult  $
    
    636 614
                    tcSkolemise ds_flag GenSigCtxt exp_arg_ty $ \ exp_arg_rho ->
    
    ... ... @@ -888,8 +866,7 @@ tcInstFun do_ql inst_final (fun_orig, rn_fun, fun_lspan) tc_fun fun_sigma rn_arg
    888 866
                     matchActualFunTy herald
    
    889 867
                       (Just $ HsExprTcThing tc_fun)
    
    890 868
                       (n_val_args, fun_sigma) fun_ty
    
    891
    -           ; ds_flag <- getDeepSubsumptionFlag_DataConHead tc_fun
    
    892
    -           ; arg' <- quickLookArg ds_flag do_ql pos ctxt (rn_fun, fun_lspan) arg arg_ty
    
    869
    +           ; arg' <- quickLookArg do_ql pos ctxt (rn_fun, fun_lspan) arg arg_ty
    
    893 870
                ; let acc' = arg' : addArgWrap (mkWpCastN fun_co) acc
    
    894 871
                ; go (pos+1) acc' res_ty rest_args }
    
    895 872
     
    
    ... ... @@ -910,7 +887,7 @@ tcInstFun do_ql inst_final (fun_orig, rn_fun, fun_lspan) tc_fun fun_sigma rn_arg
    910 887
         mk_herald :: HsExpr GhcTc -> HsExpr GhcRn -> ExpectedFunTyCtxt
    
    911 888
         mk_herald tc_fun arg
    
    912 889
           = case fun_orig of
    
    913
    -           DoStmtOrigin -> ExpectedTySyntax DoStmtOrigin arg
    
    890
    +           DoStmtOrigin -> ExpectedFunTySyntaxOp DoStmtOrigin tc_fun
    
    914 891
                _ -> ExpectedFunTyArg (HsExprTcThing tc_fun) arg
    
    915 892
     
    
    916 893
     -- Is the argument supposed to instantiate a forall?
    
    ... ... @@ -1877,17 +1854,18 @@ This turned out to be more subtle than I expected. Wrinkles:
    1877 1854
     
    
    1878 1855
     -}
    
    1879 1856
     
    
    1880
    -quickLookArg :: DeepSubsumptionFlag -> QLFlag -> Int
    
    1857
    +quickLookArg :: QLFlag -> Int
    
    1881 1858
                  -> SrcSpan -- ^ location span of the whole application
    
    1882 1859
                  -> (HsExpr GhcRn, SrcSpan) -- ^ Head of the application chain and its source span
    
    1883 1860
                  -> LHsExpr GhcRn          -- ^ Argument
    
    1884 1861
                  -> Scaled TcSigmaTypeFRR  -- ^ Type expected by the function
    
    1885 1862
                  -> TcM (HsExprArg 'TcpInst)
    
    1886 1863
     -- See Note [Quick Look at value arguments]
    
    1887
    -quickLookArg _ NoQL _ app_lspan _ larg orig_arg_ty
    
    1864
    +quickLookArg NoQL _ app_lspan _ larg orig_arg_ty
    
    1888 1865
       = skipQuickLook app_lspan larg orig_arg_ty
    
    1889
    -quickLookArg ds_flag DoQL pos app_lspan fun_and_lspan larg orig_arg_ty
    
    1890
    -  = do { is_rho <- tcIsDeepRho ds_flag (scaledThing orig_arg_ty)
    
    1866
    +quickLookArg DoQL pos app_lspan fun_and_lspan larg orig_arg_ty
    
    1867
    +  = do { ds_flag <- getDeepSubsumptionFlag
    
    1868
    +       ; is_rho <- tcIsDeepRho ds_flag (scaledThing orig_arg_ty)
    
    1891 1869
            ; traceTc "qla" (ppr orig_arg_ty $$ ppr is_rho)
    
    1892 1870
            ; if not is_rho
    
    1893 1871
              then skipQuickLook app_lspan larg orig_arg_ty
    

  • compiler/GHC/Tc/Gen/App.hs-boot deleted
    1
    -module GHC.Tc.Gen.App where
    
    2
    -
    
    3
    -import GHC.Hs ( HsExpr )
    
    4
    -import GHC.Tc.Types  ( TcM )
    
    5
    -import GHC.Tc.Types.Origin  ( CtOrigin )
    
    6
    -import GHC.Tc.Utils.TcType ( TcSigmaType )
    
    7
    -import GHC.Hs.Extension ( GhcRn, GhcTc )
    
    8
    -
    
    9
    -
    
    10
    -import GHC.Prelude (Bool)
    
    11
    -
    
    12
    -tcExprSigma :: Bool -> CtOrigin -> HsExpr GhcRn -> TcM (HsExpr GhcTc, TcSigmaType)

  • compiler/GHC/Tc/Gen/Expr.hs
    ... ... @@ -13,7 +13,7 @@
    13 13
     module GHC.Tc.Gen.Expr
    
    14 14
            ( tcCheckPolyExpr, tcCheckPolyExprNC,
    
    15 15
              tcCheckMonoExpr, tcCheckMonoExprNC,
    
    16
    -         tcInferExpr, tcInferSigma,
    
    16
    +         tcInferExpr, tcInferSigma, tcInferExprSigma,
    
    17 17
              tcInferRho, tcInferRhoNC,
    
    18 18
              tcMonoLExpr, tcMonoLExprNC,
    
    19 19
              tcInferRhoFRR, tcInferRhoFRRNC,
    
    ... ... @@ -237,6 +237,9 @@ tcPolyExprCheck expr res_ty
    237 237
     tcInferSigma :: LHsExpr GhcRn -> TcM (LHsExpr GhcTc, TcSigmaType)
    
    238 238
     tcInferSigma = tcInferExpr IIF_Sigma
    
    239 239
     
    
    240
    +tcInferExprSigma :: HsExpr GhcRn -> TcM (HsExpr GhcTc, TcSigmaType)
    
    241
    +tcInferExprSigma e = runInfer IIF_Sigma IFRR_Any (tcExpr e)
    
    242
    +
    
    240 243
     tcInferRho, tcInferRhoNC :: LHsExpr GhcRn -> TcM (LHsExpr GhcTc, TcRhoType)
    
    241 244
     -- Infer a *rho*-type. The return type is always instantiated.
    
    242 245
     tcInferRho   = tcInferExpr   IIF_DeepRho
    
    ... ... @@ -306,22 +309,25 @@ tcExpr :: HsExpr GhcRn
    306 309
     --   - HsAppType       type applications
    
    307 310
     --   - ExprWithTySig   (e :: type)
    
    308 311
     --   - HsRecSel        overloaded record fields
    
    309
    ---   - ExpandedThingRn renamer/pre-typechecker expansions
    
    310 312
     --   - HsOpApp         operator applications
    
    311 313
     --   - HsOverLit       overloaded literals
    
    312 314
     -- These constructors are the union of
    
    313 315
     --   - ones taken apart by GHC.Tc.Gen.Head.splitHsApps
    
    314 316
     --   - ones understood by GHC.Tc.Gen.Head.tcInferAppHead_maybe
    
    315 317
     -- See Note [Application chains and heads] in GHC.Tc.Gen.App
    
    316
    --- Se Note [Typechecking by expansion: overview]
    
    317 318
     tcExpr e@(HsVar {})              res_ty = tcApp e res_ty
    
    318 319
     tcExpr e@(HsApp {})              res_ty = tcApp e res_ty
    
    319 320
     tcExpr e@(OpApp {})              res_ty = tcApp e res_ty
    
    320 321
     tcExpr e@(HsAppType {})          res_ty = tcApp e res_ty
    
    321 322
     tcExpr e@(ExprWithTySig {})      res_ty = tcApp e res_ty
    
    323
    +tcExpr e@(XExpr (HsRecSelRn{}))  res_ty = tcApp e res_ty
    
    322 324
     
    
    325
    +-- Renamer expanded expressions (eg. Right/Left sections)
    
    326
    +-- or tcExpr expanded expressions (eg. Do statements and Record updates)
    
    327
    +-- are type checked using tcHsExpansion.
    
    328
    +-- See Note [Typechecking by expansion: overview]
    
    323 329
     tcExpr (XExpr (ExpandedThingRn hse)) res_ty = tcHsExpansion hse res_ty
    
    324
    -tcExpr e@(XExpr{})               res_ty = tcApp e res_ty
    
    330
    +
    
    325 331
     
    
    326 332
     -- Typecheck an occurrence of an unbound Id
    
    327 333
     --
    
    ... ... @@ -527,8 +533,9 @@ tcExpr (HsCase ctxt scrut matches) res_ty
    527 533
     
    
    528 534
     tcExpr (HsIf x pred b1 b2) res_ty
    
    529 535
       = do { pred'    <- tcCheckMonoExpr pred boolTy
    
    530
    -       ; (u1,b1') <- tcCollectingUsage $ tcMonoLExpr b1 res_ty
    
    531
    -       ; (u2,b2') <- tcCollectingUsage $ tcMonoLExpr b2 res_ty
    
    536
    +       ; let res_ty' = adjustExpTypeForCaseBranches res_ty [b1,b2]
    
    537
    +       ; (u1,b1') <- tcCollectingUsage $ tcMonoLExpr b1 res_ty'
    
    538
    +       ; (u2,b2') <- tcCollectingUsage $ tcMonoLExpr b2 res_ty'
    
    532 539
            ; tcEmitBindingUsage (supUE u1 u2)
    
    533 540
            ; return (HsIf x pred' b1' b2') }
    
    534 541
     
    

  • compiler/GHC/Tc/Gen/Expr.hs-boot
    ... ... @@ -35,6 +35,8 @@ tcInferRho, tcInferRhoNC ::
    35 35
     tcInferRhoFRR, tcInferRhoFRRNC ::
    
    36 36
       FixedRuntimeRepContext -> LHsExpr GhcRn -> TcM (LHsExpr GhcTc, TcRhoType)
    
    37 37
     
    
    38
    +tcInferExprSigma :: HsExpr GhcRn -> TcM (HsExpr GhcTc, TcSigmaType)
    
    39
    +
    
    38 40
     tcInferExpr :: InferInstFlag -> LHsExpr GhcRn -> TcM (LHsExpr GhcTc, TcType)
    
    39 41
     
    
    40 42
     tcSyntaxOp :: CtOrigin
    

  • compiler/GHC/Tc/Gen/Head.hs
    ... ... @@ -21,15 +21,14 @@ module GHC.Tc.Gen.Head
    21 21
     
    
    22 22
            , pprArgInst, addFunResCtxt ) where
    
    23 23
     
    
    24
    -import {-# SOURCE #-} GHC.Tc.Gen.Expr( tcExpr, tcCheckPolyExprNC, tcPolyLExprSig )
    
    24
    +import {-# SOURCE #-} GHC.Tc.Gen.Expr( tcCheckPolyExprNC, tcPolyLExprSig, tcInferExprSigma )
    
    25 25
     import {-# SOURCE #-} GHC.Tc.Gen.Splice( getUntypedSpliceBody )
    
    26
    -import {-# SOURCE #-} GHC.Tc.Gen.App( tcExprSigma )
    
    27 26
     
    
    28 27
     import GHC.Prelude
    
    29 28
     import GHC.Hs
    
    30 29
     import GHC.Hs.Syn.Type
    
    31 30
     
    
    32
    -import GHC.Rename.Utils (mkExpandedTc, mkExpandedExprTc)
    
    31
    +import GHC.Rename.Utils (mkExpandedExprTc)
    
    33 32
     
    
    34 33
     import GHC.Tc.Gen.HsType
    
    35 34
     import GHC.Tc.Gen.Bind( chooseInferredQuantifiers )
    
    ... ... @@ -446,8 +445,7 @@ tcInferAppHead (fun,fun_lspan)
    446 445
         do { mb_tc_fun <- tcInferAppHead_maybe fun
    
    447 446
            ; case mb_tc_fun of
    
    448 447
                 Just (fun', fun_sigma) -> return (fun', fun_sigma)
    
    449
    -            Nothing -> runInferRho (tcExpr fun)
    
    450
    -
    
    448
    +            Nothing                -> tcInferExprSigma fun
    
    451 449
            }
    
    452 450
     
    
    453 451
     tcInferAppHead_maybe :: HsExpr GhcRn
    
    ... ... @@ -457,23 +455,11 @@ tcInferAppHead_maybe :: HsExpr GhcRn
    457 455
     -- XExpr's although complicated needs to be looked through, useful for QL things when
    
    458 456
     -- the argument is an XExpr
    
    459 457
     tcInferAppHead_maybe fun = case fun of
    
    460
    -      HsVar _ nm
    
    461
    -        -> Just <$> tcInferId nm
    
    462
    -      ExprWithTySig _ e hs_ty
    
    463
    -        -> Just <$>tcExprWithSig e hs_ty
    
    464
    -      HsOverLit _ lit
    
    465
    -        -> Just <$> tcInferOverLit lit
    
    466
    -      XExpr (HsRecSelRn f)
    
    467
    -        -> Just <$> tcInferRecSelId f
    
    468
    -      XExpr (ExpandedThingRn (HSE o (L loc e)))
    
    469
    -        -> setSrcSpan (locA loc) $ Just <$>
    
    470
    -           do { (e', ty) <- tcExprSigma False (hsCtxtCtOrigin o) e
    
    471
    -              ; return (mkExpandedTc o (L loc e'), ty) }
    
    472
    -                      -- We do not want to instantiate the type of the head as there may be
    
    473
    -                      -- visible type applications in the argument.
    
    474
    -                      -- c.f. T19167
    
    475
    -      _
    
    476
    -        -> return Nothing
    
    458
    +      HsVar _ nm              -> Just <$> tcInferId nm
    
    459
    +      ExprWithTySig _ e hs_ty -> Just <$>tcExprWithSig e hs_ty
    
    460
    +      HsOverLit _ lit         -> Just <$> tcInferOverLit lit
    
    461
    +      XExpr (HsRecSelRn f)    -> Just <$> tcInferRecSelId f
    
    462
    +      _                       -> return Nothing
    
    477 463
     
    
    478 464
     {- *********************************************************************
    
    479 465
     *                                                                      *
    

  • compiler/GHC/Tc/Gen/Match.hs
    ... ... @@ -219,10 +219,10 @@ tcMatches :: (AnnoBody body, Outputable (body GhcTc))
    219 219
               -> MatchGroup GhcRn (LocatedA (body GhcRn))
    
    220 220
               -> TcM (MatchGroup GhcTc (LocatedA (body GhcTc)))
    
    221 221
     
    
    222
    -tcMatches ctxt tc_body pat_tys rhs_ty (MG { mg_alts = L l matches
    
    222
    +tcMatches ctxt tc_body pat_tys exp_ty (MG { mg_alts = L l matches
    
    223 223
                                               , mg_ext = origin })
    
    224 224
       | null matches  -- Deal with case e of {}
    
    225
    -    -- Since there are no branches, no one else will fill in rhs_ty
    
    225
    +    -- Since there are no branches, no one else will fill in exp_ty
    
    226 226
         -- when in inference mode, so we must do it ourselves,
    
    227 227
         -- here, using expTypeToType
    
    228 228
       = do { tcEmitBindingUsage bottomUE
    
    ... ... @@ -233,17 +233,19 @@ tcMatches ctxt tc_body pat_tys rhs_ty (MG { mg_alts = L l matches
    233 233
                [ExpForAllPatTy tvb] -> failWithTc $ TcRnEmptyCase ctxt (EmptyCaseForall tvb)
    
    234 234
                []                   -> panic "tcMatches: no arguments in EmptyCase"
    
    235 235
                _t1:(_t2:_ts)        -> panic "tcMatches: multiple arguments in EmptyCase"
    
    236
    -       ; rhs_ty <- expTypeToType rhs_ty
    
    236
    +       ; rhs_ty <- expTypeToType exp_ty
    
    237 237
            ; return (MG { mg_alts = L l []
    
    238 238
                         , mg_ext = MatchGroupTc [pat_ty] rhs_ty origin
    
    239 239
                         }) }
    
    240 240
     
    
    241 241
       | otherwise
    
    242
    -  = do { umatches <- mapM (tcCollectingUsage . tcMatch tc_body pat_tys rhs_ty) matches
    
    243
    -       ; let (usages, matches') = unzip umatches
    
    242
    +  = do { let exp_ty' = adjustExpTypeForCaseBranches exp_ty matches
    
    243
    +             tc_match match = tcCollectingUsage $
    
    244
    +                              tcMatch tc_body pat_tys exp_ty' match
    
    245
    +       ; (usages, matches') <- mapAndUnzipM tc_match matches
    
    244 246
            ; tcEmitBindingUsage $ supUEs usages
    
    245 247
            ; pat_tys  <- mapM readScaledExpType (filter_out_forall_pat_tys pat_tys)
    
    246
    -       ; rhs_ty   <- readExpType rhs_ty
    
    248
    +       ; rhs_ty   <- readExpType exp_ty
    
    247 249
            ; traceTc "tcMatches" (ppr matches' $$ ppr pat_tys $$ ppr rhs_ty)
    
    248 250
            ; return (MG { mg_alts   = L l matches'
    
    249 251
                         , mg_ext    = MatchGroupTc pat_tys rhs_ty origin
    

  • compiler/GHC/Tc/Types/Origin.hs
    ... ... @@ -1530,15 +1530,12 @@ data ExpectedFunTyCtxt
    1530 1530
       -- | A rebindable syntax operator is expected to have a function type.
    
    1531 1531
       --
    
    1532 1532
       -- Test cases for representation-polymorphism checks:
    
    1533
    -  --   RepPolyDoBind, RepPolyDoBody{1,2}, RepPolyMc{Bind,Body,Guard}, RepPolyNPlusK
    
    1533
    +  --   RepPolyMc{Bind,Body,Guard}, RepPolyNPlusK
    
    1534 1534
       = forall (p :: Pass)
    
    1535 1535
          . (OutputableBndrId p)
    
    1536 1536
         => ExpectedFunTySyntaxOp !CtOrigin !(HsExpr (GhcPass p))
    
    1537 1537
           -- ^ rebindable syntax operator
    
    1538 1538
     
    
    1539
    -  -- |
    
    1540
    -  | ExpectedTySyntax !CtOrigin !(HsExpr GhcRn)
    
    1541
    -
    
    1542 1539
       -- | A view pattern must have a function type.
    
    1543 1540
       --
    
    1544 1541
       -- Test cases for representation-polymorphism checks:
    
    ... ... @@ -1594,9 +1591,6 @@ pprExpectedFunTyCtxt funTy_origin i =
    1594 1591
                      , text "the rebindable syntax operator"
    
    1595 1592
                      , quotes (ppr op) ]
    
    1596 1593
                , nest 2 (ppr orig) ]
    
    1597
    -    ExpectedTySyntax orig arg ->
    
    1598
    -      vcat [ text "The expression" <+> quotes (ppr arg)
    
    1599
    -           , nest 2 (ppr orig) ]
    
    1600 1594
         ExpectedFunTyViewPat expr ->
    
    1601 1595
           vcat [ the_arg_of <+> text "the view pattern"
    
    1602 1596
                , nest 2 (ppr expr) ]
    
    ... ... @@ -1625,8 +1619,6 @@ pprExpectedFunTyCtxt funTy_origin i =
    1625 1619
     pprExpectedFunTyHerald :: ExpectedFunTyCtxt -> SDoc
    
    1626 1620
     pprExpectedFunTyHerald (ExpectedFunTySyntaxOp {})
    
    1627 1621
       = text "This rebindable syntax expects a function with"
    
    1628
    -pprExpectedFunTyHerald (ExpectedTySyntax orig _)
    
    1629
    -  = pprCtOriginBriefly orig
    
    1630 1622
     pprExpectedFunTyHerald (ExpectedFunTyViewPat {})
    
    1631 1623
       = text "A view pattern expression expects"
    
    1632 1624
     pprExpectedFunTyHerald (ExpectedFunTyArg fun _)
    

  • compiler/GHC/Tc/Utils/TcMType.hs
    ... ... @@ -63,7 +63,7 @@ module GHC.Tc.Utils.TcMType (
    63 63
       mkCheckExpType, newInferExpType, newInferExpTypeFRR,
    
    64 64
       runInfer, runInferRho, runInferSigma, runInferKind, runInferRhoFRR, runInferSigmaFRR,
    
    65 65
       readExpType, readExpType_maybe, readScaledExpType,
    
    66
    -  expTypeToType, scaledExpTypeToType,
    
    66
    +  expTypeToType, scaledExpTypeToType, adjustExpTypeForCaseBranches,
    
    67 67
       checkingExpType_maybe, checkingExpType,
    
    68 68
       inferResultToType, ensureMonoType, promoteTcType,
    
    69 69
     
    
    ... ... @@ -499,6 +499,17 @@ inferResultToType (IR { ir_uniq = u, ir_lvl = tc_lvl
    499 499
                                 ; let conc_orig = ConcreteFRR $ FixedRuntimeRepOrigin tau frr
    
    500 500
                                 ; return tau }
    
    501 501
     
    
    502
    +adjustExpTypeForCaseBranches :: ExpRhoType -> [branch] -> ExpRhoType
    
    503
    +-- See Note [fillInferResult: multiple branches]
    
    504
    +adjustExpTypeForCaseBranches exp_ty branches
    
    505
    +  = case exp_ty of
    
    506
    +      Infer ir | IR { ir_inst = IIF_Sigma } <- ir
    
    507
    +               , branches `lengthAtLeast` 2
    
    508
    +               -> Infer (ir { ir_inst = IIF_DeepRho })
    
    509
    +               | otherwise
    
    510
    +               -> exp_ty
    
    511
    +      Check {} -> exp_ty
    
    512
    +
    
    502 513
     {- Note [inferResultToType]
    
    503 514
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    504 515
     expTypeToType and inferResultType convert an InferResult to a monotype.
    

  • compiler/GHC/Tc/Utils/Unify.hs
    ... ... @@ -426,7 +426,7 @@ Some examples:
    426 426
     
    
    427 427
     tcSkolemiseGeneral
    
    428 428
       :: HasDebugCallStack
    
    429
    -  => DeepSubsumptionFlag
    
    429
    +  => DeepSubsumptionFlag   -- Ignores the DeepSubsumptionDepth
    
    430 430
       -> UserTypeCtxt
    
    431 431
       -> TcType -> TcType   -- top_ty and expected_ty
    
    432 432
             -- Here, top_ty      is the type we started to skolemise; used only in SigSkol
    
    ... ... @@ -1169,7 +1169,7 @@ fillInferResultNoInst act_res_ty (IR { ir_uniq = u
    1169 1169
     
    
    1170 1170
                          ; return final_co } }
    
    1171 1171
     
    
    1172
    -fillInferResult :: DeepSubsumptionFlag -> CtOrigin -> TcType -> InferResult -> TcM HsWrapper
    
    1172
    +fillInferResult :: DeepSubsumptionFlag -> CtOrigin -> TcSigmaType -> InferResult -> TcM HsWrapper
    
    1173 1173
     -- See Note [Instantiation of InferResult]
    
    1174 1174
     fillInferResult ds_flag ct_orig res_ty ires@(IR { ir_inst = iif })
    
    1175 1175
       = case iif of
    
    ... ... @@ -1203,7 +1203,7 @@ There are two things to worry about:
    1203 1203
             T1 -> e1
    
    1204 1204
             T2 -> e2
    
    1205 1205
     
    
    1206
    -Our typing rules are:
    
    1206
    +In general our typing rules are:
    
    1207 1207
     
    
    1208 1208
     * The RHS of a existential or GADT alternative must always be a
    
    1209 1209
       monotype, regardless of the number of alternatives.
    
    ... ... @@ -1218,17 +1218,13 @@ Our typing rules are:
    1218 1218
            We use choice (2) in that Section.
    
    1219 1219
            (GHC 8.10 and earlier used choice (1).)
    
    1220 1220
     
    
    1221
    -  But note that
    
    1222
    -      case e of
    
    1223
    -        True  -> hr
    
    1224
    -        False -> \x -> hr x
    
    1225
    -  will fail, because we still /infer/ both branches, so the \x will get
    
    1226
    -  a (monotype) unification variable, which will fail to unify with
    
    1227
    -  (forall a. a->a)
    
    1221
    +Note [fillInferResult: GADTs and existentials]
    
    1222
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1223
    +We can detect the GADT/existential situation, case (1) of Note [fillInferResult],
    
    1224
    +by seeing that the current TcLevel is greater than that stored in ir_lvl of the
    
    1225
    +Infer ExpType.  We bump the level whenever we go past a GADT/existential match.
    
    1228 1226
     
    
    1229
    -For (1) we can detect the GADT/existential situation by seeing that
    
    1230
    -the current TcLevel is greater than that stored in ir_lvl of the Infer
    
    1231
    -ExpType.  We bump the level whenever we go past a GADT/existential match.
    
    1227
    +We insist that the RHS has a monotype, regardless of the number of alternatives.
    
    1232 1228
     
    
    1233 1229
     Then, before filling the hole use promoteTcType to promote the type
    
    1234 1230
     to the outer ir_lvl.  promoteTcType does this
    
    ... ... @@ -1239,11 +1235,6 @@ That forces the type to be a monotype (since unification variables can
    1239 1235
     only unify with monotypes); and catches skolem-escapes because the
    
    1240 1236
     alpha is untouchable until the equality floats out.
    
    1241 1237
     
    
    1242
    -For (2), we simply look to see if the hole is filled already.
    
    1243
    -  - if not, we promote (as above) and fill the hole
    
    1244
    -  - if it is filled, we simply unify with the type that is
    
    1245
    -    already there
    
    1246
    -
    
    1247 1238
     (FIR1) There is one wrinkle.  Suppose we have
    
    1248 1239
                  case e of
    
    1249 1240
                     T1 -> e1 :: (forall a. a->a) -> Int
    
    ... ... @@ -1258,7 +1249,47 @@ For (2), we simply look to see if the hole is filled already.
    1258 1249
         So if we check G2 second, we still want to emit a constraint that restricts
    
    1259 1250
         the RHS to be a monotype. This is done by ensureMonoType, and it works
    
    1260 1251
         by simply generating a constraint (alpha ~ ty), where alpha is a fresh
    
    1261
    -unification variable.  We discard the evidence.
    
    1252
    +    unification variable.  We discard the evidence.
    
    1253
    +
    
    1254
    +Note [fillInferResult: multiple branches]
    
    1255
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1256
    +If there are multiple case branches, case (2) of Note [fillInferResult]
    
    1257
    +we simply look to see if the hole is filled already.
    
    1258
    +  - if not, we promote (as above) and fill the hole
    
    1259
    +  - if it is filled, we simply unify with the type that is already there
    
    1260
    +
    
    1261
    +But consider
    
    1262
    +    case x of
    
    1263
    +      True  -> True
    
    1264
    +      False -> undefined
    
    1265
    +and suppose we call `tcInferSigma` on this expression, so that the `ir_inst`
    
    1266
    +field of the expected result type is `IIF_Sigma`.   The danger is that we'll
    
    1267
    +fill the hole with `Bool` (from the `True`) and then reject when we try to
    
    1268
    +unify that with `forall a. a->a`, from the call to `undefined`.
    
    1269
    +
    
    1270
    +Another example:
    
    1271
    +   case x of
    
    1272
    +     True  -> (e1 :: forall a b. a->b)
    
    1273
    +     False -> (e3 :: forall b a. a->b)
    
    1274
    +
    
    1275
    +To avoid this, we never infer a sigma-type from a multi-branch `case`.  Instead
    
    1276
    +we just zap the `IIF_Sigma` to `IIF_DeepRho` when walking inside the branches
    
    1277
    +of multi-arm case-expression, or an if-expression. See calls to
    
    1278
    +`adjustExpTypeForCaseBranches`.
    
    1279
    +
    
    1280
    +This does mean that this would work:
    
    1281
    +   (let x = 77+55 in h x x) @Int
    
    1282
    +where
    
    1283
    +   h :: Int -> Int -> forall a. a->a
    
    1284
    +The `@Int` would instantiate the `forall a`.
    
    1285
    +
    
    1286
    +Note that
    
    1287
    +      case e of
    
    1288
    +        True  -> hr
    
    1289
    +        False -> \x -> hr x
    
    1290
    +      where hr :: (forall a. a->a) -> Int
    
    1291
    +will fail, because we still /infer/ both branches, so the \x will get a
    
    1292
    +(monotype) unification variable, which will fail to unify with (forall a. a->a)
    
    1262 1293
     
    
    1263 1294
     Note [Instantiation of InferResult]
    
    1264 1295
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    ... ... @@ -2068,37 +2099,19 @@ getDeepSubsumptionFlag =
    2068 2099
     -- | Variant of 'getDeepSubsumptionFlag' which enables a top-level subsumption
    
    2069 2100
     -- in order to implement the plan of Note [Typechecking data constructors].
    
    2070 2101
     getDeepSubsumptionFlag_DataConHead :: HsExpr GhcTc -> TcM DeepSubsumptionFlag
    
    2071
    -getDeepSubsumptionFlag_DataConHead app_head =
    
    2072
    -  do { user_ds <- xoptM LangExt.DeepSubsumption
    
    2073
    -     ; traceTc "getDeepSubsumptionFlag_DataConHead" (ppr app_head)
    
    2074
    -     ; return $
    
    2075
    -         if | user_ds
    
    2076
    -            -> Deep DeepSub
    
    2077
    -            | otherwise
    
    2078
    -            -> go app_head
    
    2079
    -     }
    
    2102
    +getDeepSubsumptionFlag_DataConHead app_head
    
    2103
    +  = do { user_ds <- xoptM LangExt.DeepSubsumption
    
    2104
    +       ; return $ if | user_ds          -> Deep DeepSub
    
    2105
    +                     | dc_head app_head -> Deep TopSub
    
    2106
    +                     | otherwise        -> Shallow  }
    
    2080 2107
       where
    
    2081
    -    go :: HsExpr GhcTc -> DeepSubsumptionFlag
    
    2082
    -    go app_head
    
    2083
    -     | XExpr (ConLikeTc (RealDataCon {})) <- app_head
    
    2084
    -     = Deep TopSub
    
    2085
    -     | XExpr (ExpandedThingTc (HSE _ (L _ f))) <- app_head
    
    2086
    -     = go f
    
    2087
    -     | XExpr (WrapExpr _ f) <- app_head
    
    2088
    -     = go f
    
    2089
    -     | HsVar _ f <- app_head
    
    2090
    -     , isDataConId (unLoc f)
    
    2091
    -     = Deep TopSub
    
    2092
    -     | HsApp _ f _ <- app_head
    
    2093
    -     = go (unLoc f)
    
    2094
    -     | HsAppType _ f _ <- app_head
    
    2095
    -     = go (unLoc f)
    
    2096
    -     | OpApp _ _ f _ <- app_head
    
    2097
    -     = go (unLoc f)
    
    2098
    -     | HsPar _ f <- app_head
    
    2099
    -     = go (unLoc f)
    
    2100
    -     | otherwise
    
    2101
    -     = Shallow
    
    2108
    +    dc_head (XExpr (ConLikeTc (RealDataCon {}))) = True
    
    2109
    +    dc_head (XExpr (WrapExpr _ f))  = dc_head f
    
    2110
    +    dc_head (HsApp _ (L _ f) _)     = dc_head f
    
    2111
    +    dc_head (HsAppType _ (L _ f) _) = dc_head f
    
    2112
    +    dc_head (OpApp _ _ (L _ f) _)   = dc_head f
    
    2113
    +    dc_head (HsPar _ (L _ f))       = dc_head f
    
    2114
    +    dc_head _ = False
    
    2102 2115
     
    
    2103 2116
     
    
    2104 2117
     -- | 'tc_sub_type_deep' is where the actual work happens for deep subsumption.
    

  • testsuite/tests/rep-poly/RepPolyDoBind.stderr
    1 1
     RepPolyDoBind.hs:26:3: error: [GHC-55287]
    
    2
    -    • The expression ‘undefined
    
    2
    +    • The first argument of the rebindable syntax operator ‘(>>=)
    
    3 3
             arising from a do statement
    
    4 4
           does not have a fixed runtime representation.
    
    5 5
           Its type is:
    

  • testsuite/tests/rep-poly/RepPolyDoBody1.stderr
    1 1
     RepPolyDoBody1.hs:24:3: error: [GHC-55287]
    
    2
    -    • The expression ‘undefined :: ma
    
    2
    +    • The first argument of the rebindable syntax operator ‘(>>)
    
    3 3
             arising from a do statement
    
    4 4
           does not have a fixed runtime representation.
    
    5 5
           Its type is:
    

  • testsuite/tests/rep-poly/RepPolyDoBody2.stderr
    1 1
     RepPolyDoBody2.hs:23:3: error: [GHC-55287]
    
    2
    -    • The expression ‘return ()’
    
    2
    +    • The first argument of the rebindable syntax operator ‘(>>)’
    
    3 3
             arising from a do statement
    
    4 4
           does not have a fixed runtime representation.
    
    5 5
           Its type is: