[Git][ghc/ghc][wip/spj-apporv-Oct24] Wrinkle [Why GeneratedSrcSpanDetails in GeneratedSrcSpan] in Note...
Apoorv Ingle pushed to branch wip/spj-apporv-Oct24 at Glasgow Haskell Compiler / GHC Commits: 12945b32 by Apoorv Ingle at 2026-03-23T10:45:43-05:00 Wrinkle [Why GeneratedSrcSpanDetails in GeneratedSrcSpan] in Note [Typechecking by expansion: overview] - - - - - 2 changed files: - compiler/GHC/Tc/Gen/Expr.hs - compiler/GHC/Types/SrcLoc.hs Changes: ===================================== compiler/GHC/Tc/Gen/Expr.hs ===================================== @@ -849,6 +849,60 @@ The rest of this Note explains how that is done. * 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! + +Wrinkle [Why GeneratedSrcSpanDetails in GeneratedSrcSpan] + +* HIE Ast is used to compute information necessary for IDE tooling + and haddock document generation. + https://gitlab.haskell.org/ghc/ghc/-/wikis/hie-files +* Compiler expanded code may give rise to constraints that are solved and stored + as evidences in the typecheck AST, LHsExpr GhcTc. Naturally, those evidences are marked as + arising from a generated code. Some of these evidences, however, need to be + traced back to the original user written code. (c.f. T23540 haddockHypsrcTest) + +* Eg. consider the user written overloaded list example: + + data BetterList x = Nil | Cons x (BetterList x) + + list :: BetterList Modulo1 + list = [0, 1, 2, 3, Zero] + + Over loadedlists are expanded by the compiler to + + list = XExpr (ExpandedThingRn + { hs_ctxt = [0, 1, 2, 3, Zero] + , hs_ex = L <gen> fromListN 5 [0, 1, 2, 3, Zero] }) + + where `fromListN :: IsList l => Int -> [Item l] -> l` + + + HIE AST needs to associate the implicit typeclass evidence bound by + the call to `fromListN` to the user written expression `[0, 1, 2, 3, Zero]` + + ========================== + At point (43,8), we found: + ========================== + ┌ + │ $dIsList at T23540.hs:1:1, of type: IsList (BetterList Modulo1) + │ is an evidence variable bound by a let, depending on: [$fIsListBetterList] + │ with scope: ModuleScope + │ + │ Defined at <no location info> + └ + | + `- ┌ + │ $fIsListBetterList at T23540.hs:36:10-30, of type: forall x. IsList (BetterList x) + │ is an evidence variable bound by an instance of class IsList + │ with scope: ModuleScope + │ + │ Defined at T23540.hs:36:10 + └ + + We hence embedd the original source code in generated source span details in `SrcSpan.GeneratedSrcSpan` + and the function `Iface.Ext.Ast.getUnlocatedEvBinds` can build the appropriate Hie ast Node pointing + to the user written expression. + + -} tcHsExpansion :: HsExpansion GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc) ===================================== compiler/GHC/Types/SrcLoc.hs ===================================== @@ -390,13 +390,16 @@ instance Semigroup BufSpan where -- or a human-readable description of a location. data SrcSpan = RealSrcSpan !RealSrcSpan !(Strict.Maybe BufSpan) -- See Note [Why Maybe BufPos] - | GeneratedSrcSpan !GeneratedSrcSpanDetails + | GeneratedSrcSpan !GeneratedSrcSpanDetails -- See Wrinkle [Why GeneratedSrcSpanDetails in GeneratedSrcSpan] + -- in Note [Typechecking by expansion: overview] | UnhelpfulSpan !UnhelpfulSpanReason deriving (Eq, Show) -- Show is used by GHC.Parser.Lexer, because we -- derive Show for Token --- Needed for HIE. See Note [Source locations for implicit function calls] in GHC.Iface.Ext.Ast +-- Needed for HIE. +-- See Note [Source locations for implicit function calls] in GHC.Iface.Ext.Ast +-- See Wrinkle [Why GeneratedSrcSpanDetails in GeneratedSrcSpan] in Note [Typechecking by expansion: overview] data GeneratedSrcSpanDetails = OrigSpan !RealSrcSpan -- this the span of the user written thing | UnhelpfulGenerated -- we do not have the original location. View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/12945b324b5c15f9d671094ac51d36da... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/12945b324b5c15f9d671094ac51d36da... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Apoorv Ingle (@ani)