Apoorv Ingle pushed to branch wip/ani/tc-expand at Glasgow Haskell Compiler / GHC

Commits:

20 changed files:

Changes:

  • compiler/GHC/Hs/Expr.hs
    ... ... @@ -660,44 +660,24 @@ type instance XXExpr GhcTc = XXExprGhcTc
    660 660
     *                                                                      *
    
    661 661
     ********************************************************************* -}
    
    662 662
     
    
    663
    +-- See Note [Rebindable syntax and XXExprGhcRn]
    
    664
    +-- See Note [Expanding HsDo with XXExprGhcRn] in `GHC.Tc.Gen.Do`
    
    665
    +data HsExpansion p = HSE { hs_ctxt :: HsCtxt -- The original source thing context to be used for error messages
    
    666
    +                         , expanded_expr :: LHsExpr p } -- The compiler generated, expanded expression
    
    667
    +                                                        -- This is located because of do statements (TODO ANI : Add Note)
    
    668
    +
    
    663 669
     data XXExprGhcRn
    
    664
    -  = ExpandedThingRn { xrn_orig     :: HsCtxt         -- The original source thing context to be used for error messages
    
    665
    -                    , xrn_expanded :: HsExpr GhcRn    -- The compiler generated, expanded thing
    
    666
    -                    }
    
    670
    +  = ExpandedThingRn (HsExpansion GhcRn)  -- ^ Renamed/Pre Typecheck expanded expression
    
    667 671
     
    
    668 672
       | HsRecSelRn  (FieldOcc GhcRn)   -- ^ Variable pointing to record selector
    
    669 673
                                -- See Note [Non-overloaded record field selectors] and
    
    670 674
                                -- Note [Record selectors in the AST]
    
    671 675
     
    
    672
    --- | Build an expression using the extension constructor `XExpr`,
    
    673
    ---   and the two components of the expansion: original expression and
    
    674
    ---   expanded expressions.
    
    675
    -mkExpandedExpr
    
    676
    -  :: HsExpr GhcRn         -- ^ source expression context
    
    677
    -  -> HsExpr GhcRn         -- ^ expanded expression
    
    678
    -  -> HsExpr GhcRn         -- ^ suitably wrapped 'XXExprGhcRn'
    
    679
    -mkExpandedExpr oExpr eExpr = XExpr (ExpandedThingRn { xrn_orig = ExprCtxt oExpr
    
    680
    -                                                    , xrn_expanded = eExpr })
    
    681
    -
    
    682
    --- | Build an expression using the extension constructor `XExpr`,
    
    683
    ---   and the two components of the expansion: original do stmt and
    
    684
    ---   expanded expression
    
    685
    -mkExpandedStmt
    
    686
    -  :: ExprLStmt GhcRn      -- ^ source statement context
    
    687
    -  -> HsDoFlavour          -- ^ source statements do flavour
    
    688
    -  -> HsExpr GhcRn         -- ^ expanded expression
    
    689
    -  -> HsExpr GhcRn         -- ^ suitably wrapped 'XXExprGhcRn'
    
    690
    -mkExpandedStmt oStmt flav eExpr = XExpr (ExpandedThingRn { xrn_orig = StmtErrCtxt (HsDoStmt flav) oStmt
    
    691
    -                                                         , xrn_expanded = eExpr })
    
    692
    -
    
    693 676
     data XXExprGhcTc
    
    694 677
       = WrapExpr        -- Type and evidence application and abstractions
    
    695 678
           HsWrapper (HsExpr GhcTc)
    
    696 679
     
    
    697
    -  | ExpandedThingTc                         -- See Note [Rebindable syntax and XXExprGhcRn]
    
    698
    -                                            -- See Note [Expanding HsDo with XXExprGhcRn] in `GHC.Tc.Gen.Do`
    
    699
    -         { xtc_orig     :: HsCtxt        -- The original user written thing
    
    700
    -         , xtc_expanded :: HsExpr GhcTc }   -- The expanded typechecked expression
    
    680
    +  | ExpandedThingTc (HsExpansion GhcTc) -- ^ Typechecked expanded expression
    
    701 681
     
    
    702 682
       | ConLikeTc
    
    703 683
           -- ^ A 'ConLike', either a data constructor or pattern synonym
    
    ... ... @@ -722,22 +702,6 @@ data XXExprGhcTc
    722 702
                                  -- See Note [Non-overloaded record field selectors] and
    
    723 703
                                  -- Note [Record selectors in the AST]
    
    724 704
     
    
    725
    -
    
    726
    --- | Build a 'XXExprGhcRn' out of an extension constructor,
    
    727
    ---   and the two components of the expansion: original and
    
    728
    ---   expanded typechecked expressions.
    
    729
    -mkExpandedExprTc
    
    730
    -  :: HsExpr GhcRn           -- ^ source expression
    
    731
    -  -> HsExpr GhcTc           -- ^ expanded typechecked expression
    
    732
    -  -> HsExpr GhcTc           -- ^ suitably wrapped 'XXExprGhcRn'
    
    733
    -mkExpandedExprTc oExpr eExpr = mkExpandedTc (ExprCtxt oExpr) eExpr
    
    734
    -
    
    735
    -mkExpandedTc
    
    736
    -  :: HsCtxt          -- ^ source, user written do statement/expression
    
    737
    -  -> HsExpr GhcTc           -- ^ expanded typechecked expression
    
    738
    -  -> HsExpr GhcTc           -- ^ suitably wrapped 'XXExprGhcRn'
    
    739
    -mkExpandedTc o e = XExpr (ExpandedThingTc o e)
    
    740
    -
    
    741 705
     {- *********************************************************************
    
    742 706
     *                                                                      *
    
    743 707
                 Pretty-printing expressions
    
    ... ... @@ -1055,9 +1019,15 @@ ppr_expr (XExpr x) = case ghcPass @p of
    1055 1019
       GhcRn -> ppr x
    
    1056 1020
       GhcTc -> ppr x
    
    1057 1021
     
    
    1058
    -instance Outputable XXExprGhcRn where
    
    1059
    -  ppr (HsRecSelRn f)        = pprPrefixOcc f
    
    1060
    -  ppr (ExpandedThingRn o e) = ifPprDebug (braces $ vcat [pprCtxt o, text ";;" , ppr e]) (pprCtxt o)
    
    1022
    +
    
    1023
    +ppr_hse :: forall p. (IsPass p) => HsExpansion (GhcPass p) -> SDoc
    
    1024
    +ppr_hse hse
    
    1025
    +  = case ghcPass @p of
    
    1026
    +      GhcPs -> empty
    
    1027
    +      GhcRn -> case hse of
    
    1028
    +                 HSE o e -> ifPprDebug (braces $ vcat [pprCtxt o, text ";;" , ppr e]) (pprCtxt o)
    
    1029
    +      GhcTc -> case hse of
    
    1030
    +                 HSE o e -> ifPprDebug (braces $ vcat [pprCtxt o, text ";;" , ppr e]) (pprCtxt o)
    
    1061 1031
         where
    
    1062 1032
           ppr_builder prefix x = ifPprDebug (braces (text prefix <+> parens x)) x
    
    1063 1033
           pprCtxt :: HsCtxt -> SDoc
    
    ... ... @@ -1067,26 +1037,17 @@ instance Outputable XXExprGhcRn where
    1067 1037
           pprCtxt (FunAppCtxt (FunAppCtxtExpr _ e) _) = ppr_builder "<FunAppCtxt>:"  (ppr e)
    
    1068 1038
           pprCtxt _ = ppr_builder "<MiscHsCtxt>:" empty
    
    1069 1039
     
    
    1040
    +instance Outputable XXExprGhcRn where
    
    1041
    +  ppr (HsRecSelRn f)        = pprPrefixOcc f
    
    1042
    +  ppr (ExpandedThingRn hse) = ppr_hse hse
    
    1043
    +
    
    1044
    +
    
    1070 1045
     instance Outputable XXExprGhcTc where
    
    1071 1046
       ppr (WrapExpr co_fn e)
    
    1072 1047
         = pprHsWrapper co_fn (\_parens -> pprExpr e)
    
    1073 1048
     
    
    1074
    -  ppr (ExpandedThingTc o e)
    
    1075
    -    = ifPprDebug (braces $ vcat [pprCtxt o, ppr e]) (pprCtxt o)
    
    1076
    -
    
    1077
    -    where
    
    1078
    -      ppr_builder prefix x = ifPprDebug (braces (text prefix <+> parens x)) x
    
    1079
    -      pprCtxt :: HsCtxt -> SDoc
    
    1080
    -      pprCtxt (ExprCtxt e) = ppr_builder "<OrigExpr>:"  (ppr e)
    
    1081
    -      pprCtxt (StmtErrCtxt _ stmt) = ppr_builder "<OrigStmt>:" (ppr stmt)
    
    1082
    -      pprCtxt (StmtErrCtxtPat pat) = ppr_builder "<OrigPat>:" (ppr pat)
    
    1083
    -      pprCtxt (FunAppCtxt (FunAppCtxtExpr _ e) _) = ppr_builder "<FunAppCtxt>:"  (ppr e)
    
    1084
    -      pprCtxt _ = ppr_builder "<MiscHsCtxt>:" empty
    
    1085
    -
    
    1086
    -            -- e is the expanded expression, we print the original
    
    1087
    -            -- expression (HsExpr GhcRn), not the
    
    1088
    -            -- expanded typechecked one (HsExpr GhcTc),
    
    1089
    -            -- unless we are in ppr's debug mode printed both
    
    1049
    +  ppr (ExpandedThingTc hse)
    
    1050
    +    = ppr_hse hse
    
    1090 1051
     
    
    1091 1052
       ppr (ConLikeTc con) = pprPrefixOcc con
    
    1092 1053
        -- Used in error messages generated by
    
    ... ... @@ -1119,12 +1080,12 @@ ppr_infix_expr (XExpr x) = case ghcPass @p of
    1119 1080
     ppr_infix_expr _ = Nothing
    
    1120 1081
     
    
    1121 1082
     ppr_infix_expr_rn :: XXExprGhcRn -> Maybe SDoc
    
    1122
    -ppr_infix_expr_rn (ExpandedThingRn thing _) = ppr_infix_hs_expansion thing
    
    1083
    +ppr_infix_expr_rn (ExpandedThingRn (HSE thing _)) = ppr_infix_hs_expansion thing
    
    1123 1084
     ppr_infix_expr_rn (HsRecSelRn f)            = Just (pprInfixOcc f)
    
    1124 1085
     
    
    1125 1086
     ppr_infix_expr_tc :: XXExprGhcTc -> Maybe SDoc
    
    1126 1087
     ppr_infix_expr_tc (WrapExpr _ e)    = ppr_infix_expr e
    
    1127
    -ppr_infix_expr_tc (ExpandedThingTc thing _)  = ppr_infix_hs_expansion thing
    
    1088
    +ppr_infix_expr_tc (ExpandedThingTc (HSE thing _))  = ppr_infix_hs_expansion thing
    
    1128 1089
     ppr_infix_expr_tc (ConLikeTc con)            = Just (pprInfixOcc (conLikeName con))
    
    1129 1090
     ppr_infix_expr_tc (HsTick {})                = Nothing
    
    1130 1091
     ppr_infix_expr_tc (HsBinTick {})             = Nothing
    
    ... ... @@ -1212,14 +1173,14 @@ hsExprNeedsParens prec = go
    1212 1173
     
    
    1213 1174
         go_x_tc :: XXExprGhcTc -> Bool
    
    1214 1175
         go_x_tc (WrapExpr _ e)                   = hsExprNeedsParens prec e
    
    1215
    -    go_x_tc (ExpandedThingTc thing _)        = hsExpandedNeedsParens thing
    
    1176
    +    go_x_tc (ExpandedThingTc (HSE thing _))  = hsExpandedNeedsParens thing
    
    1216 1177
         go_x_tc (ConLikeTc {})                   = False
    
    1217 1178
         go_x_tc (HsTick _ (L _ e))               = hsExprNeedsParens prec e
    
    1218 1179
         go_x_tc (HsBinTick _ _ (L _ e))          = hsExprNeedsParens prec e
    
    1219 1180
         go_x_tc (HsRecSelTc{})                   = False
    
    1220 1181
     
    
    1221 1182
         go_x_rn :: XXExprGhcRn -> Bool
    
    1222
    -    go_x_rn (ExpandedThingRn thing _ )   = hsExpandedNeedsParens thing
    
    1183
    +    go_x_rn (ExpandedThingRn (HSE thing _))  = hsExpandedNeedsParens thing
    
    1223 1184
         go_x_rn (HsRecSelRn{})               = False
    
    1224 1185
     
    
    1225 1186
         hsExpandedNeedsParens :: HsCtxt -> Bool
    
    ... ... @@ -1264,14 +1225,14 @@ isAtomicHsExpr (XExpr x)
    1264 1225
       where
    
    1265 1226
         go_x_tc :: XXExprGhcTc -> Bool
    
    1266 1227
         go_x_tc (WrapExpr _ e)            = isAtomicHsExpr e
    
    1267
    -    go_x_tc (ExpandedThingTc thing _) = isAtomicExpandedThingRn thing
    
    1228
    +    go_x_tc (ExpandedThingTc (HSE thing _)) = isAtomicExpandedThingRn thing
    
    1268 1229
         go_x_tc (ConLikeTc {})            = True
    
    1269 1230
         go_x_tc (HsTick {})               = False
    
    1270 1231
         go_x_tc (HsBinTick {})            = False
    
    1271 1232
         go_x_tc (HsRecSelTc{})            = True
    
    1272 1233
     
    
    1273 1234
         go_x_rn :: XXExprGhcRn -> Bool
    
    1274
    -    go_x_rn (ExpandedThingRn thing _)   = isAtomicExpandedThingRn thing
    
    1235
    +    go_x_rn (ExpandedThingRn (HSE thing _))   = isAtomicExpandedThingRn thing
    
    1275 1236
         go_x_rn (HsRecSelRn{})              = True
    
    1276 1237
     
    
    1277 1238
         isAtomicExpandedThingRn :: HsCtxt -> Bool
    

  • compiler/GHC/Hs/Instances.hs
    ... ... @@ -647,6 +647,9 @@ instance Data HsCtxt where
    647 647
     
    
    648 648
     deriving instance Data XXExprGhcRn
    
    649 649
     
    
    650
    +deriving instance Data (HsExpansion GhcRn)
    
    651
    +deriving instance Data (HsExpansion GhcTc)
    
    652
    +
    
    650 653
     deriving instance Data a => Data (WithUserRdr a)
    
    651 654
     
    
    652 655
     -- -------------------------------
    

  • compiler/GHC/Hs/Syn/Type.hs
    ... ... @@ -153,7 +153,7 @@ hsExprType (HsQual x _ _) = dataConCantHappen x
    153 153
     hsExprType (HsForAll x _ _) = dataConCantHappen x
    
    154 154
     hsExprType (HsFunArr x _ _ _) = dataConCantHappen x
    
    155 155
     hsExprType (XExpr (WrapExpr wrap e)) = hsWrapperType wrap $ hsExprType e
    
    156
    -hsExprType (XExpr (ExpandedThingTc _ e))  = hsExprType e
    
    156
    +hsExprType (XExpr (ExpandedThingTc (HSE _ e)))  = lhsExprType e
    
    157 157
     hsExprType (XExpr (ConLikeTc con)) = conLikeType con
    
    158 158
     hsExprType (XExpr (HsTick _ e)) = lhsExprType e
    
    159 159
     hsExprType (XExpr (HsBinTick _ _ e)) = lhsExprType e
    

  • compiler/GHC/HsToCore/Expr.hs
    ... ... @@ -41,7 +41,6 @@ import GHC.Hs
    41 41
     --     needs to see source types
    
    42 42
     import GHC.Tc.Utils.TcType
    
    43 43
     import GHC.Tc.Types.Evidence
    
    44
    -import GHC.Tc.Types.ErrCtxt
    
    45 44
     import GHC.Tc.Utils.Monad
    
    46 45
     import GHC.Tc.Instance.Class (lookupHasFieldLabel)
    
    47 46
     
    
    ... ... @@ -308,10 +307,7 @@ dsExpr e@(XExpr ext_expr_tc)
    308 307
           WrapExpr {}   -> dsApp e
    
    309 308
           ConLikeTc {}  -> dsApp e
    
    310 309
     
    
    311
    -      ExpandedThingTc o e
    
    312
    -        | StmtErrCtxt _ (L loc _) <- o -- c.f. T14546d. We have lost the location of the first statement in the GhcRn -> GhcTc
    
    313
    -        -> putSrcSpanDsA loc $ dsExpr e
    
    314
    -        | otherwise -> dsExpr e
    
    310
    +      ExpandedThingTc (HSE _ e) -> dsLExpr e
    
    315 311
     
    
    316 312
           -- Hpc Support
    
    317 313
           HsTick tickish e -> do
    

  • compiler/GHC/HsToCore/Match.hs
    ... ... @@ -1166,8 +1166,8 @@ viewLExprEq (e1,_) (e2,_) = lexp e1 e2
    1166 1166
         -- we have to compare the wrappers
    
    1167 1167
         exp (XExpr (WrapExpr h e)) (XExpr (WrapExpr h' e')) =
    
    1168 1168
           wrap h h' && exp e e'
    
    1169
    -    exp (XExpr (ExpandedThingTc _ x)) (XExpr (ExpandedThingTc _ x'))
    
    1170
    -      = exp x x'
    
    1169
    +    exp (XExpr (ExpandedThingTc (HSE _ x))) (XExpr (ExpandedThingTc (HSE _ x')))
    
    1170
    +      = lexp x x'
    
    1171 1171
         exp (HsVar _ i) (HsVar _ i') = i == i'
    
    1172 1172
         exp (HsIPVar _ i) (HsIPVar _ i') =
    
    1173 1173
           -- the instance for IPName derives using the id, so follow the HsVar case
    

  • compiler/GHC/HsToCore/Quote.hs
    ... ... @@ -1739,7 +1739,7 @@ repE e@(XExpr (ExpandedThingRn o x))
    1739 1739
       | ExprCtxt e <- o
    
    1740 1740
       = do { rebindable_on <- lift $ xoptM LangExt.RebindableSyntax
    
    1741 1741
            ; if rebindable_on  -- See Note [Quotation and rebindable syntax]
    
    1742
    -         then repE x
    
    1742
    +         then repLE x
    
    1743 1743
              else repE e }
    
    1744 1744
       | otherwise
    
    1745 1745
       = notHandled (ThExpressionForm e)
    

  • compiler/GHC/HsToCore/Ticks.hs
    ... ... @@ -415,7 +415,7 @@ addTickLHsExpr e@(L pos e0) = do
    415 415
       d <- getDensity
    
    416 416
       case d of
    
    417 417
         TickForBreakPoints | isGoodBreakExpr e0 -> tick_it
    
    418
    -    TickForCoverage    | XExpr (ExpandedThingTc StmtErrCtxt{} _) <- e0 -- expansion ticks are handled separately
    
    418
    +    TickForCoverage    | XExpr (ExpandedThingTc (HSE StmtErrCtxt{} _)) <- e0 -- expansion ticks are handled separately
    
    419 419
                            -> dont_tick_it
    
    420 420
                            | otherwise -> tick_it
    
    421 421
         TickCallSites      | isCallSite e0      -> tick_it
    
    ... ... @@ -484,15 +484,15 @@ addTickLHsExprNever (L pos e0) = do
    484 484
     -- General heuristic: expressions which are calls (do not denote
    
    485 485
     -- values) are good break points.
    
    486 486
     isGoodBreakExpr :: HsExpr GhcTc -> Bool
    
    487
    -isGoodBreakExpr (XExpr (ExpandedThingTc (StmtErrCtxt{}) _)) = False
    
    487
    +isGoodBreakExpr (XExpr (ExpandedThingTc (HSE StmtErrCtxt{} _))) = False
    
    488 488
     isGoodBreakExpr e = isCallSite e
    
    489 489
     
    
    490 490
     isCallSite :: HsExpr GhcTc -> Bool
    
    491 491
     isCallSite HsApp{}     = True
    
    492 492
     isCallSite HsAppType{} = True
    
    493 493
     isCallSite HsCase{}    = True
    
    494
    -isCallSite (XExpr (ExpandedThingTc _ e))
    
    495
    -  = isCallSite e
    
    494
    +isCallSite (XExpr (ExpandedThingTc (HSE _ e)))
    
    495
    +  = isCallSite (unLoc e)
    
    496 496
     
    
    497 497
     -- NB: OpApp, SectionL, SectionR are all expanded out
    
    498 498
     isCallSite _           = False
    
    ... ... @@ -637,7 +637,7 @@ addTickHsExpr (HsProc x pat cmdtop) =
    637 637
     addTickHsExpr (XExpr (WrapExpr w e)) =
    
    638 638
             liftM (XExpr . WrapExpr w) $
    
    639 639
                   (addTickHsExpr e)        -- Explicitly no tick on inside
    
    640
    -addTickHsExpr (XExpr (ExpandedThingTc o e)) = addTickHsExpanded o e
    
    640
    +addTickHsExpr (XExpr (ExpandedThingTc hse)) = addTickHsExpanded hse
    
    641 641
     
    
    642 642
     addTickHsExpr e@(XExpr (ConLikeTc {})) = return e
    
    643 643
       -- We used to do a freeVar on a pat-syn builder, but actually
    
    ... ... @@ -660,14 +660,14 @@ addTickHsExpr (HsDo srcloc cxt (L l stmts))
    660 660
                         ListComp -> Just $ BinBox QualBinBox
    
    661 661
                         _        -> Nothing
    
    662 662
     
    
    663
    -addTickHsExpanded :: HsCtxt -> HsExpr GhcTc -> TM (HsExpr GhcTc)
    
    664
    -addTickHsExpanded o e = liftM (XExpr . ExpandedThingTc o) $ case o of
    
    663
    +addTickHsExpanded :: HsExpansion GhcTc -> TM (HsExpr GhcTc)
    
    664
    +addTickHsExpanded (HSE o e) = liftM (XExpr . ExpandedThingTc . HSE o) $ case o of
    
    665 665
       -- We always want statements to get a tick, so we can step over each one.
    
    666 666
       -- To avoid duplicates we blacklist SrcSpans we already inserted here.
    
    667 667
       StmtErrCtxt _ (L pos _) -> do_tick_black pos
    
    668 668
       _                    -> skip
    
    669 669
       where
    
    670
    -    skip = addTickHsExpr e
    
    670
    +    skip = addTickLHsExpr e
    
    671 671
         do_tick_black pos = do
    
    672 672
           d <- getDensity
    
    673 673
           case d of
    
    ... ... @@ -675,9 +675,9 @@ addTickHsExpanded o e = liftM (XExpr . ExpandedThingTc o) $ case o of
    675 675
              TickForBreakPoints -> tick_it_black pos
    
    676 676
              _                  -> skip
    
    677 677
         tick_it_black pos =
    
    678
    -      unLoc <$> allocTickBox (ExpBox False) False False (locA pos)
    
    678
    +      allocTickBox (ExpBox False) False False (locA pos)
    
    679 679
                                  (withBlackListed (locA pos) $
    
    680
    -                               addTickHsExpr e)
    
    680
    +                               addTickHsExpr (unLoc e))
    
    681 681
     
    
    682 682
     addTickTupArg :: HsTupArg GhcTc -> TM (HsTupArg GhcTc)
    
    683 683
     addTickTupArg (Present x e)  = do { e' <- addTickLHsExpr e
    

  • compiler/GHC/Iface/Ext/Ast.hs
    ... ... @@ -755,10 +755,10 @@ instance HiePass p => HasType (LocatedA (HsExpr (GhcPass p))) where
    755 755
             RecordCon con_expr _ _ -> computeType con_expr
    
    756 756
             ExprWithTySig _ e _ -> computeLType e
    
    757 757
             HsPragE _ _ e -> computeLType e
    
    758
    -        XExpr (ExpandedThingTc thing e)
    
    758
    +        XExpr (ExpandedThingTc (HSE thing e))
    
    759 759
               | ExprCtxt (HsGetField{}) <- thing -- for record-dot-syntax
    
    760
    -          -> Just (hsExprType e)
    
    761
    -          | otherwise -> computeType e
    
    760
    +          -> Just (lhsExprType e)
    
    761
    +          | otherwise -> computeLType e
    
    762 762
             XExpr (HsTick _ e) -> computeLType e
    
    763 763
             XExpr (HsBinTick _ _ e) -> computeLType e
    
    764 764
             e -> Just (hsExprType e)
    
    ... ... @@ -1352,8 +1352,8 @@ instance HiePass p => ToHie (LocatedA (HsExpr (GhcPass p))) where
    1352 1352
               WrapExpr w a
    
    1353 1353
                 -> [ toHie $ L mspan a
    
    1354 1354
                    , toHie (L mspan w) ]
    
    1355
    -          ExpandedThingTc _ e
    
    1356
    -            -> [ toHie (L mspan e) ]
    
    1355
    +          ExpandedThingTc (HSE _ e)
    
    1356
    +            -> [ toHie e ]
    
    1357 1357
               ConLikeTc con
    
    1358 1358
                 -> [ toHie $ C Use $ L mspan $ conLikeName con ]
    
    1359 1359
               HsTick _ expr
    

  • compiler/GHC/Rename/Utils.hs
    ... ... @@ -24,6 +24,8 @@ module GHC.Rename.Utils (
    24 24
             genSimpleFunBind, genFunBind,
    
    25 25
             genHsLamDoExp, genHsCaseAltDoExp, genSimpleMatch, genHsLet,
    
    26 26
     
    
    27
    +        mkExpandedRn, mkExpandedExpr, mkExpandedStmt, mkExpandedLExpr, mkExpandedTc, mkExpandedExprTc,
    
    28
    +
    
    27 29
             mkRnSyntaxExpr,
    
    28 30
     
    
    29 31
             newLocalBndrRn, newLocalBndrsRn,
    
    ... ... @@ -45,7 +47,6 @@ import GHC.Core.Type
    45 47
     import GHC.Hs
    
    46 48
     import GHC.Types.Name.Reader
    
    47 49
     import GHC.Tc.Errors.Types
    
    48
    --- import GHC.Tc.Utils.Env
    
    49 50
     import GHC.Tc.Utils.Monad
    
    50 51
     import GHC.Types.Name
    
    51 52
     import GHC.Types.Name.Set
    
    ... ... @@ -816,3 +817,51 @@ genSimpleMatch ctxt pats rhs
    816 817
       = wrapGenSpan $
    
    817 818
         Match { m_ext = noExtField, m_ctxt = ctxt, m_pats = noLocA pats
    
    818 819
               , m_grhss = unguardedGRHSs generatedSrcSpan rhs noAnn }
    
    820
    +
    
    821
    +
    
    822
    +-- | Build an expression using the extension constructor `XExpr`,
    
    823
    +--   and the two components of the expansion: original expression and
    
    824
    +--   expanded expressions.
    
    825
    +mkExpandedExpr
    
    826
    +  :: HsExpr GhcRn         -- ^ source expression context
    
    827
    +  -> HsExpr GhcRn         -- ^ expanded expression
    
    828
    +  -> HsExpr GhcRn         -- ^ suitably wrapped 'XXExprGhcRn'
    
    829
    +mkExpandedExpr oExpr eExpr = mkExpandedRn (ExprCtxt oExpr) (wrapGenSpan eExpr)
    
    830
    +
    
    831
    +mkExpandedLExpr
    
    832
    +  :: HsExpr GhcRn         -- ^ source expression context
    
    833
    +  -> LHsExpr GhcRn         -- ^ expanded expression
    
    834
    +  -> HsExpr GhcRn         -- ^ suitably wrapped 'XXExprGhcRn'
    
    835
    +mkExpandedLExpr oExpr eExpr = mkExpandedRn (ExprCtxt oExpr) eExpr
    
    836
    +
    
    837
    +-- | Build an expression using the extension constructor `XExpr`,
    
    838
    +--   and the two components of the expansion: original do stmt and
    
    839
    +--   expanded expression
    
    840
    +mkExpandedStmt
    
    841
    +  :: ExprLStmt GhcRn      -- ^ source statement context
    
    842
    +  -> HsDoFlavour          -- ^ source statements do flavour
    
    843
    +  -> HsExpr GhcRn         -- ^ expanded expression
    
    844
    +  -> HsExpr GhcRn         -- ^ suitably wrapped 'XXExprGhcRn'
    
    845
    +mkExpandedStmt oStmt flav eExpr = mkExpandedRn (StmtErrCtxt (HsDoStmt flav) oStmt) (wrapGenSpan eExpr)
    
    846
    +
    
    847
    +mkExpandedRn
    
    848
    +  :: HsCtxt          -- ^ source, user written do statement/expression
    
    849
    +  -> LHsExpr GhcRn           -- ^ expanded typechecked expression
    
    850
    +  -> HsExpr GhcRn          -- ^ suitably wrapped 'XXExprGhcRn'
    
    851
    +mkExpandedRn o e = XExpr (ExpandedThingRn (HSE o e))
    
    852
    +
    
    853
    +
    
    854
    +-- | Build a 'XXExprGhcRn' out of an extension constructor,
    
    855
    +--   and the two components of the expansion: original and
    
    856
    +--   expanded typechecked expressions.
    
    857
    +mkExpandedExprTc
    
    858
    +  :: HsExpr GhcRn           -- ^ source expression
    
    859
    +  -> HsExpr GhcTc           -- ^ expanded typechecked expression
    
    860
    +  -> HsExpr GhcTc           -- ^ suitably wrapped 'XXExprGhcRn'
    
    861
    +mkExpandedExprTc oExpr eExpr = mkExpandedTc (ExprCtxt oExpr) (wrapGenSpan eExpr)
    
    862
    +
    
    863
    +mkExpandedTc
    
    864
    +  :: HsCtxt          -- ^ source, user written do statement/expression
    
    865
    +  -> LHsExpr GhcTc           -- ^ expanded typechecked expression
    
    866
    +  -> HsExpr GhcTc           -- ^ suitably wrapped 'XXExprGhcTc'
    
    867
    +mkExpandedTc o e = XExpr (ExpandedThingTc (HSE o e))

  • compiler/GHC/Tc/Gen/App.hs
    ... ... @@ -957,8 +957,8 @@ addArgCtxt arg_no (app_head, app_head_lspan) (L arg_loc arg) thing_inside
    957 957
                                         , ppr arg
    
    958 958
                                         , ppr arg_no])
    
    959 959
            ; setSrcSpanA arg_loc $
    
    960
    -           addNthFunArgErrCtxt app_head arg arg_no $
    
    961
    -             thing_inside
    
    960
    +          addErrCtxt (FunAppCtxt (FunAppCtxtExpr app_head arg) arg_no) $
    
    961
    +            thing_inside
    
    962 962
            }
    
    963 963
       | otherwise
    
    964 964
       = do { traceTc "addArgCtxt" (vcat [text "generated Head"
    
    ... ... @@ -969,16 +969,6 @@ addArgCtxt arg_no (app_head, app_head_lspan) (L arg_loc arg) thing_inside
    969 969
            ; addLExprCtxt (locA arg_loc) arg $
    
    970 970
               thing_inside
    
    971 971
            }
    
    972
    - where
    
    973
    -    addNthFunArgErrCtxt :: HsExpr GhcRn -> HsExpr GhcRn -> Int -> TcM a -> TcM a
    
    974
    -    addNthFunArgErrCtxt app_head arg arg_no thing_inside
    
    975
    -      | XExpr (ExpandedThingRn _ _) <- arg
    
    976
    -      = addExpansionErrCtxt (FunAppCtxt (FunAppCtxtExpr app_head arg) arg_no) $
    
    977
    -          thing_inside
    
    978
    -      | otherwise
    
    979
    -      = addErrCtxt (FunAppCtxt (FunAppCtxtExpr app_head arg) arg_no) $
    
    980
    -          thing_inside
    
    981
    -
    
    982 972
     
    
    983 973
     
    
    984 974
     {- *********************************************************************
    
    ... ... @@ -1256,7 +1246,7 @@ expr_to_type earg =
    1256 1246
           | otherwise = not_in_scope
    
    1257 1247
           where occ = occName rdr
    
    1258 1248
                 not_in_scope = failWith $ TcRnNotInScope NotInScope rdr
    
    1259
    -    go (L l (XExpr (ExpandedThingRn (ExprCtxt orig) _))) =
    
    1249
    +    go (L l (XExpr (ExpandedThingRn (HSE (ExprCtxt orig) _)))) =
    
    1260 1250
           -- Use the original, user-written expression (before expansion).
    
    1261 1251
           -- Example. Say we have   vfun :: forall a -> blah
    
    1262 1252
           --          and the call  vfun (Maybe [1,2,3])
    
    ... ... @@ -1947,16 +1937,13 @@ quickLookArg1 :: Int -> SrcSpan -> (HsExpr GhcRn, SrcSpan) -> LHsExpr GhcRn
    1947 1937
                   -> Scaled TcRhoType  -- Deeply skolemised
    
    1948 1938
                   -> TcM (HsExprArg 'TcpInst)
    
    1949 1939
     -- quickLookArg1 implements the "QL Argument" judgement in Fig 5 of the paper
    
    1950
    -quickLookArg1 pos app_lspan (fun, fun_lspan) larg@(L arg_loc arg) sc_arg_ty@(Scaled _ orig_arg_rho)
    
    1940
    +quickLookArg1 pos app_lspan (fun, fun_lspan) larg@(L _ arg) sc_arg_ty@(Scaled _ orig_arg_rho)
    
    1951 1941
       = addArgCtxt pos (fun, fun_lspan) larg $ -- Context needed for constraints
    
    1952 1942
                                -- generated by calls in arg
    
    1953
    -    do { ((rn_fun_arg, fun_lspan_arg'), rn_args) <- splitHsApps arg
    
    1954
    -       ; let fun_lspan_arg | null rn_args = locA arg_loc -- arg is an id (or an XExpr) so use the arg_loc in tcInstFun
    
    1955
    -                           | otherwise = fun_lspan_arg'
    
    1943
    +    do { ((rn_fun_arg, fun_lspan_arg), rn_args) <- splitHsApps arg
    
    1956 1944
     
    
    1957 1945
            -- Step 1: get the type of the head of the argument
    
    1958
    -       ; (fun_ue, mb_fun_ty) <- maybe_update_err_ctxt fun_lspan_arg rn_fun_arg $
    
    1959
    -                                (tcCollectingUsage $ tcInferAppHead_maybe rn_fun_arg)
    
    1946
    +       ; (fun_ue, mb_fun_ty) <- (tcCollectingUsage $ tcInferAppHead_maybe rn_fun_arg)
    
    1960 1947
              -- tcCollectingUsage: the use of an Id at the head generates usage-info
    
    1961 1948
              -- See the call to `tcEmitBindingUsage` in `check_local_id`.  So we must
    
    1962 1949
              -- capture and save it in the `EValArgQL`.  See (QLA6) in
    
    ... ... @@ -1981,7 +1968,7 @@ quickLookArg1 pos app_lspan (fun, fun_lspan) larg@(L arg_loc arg) sc_arg_ty@(Sca
    1981 1968
                  <- captureConstraints $
    
    1982 1969
                     tcInstFun do_ql True ds_flag_arg (arg_orig, rn_fun_arg, fun_lspan_arg) tc_fun_arg_head fun_sigma_arg_head rn_args
    
    1983 1970
                     -- We must capture type-class and equality constraints here, but
    
    1984
    -                -- not equality constraints.  See (QLA6) in Note [Quick Look at
    
    1971
    +                -- not usage information.  See (QLA6) in Note [Quick Look at
    
    1985 1972
                     -- value arguments]
    
    1986 1973
     
    
    1987 1974
            ; traceTc "quickLookArg 2" $
    
    ... ... @@ -2025,20 +2012,6 @@ quickLookArg1 pos app_lspan (fun, fun_lspan) larg@(L arg_loc arg) sc_arg_ty@(Sca
    2025 2012
                                , eaql_res_rho  = app_res_rho }) }}}
    
    2026 2013
     
    
    2027 2014
     
    
    2028
    -maybe_update_err_ctxt :: SrcSpan -> HsExpr GhcRn ->  TcM a -> TcM a
    
    2029
    -maybe_update_err_ctxt fun_lspan_arg rn_fun_arg thing_inside
    
    2030
    -  | not (isGeneratedSrcSpan fun_lspan_arg)
    
    2031
    -  , XExpr (ExpandedThingRn{}) <- rn_fun_arg
    
    2032
    -  = do igc <- inGeneratedCode
    
    2033
    -       if igc
    
    2034
    -        then thing_inside
    
    2035
    -        else addLExprCtxt fun_lspan_arg rn_fun_arg $
    
    2036
    -               thing_inside
    
    2037
    -  | otherwise
    
    2038
    -  = thing_inside
    
    2039
    -
    
    2040
    -
    
    2041
    -
    
    2042 2015
     mk_origin :: SrcSpan       -- SrcSpan of the argument
    
    2043 2016
               -> HsExpr GhcRn  -- The head of the expression application chain
    
    2044 2017
               -> TcM CtOrigin
    

  • compiler/GHC/Tc/Gen/Do.hs
    ... ... @@ -14,8 +14,7 @@ module GHC.Tc.Gen.Do (expandDoStmts) where
    14 14
     
    
    15 15
     import GHC.Prelude
    
    16 16
     
    
    17
    -import GHC.Rename.Utils ( wrapGenSpan, genHsExpApps, genHsApp, genHsLet,
    
    18
    -                          genHsLamDoExp, genHsCaseAltDoExp, genWildPat )
    
    17
    +import GHC.Rename.Utils
    
    19 18
     import GHC.Rename.Env   ( irrefutableConLikeRn )
    
    20 19
     
    
    21 20
     import GHC.Tc.Utils.Monad
    
    ... ... @@ -46,7 +45,7 @@ import Data.List ((\\))
    46 45
     --   so that they can be typechecked.
    
    47 46
     --   See Note [Expanding HsDo with XXExprGhcRn] below for `HsDo` specific commentary
    
    48 47
     --   and Note [Handling overloaded and rebindable constructs] for high level commentary
    
    49
    -expandDoStmts :: HsDoFlavour -> [ExprLStmt GhcRn] -> TcM (LHsExpr GhcRn)
    
    48
    +expandDoStmts :: HsDoFlavour -> [ExprLStmt GhcRn] -> TcM (HsExpansion GhcRn)
    
    50 49
     expandDoStmts doFlav stmts = expand_do_stmts doFlav stmts
    
    51 50
     
    
    52 51
     -- | The main work horse for expanding do block statements into applications of binds and thens
    
    ... ... @@ -114,7 +113,7 @@ expand_do_stmts doFlavour (stmt@(L loc (BindStmt xbsrn pat e)): lstmts)
    114 113
       | otherwise
    
    115 114
       = pprPanic "expand_do_stmts: The impossible happened, missing bind operator from renamer" (text "stmt" <+> ppr  stmt)
    
    116 115
     
    
    117
    -expand_do_stmts doFlavour (stmt@(L loc (BodyStmt _ (L e_lspan e) (SyntaxExprRn then_op) _)) : lstmts) =
    
    116
    +expand_do_stmts doFlavour (stmt@(L loc (BodyStmt _ (L _ e) (SyntaxExprRn then_op) _)) : lstmts) =
    
    118 117
     -- See Note [BodyStmt] in Language.Haskell.Syntax.Expr
    
    119 118
     -- See  Note [Expanding HsDo with XXExprGhcRn] Equation (1) below
    
    120 119
     --              stmts ~~> stmts'
    
    ... ... @@ -122,7 +121,7 @@ expand_do_stmts doFlavour (stmt@(L loc (BodyStmt _ (L e_lspan e) (SyntaxExprRn t
    122 121
     --      e ; stmts ~~> (>>) e stmts'
    
    123 122
       do expand_stmts_expr <- expand_do_stmts doFlavour lstmts
    
    124 123
          let expansion = genHsExpApps then_op  -- (>>)
    
    125
    -                     [ L e_lspan (mkExpandedStmt stmt doFlavour e)
    
    124
    +                     [ wrapGenSpan e
    
    126 125
                          , expand_stmts_expr ]
    
    127 126
          return $ L loc (mkExpandedStmt stmt doFlavour expansion)
    
    128 127
     
    
    ... ... @@ -235,7 +234,7 @@ See Note [Handling overloaded and rebindable constructs] in GHC.Rename.Expr
    235 234
     To disabmiguate desugaring (`HsExpr GhcTc -> Core.Expr`) we use the phrase expansion
    
    236 235
     (`HsExpr GhcRn -> HsExpr GhcRn`)
    
    237 236
     
    
    238
    -This expansion is done right before typechecking and after renaming
    
    237
    +This expansion is done after renaming and before typechecking
    
    239 238
     See Part 2. of Note [Doing XXExprGhcRn in the Renamer vs Typechecker] in `GHC.Rename.Expr`
    
    240 239
     
    
    241 240
     Historical note START
    
    ... ... @@ -424,10 +423,10 @@ It stores the original statement (with location) and the expanded expression
    424 423
           ‹ExpandedThingRn do { e1; e2; e3 }›                        -- Original Do Expression
    
    425 424
                                                                      -- Expanded Do Expression
    
    426 425
               (‹ExpandedThingRn e1›                                  -- Original Statement
    
    427
    -               ({(>>) ‹ExpandedThingRn e1› e1}                           -- Expanded Expression
    
    426
    +               ({(>>) e1}                           -- Expanded Expression
    
    428 427
                           (‹ExpandedThingRn e2›
    
    429
    -                         ({(>>) ‹ExpandedThingRn e2› e2}
    
    430
    -                                (‹ExpandedThingRn e3› {e3})))))
    
    428
    +                         ({(>>) e2}
    
    429
    +                             (‹ExpandedThingRn e3› {e3})))))
    
    431 430
     
    
    432 431
       * Whenever the typechecker steps through an `ExpandedThingRn`,
    
    433 432
         we push the original statement in the error context, set the error location to the
    
    ... ... @@ -482,6 +481,4 @@ It stores the original statement (with location) and the expanded expression
    482 481
     
    
    483 482
     
    
    484 483
     mkExpandedPatRn :: LPat GhcRn -> HsExpr GhcRn -> HsExpr GhcRn
    
    485
    -mkExpandedPatRn pat e = XExpr $ ExpandedThingRn
    
    486
    -                                   { xrn_orig = StmtErrCtxtPat pat
    
    487
    -                                   , xrn_expanded = e}
    484
    +mkExpandedPatRn pat e = mkExpandedRn (StmtErrCtxtPat pat) (wrapGenSpan e)

  • compiler/GHC/Tc/Gen/Expr.hs
    ... ... @@ -36,6 +36,7 @@ import GHC.Rename.Env ( addUsedGRE, getUpdFieldLbls )
    36 36
     
    
    37 37
     import GHC.Tc.Gen.App
    
    38 38
     import GHC.Tc.Gen.Head
    
    39
    +import GHC.Tc.Gen.Do
    
    39 40
     import GHC.Tc.Gen.Bind        ( tcLocalBinds )
    
    40 41
     import GHC.Tc.Gen.HsType
    
    41 42
     import GHC.Tc.Gen.Arrow
    
    ... ... @@ -92,6 +93,8 @@ import GHC.Data.Maybe
    92 93
     import Control.Monad
    
    93 94
     import qualified Data.List.NonEmpty as NE
    
    94 95
     
    
    96
    +import qualified GHC.LanguageExtensions as LangExt
    
    97
    +
    
    95 98
     {-
    
    96 99
     ************************************************************************
    
    97 100
     *                                                                      *
    
    ... ... @@ -267,13 +270,6 @@ tcCheckMonoExpr, tcCheckMonoExprNC
    267 270
     tcCheckMonoExpr   expr res_ty = tcMonoLExpr  expr (mkCheckExpType res_ty)
    
    268 271
     tcCheckMonoExprNC expr res_ty = tcMonoLExprNC expr (mkCheckExpType res_ty)
    
    269 272
     
    
    270
    -
    
    271
    --- Expand the HsExpr if it is typechecked after expansions
    
    272
    --- See Note [Handling overloaded and rebindable constructs]
    
    273
    --- See Note [Typechecking by expansion: overview]
    
    274
    -expand_expr :: HsExpr GhcRn -> TcM (HsExpr GhcRn)
    
    275
    -expand_expr x = return x
    
    276
    -
    
    277 273
     ---------------
    
    278 274
     tcMonoLExpr, tcMonoLExprNC
    
    279 275
         :: LHsExpr GhcRn     -- Expression to type check
    
    ... ... @@ -282,8 +278,7 @@ tcMonoLExpr, tcMonoLExprNC
    282 278
         -> TcM (LHsExpr GhcTc)
    
    283 279
     
    
    284 280
     tcMonoLExpr (L loc expr) res_ty
    
    285
    -  = do expanded_expr <- expand_expr expr
    
    286
    -       addLExprCtxt (locA loc) expanded_expr $  -- Note [Error contexts in generated code]
    
    281
    +  = do addLExprCtxt (locA loc) expr $  -- Note [Error contexts in generated code]
    
    287 282
              do  { expr' <- tcExpr expr res_ty
    
    288 283
                  ; return (L loc expr') }
    
    289 284
     
    
    ... ... @@ -321,7 +316,8 @@ tcExpr e@(OpApp {}) res_ty = tcApp e res_ty
    321 316
     tcExpr e@(HsAppType {})          res_ty = tcApp e res_ty
    
    322 317
     tcExpr e@(ExprWithTySig {})      res_ty = tcApp e res_ty
    
    323 318
     
    
    324
    -tcExpr (XExpr e')                res_ty = tcXExpr e' res_ty
    
    319
    +tcExpr (XExpr (ExpandedThingRn hse)) res_ty = tcXExpr hse res_ty
    
    320
    +tcExpr e@(XExpr{})                res_ty = tcApp e res_ty
    
    325 321
     
    
    326 322
     -- Typecheck an occurrence of an unbound Id
    
    327 323
     --
    
    ... ... @@ -562,7 +558,22 @@ tcExpr (HsMultiIf _ alts) res_ty
    562 558
            ; res_ty <- readExpType res_ty
    
    563 559
            ; return (HsMultiIf res_ty alts') }
    
    564 560
     
    
    565
    -tcExpr (HsDo _ do_or_lc stmts) res_ty
    
    561
    +tcExpr expr@(HsDo _ do_or_lc stmts) res_ty
    
    562
    +  | DoExpr{} <- do_or_lc
    
    563
    +  -- ApplicativeDo are typechecked using tcDoStmts
    
    564
    +  = do isApplicativeDo <- xoptM LangExt.ApplicativeDo
    
    565
    +       if isApplicativeDo
    
    566
    +         then tcDoStmts do_or_lc stmts res_ty
    
    567
    +         -- Expand expression on the fly otherwise
    
    568
    +         -- See Note [Typechecking by expansion: overview]
    
    569
    +         else do { expr' <- expandDoStmts do_or_lc (unLoc stmts)
    
    570
    +                 ; tcLExpr expr' res_ty }
    
    571
    +  | MDoExpr{} <- do_or_lc
    
    572
    +  = do expr' <- expandDoStmts do_or_lc (unLoc stmts)
    
    573
    +       tcExpr expr' res_ty
    
    574
    +  | otherwise
    
    575
    +  --
    
    576
    +
    
    566 577
       = tcDoStmts do_or_lc stmts res_ty
    
    567 578
     
    
    568 579
     tcExpr (HsProc x pat cmd) res_ty
    
    ... ... @@ -678,7 +689,7 @@ tcExpr expr@(RecordUpd { rupd_expr = record_expr
    678 689
     
    
    679 690
             ; (ds_expr, ds_res_ty, err_msg)
    
    680 691
                 <- expandRecordUpd record_expr possible_parents rbnds res_ty
    
    681
    -        ; addExpansionErrCtxt err_msg $
    
    692
    +        ; addErrCtxt err_msg $
    
    682 693
               do { -- Typecheck the expanded expression.
    
    683 694
                    expr' <- tcExpr ds_expr (Check ds_res_ty)
    
    684 695
                    -- NB: it's important to use ds_res_ty and not res_ty here.
    
    ... ... @@ -776,12 +787,12 @@ directly, it's much easier to
    776 787
     
    
    777 788
     Example: record updates.  The typechecker looks like this:
    
    778 789
     
    
    779
    -   tcExpr e@(RecordUpd{}) rho = do { ee <- expandExpr e
    
    780
    -                                   ; tcExpr ee rho }
    
    790
    +   tcExpr e@(HsDo{}) rho = do { ee <- expandExpr e
    
    791
    +                              ; tcExpr ee rho }
    
    781 792
     
    
    782
    -The `expandExpr` replaces the record update (e { x = rhs })
    
    793
    +The `expandExpr` replaces the HsDo { x <- e1; return x }
    
    783 794
     with something like
    
    784
    -   case e of { MkT a b _ d -> MkT a b rhs d }
    
    795
    +   e1 >>= \ x -> x
    
    785 796
     and we then typecheck the latter.
    
    786 797
     
    
    787 798
     See also Note [Handling overloaded and rebindable constructs]
    
    ... ... @@ -798,18 +809,23 @@ The rest of this Note explains how that is done.
    798 809
                                            , xrn_expanded = ee } ))
    
    799 810
       where `ee` is the expansion of the user written thing `ue`
    
    800 811
     
    
    801
    -* The type checker context has 2 key fields that describe the context:
    
    812
    +* The type checker context has 3 key fields that describe the context:
    
    802 813
          TcLclCtxt { tcl_loc      :: RealSrcSpan
    
    814
    +               , tcl_in_gen_code :: Bool
    
    803 815
                    , tcl_err_ctxt :: [ErrCtxt]
    
    804 816
                    , ... }
    
    805 817
       Note `tcl_loc` always points to a real place in the source code,
    
    806 818
       hence `RealSrcSpan`.
    
    807 819
     
    
    808 820
       The `tcl_err_ctxt` is a stack of contexts, each saying something
    
    809
    -  like "In the expression: x+y" or "In the record update: r { x=2 }"
    
    821
    +  like "In the expression: x+y" or "In second argument of `$` namely 'r { x=2 }'"
    
    822
    +
    
    823
    +  The `tcl_in_gen_code` is a boolean that keeps track of whether
    
    824
    +  the current expression being typechecked is compiler generated
    
    825
    +  or user generated.
    
    810 826
     
    
    811 827
     * Now, when
    
    812
    -      tcMonoLHsExpr :: LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
    
    828
    +      tcMonoLExpr :: LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
    
    813 829
       gets a located expression, it does 2 things:
    
    814 830
         * Calls `addLExprCtxt` to perform error context management
    
    815 831
         * Calls `tcExpr` to typecheck the expression.
    
    ... ... @@ -834,16 +850,12 @@ The rest of this Note explains how that is done.
    834 850
     
    
    835 851
     -}
    
    836 852
     
    
    837
    -tcXExpr :: XXExprGhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
    
    838
    -tcXExpr (ExpandedThingRn o e) res_ty
    
    853
    +tcXExpr :: HsExpansion GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
    
    854
    +tcXExpr (HSE o e) res_ty
    
    839 855
        = mkExpandedTc o <$> -- necessary for hpc ticks
    
    840 856
              -- Need to call tcExpr and not tcApp
    
    841 857
              -- as e can be let statement which tcApp cannot gracefully handle
    
    842
    -         tcExpr e res_ty
    
    843
    -
    
    844
    --- For record selection, same as HsVar case
    
    845
    -tcXExpr xe res_ty = tcApp (XExpr xe) res_ty
    
    846
    -
    
    858
    +         tcMonoLExpr e res_ty
    
    847 859
     
    
    848 860
     {-
    
    849 861
     ************************************************************************
    
    ... ... @@ -1846,3 +1858,14 @@ checkMissingFields con_like rbinds arg_tys
    1846 1858
         field_strs = conLikeImplBangs con_like
    
    1847 1859
     
    
    1848 1860
         fl `elemField` flds = any (\ fl' -> flSelector fl == fl') flds
    
    1861
    +
    
    1862
    +
    
    1863
    +-- Expands the expression on the fly
    
    1864
    +-- See Note [Handling overloaded and rebindable constructs]
    
    1865
    +-- See Note [Typechecking by expansion: overview]
    
    1866
    +tcExpandExpr :: HsExpr GhcRn -> TcM (HsExpr GhcRn)
    
    1867
    +tcExpandExpr orig_expr@(HsDo _ flav (L _ stmts))
    
    1868
    +  = do { expanded_expr <- expandDoStmts flav stmts
    
    1869
    +       ; return (mkExpandedLExpr orig_expr expanded_expr) }
    
    1870
    +
    
    1871
    +tcExpandExpr e = return e

  • compiler/GHC/Tc/Gen/Head.hs
    ... ... @@ -29,6 +29,8 @@ import GHC.Prelude
    29 29
     import GHC.Hs
    
    30 30
     import GHC.Hs.Syn.Type
    
    31 31
     
    
    32
    +import GHC.Rename.Utils (mkExpandedTc, mkExpandedExprTc)
    
    33
    +
    
    32 34
     import GHC.Tc.Gen.HsType
    
    33 35
     import GHC.Tc.Gen.Bind( chooseInferredQuantifiers )
    
    34 36
     import GHC.Tc.Gen.Sig( tcUserTypeSig, tcInstSig )
    
    ... ... @@ -460,12 +462,12 @@ tcInferAppHead_maybe fun = case fun of
    460 462
           ExprWithTySig _ e hs_ty     -> Just <$> with_get_ds (tcExprWithSig e hs_ty)
    
    461 463
           HsOverLit _ lit             -> Just <$> with_get_ds (tcInferOverLit lit)
    
    462 464
           XExpr (HsRecSelRn f)        -> Just <$> with_get_ds (tcInferRecSelId f)
    
    463
    -      XExpr (ExpandedThingRn o e) -> Just <$> (
    
    465
    +      XExpr (ExpandedThingRn (HSE o (L loc e))) -> setSrcSpan (locA loc) $ Just <$>  (
    
    464 466
                                                   -- We do not want to instantiate the type of the head as there may be
    
    465 467
                                                   -- visible type applications in the argument.
    
    466 468
                                                   -- c.f. T19167
    
    467
    -                                              (\ (e, ds_flag, ty) -> (mkExpandedTc o e, ds_flag, ty)) <$>
    
    468
    -                                                 tcExprSigma False (errCtxtCtOrigin o) e
    
    469
    +                                              (\ (e, ds_flag, ty) -> (mkExpandedTc o (L loc e), ds_flag, ty)) <$>
    
    470
    +                                                  tcExprSigma False (errCtxtCtOrigin o) e
    
    469 471
                                                   )
    
    470 472
           _                           -> return Nothing
    
    471 473
     
    

  • compiler/GHC/Tc/Gen/Match.hs
    ... ... @@ -35,7 +35,7 @@ where
    35 35
     import GHC.Prelude
    
    36 36
     
    
    37 37
     import {-# SOURCE #-}   GHC.Tc.Gen.Expr( tcSyntaxOp, tcInferRho, tcInferRhoFRRNC
    
    38
    -                                       , tcMonoLExprNC, tcMonoLExpr, tcExpr
    
    38
    +                                       , tcMonoLExprNC, tcExpr
    
    39 39
                                            , tcCheckMonoExpr, tcCheckMonoExprNC
    
    40 40
                                            , tcCheckPolyExpr, tcPolyLExpr )
    
    41 41
     
    
    ... ... @@ -44,7 +44,6 @@ import GHC.Tc.Errors.Types
    44 44
     import GHC.Tc.Utils.Monad
    
    45 45
     import GHC.Tc.Utils.Env
    
    46 46
     import GHC.Tc.Gen.Pat
    
    47
    -import GHC.Tc.Gen.Do
    
    48 47
     import GHC.Tc.Gen.Head( tcCheckId )
    
    49 48
     import GHC.Tc.Utils.TcMType
    
    50 49
     import GHC.Tc.Utils.TcType
    
    ... ... @@ -391,32 +390,14 @@ tcDoStmts MonadComp (L l stmts) res_ty
    391 390
             ; res_ty <- readExpType res_ty
    
    392 391
             ; return (HsDo res_ty MonadComp (L l stmts')) }
    
    393 392
     
    
    394
    -tcDoStmts ctxt@GhciStmtCtxt _ _ = pprPanic "tcDoStmts" (pprHsDoFlavour ctxt)
    
    395
    -
    
    396
    -tcDoStmts doExpr@(DoExpr _) ss@(L l stmts) res_ty
    
    397
    -  = do  { isApplicativeDo <- xoptM LangExt.ApplicativeDo
    
    398
    -        ; if isApplicativeDo
    
    399
    -          then do { stmts' <- tcStmts (HsDoStmt doExpr) tcDoStmt stmts res_ty
    
    400
    -                  ; res_ty <- readExpType res_ty
    
    401
    -                  ; return (HsDo res_ty doExpr (L l stmts')) }
    
    402
    -          else do { expanded_expr <- expandDoStmts doExpr stmts -- Do expansion on the fly
    
    403
    -                  ; traceTc "tcDoStmts" (ppr expanded_expr)
    
    404
    -                  ; let orig = HsDo noExtField doExpr ss
    
    405
    -                  ; mkExpandedExprTc orig <$> (
    
    406
    -                       -- We lose the location on the first statement location in GhcTc, unfortunately.
    
    407
    -                       -- It is needed for get the pattern match warnings right cf. T14546d
    
    408
    -                       -- That location is currently recovered from the location stored in OrigStmt
    
    409
    -                       -- in dsExpr of ExpandedThingTc
    
    410
    -                        unLoc <$> tcMonoLExpr expanded_expr res_ty)
    
    411
    -                  }
    
    412
    -        }
    
    413 393
     
    
    414
    -tcDoStmts mDoExpr ss@(L _ stmts) res_ty
    
    415
    -  = do  { expanded_expr <- expandDoStmts mDoExpr stmts -- Do expansion on the fly
    
    416
    -        ; let orig = HsDo noExtField mDoExpr ss
    
    417
    -        ; e' <- tcMonoLExpr expanded_expr res_ty
    
    418
    -        ; return (mkExpandedExprTc orig (unLoc e'))
    
    419
    -        }
    
    394
    +tcDoStmts doExpr@(DoExpr _) (L l stmts) res_ty
    
    395
    +  = do { stmts' <- tcStmts (HsDoStmt doExpr) tcDoStmt stmts res_ty
    
    396
    +       ; res_ty <- readExpType res_ty
    
    397
    +       ; return (HsDo res_ty doExpr (L l stmts')) }
    
    398
    +
    
    399
    +-- NB: ghcistmts should fail, MDoExpr is handled by expansions
    
    400
    +tcDoStmts ctxt _ _ = pprPanic "tcDoStmts" (pprHsDoFlavour ctxt)
    
    420 401
     
    
    421 402
     tcBody :: LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTc)
    
    422 403
     tcBody body res_ty
    

  • compiler/GHC/Tc/Types/Origin.hs
    ... ... @@ -625,7 +625,7 @@ exprCtOrigin (HsIf {}) = IfThenElseOrigin
    625 625
     exprCtOrigin (HsProjection _ p)   = RecordFieldProjectionOrigin (FieldLabelStrings $ fmap noLocA p)
    
    626 626
     exprCtOrigin (RecordUpd{})        = RecordUpdOrigin
    
    627 627
     exprCtOrigin (HsGetField _ _ f)   = GetFieldOrigin (fmap field_label $ dfoLabel (unLoc f))
    
    628
    -exprCtOrigin (XExpr (ExpandedThingRn o _)) = errCtxtCtOrigin o
    
    628
    +exprCtOrigin (XExpr (ExpandedThingRn (HSE o _))) = errCtxtCtOrigin o
    
    629 629
     exprCtOrigin (XExpr (HsRecSelRn f))  = OccurrenceOfRecSel $ L (getLoc $ foLabel f) (foExt f)
    
    630 630
     
    
    631 631
     srcCodeOriginCtOrigin :: HsCtxt -> CtOrigin
    

  • compiler/GHC/Tc/Utils/Monad.hs
    ... ... @@ -88,7 +88,7 @@ module GHC.Tc.Utils.Monad(
    88 88
     
    
    89 89
       -- * Context management for the type checker
    
    90 90
       getErrCtxt, setErrCtxt, addErrCtxt,
    
    91
    -  addLExprCtxt, addExpansionErrCtxt,
    
    91
    +  addLExprCtxt,
    
    92 92
       popErrCtxt, getCtLocM, setCtLocM, mkCtLocEnv,
    
    93 93
     
    
    94 94
       -- * Diagnostic message generation (type checker)
    
    ... ... @@ -1089,7 +1089,7 @@ setSrcSpan :: SrcSpan -> TcRn a -> TcRn a
    1089 1089
     setSrcSpan (RealSrcSpan loc _) thing_inside
    
    1090 1090
       = updLclCtxt (\ctxt -> ctxt {tcl_loc = loc, tcl_in_gen_code = False}) thing_inside
    
    1091 1091
     setSrcSpan (GeneratedSrcSpan{}) thing_inside
    
    1092
    -  = setInGeneratedCode $ thing_inside
    
    1092
    +  = updLclCtxt (\ctxt -> ctxt {tcl_in_gen_code = True}) thing_inside
    
    1093 1093
     setSrcSpan _ thing_inside
    
    1094 1094
       = thing_inside
    
    1095 1095
     
    
    ... ... @@ -1316,11 +1316,16 @@ problem.
    1316 1316
     
    
    1317 1317
     Note [Error contexts in generated code]
    
    1318 1318
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1319
    -* If the `SrcSpan` is a `RealSrcSpan`, `setSrcSpan` updates the `tcl_loc`,
    
    1320
    -  and makes the `ErrCtxStack` a `UserCodeCtxt`
    
    1321
    -* it is a no-op otherwise
    
    1322 1319
     
    
    1323
    -So, it's better to do a `setSrcSpan` /before/ `addErrCtxt`.
    
    1320
    +* addLExpr updates updates the ErrCtxt stored in LclEnv with the following logic
    
    1321
    +  - If the `SrcSpan` is a `RealSrcSpan`, `setSrcSpan` updates the `tcl_loc` to the given value
    
    1322
    +    and sets `tcl_in_gen_code` to `False`. Meaning we are not type checking a compiler generated
    
    1323
    +    expression. And thus it can add the expression on to the ErrCtxt Stack
    
    1324
    +  - If the `SrcSpan` is a GeneratedSrcSpan then `tcl_in_gen_code` is set to `True`, meaning
    
    1325
    +    the expression in hand is compiler generated, and hence it is not added on to the stack.
    
    1326
    +
    
    1327
    +This ensures that the error messages do not leak compiler generated expressions which can
    
    1328
    +be confusing to the users.
    
    1324 1329
     
    
    1325 1330
     - See Note [Rebindable syntax and XXExprGhcRn] in `GHC.Hs.Expr` for
    
    1326 1331
     more discussion of this fancy footwork
    
    ... ... @@ -1329,33 +1334,32 @@ relation with pattern-match checks
    1329 1334
     - See Note [ErrCtxtStack Manipulation] in `GHC.Tc.Types.LclEnv` for info about `ErrCtxtStack`
    
    1330 1335
     -}
    
    1331 1336
     
    
    1337
    +-- See Note [Error contexts in generated code]
    
    1332 1338
     addLExprCtxt :: SrcSpan -> HsExpr GhcRn -> TcRn a -> TcRn a
    
    1333 1339
     addLExprCtxt lspan e thing_inside
    
    1334
    -  | not (isGeneratedSrcSpan lspan)
    
    1335 1340
       = setSrcSpan lspan $ add_expr_ctxt e thing_inside
    
    1336
    -  | otherwise   -- no op in generated code
    
    1337
    -  = thing_inside
    
    1338 1341
         where
    
    1339
    -       add_expr_ctxt :: HsExpr GhcRn -> TcRn a -> TcRn a
    
    1340
    -       add_expr_ctxt e thing_inside
    
    1341
    -         = case e of
    
    1342
    -             -- The HsHole special case addresses situations like
    
    1343
    -             --    f x = _
    
    1344
    -             -- when we don't want to say "In the expression: _",
    
    1345
    -             -- because it is mentioned in the error message itself
    
    1346
    -             HsHole{} -> thing_inside
    
    1347
    -
    
    1348
    -             -- There is a special case for expressions with signatures to avoid having too verbose
    
    1349
    -             -- error context. So here we flip the ErrCtxt state to expanded if the expression is expanded.
    
    1350
    -             -- c.f. RecordDotSyntaxFail9
    
    1351
    -             ExprWithTySig _ (L _ e') _
    
    1352
    -               | XExpr (ExpandedThingRn o _) <- e' -> addExpansionErrCtxt o thing_inside
    
    1353
    -
    
    1354
    -             -- Flip error ctxt into expansion mode
    
    1355
    -             XExpr (ExpandedThingRn o _) -> addExpansionErrCtxt o thing_inside
    
    1356
    -
    
    1357
    -             _ -> addErrCtxt (ExprCtxt e) thing_inside
    
    1358
    -
    
    1342
    +      add_expr_ctxt :: HsExpr GhcRn -> TcRn a -> TcRn a
    
    1343
    +      add_expr_ctxt e thing_inside
    
    1344
    +         = do { igc <- inGeneratedCode
    
    1345
    +              ; if igc -- generated
    
    1346
    +                then thing_inside
    
    1347
    +                else case e of
    
    1348
    +                       -- The HsHole special case addresses situations like
    
    1349
    +                       --    f x = _
    
    1350
    +                       -- when we don't want to say "In the expression: _",
    
    1351
    +                       -- because it is mentioned in the error message itself
    
    1352
    +                       HsHole{} -> thing_inside
    
    1353
    +
    
    1354
    +                     -- There is a special case for expressions with signatures to avoid having too verbose
    
    1355
    +                     -- error context. c.f. RecordDotSyntaxFail9
    
    1356
    +                     -- Add the original HsCtxt if we are typechecking an expanded expression
    
    1357
    +                       ExprWithTySig _ (L _ e') _
    
    1358
    +                         | XExpr (ExpandedThingRn (HSE o _)) <- e' -> addErrCtxt o thing_inside
    
    1359
    +                       XExpr (ExpandedThingRn (HSE o _)) -> addErrCtxt o thing_inside
    
    1360
    +
    
    1361
    +                       _ -> addErrCtxt (ExprCtxt e) thing_inside
    
    1362
    +              }
    
    1359 1363
     
    
    1360 1364
     getErrCtxt :: TcM [ErrCtxt]
    
    1361 1365
     getErrCtxt = do { env <- getLclEnv; return (getLclEnvErrCtxt env) }
    
    ... ... @@ -1369,11 +1373,6 @@ addErrCtxt :: HsCtxt -> TcM a -> TcM a
    1369 1373
     {-# INLINE addErrCtxt #-}  -- Note [Inlining addErrCtxt]
    
    1370 1374
     addErrCtxt ctxt = pushCtxt ctxt
    
    1371 1375
     
    
    1372
    --- See Note [ErrCtxtStack Manipulation]
    
    1373
    -addExpansionErrCtxt :: HsCtxt -> TcM a -> TcM a
    
    1374
    -{-# INLINE addExpansionErrCtxt #-}  -- Note [Inlining addErrCtxt]
    
    1375
    -addExpansionErrCtxt ctxt thing_inside = setInGeneratedCode $ pushCtxt ctxt thing_inside
    
    1376
    -
    
    1377 1376
     -- See Note [Rebindable syntax and XXExprGhcRn] in GHC.Hs.Expr
    
    1378 1377
     pushCtxt :: ErrCtxt -> TcM a -> TcM a
    
    1379 1378
     {-# INLINE pushCtxt #-} -- Note [Inlining addErrCtxt]
    

  • compiler/GHC/Tc/Utils/Unify.hs
    ... ... @@ -105,7 +105,7 @@ import GHC.Types.Var.Set
    105 105
     import GHC.Types.Var.Env
    
    106 106
     import GHC.Types.Basic
    
    107 107
     import GHC.Types.Unique.Set (nonDetEltsUniqSet)
    
    108
    -import GHC.Types.SrcLoc (unLoc)
    
    108
    +import GHC.Types.SrcLoc (unLoc, GenLocated (..))
    
    109 109
     
    
    110 110
     import GHC.Utils.Misc
    
    111 111
     import GHC.Utils.Outputable as Outputable
    
    ... ... @@ -2047,7 +2047,7 @@ getDeepSubsumptionFlag_DataConHead app_head =
    2047 2047
         go app_head
    
    2048 2048
          | XExpr (ConLikeTc (RealDataCon {})) <- app_head
    
    2049 2049
          = Deep TopSub
    
    2050
    -     | XExpr (ExpandedThingTc _ f) <- app_head
    
    2050
    +     | XExpr (ExpandedThingTc (HSE _ (L _ f))) <- app_head
    
    2051 2051
          = go f
    
    2052 2052
          | XExpr (WrapExpr _ f) <- app_head
    
    2053 2053
          = go f
    

  • compiler/GHC/Tc/Zonk/Type.hs
    ... ... @@ -1095,9 +1095,9 @@ zonkExpr (XExpr (WrapExpr co_fn expr))
    1095 1095
         do new_expr <- zonkExpr expr
    
    1096 1096
            return (XExpr (WrapExpr new_co_fn new_expr))
    
    1097 1097
     
    
    1098
    -zonkExpr (XExpr (ExpandedThingTc thing e))
    
    1099
    -  = do e' <- zonkExpr e
    
    1100
    -       return $ XExpr (ExpandedThingTc thing e')
    
    1098
    +zonkExpr (XExpr (ExpandedThingTc (HSE thing e)))
    
    1099
    +  = do e' <- zonkLExpr e
    
    1100
    +       return $ XExpr (ExpandedThingTc (HSE thing e'))
    
    1101 1101
     
    
    1102 1102
     zonkExpr e@(XExpr (ConLikeTc {}))
    
    1103 1103
       = return e
    

  • testsuite/tests/ghci/prog-mhu001/prog-mhu001c.stdout
    ... ... @@ -9,6 +9,7 @@ e/E.hs:(15,3)-(15,6): GHC.Internal.Types.Int -> GHC.Internal.Base.String
    9 9
     e/E.hs:(22,3)-(22,6): E.E -> GHC.Internal.Base.String
    
    10 10
     e/E.hs:(25,3)-(25,10): GHC.Internal.Base.String -> GHC.Internal.Types.IO ()
    
    11 11
     e/E.hs:(25,12)-(25,37): GHC.Internal.Base.String
    
    12
    +e/E.hs:(25,3)-(25,37): GHC.Internal.Types.IO ()
    
    12 13
     e/E.hs:(24,16)-(25,37): GHC.Internal.Types.IO ()
    
    13 14
     e/E.hs:(19,9)-(19,9): E.E
    
    14 15
     e/E.hs:(5,7)-(5,8): GHC.Internal.Bignum.Integer.Integer
    

  • testsuite/tests/parser/should_fail/RecordDotSyntaxFail11.stderr
    ... ... @@ -18,7 +18,9 @@ RecordDotSyntaxFail11.hs:8:11: error: [GHC-39999]
    18 18
         • No instance for ‘GHC.Internal.Records.HasField "baz" Int a0’
    
    19 19
             arising from the record selector ‘foo.bar.baz’
    
    20 20
           NB: ‘Int’ is not a record type.
    
    21
    -    • In the expression: (.foo.bar.baz)
    
    22
    -      In the second argument of ‘($)’, namely ‘(.foo.bar.baz) a’
    
    21
    +    • In the second argument of ‘($)’, namely ‘(.foo.bar.baz) a’
    
    23 22
           In a stmt of a 'do' block: print $ (.foo.bar.baz) a
    
    23
    +      In the expression:
    
    24
    +        do let a = Foo {foo = ...}
    
    25
    +           print $ (.foo.bar.baz) a
    
    24 26