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 add Overview note - - - - - dd638011 by Apoorv Ingle at 2026-01-15T12:40:36-06:00 fix for failing test case - - - - - 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: ===================================== compiler/GHC/Tc/Gen/Do.hs ===================================== @@ -114,7 +114,7 @@ expand_do_stmts doFlavour (stmt@(L loc (BindStmt xbsrn pat e)): lstmts) | otherwise = pprPanic "expand_do_stmts: The impossible happened, missing bind operator from renamer" (text "stmt" <+> ppr stmt) -expand_do_stmts doFlavour (stmt@(L loc (BodyStmt _ (L _e_lspan e) (SyntaxExprRn then_op) _)) : lstmts) = +expand_do_stmts doFlavour (stmt@(L loc (BodyStmt _ (L e_lspan e) (SyntaxExprRn then_op) _)) : lstmts) = -- See Note [BodyStmt] in Language.Haskell.Syntax.Expr -- See Note [Expanding HsDo with XXExprGhcRn] Equation (1) below -- stmts ~~> stmts' @@ -122,8 +122,8 @@ expand_do_stmts doFlavour (stmt@(L loc (BodyStmt _ (L _e_lspan e) (SyntaxExprRn -- e ; stmts ~~> (>>) e stmts' do expand_stmts_expr <- expand_do_stmts doFlavour lstmts let expansion = genHsExpApps then_op -- (>>) - [ -- L e_lspan (mkExpandedStmt stmt doFlavour e) - wrapGenSpan e + [ L e_lspan (mkExpandedStmt stmt doFlavour e) + -- wrapGenSpan e , expand_stmts_expr ] return $ L loc (mkExpandedStmt stmt doFlavour expansion) ===================================== compiler/GHC/Tc/Gen/Expr.hs ===================================== @@ -304,6 +304,7 @@ tcExpr :: HsExpr GhcRn -- - ones taken apart by GHC.Tc.Gen.Head.splitHsApps -- - ones understood by GHC.Tc.Gen.Head.tcInferAppHead_maybe -- See Note [Application chains and heads] in GHC.Tc.Gen.App +-- Se Note [Overview of Typechecking an XExpr] tcExpr e@(HsVar {}) res_ty = tcApp e res_ty tcExpr e@(HsApp {}) res_ty = tcApp e res_ty tcExpr e@(OpApp {}) res_ty = tcApp e res_ty @@ -761,6 +762,50 @@ tcExpr (SectionR {}) ty = pprPanic "tcExpr:SectionR" (ppr ty) ************************************************************************ -} +{- Note [Overview of Typechecking an XExpr] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Certain constructs undergo expansion right before type checking. + + tcExpr ue@(RecordUpd{}) rho = do { ee <- expand e; tcExpr ee rho } + +See Note [Handling overloaded and rebindable constructs] and +Note [Doing XXExprGhcRn in the Renamer vs Typechecker] +for details about which constructs are expanded. + +The expansion process typically takes a user written thing + L lspan ue +and returns + L lspan (XExpr (ExpandedThingRn { xrn_orig = ue + , xrn_expanded = ee } )) + +where `ee` is the expansion of the user written thing `ue` + +Now, when a `tcMonoLHsExpr :: LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)` +gets a located expression, It does 2 things: +1. calls `addLExprCtxt` to perform error context management, and; +2. calls tcExpr to typecheck the expression. + +The type checker context has 2 key fields: + + TcLclCtxt { tcl_loc :: RealSrcSpan + , tcl_err_ctxt :: [ErrCtxt] + , ... } + +When called on an XExpr, `addLExprCtxt` updates the location of `tcl_loc` with +the `lspan` above and adds an ErrCtxt on top of the `tcl_err_ctxt`. If the +`lspan` is generated, then `addLExprCtxt` is a no-op. + +The type checker error stack element `GHC.Tc.Types.ErrCtxt.ErrCtxt` has two fields + + ErrCtxt = EC CodeSrcFlag ErrCtxtMsgM + +`CodeSrcFlag` says whether we are typechecking an expanded thing, and what that expanded thing is +`ErrCtxtMsgM` stores the pre-text error message itself. 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. + +-} + tcXExpr :: XXExprGhcRn -> ExpRhoType -> TcM (HsExpr GhcTc) tcXExpr (ExpandedThingRn o e) res_ty = mkExpandedTc o <$> -- necessary for hpc ticks @@ -1482,7 +1527,7 @@ expandRecordUpd record_expr possible_parents rbnds res_ty ds_expr = HsLet noExtField let_binds (wrapGenSpan case_expr) case_expr :: HsExpr GhcRn - case_expr = HsCase RecUpd (wrapGenSpan (unLoc record_expr)) + case_expr = HsCase RecUpd record_expr $ mkMatchGroup (Generated OtherExpansion DoPmc) (wrapGenSpan matches) matches :: [LMatch GhcRn (LHsExpr GhcRn)] matches = map make_pat (NE.toList relevant_cons) ===================================== compiler/GHC/Tc/Types/LclEnv.hs ===================================== @@ -199,6 +199,7 @@ 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 :: ErrCtxt -> TcLclEnv -> TcLclEnv addLclEnvErrCtxt ec = setLclEnvSrcCodeOrigin ec ===================================== compiler/GHC/Tc/Utils/Monad.hs ===================================== @@ -1333,6 +1333,7 @@ addErrCtxt :: ErrCtxtMsg -> TcM a -> TcM a {-# INLINE addErrCtxt #-} -- Note [Inlining addErrCtxt] addErrCtxt msg = addErrCtxtM (\env -> return (env, msg)) +-- See Note [ErrCtxtStack Manipulation] addExpansionErrCtxt :: SrcCodeOrigin -> ErrCtxtMsg -> TcM a -> TcM a {-# INLINE addExpansionErrCtxt #-} -- Note [Inlining addErrCtxt] addExpansionErrCtxt o msg = addExpansionErrCtxtM o (\env -> return (env, msg)) @@ -1343,6 +1344,7 @@ addErrCtxtM :: (TidyEnv -> ZonkM (TidyEnv, ErrCtxtMsg)) -> TcM a -> TcM a {-# INLINE addErrCtxtM #-} -- Note [Inlining addErrCtxt] addErrCtxtM ctxt = pushCtxt (MkErrCtxt VanillaUserSrcCode ctxt) +-- See Note [ErrCtxtStack Manipulation] addExpansionErrCtxtM :: SrcCodeOrigin -> (TidyEnv -> ZonkM (TidyEnv, ErrCtxtMsg)) -> TcM a -> TcM a {-# INLINE addExpansionErrCtxtM #-} -- Note [Inlining addErrCtxt] addExpansionErrCtxtM o ctxt = pushCtxt (MkErrCtxt (ExpansionCodeCtxt o) ctxt) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/6b8198f1d3f0d7fd7d599c449ede1d7... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/6b8198f1d3f0d7fd7d599c449ede1d7... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Apoorv Ingle (@ani)