Apoorv Ingle pushed to branch wip/spj-apporv-Oct24 at Glasgow Haskell Compiler / GHC
Commits:
-
fcac5349
by Apoorv Ingle at 2026-01-14T16:54:41-06:00
-
dd638011
by Apoorv Ingle at 2026-01-15T12:40:36-06:00
4 changed files:
- compiler/GHC/Tc/Gen/Do.hs
- compiler/GHC/Tc/Gen/Expr.hs
- compiler/GHC/Tc/Types/LclEnv.hs
- compiler/GHC/Tc/Utils/Monad.hs
Changes:
| ... | ... | @@ -114,7 +114,7 @@ expand_do_stmts doFlavour (stmt@(L loc (BindStmt xbsrn pat e)): lstmts) |
| 114 | 114 | | otherwise
|
| 115 | 115 | = pprPanic "expand_do_stmts: The impossible happened, missing bind operator from renamer" (text "stmt" <+> ppr stmt)
|
| 116 | 116 | |
| 117 | -expand_do_stmts doFlavour (stmt@(L loc (BodyStmt _ (L _e_lspan e) (SyntaxExprRn then_op) _)) : lstmts) =
|
|
| 117 | +expand_do_stmts doFlavour (stmt@(L loc (BodyStmt _ (L e_lspan e) (SyntaxExprRn then_op) _)) : lstmts) =
|
|
| 118 | 118 | -- See Note [BodyStmt] in Language.Haskell.Syntax.Expr
|
| 119 | 119 | -- See Note [Expanding HsDo with XXExprGhcRn] Equation (1) below
|
| 120 | 120 | -- stmts ~~> stmts'
|
| ... | ... | @@ -122,8 +122,8 @@ expand_do_stmts doFlavour (stmt@(L loc (BodyStmt _ (L _e_lspan e) (SyntaxExprRn |
| 122 | 122 | -- e ; stmts ~~> (>>) e stmts'
|
| 123 | 123 | do expand_stmts_expr <- expand_do_stmts doFlavour lstmts
|
| 124 | 124 | let expansion = genHsExpApps then_op -- (>>)
|
| 125 | - [ -- L e_lspan (mkExpandedStmt stmt doFlavour e)
|
|
| 126 | - wrapGenSpan e
|
|
| 125 | + [ L e_lspan (mkExpandedStmt stmt doFlavour e)
|
|
| 126 | + -- wrapGenSpan e
|
|
| 127 | 127 | , expand_stmts_expr ]
|
| 128 | 128 | return $ L loc (mkExpandedStmt stmt doFlavour expansion)
|
| 129 | 129 |
| ... | ... | @@ -304,6 +304,7 @@ tcExpr :: HsExpr GhcRn |
| 304 | 304 | -- - ones taken apart by GHC.Tc.Gen.Head.splitHsApps
|
| 305 | 305 | -- - ones understood by GHC.Tc.Gen.Head.tcInferAppHead_maybe
|
| 306 | 306 | -- See Note [Application chains and heads] in GHC.Tc.Gen.App
|
| 307 | +-- Se Note [Overview of Typechecking an XExpr]
|
|
| 307 | 308 | tcExpr e@(HsVar {}) res_ty = tcApp e res_ty
|
| 308 | 309 | tcExpr e@(HsApp {}) res_ty = tcApp e res_ty
|
| 309 | 310 | tcExpr e@(OpApp {}) res_ty = tcApp e res_ty
|
| ... | ... | @@ -761,6 +762,50 @@ tcExpr (SectionR {}) ty = pprPanic "tcExpr:SectionR" (ppr ty) |
| 761 | 762 | ************************************************************************
|
| 762 | 763 | -}
|
| 763 | 764 | |
| 765 | +{- Note [Overview of Typechecking an XExpr]
|
|
| 766 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 767 | +Certain constructs undergo expansion right before type checking.
|
|
| 768 | + |
|
| 769 | + tcExpr ue@(RecordUpd{}) rho = do { ee <- expand e; tcExpr ee rho }
|
|
| 770 | + |
|
| 771 | +See Note [Handling overloaded and rebindable constructs] and
|
|
| 772 | +Note [Doing XXExprGhcRn in the Renamer vs Typechecker]
|
|
| 773 | +for details about which constructs are expanded.
|
|
| 774 | + |
|
| 775 | +The expansion process typically takes a user written thing
|
|
| 776 | + L lspan ue
|
|
| 777 | +and returns
|
|
| 778 | + L lspan (XExpr (ExpandedThingRn { xrn_orig = ue
|
|
| 779 | + , xrn_expanded = ee } ))
|
|
| 780 | + |
|
| 781 | +where `ee` is the expansion of the user written thing `ue`
|
|
| 782 | + |
|
| 783 | +Now, when a `tcMonoLHsExpr :: LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)`
|
|
| 784 | +gets a located expression, It does 2 things:
|
|
| 785 | +1. calls `addLExprCtxt` to perform error context management, and;
|
|
| 786 | +2. calls tcExpr to typecheck the expression.
|
|
| 787 | + |
|
| 788 | +The type checker context has 2 key fields:
|
|
| 789 | + |
|
| 790 | + TcLclCtxt { tcl_loc :: RealSrcSpan
|
|
| 791 | + , tcl_err_ctxt :: [ErrCtxt]
|
|
| 792 | + , ... }
|
|
| 793 | + |
|
| 794 | +When called on an XExpr, `addLExprCtxt` updates the location of `tcl_loc` with
|
|
| 795 | +the `lspan` above and adds an ErrCtxt on top of the `tcl_err_ctxt`. If the
|
|
| 796 | +`lspan` is generated, then `addLExprCtxt` is a no-op.
|
|
| 797 | + |
|
| 798 | +The type checker error stack element `GHC.Tc.Types.ErrCtxt.ErrCtxt` has two fields
|
|
| 799 | + |
|
| 800 | + ErrCtxt = EC CodeSrcFlag ErrCtxtMsgM
|
|
| 801 | + |
|
| 802 | +`CodeSrcFlag` says whether we are typechecking an expanded thing, and what that expanded thing is
|
|
| 803 | +`ErrCtxtMsgM` stores the pre-text error message itself. When called on an `XExpr`, `addLExprCtxt`,
|
|
| 804 | +adds the user written thing `ue`, and the error message provided by the caller on the `ErrCtxtStack`
|
|
| 805 | +See Note [ErrCtxtStack Manipulation] for more details.
|
|
| 806 | + |
|
| 807 | +-}
|
|
| 808 | + |
|
| 764 | 809 | tcXExpr :: XXExprGhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
|
| 765 | 810 | tcXExpr (ExpandedThingRn o e) res_ty
|
| 766 | 811 | = mkExpandedTc o <$> -- necessary for hpc ticks
|
| ... | ... | @@ -1482,7 +1527,7 @@ expandRecordUpd record_expr possible_parents rbnds res_ty |
| 1482 | 1527 | ds_expr = HsLet noExtField let_binds (wrapGenSpan case_expr)
|
| 1483 | 1528 | |
| 1484 | 1529 | case_expr :: HsExpr GhcRn
|
| 1485 | - case_expr = HsCase RecUpd (wrapGenSpan (unLoc record_expr))
|
|
| 1530 | + case_expr = HsCase RecUpd record_expr
|
|
| 1486 | 1531 | $ mkMatchGroup (Generated OtherExpansion DoPmc) (wrapGenSpan matches)
|
| 1487 | 1532 | matches :: [LMatch GhcRn (LHsExpr GhcRn)]
|
| 1488 | 1533 | matches = map make_pat (NE.toList relevant_cons)
|
| ... | ... | @@ -199,6 +199,7 @@ getLclEnvErrCtxt = tcl_err_ctxt . tcl_lcl_ctxt |
| 199 | 199 | setLclEnvErrCtxt :: ErrCtxtStack -> TcLclEnv -> TcLclEnv
|
| 200 | 200 | setLclEnvErrCtxt ctxt = modifyLclCtxt (\env -> env { tcl_err_ctxt = ctxt })
|
| 201 | 201 | |
| 202 | +-- See Note [ErrCtxtStack Manipulation]
|
|
| 202 | 203 | addLclEnvErrCtxt :: ErrCtxt -> TcLclEnv -> TcLclEnv
|
| 203 | 204 | addLclEnvErrCtxt ec = setLclEnvSrcCodeOrigin ec
|
| 204 | 205 |
| ... | ... | @@ -1333,6 +1333,7 @@ addErrCtxt :: ErrCtxtMsg -> TcM a -> TcM a |
| 1333 | 1333 | {-# INLINE addErrCtxt #-} -- Note [Inlining addErrCtxt]
|
| 1334 | 1334 | addErrCtxt msg = addErrCtxtM (\env -> return (env, msg))
|
| 1335 | 1335 | |
| 1336 | +-- See Note [ErrCtxtStack Manipulation]
|
|
| 1336 | 1337 | addExpansionErrCtxt :: SrcCodeOrigin -> ErrCtxtMsg -> TcM a -> TcM a
|
| 1337 | 1338 | {-# INLINE addExpansionErrCtxt #-} -- Note [Inlining addErrCtxt]
|
| 1338 | 1339 | addExpansionErrCtxt o msg = addExpansionErrCtxtM o (\env -> return (env, msg))
|
| ... | ... | @@ -1343,6 +1344,7 @@ addErrCtxtM :: (TidyEnv -> ZonkM (TidyEnv, ErrCtxtMsg)) -> TcM a -> TcM a |
| 1343 | 1344 | {-# INLINE addErrCtxtM #-} -- Note [Inlining addErrCtxt]
|
| 1344 | 1345 | addErrCtxtM ctxt = pushCtxt (MkErrCtxt VanillaUserSrcCode ctxt)
|
| 1345 | 1346 | |
| 1347 | +-- See Note [ErrCtxtStack Manipulation]
|
|
| 1346 | 1348 | addExpansionErrCtxtM :: SrcCodeOrigin -> (TidyEnv -> ZonkM (TidyEnv, ErrCtxtMsg)) -> TcM a -> TcM a
|
| 1347 | 1349 | {-# INLINE addExpansionErrCtxtM #-} -- Note [Inlining addErrCtxt]
|
| 1348 | 1350 | addExpansionErrCtxtM o ctxt = pushCtxt (MkErrCtxt (ExpansionCodeCtxt o) ctxt)
|