Simon Peyton Jones pushed to branch wip/spj-apporv-Oct24 at Glasgow Haskell Compiler / GHC Commits: c412f218 by Simon Peyton Jones at 2026-03-22T00:06:33+00:00 More from Simon - - - - - 4 changed files: - compiler/GHC/Hs/Expr.hs - compiler/GHC/Tc/Gen/Do.hs - compiler/GHC/Tc/Gen/Expr.hs - compiler/GHC/Tc/Types/LclEnv.hs Changes: ===================================== compiler/GHC/Hs/Expr.hs ===================================== @@ -662,9 +662,13 @@ type instance XXExpr GhcTc = XXExprGhcTc -- See Note [Rebindable syntax and XXExprGhcRn] -- See Note [Expanding HsDo with XXExprGhcRn] in `GHC.Tc.Gen.Do` -data HsExpansion p = HSE { hs_ctxt :: HsCtxt -- The original source thing context to be used for error messages - , expanded_expr :: LHsExpr p } -- The compiler generated, expanded expression - -- This is located because of do statements (TODO ANI : Add Note) +data HsExpansion p + = HSE { hse_ctxt :: HsCtxt -- The original source thing context, + -- to be used for error messages + , hse_exp :: LHsExpr p -- The compiler generated expansion + -- This is located because of do statements + -- (TODO ANI : Add Note) + } data XXExprGhcRn = ExpandedThingRn (HsExpansion GhcRn) -- ^ Renamed/Pre Typecheck expanded expression ===================================== compiler/GHC/Tc/Gen/Do.hs ===================================== @@ -46,7 +46,10 @@ import Data.List ((\\)) -- See Note [Expanding HsDo with XXExprGhcRn] below for `HsDo` specific commentary -- and Note [Handling overloaded and rebindable constructs] for high level commentary expandDoStmts :: HsDoFlavour -> XRec GhcRn [ExprLStmt GhcRn] -> TcM (HsExpansion GhcRn) -expandDoStmts doFlav lstmts@(L _ stmts) = HSE (ExprCtxt (HsDo noExtField doFlav lstmts)) <$> expand_do_stmts doFlav stmts +expandDoStmts doFlav lstmts@(L _ stmts) + = do { exp <- expand_do_stmts doFlav stmts + ; return $ HSE { hse_ctxt = ExprCtxt (HsDo noExtField doFlav lstmts) + , hse_exp = exp } } -- | The main work horse for expanding do block statements into applications of binds and thens -- See Note [Expanding HsDo with XXExprGhcRn] ===================================== compiler/GHC/Tc/Gen/Expr.hs ===================================== @@ -798,8 +798,8 @@ Example: Typechecking the do expression. The typechecker looks (somewhat) like t The `expandDoStmts` replaces the HsDo { x <- e1; return x } with something like - HSE { hs_ctxt = e - , expanded_expr = e1 >>= \ x -> x } + HSE { hse_ctxt = ExprCtxt e + , hse_exp = e1 >>= \ x -> x } and we then typecheck the expression `e1 >>= \ x -> x` See also Note [Handling overloaded and rebindable constructs] @@ -812,8 +812,8 @@ The rest of this Note explains how that is done. * The expansion process typically takes a user written thing L lspan ue and returns - L lspan (XExpr (ExpandedThingRn (HSE { hs_ctxt = ue - , expanded_expr = ee } )) + L lspan (XExpr (ExpandedThingRn (HSE { hse_ctxt = ue + , hse_exp = ee } )) where `ee` is the expansion of the user written thing `ue` * The type checker context has 3 key fields that describe the context: @@ -830,36 +830,32 @@ The rest of this Note explains how that is done. The `tcl_in_gen_code` is a boolean that keeps track of whether the current expression being typechecked is compiler generated or user generated. - INVARIANT: `tcl_in_gen_code` is modified only in `setSrcSpan`. + INVARIANT: `tcl_loc` and `tcl_in_gen_code` are modified only in `setSrcSpan`. * Now, when tcMonoLExpr :: LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc) gets a located expression, it does 3 things: (a) Calls `setSrcSpanA` to set the ambient source-code location - (b) Calls `addExprCtxt` to add a suitable `HsCtxt` on top of the `tcl_err_ctxt`. + (b) Calls `addExprCtxt` to push a suitable `HsCtxt` on top of the `tcl_err_ctxt`. (c) Calls `tcExpr` to typecheck the expression. * In these calls, if the `span` is generated (see `isGeneratedSrcSpan`), then - - `setSrcSpanA` sets `tcl_in_gen_code` to `True` - - `addErrCtxt` is a no-op if `tcl_in_gen_code` is True - This is how we avoid populating the TcLclCtxt with generated code. - -* The type checker error-stack element `GHC.Tc.Types.ErrCtxt.HsCtxt` - just stores an error message - - type ErrCtxtStack = [HsCtxt] - - When called on an `XExpr`, `addLExprCtxt`, adds the user written thing - `ue`, and the error message provided by the caller on the `ErrCtxtStack` See - Note [ErrCtxtStack Manipulation] for more details. - + - `setSrcSpanA` sets `tcl_in_gen_code` to `True`, and leaves `tcl_loc` unchanged + - `addExprCtxt` is a no-op if `tcl_in_gen_code` is True + The result is that `tcl_loc` has the span from the innermost /user/ tree node; + and the ErrCtxtStack in `tcl_err_ctxt` only has contexts arisign from user code. + +* Note that inside an expansion we have sub-expressions from the original program. + As soon as we enter one of those, identified by a /user/ span, `setSrcSpanA` will + sets the `tcl_loc` to reflect that span, and switch off `tcl_in_gen_code`. Nice! -} tcHsExpansion :: HsExpansion GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc) -tcHsExpansion (HSE o e) res_ty - = do e' <- tcMonoLExpr e res_ty - return $ XExpr (ExpandedThingTc (HSE o e')) +tcHsExpansion (HSE { hse_ctxt = o, hse_exp = e }) res_ty + = do { e' <- tcMonoLExpr e res_ty + ; return $ XExpr $ ExpandedThingTc $ + HSE { hse_ctxt = o, hse_exp = e' } } {- ===================================== compiler/GHC/Tc/Types/LclEnv.hs ===================================== @@ -168,14 +168,12 @@ getLclEnvErrCtxt = tcl_err_ctxt . tcl_lcl_ctxt setLclEnvErrCtxt :: ErrCtxtStack -> TcLclEnv -> TcLclEnv setLclEnvErrCtxt ctxt = modifyLclCtxt (\env -> env { tcl_err_ctxt = ctxt }) --- See Note [ErrCtxtStack Manipulation] addLclEnvErrCtxt :: HsCtxt -> TcLclEnv -> TcLclEnv addLclEnvErrCtxt ec = setLclEnvHsCtxt ec setLclEnvHsCtxt :: HsCtxt -> TcLclEnv -> TcLclEnv setLclEnvHsCtxt ec = modifyLclCtxt (setLclCtxtHsCtxt ec) --- See Note [ErrCtxtStack Manipulation] setLclCtxtHsCtxt :: HsCtxt -> TcLclCtxt -> TcLclCtxt setLclCtxtHsCtxt ec lclCtxt -- Never stack 2 statement error contexts on top of each other View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/c412f21899015334797f9471350aaebc... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/c412f21899015334797f9471350aaebc... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Simon Peyton Jones (@simonpj)