Apoorv Ingle pushed to branch wip/ani/tc-expand at Glasgow Haskell Compiler / GHC
Commits:
-
95b5f2cb
by Apoorv Ingle at 2026-03-16T13:35:36-05:00
15 changed files:
- compiler/GHC/Hs/Expr.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/Do.hs
- compiler/GHC/Tc/Gen/Expr.hs
- compiler/GHC/Tc/Gen/Head.hs
- compiler/GHC/Tc/Gen/Match.hs
- compiler/GHC/Tc/Utils/Monad.hs
- compiler/GHC/Tc/Utils/Unify.hs
- compiler/GHC/Tc/Zonk/Type.hs
Changes:
| ... | ... | @@ -661,43 +661,23 @@ type instance XXExpr GhcTc = XXExprGhcTc |
| 661 | 661 | ********************************************************************* -}
|
| 662 | 662 | |
| 663 | 663 | 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 | - }
|
|
| 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)
|
|
| 667 | 667 | |
| 668 | 668 | | HsRecSelRn (FieldOcc GhcRn) -- ^ Variable pointing to record selector
|
| 669 | 669 | -- See Note [Non-overloaded record field selectors] and
|
| 670 | 670 | -- Note [Record selectors in the AST]
|
| 671 | 671 | |
| 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 | 672 | data XXExprGhcTc
|
| 694 | 673 | = WrapExpr -- Type and evidence application and abstractions
|
| 695 | 674 | HsWrapper (HsExpr GhcTc)
|
| 696 | 675 | |
| 697 | 676 | | ExpandedThingTc -- See Note [Rebindable syntax and XXExprGhcRn]
|
| 698 | 677 | -- 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
|
|
| 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)
|
|
| 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
|
| ... | ... | @@ -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 _ 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
|
| ... | ... | @@ -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 _ e -> dsLExpr e
|
|
| 315 | 311 | |
| 316 | 312 | -- Hpc Support
|
| 317 | 313 | HsTick tickish e -> do
|
| ... | ... | @@ -1167,7 +1167,7 @@ viewLExprEq (e1,_) (e2,_) = lexp e1 e2 |
| 1167 | 1167 | exp (XExpr (WrapExpr h e)) (XExpr (WrapExpr h' e')) =
|
| 1168 | 1168 | wrap h h' && exp e e'
|
| 1169 | 1169 | exp (XExpr (ExpandedThingTc _ x)) (XExpr (ExpandedThingTc _ x'))
|
| 1170 | - = exp x 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
|
| ... | ... | @@ -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)
|
| ... | ... | @@ -492,7 +492,7 @@ isCallSite HsApp{} = True |
| 492 | 492 | isCallSite HsAppType{} = True
|
| 493 | 493 | isCallSite HsCase{} = True
|
| 494 | 494 | isCallSite (XExpr (ExpandedThingTc _ e))
|
| 495 | - = isCallSite e
|
|
| 495 | + = isCallSite (unLoc e)
|
|
| 496 | 496 | |
| 497 | 497 | -- NB: OpApp, SectionL, SectionR are all expanded out
|
| 498 | 498 | isCallSite _ = False
|
| ... | ... | @@ -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)
|
|
| 663 | +addTickHsExpanded :: HsCtxt -> LHsExpr GhcTc -> TM (HsExpr GhcTc)
|
|
| 664 | 664 | addTickHsExpanded o e = liftM (XExpr . ExpandedThingTc 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
|
| ... | ... | @@ -757,8 +757,8 @@ instance HiePass p => HasType (LocatedA (HsExpr (GhcPass p))) where |
| 757 | 757 | HsPragE _ _ e -> computeLType e
|
| 758 | 758 | XExpr (ExpandedThingTc 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)
|
| ... | ... | @@ -1353,7 +1353,7 @@ instance HiePass p => ToHie (LocatedA (HsExpr (GhcPass p))) where |
| 1353 | 1353 | -> [ toHie $ L mspan a
|
| 1354 | 1354 | , toHie (L mspan w) ]
|
| 1355 | 1355 | ExpandedThingTc _ e
|
| 1356 | - -> [ toHie (L mspan e) ]
|
|
| 1356 | + -> [ toHie e ]
|
|
| 1357 | 1357 | ConLikeTc con
|
| 1358 | 1358 | -> [ toHie $ C Use $ L mspan $ conLikeName con ]
|
| 1359 | 1359 | HsTick _ expr
|
| ... | ... | @@ -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,50 @@ 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 orig expr = XExpr (ExpandedThingRn orig expr)
|
|
| 852 | + |
|
| 853 | +-- | Build a 'XXExprGhcRn' out of an extension constructor,
|
|
| 854 | +-- and the two components of the expansion: original and
|
|
| 855 | +-- expanded typechecked expressions.
|
|
| 856 | +mkExpandedExprTc
|
|
| 857 | + :: HsExpr GhcRn -- ^ source expression
|
|
| 858 | + -> HsExpr GhcTc -- ^ expanded typechecked expression
|
|
| 859 | + -> HsExpr GhcTc -- ^ suitably wrapped 'XXExprGhcRn'
|
|
| 860 | +mkExpandedExprTc oExpr eExpr = mkExpandedTc (ExprCtxt oExpr) (wrapGenSpan eExpr)
|
|
| 861 | + |
|
| 862 | +mkExpandedTc
|
|
| 863 | + :: HsCtxt -- ^ source, user written do statement/expression
|
|
| 864 | + -> LHsExpr GhcTc -- ^ expanded typechecked expression
|
|
| 865 | + -> HsExpr GhcTc -- ^ suitably wrapped 'XXExprGhcRn'
|
|
| 866 | +mkExpandedTc o e = XExpr (ExpandedThingTc o e) |
| ... | ... | @@ -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
|
| ... | ... | @@ -77,7 +76,7 @@ expand_do_stmts flav [stmt@(L sloc (LastStmt _ body@(L body_loc _) _ ret_expr))] |
| 77 | 76 | -- See `checkLastStmt` and `Syntax.Expr.StmtLR.LastStmt`
|
| 78 | 77 | | NoSyntaxExprRn <- ret_expr
|
| 79 | 78 | -- Last statement is just body if we are not in ListComp context. See Syntax.Expr.LastStmt
|
| 80 | - = return $ L sloc (mkExpandedStmt stmt flav (unLoc body))
|
|
| 79 | + = return $ L sloc (mkExpandedStmt stmt flav (unLoc body)) -- TODO ANI: why not just body?
|
|
| 81 | 80 | |
| 82 | 81 | | SyntaxExprRn ret <- ret_expr -- We have unfortunately lost the location on the return function :(
|
| 83 | 82 | --
|
| ... | ... | @@ -484,4 +483,4 @@ It stores the original statement (with location) and the expanded expression |
| 484 | 483 | mkExpandedPatRn :: LPat GhcRn -> HsExpr GhcRn -> HsExpr GhcRn
|
| 485 | 484 | mkExpandedPatRn pat e = XExpr $ ExpandedThingRn
|
| 486 | 485 | { xrn_orig = StmtErrCtxtPat pat
|
| 487 | - , xrn_expanded = e} |
|
| 486 | + , xrn_expanded = wrapGenSpan e} |
| ... | ... | @@ -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 | * *
|
| ... | ... | @@ -562,7 +565,17 @@ tcExpr (HsMultiIf _ alts) res_ty |
| 562 | 565 | ; res_ty <- readExpType res_ty
|
| 563 | 566 | ; return (HsMultiIf res_ty alts') }
|
| 564 | 567 | |
| 565 | -tcExpr (HsDo _ do_or_lc stmts) res_ty
|
|
| 568 | +tcExpr expr@(HsDo _ do_or_lc stmts) res_ty
|
|
| 569 | + | DoExpr{} <- do_or_lc
|
|
| 570 | + = do isApplicativeDo <- xoptM LangExt.ApplicativeDo
|
|
| 571 | + if isApplicativeDo
|
|
| 572 | + then tcDoStmts do_or_lc stmts res_ty
|
|
| 573 | + else do { expr' <- tcExpandExpr expr
|
|
| 574 | + ; tcExpr expr' res_ty }
|
|
| 575 | + | MDoExpr{} <- do_or_lc
|
|
| 576 | + = do expr' <- tcExpandExpr expr
|
|
| 577 | + tcExpr expr' res_ty
|
|
| 578 | + | otherwise
|
|
| 566 | 579 | = tcDoStmts do_or_lc stmts res_ty
|
| 567 | 580 | |
| 568 | 581 | tcExpr (HsProc x pat cmd) res_ty
|
| ... | ... | @@ -809,7 +822,7 @@ The rest of this Note explains how that is done. |
| 809 | 822 | like "In the expression: x+y" or "In the record update: r { x=2 }"
|
| 810 | 823 | |
| 811 | 824 | * Now, when
|
| 812 | - tcMonoLHsExpr :: LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
|
|
| 825 | + tcMonoLExpr :: LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
|
|
| 813 | 826 | gets a located expression, it does 2 things:
|
| 814 | 827 | * Calls `addLExprCtxt` to perform error context management
|
| 815 | 828 | * Calls `tcExpr` to typecheck the expression.
|
| ... | ... | @@ -839,7 +852,7 @@ tcXExpr (ExpandedThingRn o e) res_ty |
| 839 | 852 | = mkExpandedTc o <$> -- necessary for hpc ticks
|
| 840 | 853 | -- Need to call tcExpr and not tcApp
|
| 841 | 854 | -- as e can be let statement which tcApp cannot gracefully handle
|
| 842 | - tcExpr e res_ty
|
|
| 855 | + tcMonoLExpr e res_ty
|
|
| 843 | 856 | |
| 844 | 857 | -- For record selection, same as HsVar case
|
| 845 | 858 | tcXExpr xe res_ty = tcApp (XExpr xe) res_ty
|
| ... | ... | @@ -1846,3 +1859,16 @@ checkMissingFields con_like rbinds arg_tys |
| 1846 | 1859 | field_strs = conLikeImplBangs con_like
|
| 1847 | 1860 | |
| 1848 | 1861 | fl `elemField` flds = any (\ fl' -> flSelector fl == fl') flds
|
| 1862 | + |
|
| 1863 | + |
|
| 1864 | +-- Expands the expression
|
|
| 1865 | +tcExpandExpr :: HsExpr GhcRn -> TcM (HsExpr GhcRn)
|
|
| 1866 | +tcExpandExpr orig_expr@(HsDo _ flav (L _ stmts))
|
|
| 1867 | + = do { expanded_expr <- expandDoStmts flav stmts
|
|
| 1868 | + -- We lose the location on the first statement location in GhcTc, unfortunately.
|
|
| 1869 | + -- It is needed for get the pattern match warnings right cf. T14546d
|
|
| 1870 | + -- That location is currently recovered from the location stored in StmtErrCtxt
|
|
| 1871 | + -- in dsExpr of ExpandedThingTc
|
|
| 1872 | + ; return (mkExpandedLExpr orig_expr expanded_expr) }
|
|
| 1873 | + |
|
| 1874 | +tcExpandExpr e = return e |
| ... | ... | @@ -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,11 +462,11 @@ 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 o (L loc e)) -> 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)) <$>
|
|
| 469 | + (\ (e, ds_flag, ty) -> (mkExpandedTc o (L loc e), ds_flag, ty)) <$>
|
|
| 468 | 470 | tcExprSigma False (errCtxtCtOrigin o) e
|
| 469 | 471 | )
|
| 470 | 472 | _ -> return Nothing
|
| ... | ... | @@ -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
|
| ... | ... | @@ -1334,7 +1334,7 @@ addLExprCtxt lspan e thing_inside |
| 1334 | 1334 | | not (isGeneratedSrcSpan lspan)
|
| 1335 | 1335 | = setSrcSpan lspan $ add_expr_ctxt e thing_inside
|
| 1336 | 1336 | | otherwise -- no op in generated code
|
| 1337 | - = thing_inside
|
|
| 1337 | + = setSrcSpan lspan $ thing_inside
|
|
| 1338 | 1338 | where
|
| 1339 | 1339 | add_expr_ctxt :: HsExpr GhcRn -> TcRn a -> TcRn a
|
| 1340 | 1340 | add_expr_ctxt e thing_inside
|
| ... | ... | @@ -1349,10 +1349,10 @@ addLExprCtxt lspan e thing_inside |
| 1349 | 1349 | -- error context. So here we flip the ErrCtxt state to expanded if the expression is expanded.
|
| 1350 | 1350 | -- c.f. RecordDotSyntaxFail9
|
| 1351 | 1351 | ExprWithTySig _ (L _ e') _
|
| 1352 | - | XExpr (ExpandedThingRn o _) <- e' -> addExpansionErrCtxt o thing_inside
|
|
| 1352 | + | XExpr (ExpandedThingRn o _) <- e' -> addErrCtxt o thing_inside
|
|
| 1353 | 1353 | |
| 1354 | 1354 | -- Flip error ctxt into expansion mode
|
| 1355 | - XExpr (ExpandedThingRn o _) -> addExpansionErrCtxt o thing_inside
|
|
| 1355 | + XExpr (ExpandedThingRn o _) -> addErrCtxt o thing_inside
|
|
| 1356 | 1356 | |
| 1357 | 1357 | _ -> addErrCtxt (ExprCtxt e) thing_inside
|
| 1358 | 1358 |
| ... | ... | @@ -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 _ (L _ f)) <- app_head
|
|
| 2051 | 2051 | = go f
|
| 2052 | 2052 | | XExpr (WrapExpr _ f) <- app_head
|
| 2053 | 2053 | = go f
|
| ... | ... | @@ -1096,7 +1096,7 @@ zonkExpr (XExpr (WrapExpr co_fn expr)) |
| 1096 | 1096 | return (XExpr (WrapExpr new_co_fn new_expr))
|
| 1097 | 1097 | |
| 1098 | 1098 | zonkExpr (XExpr (ExpandedThingTc thing e))
|
| 1099 | - = do e' <- zonkExpr e
|
|
| 1099 | + = do e' <- zonkLExpr e
|
|
| 1100 | 1100 | return $ XExpr (ExpandedThingTc thing e')
|
| 1101 | 1101 | |
| 1102 | 1102 | zonkExpr e@(XExpr (ConLikeTc {}))
|