Simon Peyton Jones pushed to branch wip/spj-reinstallable-base at Glasgow Haskell Compiler / GHC
Commits:
-
c77dfcec
by Simon Peyton Jones at 2026-03-20T17:38:25+00:00
-
fd037c83
by Simon Peyton Jones at 2026-03-20T19:42:39+00:00
7 changed files:
- compiler/GHC/Driver/Downsweep.hs
- compiler/GHC/Driver/Pipeline/Execute.hs
- compiler/GHC/HsToCore/Monad.hs
- compiler/GHC/Iface/Load.hs
- compiler/GHC/Tc/Utils/Env.hs
- compiler/GHC/Types/Name.hs
- nofib
Changes:
| ... | ... | @@ -27,7 +27,6 @@ import GHC.Prelude |
| 27 | 27 | import GHC.Platform.Ways
|
| 28 | 28 | |
| 29 | 29 | import GHC.Driver.Config.Finder (initFinderOpts)
|
| 30 | -import GHC.Driver.Config.Parser (initParserOpts)
|
|
| 31 | 30 | import GHC.Driver.DynFlags
|
| 32 | 31 | import GHC.Driver.Phases
|
| 33 | 32 | import {-# SOURCE #-} GHC.Driver.Pipeline (preprocess)
|
| ... | ... | @@ -59,7 +58,6 @@ import qualified GHC.Data.Maybe as M |
| 59 | 58 | import GHC.Data.OsPath ( unsafeEncodeUtf )
|
| 60 | 59 | import GHC.Data.StringBuffer
|
| 61 | 60 | import GHC.Data.Graph.Directed.Reachability
|
| 62 | -import qualified GHC.LanguageExtensions as LangExt
|
|
| 63 | 61 | |
| 64 | 62 | import GHC.Utils.Exception ( throwIO, SomeAsyncException )
|
| 65 | 63 | import GHC.Utils.Outputable
|
| ... | ... | @@ -28,7 +28,6 @@ import GHC.Unit.Module.ModIface |
| 28 | 28 | import GHC.Driver.Backend
|
| 29 | 29 | import GHC.Driver.Session
|
| 30 | 30 | import GHC.Unit.Module.ModSummary
|
| 31 | -import qualified GHC.LanguageExtensions as LangExt
|
|
| 32 | 31 | import GHC.Types.SrcLoc
|
| 33 | 32 | import GHC.Driver.Main
|
| 34 | 33 | import GHC.Driver.Downsweep
|
| ... | ... | @@ -81,7 +81,7 @@ import GHC.Core.Type |
| 81 | 81 | import GHC.Core.Multiplicity
|
| 82 | 82 | |
| 83 | 83 | import GHC.IfaceToCore
|
| 84 | -import GHC.Iface.Load( lookupKnownKeyThing )
|
|
| 84 | +import GHC.Iface.Load( KnownKeyNameSource(..), lookupKnownKeyThing )
|
|
| 85 | 85 | |
| 86 | 86 | import GHC.Tc.Utils.Monad
|
| 87 | 87 | |
| ... | ... | @@ -567,8 +567,8 @@ dsLookupKnownKey :: KnownKeyNameKey -> DsM TyThing |
| 567 | 567 | dsLookupKnownKey uniq
|
| 568 | 568 | = do { rebindable_path <- goptM Opt_RebindableKnownKeyNames
|
| 569 | 569 | ; mb_rdr_env <- if rebindable_path
|
| 570 | - then Just <$> dsGetGlobalRdrEnv
|
|
| 571 | - else return Nothing
|
|
| 570 | + then KKNS_InScope <$> dsGetGlobalRdrEnv
|
|
| 571 | + else return KKNS_FromModule
|
|
| 572 | 572 | ; dsToIfL $
|
| 573 | 573 | do { mb_res <- lookupKnownKeyThing mb_rdr_env uniq
|
| 574 | 574 | ; case mb_res of
|
| ... | ... | @@ -17,9 +17,11 @@ module GHC.Iface.Load ( |
| 17 | 17 | -- Importing one thing
|
| 18 | 18 | importDecl,
|
| 19 | 19 | checkWiredInTyCon, ifCheckWiredInThing,
|
| 20 | - lookupKnownKeyThing, lookupKnownKeyName,
|
|
| 21 | 20 | loadGlobalName,
|
| 22 | 21 | |
| 22 | + -- Known-key things
|
|
| 23 | + KnownKeyNameSource(..), lookupKnownKeyThing, lookupKnownKeyName,
|
|
| 24 | + |
|
| 23 | 25 | -- RnM/TcM functions
|
| 24 | 26 | loadModuleInterface, loadModuleInterfaces,
|
| 25 | 27 | loadSrcInterface, loadSrcInterface_maybe,
|
| ... | ... | @@ -135,17 +137,33 @@ import qualified GHC.Unit.Home.Graph as HUG |
| 135 | 137 | * *
|
| 136 | 138 | ********************************************************************* -}
|
| 137 | 139 | |
| 140 | +data KnownKeyNameSource
|
|
| 141 | + = KKNS_InScope GlobalRdrEnv
|
|
| 142 | + -- Look up the known-key name in this GlobalRdrEnv, which
|
|
| 143 | + -- is the top-level scope of the current module.
|
|
| 144 | + -- This happens when -frebindable-known-key-name is set, usually when
|
|
| 145 | + -- we are compiling `ghc-internal` or `base`
|
|
| 146 | + |
|
| 147 | + | KKNS_FromModule
|
|
| 148 | + -- Look up the known-key name in the export list of GHC.KnownKeyNames
|
|
| 149 | + -- This is the "normal path", and happens when -frebindable-known-key-name
|
|
| 150 | + -- is /not/ set
|
|
| 151 | + |
|
| 152 | +instance Outputable KnownKeyNameSource where
|
|
| 153 | + ppr KKNS_FromModule = text "FromModule"
|
|
| 154 | + ppr (KKNS_InScope env) = text "InScope" <> braces (ppr env)
|
|
| 155 | + |
|
| 138 | 156 | lookupKnownKeyThing :: HasDebugCallStack
|
| 139 | - => Maybe GlobalRdrEnv -> KnownKeyNameKey
|
|
| 157 | + => KnownKeyNameSource -> KnownKeyNameKey
|
|
| 140 | 158 | -> IfM lcl (MaybeErr IfaceMessage TyThing)
|
| 141 | 159 | lookupKnownKeyThing mb_gbl_rdr_env key
|
| 142 | 160 | = do { name <- lookupKnownKeyName mb_gbl_rdr_env key
|
| 143 | 161 | ; lookupGlobalName name }
|
| 144 | 162 | |
| 145 | 163 | lookupKnownKeyName :: HasDebugCallStack
|
| 146 | - => Maybe GlobalRdrEnv -> KnownKeyNameKey
|
|
| 164 | + => KnownKeyNameSource -> KnownKeyNameKey
|
|
| 147 | 165 | -> IfM lcl Name
|
| 148 | -lookupKnownKeyName Nothing uniq
|
|
| 166 | +lookupKnownKeyName KKNS_FromModule uniq
|
|
| 149 | 167 | = do { known_key_name_map :: UniqFM KnownKeyNameKey Name <- loadKnownKeyOccMap
|
| 150 | 168 | ; let name = lookupUFM known_key_name_map uniq
|
| 151 | 169 | `orElse` pprPanic "lookupKnownKeyThing 1"
|
| ... | ... | @@ -155,7 +173,7 @@ lookupKnownKeyName Nothing uniq |
| 155 | 173 | 2 (ppr name <+> ppr uniq)
|
| 156 | 174 | ; return name }
|
| 157 | 175 | |
| 158 | -lookupKnownKeyName (Just gbl_rdr_env) uniq
|
|
| 176 | +lookupKnownKeyName (KKNS_InScope gbl_rdr_env) uniq
|
|
| 159 | 177 | -- Just gbl_rdr_env: we have -frebindable-known-key-names on, and
|
| 160 | 178 | -- here is the top-level GlobalRdrEnv
|
| 161 | 179 | -- Look up the known-key OccName in the GlobalRdrEnv
|
| ... | ... | @@ -212,6 +230,7 @@ loadKnownKeyOccMap |
| 212 | 230 | where
|
| 213 | 231 | doc = text "Need interface for KnonwKeyNames"
|
| 214 | 232 | |
| 233 | +#ifdef DEBUG
|
|
| 215 | 234 | checkKnownKeyNamesIface :: KnownKeyNameMap -> Maybe SDoc
|
| 216 | 235 | -- Check that KnownKeyNames exports all the things defined in `basicKnownKeyTable`
|
| 217 | 236 | -- and the the uniques and occ-names agree
|
| ... | ... | @@ -223,6 +242,7 @@ checkKnownKeyNamesIface known_key_names_occ_map |
| 223 | 242 | is_bad (occ, key) = case lookupUFM known_key_names_occ_map key of
|
| 224 | 243 | Nothing -> True
|
| 225 | 244 | Just name -> getOccName name /= occ
|
| 245 | +#endif
|
|
| 226 | 246 | |
| 227 | 247 | {- *********************************************************************
|
| 228 | 248 | * *
|
| ... | ... | @@ -509,8 +509,8 @@ tcLookupKnownKeyGlobal :: HasDebugCallStack => Unique -> TcM TyThing |
| 509 | 509 | tcLookupKnownKeyGlobal uniq
|
| 510 | 510 | = do { rebindable_path <- goptM Opt_RebindableKnownKeyNames
|
| 511 | 511 | ; mb_rdr_env <- if rebindable_path
|
| 512 | - then Just <$> getGlobalRdrEnv
|
|
| 513 | - else return Nothing
|
|
| 512 | + then KKNS_InScope <$> getGlobalRdrEnv
|
|
| 513 | + else return KKNS_FromModule
|
|
| 514 | 514 | ; traceTc "tcLookupKnownKeyGlobal" (ppr rebindable_path $$ ppr mb_rdr_env)
|
| 515 | 515 | ; mb_thing <- initIfaceTcRn (lookupKnownKeyThing mb_rdr_env uniq)
|
| 516 | 516 | ; case mb_thing of
|
| ... | ... | @@ -180,6 +180,45 @@ To implement all this, here are the moving parts: |
| 180 | 180 | |
| 181 | 181 | knownKeyUniqMap :: UniqFM KnownKeyNameKey OccName
|
| 182 | 182 | |
| 183 | +* A new module `base:GHC.KnownKeyNames` exports all the known-key names.
|
|
| 184 | + There is nothing special about this module except that GHC knows its
|
|
| 185 | + name and can import it.
|
|
| 186 | + |
|
| 187 | + In effect, the `mi_exports` of `GHC/KnownKeyNames.hi` tells GHC where each
|
|
| 188 | + known-key name is defined.
|
|
| 189 | + |
|
| 190 | + This is one reason for (KnownKeyInvariant): an export list cannot have two
|
|
| 191 | + entities with the same OccName.
|
|
| 192 | + |
|
| 193 | +* Known-key name lookup (normal case: KKNS_FromModule)
|
|
| 194 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 195 | + In normal client code, suppose the desugarer calls `dsLookupKnownKeyTyCon`
|
|
| 196 | + on `rationalTyConKey`. Then, in `loadKnownKeyOccMap`
|
|
| 197 | + * GHC imports GHC.KnownKeyNames, i.e. looks for `GHC/KnownKeyNames.hi`
|
|
| 198 | + * Assuming this is successful, GHC usees its `mi_exports` to builds a mapping
|
|
| 199 | + `KnownKeyNameMap` from each known-key unique to the Name of the entity.
|
|
| 200 | + * It stashes this map in the `eps_known_keys` field of the ExternalPackageState
|
|
| 201 | + so that it doesn't need to repeat the exercise.
|
|
| 202 | + Now it can simplhy look up `rationalTyConKey` in the `eps_known_keys`. Easy!
|
|
| 203 | + See `dsLookupKnownKeyName`.
|
|
| 204 | + |
|
| 205 | +* Known-key name lookup (base case: KKNS_InScope)
|
|
| 206 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 207 | + We can't follow the above plan when compiling modules in `base` or `ghc-internal` because
|
|
| 208 | + GHC.KnownKeyNames has not yet been compiled! Instead, we use whatever is in scope with
|
|
| 209 | + the desired `OccName`, rather like `-XRebindableSyntax`.
|
|
| 210 | + |
|
| 211 | + See the `KnownKeyNameSource` argument to `lookupKnownKeyName`. When compiling modules
|
|
| 212 | + in `ghc-internal` or `base`:
|
|
| 213 | + * We switch on -frebindable-known-key-names
|
|
| 214 | + * That ensures that we pass `KKNS_InScope` to `lookupKnownKeyName`
|
|
| 215 | + * The latter now looks in the GlobalRdrEnv it is passed.
|
|
| 216 | + |
|
| 217 | + This does mean that in `base` and `ghc-internal` we occasionally need an extra import
|
|
| 218 | + to bring into scope some entities that are needed by `dsLookupKnownKeyTyCon` etc.
|
|
| 219 | + See also wrinkle (KKN1)
|
|
| 220 | + |
|
| 221 | +* Defin
|
|
| 183 | 222 | * There are two flags that control the treatment of known-key names:
|
| 184 | 223 | -frebindable-known-key-names
|
| 185 | 224 | -fdefines-known-key-names
|
| ... | ... | @@ -188,6 +227,10 @@ To implement all this, here are the moving parts: |
| 188 | 227 | * In client source code, that is, /without/ -frebindable-known-key-names,
|
| 189 | 228 | we try to import GHC.KnownKeyNames. Assuming this is succesful,
|
| 190 | 229 | |
| 230 | +Wrinkles
|
|
| 231 | + |
|
| 232 | +(KKN1) In
|
|
| 233 | + |
|
| 191 | 234 | Note [About the NameSorts]
|
| 192 | 235 | ~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 193 | 236 | 1. Initially:
|
| 1 | -Subproject commit ae985b599e958414b327e4f220d99b7248601d55 |
|
| 1 | +Subproject commit b7391df4540ac8b11b35e1b2e2c15819b5171798 |