Apoorv Ingle pushed to branch wip/ani/tc-expand at Glasgow Haskell Compiler / GHC
Commits:
-
25625e1e
by Apoorv Ingle at 2026-03-18T18:17:17-05:00
17 changed files:
- compiler/GHC/Hs/Expr.hs
- compiler/GHC/Hs/Instances.hs
- compiler/GHC/Hs/Syn/Type.hs
- compiler/GHC/HsToCore/Expr.hs
- compiler/GHC/HsToCore/Match.hs
- compiler/GHC/HsToCore/Quote.hs
- compiler/GHC/HsToCore/Ticks.hs
- compiler/GHC/Iface/Ext/Ast.hs
- compiler/GHC/Rename/Utils.hs
- compiler/GHC/Tc/Gen/App.hs
- compiler/GHC/Tc/Gen/Do.hs
- compiler/GHC/Tc/Gen/Expr.hs
- compiler/GHC/Tc/Gen/Head.hs
- compiler/GHC/Tc/Types/Origin.hs
- compiler/GHC/Tc/Utils/Monad.hs
- compiler/GHC/Tc/Utils/Unify.hs
- compiler/GHC/Tc/Zonk/Type.hs
Changes:
| ... | ... | @@ -660,10 +660,14 @@ 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 :: LHsExpr GhcRn } -- The compiler generated, expanded thing
|
|
| 666 | - -- This is located because of do statements (TODO ANI : Add Note)
|
|
| 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
|
| ... | ... | @@ -673,11 +677,7 @@ data XXExprGhcTc |
| 673 | 677 | = WrapExpr -- Type and evidence application and abstractions
|
| 674 | 678 | HsWrapper (HsExpr GhcTc)
|
| 675 | 679 | |
| 676 | - | ExpandedThingTc -- See Note [Rebindable syntax and XXExprGhcRn]
|
|
| 677 | - -- See Note [Expanding HsDo with XXExprGhcRn] in `GHC.Tc.Gen.Do`
|
|
| 678 | - { xtc_orig :: HsCtxt -- The original user written thing
|
|
| 679 | - , xtc_expanded :: LHsExpr GhcTc } -- The expanded typechecked expression
|
|
| 680 | - -- This is located because of do statements (TODO ANI: Add NOTE)
|
|
| 680 | + | ExpandedThingTc (HsExpansion GhcTc) -- ^ Typechecked expanded expression
|
|
| 681 | 681 | |
| 682 | 682 | | ConLikeTc
|
| 683 | 683 | -- ^ A 'ConLike', either a data constructor or pattern synonym
|
| ... | ... | @@ -1019,9 +1019,15 @@ ppr_expr (XExpr x) = case ghcPass @p of |
| 1019 | 1019 | GhcRn -> ppr x
|
| 1020 | 1020 | GhcTc -> ppr x
|
| 1021 | 1021 | |
| 1022 | -instance Outputable XXExprGhcRn where
|
|
| 1023 | - ppr (HsRecSelRn f) = pprPrefixOcc f
|
|
| 1024 | - 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)
|
|
| 1025 | 1031 | where
|
| 1026 | 1032 | ppr_builder prefix x = ifPprDebug (braces (text prefix <+> parens x)) x
|
| 1027 | 1033 | pprCtxt :: HsCtxt -> SDoc
|
| ... | ... | @@ -1031,26 +1037,17 @@ instance Outputable XXExprGhcRn where |
| 1031 | 1037 | pprCtxt (FunAppCtxt (FunAppCtxtExpr _ e) _) = ppr_builder "<FunAppCtxt>:" (ppr e)
|
| 1032 | 1038 | pprCtxt _ = ppr_builder "<MiscHsCtxt>:" empty
|
| 1033 | 1039 | |
| 1040 | +instance Outputable XXExprGhcRn where
|
|
| 1041 | + ppr (HsRecSelRn f) = pprPrefixOcc f
|
|
| 1042 | + ppr (ExpandedThingRn hse) = ppr_hse hse
|
|
| 1043 | + |
|
| 1044 | + |
|
| 1034 | 1045 | instance Outputable XXExprGhcTc where
|
| 1035 | 1046 | ppr (WrapExpr co_fn e)
|
| 1036 | 1047 | = pprHsWrapper co_fn (\_parens -> pprExpr e)
|
| 1037 | 1048 | |
| 1038 | - ppr (ExpandedThingTc o e)
|
|
| 1039 | - = ifPprDebug (braces $ vcat [pprCtxt o, ppr e]) (pprCtxt o)
|
|
| 1040 | - |
|
| 1041 | - where
|
|
| 1042 | - ppr_builder prefix x = ifPprDebug (braces (text prefix <+> parens x)) x
|
|
| 1043 | - pprCtxt :: HsCtxt -> SDoc
|
|
| 1044 | - pprCtxt (ExprCtxt e) = ppr_builder "<OrigExpr>:" (ppr e)
|
|
| 1045 | - pprCtxt (StmtErrCtxt _ stmt) = ppr_builder "<OrigStmt>:" (ppr stmt)
|
|
| 1046 | - pprCtxt (StmtErrCtxtPat pat) = ppr_builder "<OrigPat>:" (ppr pat)
|
|
| 1047 | - pprCtxt (FunAppCtxt (FunAppCtxtExpr _ e) _) = ppr_builder "<FunAppCtxt>:" (ppr e)
|
|
| 1048 | - pprCtxt _ = ppr_builder "<MiscHsCtxt>:" empty
|
|
| 1049 | - |
|
| 1050 | - -- e is the expanded expression, we print the original
|
|
| 1051 | - -- expression (HsExpr GhcRn), not the
|
|
| 1052 | - -- expanded typechecked one (HsExpr GhcTc),
|
|
| 1053 | - -- unless we are in ppr's debug mode printed both
|
|
| 1049 | + ppr (ExpandedThingTc hse)
|
|
| 1050 | + = ppr_hse hse
|
|
| 1054 | 1051 | |
| 1055 | 1052 | ppr (ConLikeTc con) = pprPrefixOcc con
|
| 1056 | 1053 | -- Used in error messages generated by
|
| ... | ... | @@ -1083,12 +1080,12 @@ ppr_infix_expr (XExpr x) = case ghcPass @p of |
| 1083 | 1080 | ppr_infix_expr _ = Nothing
|
| 1084 | 1081 | |
| 1085 | 1082 | ppr_infix_expr_rn :: XXExprGhcRn -> Maybe SDoc
|
| 1086 | -ppr_infix_expr_rn (ExpandedThingRn thing _) = ppr_infix_hs_expansion thing
|
|
| 1083 | +ppr_infix_expr_rn (ExpandedThingRn (HSE thing _)) = ppr_infix_hs_expansion thing
|
|
| 1087 | 1084 | ppr_infix_expr_rn (HsRecSelRn f) = Just (pprInfixOcc f)
|
| 1088 | 1085 | |
| 1089 | 1086 | ppr_infix_expr_tc :: XXExprGhcTc -> Maybe SDoc
|
| 1090 | 1087 | ppr_infix_expr_tc (WrapExpr _ e) = ppr_infix_expr e
|
| 1091 | -ppr_infix_expr_tc (ExpandedThingTc thing _) = ppr_infix_hs_expansion thing
|
|
| 1088 | +ppr_infix_expr_tc (ExpandedThingTc (HSE thing _)) = ppr_infix_hs_expansion thing
|
|
| 1092 | 1089 | ppr_infix_expr_tc (ConLikeTc con) = Just (pprInfixOcc (conLikeName con))
|
| 1093 | 1090 | ppr_infix_expr_tc (HsTick {}) = Nothing
|
| 1094 | 1091 | ppr_infix_expr_tc (HsBinTick {}) = Nothing
|
| ... | ... | @@ -1176,14 +1173,14 @@ hsExprNeedsParens prec = go |
| 1176 | 1173 | |
| 1177 | 1174 | go_x_tc :: XXExprGhcTc -> Bool
|
| 1178 | 1175 | go_x_tc (WrapExpr _ e) = hsExprNeedsParens prec e
|
| 1179 | - go_x_tc (ExpandedThingTc thing _) = hsExpandedNeedsParens thing
|
|
| 1176 | + go_x_tc (ExpandedThingTc (HSE thing _)) = hsExpandedNeedsParens thing
|
|
| 1180 | 1177 | go_x_tc (ConLikeTc {}) = False
|
| 1181 | 1178 | go_x_tc (HsTick _ (L _ e)) = hsExprNeedsParens prec e
|
| 1182 | 1179 | go_x_tc (HsBinTick _ _ (L _ e)) = hsExprNeedsParens prec e
|
| 1183 | 1180 | go_x_tc (HsRecSelTc{}) = False
|
| 1184 | 1181 | |
| 1185 | 1182 | go_x_rn :: XXExprGhcRn -> Bool
|
| 1186 | - go_x_rn (ExpandedThingRn thing _ ) = hsExpandedNeedsParens thing
|
|
| 1183 | + go_x_rn (ExpandedThingRn (HSE thing _)) = hsExpandedNeedsParens thing
|
|
| 1187 | 1184 | go_x_rn (HsRecSelRn{}) = False
|
| 1188 | 1185 | |
| 1189 | 1186 | hsExpandedNeedsParens :: HsCtxt -> Bool
|
| ... | ... | @@ -1228,14 +1225,14 @@ isAtomicHsExpr (XExpr x) |
| 1228 | 1225 | where
|
| 1229 | 1226 | go_x_tc :: XXExprGhcTc -> Bool
|
| 1230 | 1227 | go_x_tc (WrapExpr _ e) = isAtomicHsExpr e
|
| 1231 | - go_x_tc (ExpandedThingTc thing _) = isAtomicExpandedThingRn thing
|
|
| 1228 | + go_x_tc (ExpandedThingTc (HSE thing _)) = isAtomicExpandedThingRn thing
|
|
| 1232 | 1229 | go_x_tc (ConLikeTc {}) = True
|
| 1233 | 1230 | go_x_tc (HsTick {}) = False
|
| 1234 | 1231 | go_x_tc (HsBinTick {}) = False
|
| 1235 | 1232 | go_x_tc (HsRecSelTc{}) = True
|
| 1236 | 1233 | |
| 1237 | 1234 | go_x_rn :: XXExprGhcRn -> Bool
|
| 1238 | - go_x_rn (ExpandedThingRn thing _) = isAtomicExpandedThingRn thing
|
|
| 1235 | + go_x_rn (ExpandedThingRn (HSE thing _)) = isAtomicExpandedThingRn thing
|
|
| 1239 | 1236 | go_x_rn (HsRecSelRn{}) = True
|
| 1240 | 1237 | |
| 1241 | 1238 | isAtomicExpandedThingRn :: HsCtxt -> Bool
|
| ... | ... | @@ -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 | -- -------------------------------
|
| ... | ... | @@ -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)) = lhsExprType 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
|
| ... | ... | @@ -307,7 +307,7 @@ dsExpr e@(XExpr ext_expr_tc) |
| 307 | 307 | WrapExpr {} -> dsApp e
|
| 308 | 308 | ConLikeTc {} -> dsApp e
|
| 309 | 309 | |
| 310 | - ExpandedThingTc _ e -> dsLExpr e
|
|
| 310 | + ExpandedThingTc (HSE _ e) -> dsLExpr e
|
|
| 311 | 311 | |
| 312 | 312 | -- Hpc Support
|
| 313 | 313 | HsTick tickish e -> do
|
| ... | ... | @@ -1166,7 +1166,7 @@ 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'))
|
|
| 1169 | + exp (XExpr (ExpandedThingTc (HSE _ x))) (XExpr (ExpandedThingTc (HSE _ x')))
|
|
| 1170 | 1170 | = lexp x x'
|
| 1171 | 1171 | exp (HsVar _ i) (HsVar _ i') = i == i'
|
| 1172 | 1172 | exp (HsIPVar _ i) (HsIPVar _ i') =
|
| ... | ... | @@ -1735,7 +1735,7 @@ repE (HsFunArr _ mult arg res) = do |
| 1735 | 1735 | arg' <- repLE arg
|
| 1736 | 1736 | res' <- repLE res
|
| 1737 | 1737 | repApps fun [arg', res']
|
| 1738 | -repE e@(XExpr (ExpandedThingRn o x))
|
|
| 1738 | +repE e@(XExpr (ExpandedThingRn (HSE 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]
|
| ... | ... | @@ -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,14 +484,14 @@ 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))
|
|
| 494 | +isCallSite (XExpr (ExpandedThingTc (HSE _ e)))
|
|
| 495 | 495 | = isCallSite (unLoc e)
|
| 496 | 496 | |
| 497 | 497 | -- NB: OpApp, SectionL, SectionR are all expanded out
|
| ... | ... | @@ -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,8 +660,8 @@ addTickHsExpr (HsDo srcloc cxt (L l stmts)) |
| 660 | 660 | ListComp -> Just $ BinBox QualBinBox
|
| 661 | 661 | _ -> Nothing
|
| 662 | 662 | |
| 663 | -addTickHsExpanded :: HsCtxt -> LHsExpr 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
|
| ... | ... | @@ -755,7 +755,7 @@ 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 | 760 | -> Just (lhsExprType e)
|
| 761 | 761 | | otherwise -> computeLType e
|
| ... | ... | @@ -1352,7 +1352,7 @@ 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
|
|
| 1355 | + ExpandedThingTc (HSE _ e)
|
|
| 1356 | 1356 | -> [ toHie e ]
|
| 1357 | 1357 | ConLikeTc con
|
| 1358 | 1358 | -> [ toHie $ C Use $ L mspan $ conLikeName con ]
|
| ... | ... | @@ -847,8 +847,9 @@ mkExpandedStmt oStmt flav eExpr = mkExpandedRn (StmtErrCtxt (HsDoStmt flav) oStm |
| 847 | 847 | mkExpandedRn
|
| 848 | 848 | :: HsCtxt -- ^ source, user written do statement/expression
|
| 849 | 849 | -> LHsExpr GhcRn -- ^ expanded typechecked expression
|
| 850 | - -> HsExpr GhcRn -- ^ suitably wrapped 'XXExprGhcRn'
|
|
| 851 | -mkExpandedRn orig expr = XExpr (ExpandedThingRn orig expr)
|
|
| 850 | + -> HsExpr GhcRn -- ^ suitably wrapped 'XXExprGhcRn'
|
|
| 851 | +mkExpandedRn o e = XExpr (ExpandedThingRn (HSE o e))
|
|
| 852 | + |
|
| 852 | 853 | |
| 853 | 854 | -- | Build a 'XXExprGhcRn' out of an extension constructor,
|
| 854 | 855 | -- and the two components of the expansion: original and
|
| ... | ... | @@ -862,5 +863,5 @@ mkExpandedExprTc oExpr eExpr = mkExpandedTc (ExprCtxt oExpr) (wrapGenSpan eExpr) |
| 862 | 863 | mkExpandedTc
|
| 863 | 864 | :: HsCtxt -- ^ source, user written do statement/expression
|
| 864 | 865 | -> LHsExpr GhcTc -- ^ expanded typechecked expression
|
| 865 | - -> HsExpr GhcTc -- ^ suitably wrapped 'XXExprGhcRn'
|
|
| 866 | -mkExpandedTc o e = XExpr (ExpandedThingTc o e) |
|
| 866 | + -> HsExpr GhcTc -- ^ suitably wrapped 'XXExprGhcTc'
|
|
| 867 | +mkExpandedTc o e = XExpr (ExpandedThingTc (HSE o e)) |
| ... | ... | @@ -1246,7 +1246,7 @@ expr_to_type earg = |
| 1246 | 1246 | | otherwise = not_in_scope
|
| 1247 | 1247 | where occ = occName rdr
|
| 1248 | 1248 | not_in_scope = failWith $ TcRnNotInScope NotInScope rdr
|
| 1249 | - go (L l (XExpr (ExpandedThingRn (ExprCtxt orig) _))) =
|
|
| 1249 | + go (L l (XExpr (ExpandedThingRn (HSE (ExprCtxt orig) _)))) =
|
|
| 1250 | 1250 | -- Use the original, user-written expression (before expansion).
|
| 1251 | 1251 | -- Example. Say we have vfun :: forall a -> blah
|
| 1252 | 1252 | -- and the call vfun (Maybe [1,2,3])
|
| ... | ... | @@ -1937,7 +1937,7 @@ quickLookArg1 :: Int -> SrcSpan -> (HsExpr GhcRn, SrcSpan) -> LHsExpr GhcRn |
| 1937 | 1937 | -> Scaled TcRhoType -- Deeply skolemised
|
| 1938 | 1938 | -> TcM (HsExprArg 'TcpInst)
|
| 1939 | 1939 | -- quickLookArg1 implements the "QL Argument" judgement in Fig 5 of the paper
|
| 1940 | -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)
|
|
| 1941 | 1941 | = addArgCtxt pos (fun, fun_lspan) larg $ -- Context needed for constraints
|
| 1942 | 1942 | -- generated by calls in arg
|
| 1943 | 1943 | do { ((rn_fun_arg, fun_lspan_arg), rn_args) <- splitHsApps arg
|
| ... | ... | @@ -1968,7 +1968,7 @@ quickLookArg1 pos app_lspan (fun, fun_lspan) larg@(L arg_loc arg) sc_arg_ty@(Sca |
| 1968 | 1968 | <- captureConstraints $
|
| 1969 | 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
|
| 1970 | 1970 | -- We must capture type-class and equality constraints here, but
|
| 1971 | - -- not equality constraints. See (QLA6) in Note [Quick Look at
|
|
| 1971 | + -- not usage information. See (QLA6) in Note [Quick Look at
|
|
| 1972 | 1972 | -- value arguments]
|
| 1973 | 1973 | |
| 1974 | 1974 | ; traceTc "quickLookArg 2" $
|
| ... | ... | @@ -45,8 +45,8 @@ import Data.List ((\\)) |
| 45 | 45 | -- so that they can be typechecked.
|
| 46 | 46 | -- See Note [Expanding HsDo with XXExprGhcRn] below for `HsDo` specific commentary
|
| 47 | 47 | -- and Note [Handling overloaded and rebindable constructs] for high level commentary
|
| 48 | -expandDoStmts :: HsDoFlavour -> [ExprLStmt GhcRn] -> TcM (LHsExpr GhcRn)
|
|
| 49 | -expandDoStmts doFlav stmts = expand_do_stmts doFlav stmts
|
|
| 48 | +expandDoStmts :: HsDoFlavour -> XRec GhcRn [ExprLStmt GhcRn] -> TcM (HsExpansion GhcRn)
|
|
| 49 | +expandDoStmts doFlav lstmts@(L _ stmts) = HSE (ExprCtxt (HsDo noExtField doFlav lstmts)) <$> expand_do_stmts doFlav stmts
|
|
| 50 | 50 | |
| 51 | 51 | -- | The main work horse for expanding do block statements into applications of binds and thens
|
| 52 | 52 | -- See Note [Expanding HsDo with XXExprGhcRn]
|
| ... | ... | @@ -234,7 +234,7 @@ See Note [Handling overloaded and rebindable constructs] in GHC.Rename.Expr |
| 234 | 234 | To disabmiguate desugaring (`HsExpr GhcTc -> Core.Expr`) we use the phrase expansion
|
| 235 | 235 | (`HsExpr GhcRn -> HsExpr GhcRn`)
|
| 236 | 236 | |
| 237 | -This expansion is done right before typechecking and after renaming
|
|
| 237 | +This expansion is done after renaming and before typechecking
|
|
| 238 | 238 | See Part 2. of Note [Doing XXExprGhcRn in the Renamer vs Typechecker] in `GHC.Rename.Expr`
|
| 239 | 239 | |
| 240 | 240 | Historical note START
|
| ... | ... | @@ -423,10 +423,10 @@ It stores the original statement (with location) and the expanded expression |
| 423 | 423 | ‹ExpandedThingRn do { e1; e2; e3 }› -- Original Do Expression
|
| 424 | 424 | -- Expanded Do Expression
|
| 425 | 425 | (‹ExpandedThingRn e1› -- Original Statement
|
| 426 | - ({(>>) ‹ExpandedThingRn e1› e1} -- Expanded Expression
|
|
| 426 | + ({(>>) e1} -- Expanded Expression
|
|
| 427 | 427 | (‹ExpandedThingRn e2›
|
| 428 | - ({(>>) ‹ExpandedThingRn e2› e2}
|
|
| 429 | - (‹ExpandedThingRn e3› {e3})))))
|
|
| 428 | + ({(>>) e2}
|
|
| 429 | + (‹ExpandedThingRn e3› {e3})))))
|
|
| 430 | 430 | |
| 431 | 431 | * Whenever the typechecker steps through an `ExpandedThingRn`,
|
| 432 | 432 | we push the original statement in the error context, set the error location to the
|
| ... | ... | @@ -481,6 +481,4 @@ It stores the original statement (with location) and the expanded expression |
| 481 | 481 | |
| 482 | 482 | |
| 483 | 483 | mkExpandedPatRn :: LPat GhcRn -> HsExpr GhcRn -> HsExpr GhcRn
|
| 484 | -mkExpandedPatRn pat e = XExpr $ ExpandedThingRn
|
|
| 485 | - { xrn_orig = StmtErrCtxtPat pat
|
|
| 486 | - , xrn_expanded = wrapGenSpan e} |
|
| 484 | +mkExpandedPatRn pat e = mkExpandedRn (StmtErrCtxtPat pat) (wrapGenSpan e) |
| ... | ... | @@ -316,7 +316,8 @@ tcExpr e@(OpApp {}) res_ty = tcApp e res_ty |
| 316 | 316 | tcExpr e@(HsAppType {}) res_ty = tcApp e res_ty
|
| 317 | 317 | tcExpr e@(ExprWithTySig {}) res_ty = tcApp e res_ty
|
| 318 | 318 | |
| 319 | -tcExpr (XExpr e') res_ty = tcXExpr e' res_ty
|
|
| 319 | +tcExpr (XExpr (ExpandedThingRn hse)) res_ty = tcHsExpansion hse res_ty
|
|
| 320 | +tcExpr e@(XExpr{}) res_ty = tcApp e res_ty
|
|
| 320 | 321 | |
| 321 | 322 | -- Typecheck an occurrence of an unbound Id
|
| 322 | 323 | --
|
| ... | ... | @@ -557,7 +558,7 @@ tcExpr (HsMultiIf _ alts) res_ty |
| 557 | 558 | ; res_ty <- readExpType res_ty
|
| 558 | 559 | ; return (HsMultiIf res_ty alts') }
|
| 559 | 560 | |
| 560 | -tcExpr expr@(HsDo _ do_or_lc stmts) res_ty
|
|
| 561 | +tcExpr (HsDo _ do_or_lc stmts) res_ty
|
|
| 561 | 562 | | DoExpr{} <- do_or_lc
|
| 562 | 563 | -- ApplicativeDo are typechecked using tcDoStmts
|
| 563 | 564 | = do isApplicativeDo <- xoptM LangExt.ApplicativeDo
|
| ... | ... | @@ -565,12 +566,13 @@ tcExpr expr@(HsDo _ do_or_lc stmts) res_ty |
| 565 | 566 | then tcDoStmts do_or_lc stmts res_ty
|
| 566 | 567 | -- Expand expression on the fly otherwise
|
| 567 | 568 | -- See Note [Typechecking by expansion: overview]
|
| 568 | - else do { expr' <- tcExpandExpr expr
|
|
| 569 | - ; tcExpr expr' res_ty }
|
|
| 569 | + else do { hse <- expandDoStmts do_or_lc stmts
|
|
| 570 | + ; tcHsExpansion hse res_ty }
|
|
| 570 | 571 | | MDoExpr{} <- do_or_lc
|
| 571 | - = do expr' <- tcExpandExpr expr
|
|
| 572 | - tcExpr expr' res_ty
|
|
| 572 | + = do hse <- expandDoStmts do_or_lc stmts
|
|
| 573 | + tcHsExpansion hse res_ty
|
|
| 573 | 574 | | otherwise
|
| 575 | + -- ListComp, MonadComp are handled by tcDoStmts
|
|
| 574 | 576 | = tcDoStmts do_or_lc stmts res_ty
|
| 575 | 577 | |
| 576 | 578 | tcExpr (HsProc x pat cmd) res_ty
|
| ... | ... | @@ -686,7 +688,7 @@ tcExpr expr@(RecordUpd { rupd_expr = record_expr |
| 686 | 688 | |
| 687 | 689 | ; (ds_expr, ds_res_ty, err_msg)
|
| 688 | 690 | <- expandRecordUpd record_expr possible_parents rbnds res_ty
|
| 689 | - ; setInGeneratedCode $ addErrCtxt err_msg $
|
|
| 691 | + ; addErrCtxt err_msg $
|
|
| 690 | 692 | do { -- Typecheck the expanded expression.
|
| 691 | 693 | expr' <- tcExpr ds_expr (Check ds_res_ty)
|
| 692 | 694 | -- NB: it's important to use ds_res_ty and not res_ty here.
|
| ... | ... | @@ -815,7 +817,7 @@ The rest of this Note explains how that is done. |
| 815 | 817 | hence `RealSrcSpan`.
|
| 816 | 818 | |
| 817 | 819 | The `tcl_err_ctxt` is a stack of contexts, each saying something
|
| 818 | - like "In the expression: x+y" or "In the record update: r { x=2 }"
|
|
| 820 | + like "In the expression: x+y" or "In second argument of `$` namely 'r { x=2 }'"
|
|
| 819 | 821 | |
| 820 | 822 | The `tcl_in_gen_code` is a boolean that keeps track of whether
|
| 821 | 823 | the current expression being typechecked is compiler generated
|
| ... | ... | @@ -847,17 +849,13 @@ The rest of this Note explains how that is done. |
| 847 | 849 | |
| 848 | 850 | -}
|
| 849 | 851 | |
| 850 | -tcXExpr :: XXExprGhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
|
|
| 851 | -tcXExpr (ExpandedThingRn o e) res_ty
|
|
| 852 | +tcHsExpansion :: HsExpansion GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
|
|
| 853 | +tcHsExpansion (HSE o e) res_ty
|
|
| 852 | 854 | = mkExpandedTc o <$> -- necessary for hpc ticks
|
| 853 | 855 | -- Need to call tcExpr and not tcApp
|
| 854 | 856 | -- as e can be let statement which tcApp cannot gracefully handle
|
| 855 | 857 | tcMonoLExpr e res_ty
|
| 856 | 858 | |
| 857 | --- For record selection, same as HsVar case
|
|
| 858 | -tcXExpr xe res_ty = tcApp (XExpr xe) res_ty
|
|
| 859 | - |
|
| 860 | - |
|
| 861 | 859 | {-
|
| 862 | 860 | ************************************************************************
|
| 863 | 861 | * *
|
| ... | ... | @@ -1859,14 +1857,3 @@ checkMissingFields con_like rbinds arg_tys |
| 1859 | 1857 | field_strs = conLikeImplBangs con_like
|
| 1860 | 1858 | |
| 1861 | 1859 | fl `elemField` flds = any (\ fl' -> flSelector fl == fl') flds |
| 1862 | - |
|
| 1863 | - |
|
| 1864 | --- Expands the expression on the fly
|
|
| 1865 | --- See Note [Handling overloaded and rebindable constructs]
|
|
| 1866 | --- See Note [Typechecking by expansion: overview]
|
|
| 1867 | -tcExpandExpr :: HsExpr GhcRn -> TcM (HsExpr GhcRn)
|
|
| 1868 | -tcExpandExpr orig_expr@(HsDo _ flav (L _ stmts))
|
|
| 1869 | - = do { expanded_expr <- expandDoStmts flav stmts
|
|
| 1870 | - ; return (mkExpandedLExpr orig_expr expanded_expr) }
|
|
| 1871 | - |
|
| 1872 | -tcExpandExpr e = return e |
| ... | ... | @@ -462,7 +462,7 @@ tcInferAppHead_maybe fun = case fun of |
| 462 | 462 | ExprWithTySig _ e hs_ty -> Just <$> with_get_ds (tcExprWithSig e hs_ty)
|
| 463 | 463 | HsOverLit _ lit -> Just <$> with_get_ds (tcInferOverLit lit)
|
| 464 | 464 | XExpr (HsRecSelRn f) -> Just <$> with_get_ds (tcInferRecSelId f)
|
| 465 | - XExpr (ExpandedThingRn o (L loc e)) -> setSrcSpan (locA loc) $ Just <$> (
|
|
| 465 | + XExpr (ExpandedThingRn (HSE o (L loc e))) -> setSrcSpan (locA loc) $ Just <$> (
|
|
| 466 | 466 | -- We do not want to instantiate the type of the head as there may be
|
| 467 | 467 | -- visible type applications in the argument.
|
| 468 | 468 | -- c.f. T19167
|
| ... | ... | @@ -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
|
| ... | ... | @@ -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 | |
| ... | ... | @@ -1355,8 +1355,8 @@ addLExprCtxt lspan e thing_inside |
| 1355 | 1355 | -- error context. c.f. RecordDotSyntaxFail9
|
| 1356 | 1356 | -- Add the original HsCtxt if we are typechecking an expanded expression
|
| 1357 | 1357 | ExprWithTySig _ (L _ e') _
|
| 1358 | - | XExpr (ExpandedThingRn o _) <- e' -> addErrCtxt o thing_inside
|
|
| 1359 | - XExpr (ExpandedThingRn o _) -> addErrCtxt o thing_inside
|
|
| 1358 | + | XExpr (ExpandedThingRn (HSE o _)) <- e' -> addErrCtxt o thing_inside
|
|
| 1359 | + XExpr (ExpandedThingRn (HSE o _)) -> addErrCtxt o thing_inside
|
|
| 1360 | 1360 | |
| 1361 | 1361 | _ -> addErrCtxt (ExprCtxt e) thing_inside
|
| 1362 | 1362 | }
|
| ... | ... | @@ -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 _ (L _ 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
|
| ... | ... | @@ -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))
|
|
| 1098 | +zonkExpr (XExpr (ExpandedThingTc (HSE thing e)))
|
|
| 1099 | 1099 | = do e' <- zonkLExpr e
|
| 1100 | - return $ XExpr (ExpandedThingTc thing e')
|
|
| 1100 | + return $ XExpr (ExpandedThingTc (HSE thing e'))
|
|
| 1101 | 1101 | |
| 1102 | 1102 | zonkExpr e@(XExpr (ConLikeTc {}))
|
| 1103 | 1103 | = return e
|