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 Import wibbles - - - - - fd037c83 by Simon Peyton Jones at 2026-03-20T19:42:39+00:00 Wibbles - - - - - 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: ===================================== compiler/GHC/Driver/Downsweep.hs ===================================== @@ -27,7 +27,6 @@ import GHC.Prelude import GHC.Platform.Ways import GHC.Driver.Config.Finder (initFinderOpts) -import GHC.Driver.Config.Parser (initParserOpts) import GHC.Driver.DynFlags import GHC.Driver.Phases import {-# SOURCE #-} GHC.Driver.Pipeline (preprocess) @@ -59,7 +58,6 @@ import qualified GHC.Data.Maybe as M import GHC.Data.OsPath ( unsafeEncodeUtf ) import GHC.Data.StringBuffer import GHC.Data.Graph.Directed.Reachability -import qualified GHC.LanguageExtensions as LangExt import GHC.Utils.Exception ( throwIO, SomeAsyncException ) import GHC.Utils.Outputable ===================================== compiler/GHC/Driver/Pipeline/Execute.hs ===================================== @@ -28,7 +28,6 @@ import GHC.Unit.Module.ModIface import GHC.Driver.Backend import GHC.Driver.Session import GHC.Unit.Module.ModSummary -import qualified GHC.LanguageExtensions as LangExt import GHC.Types.SrcLoc import GHC.Driver.Main import GHC.Driver.Downsweep ===================================== compiler/GHC/HsToCore/Monad.hs ===================================== @@ -81,7 +81,7 @@ import GHC.Core.Type import GHC.Core.Multiplicity import GHC.IfaceToCore -import GHC.Iface.Load( lookupKnownKeyThing ) +import GHC.Iface.Load( KnownKeyNameSource(..), lookupKnownKeyThing ) import GHC.Tc.Utils.Monad @@ -567,8 +567,8 @@ dsLookupKnownKey :: KnownKeyNameKey -> DsM TyThing dsLookupKnownKey uniq = do { rebindable_path <- goptM Opt_RebindableKnownKeyNames ; mb_rdr_env <- if rebindable_path - then Just <$> dsGetGlobalRdrEnv - else return Nothing + then KKNS_InScope <$> dsGetGlobalRdrEnv + else return KKNS_FromModule ; dsToIfL $ do { mb_res <- lookupKnownKeyThing mb_rdr_env uniq ; case mb_res of ===================================== compiler/GHC/Iface/Load.hs ===================================== @@ -17,9 +17,11 @@ module GHC.Iface.Load ( -- Importing one thing importDecl, checkWiredInTyCon, ifCheckWiredInThing, - lookupKnownKeyThing, lookupKnownKeyName, loadGlobalName, + -- Known-key things + KnownKeyNameSource(..), lookupKnownKeyThing, lookupKnownKeyName, + -- RnM/TcM functions loadModuleInterface, loadModuleInterfaces, loadSrcInterface, loadSrcInterface_maybe, @@ -135,17 +137,33 @@ import qualified GHC.Unit.Home.Graph as HUG * * ********************************************************************* -} +data KnownKeyNameSource + = KKNS_InScope GlobalRdrEnv + -- Look up the known-key name in this GlobalRdrEnv, which + -- is the top-level scope of the current module. + -- This happens when -frebindable-known-key-name is set, usually when + -- we are compiling `ghc-internal` or `base` + + | KKNS_FromModule + -- Look up the known-key name in the export list of GHC.KnownKeyNames + -- This is the "normal path", and happens when -frebindable-known-key-name + -- is /not/ set + +instance Outputable KnownKeyNameSource where + ppr KKNS_FromModule = text "FromModule" + ppr (KKNS_InScope env) = text "InScope" <> braces (ppr env) + lookupKnownKeyThing :: HasDebugCallStack - => Maybe GlobalRdrEnv -> KnownKeyNameKey + => KnownKeyNameSource -> KnownKeyNameKey -> IfM lcl (MaybeErr IfaceMessage TyThing) lookupKnownKeyThing mb_gbl_rdr_env key = do { name <- lookupKnownKeyName mb_gbl_rdr_env key ; lookupGlobalName name } lookupKnownKeyName :: HasDebugCallStack - => Maybe GlobalRdrEnv -> KnownKeyNameKey + => KnownKeyNameSource -> KnownKeyNameKey -> IfM lcl Name -lookupKnownKeyName Nothing uniq +lookupKnownKeyName KKNS_FromModule uniq = do { known_key_name_map :: UniqFM KnownKeyNameKey Name <- loadKnownKeyOccMap ; let name = lookupUFM known_key_name_map uniq `orElse` pprPanic "lookupKnownKeyThing 1" @@ -155,7 +173,7 @@ lookupKnownKeyName Nothing uniq 2 (ppr name <+> ppr uniq) ; return name } -lookupKnownKeyName (Just gbl_rdr_env) uniq +lookupKnownKeyName (KKNS_InScope gbl_rdr_env) uniq -- Just gbl_rdr_env: we have -frebindable-known-key-names on, and -- here is the top-level GlobalRdrEnv -- Look up the known-key OccName in the GlobalRdrEnv @@ -212,6 +230,7 @@ loadKnownKeyOccMap where doc = text "Need interface for KnonwKeyNames" +#ifdef DEBUG checkKnownKeyNamesIface :: KnownKeyNameMap -> Maybe SDoc -- Check that KnownKeyNames exports all the things defined in `basicKnownKeyTable` -- and the the uniques and occ-names agree @@ -223,6 +242,7 @@ checkKnownKeyNamesIface known_key_names_occ_map is_bad (occ, key) = case lookupUFM known_key_names_occ_map key of Nothing -> True Just name -> getOccName name /= occ +#endif {- ********************************************************************* * * ===================================== compiler/GHC/Tc/Utils/Env.hs ===================================== @@ -509,8 +509,8 @@ tcLookupKnownKeyGlobal :: HasDebugCallStack => Unique -> TcM TyThing tcLookupKnownKeyGlobal uniq = do { rebindable_path <- goptM Opt_RebindableKnownKeyNames ; mb_rdr_env <- if rebindable_path - then Just <$> getGlobalRdrEnv - else return Nothing + then KKNS_InScope <$> getGlobalRdrEnv + else return KKNS_FromModule ; traceTc "tcLookupKnownKeyGlobal" (ppr rebindable_path $$ ppr mb_rdr_env) ; mb_thing <- initIfaceTcRn (lookupKnownKeyThing mb_rdr_env uniq) ; case mb_thing of ===================================== compiler/GHC/Types/Name.hs ===================================== @@ -180,6 +180,45 @@ To implement all this, here are the moving parts: knownKeyUniqMap :: UniqFM KnownKeyNameKey OccName +* A new module `base:GHC.KnownKeyNames` exports all the known-key names. + There is nothing special about this module except that GHC knows its + name and can import it. + + In effect, the `mi_exports` of `GHC/KnownKeyNames.hi` tells GHC where each + known-key name is defined. + + This is one reason for (KnownKeyInvariant): an export list cannot have two + entities with the same OccName. + +* Known-key name lookup (normal case: KKNS_FromModule) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + In normal client code, suppose the desugarer calls `dsLookupKnownKeyTyCon` + on `rationalTyConKey`. Then, in `loadKnownKeyOccMap` + * GHC imports GHC.KnownKeyNames, i.e. looks for `GHC/KnownKeyNames.hi` + * Assuming this is successful, GHC usees its `mi_exports` to builds a mapping + `KnownKeyNameMap` from each known-key unique to the Name of the entity. + * It stashes this map in the `eps_known_keys` field of the ExternalPackageState + so that it doesn't need to repeat the exercise. + Now it can simplhy look up `rationalTyConKey` in the `eps_known_keys`. Easy! + See `dsLookupKnownKeyName`. + +* Known-key name lookup (base case: KKNS_InScope) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + We can't follow the above plan when compiling modules in `base` or `ghc-internal` because + GHC.KnownKeyNames has not yet been compiled! Instead, we use whatever is in scope with + the desired `OccName`, rather like `-XRebindableSyntax`. + + See the `KnownKeyNameSource` argument to `lookupKnownKeyName`. When compiling modules + in `ghc-internal` or `base`: + * We switch on -frebindable-known-key-names + * That ensures that we pass `KKNS_InScope` to `lookupKnownKeyName` + * The latter now looks in the GlobalRdrEnv it is passed. + + This does mean that in `base` and `ghc-internal` we occasionally need an extra import + to bring into scope some entities that are needed by `dsLookupKnownKeyTyCon` etc. + See also wrinkle (KKN1) + +* Defin * There are two flags that control the treatment of known-key names: -frebindable-known-key-names -fdefines-known-key-names @@ -188,6 +227,10 @@ To implement all this, here are the moving parts: * In client source code, that is, /without/ -frebindable-known-key-names, we try to import GHC.KnownKeyNames. Assuming this is succesful, +Wrinkles + +(KKN1) In + Note [About the NameSorts] ~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. Initially: ===================================== nofib ===================================== @@ -1 +1 @@ -Subproject commit ae985b599e958414b327e4f220d99b7248601d55 +Subproject commit b7391df4540ac8b11b35e1b2e2c15819b5171798 View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/f8a69d19f61a61831bb74a91e88b9c1... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/f8a69d19f61a61831bb74a91e88b9c1... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Simon Peyton Jones (@simonpj)