[Git][ghc/ghc][wip/spj-reinstallable-base2] Small changes in response to reviews
Simon Peyton Jones pushed to branch wip/spj-reinstallable-base2 at Glasgow Haskell Compiler / GHC Commits: c256a107 by Simon Peyton Jones at 2026-04-21T21:39:18+01:00 Small changes in response to reviews - - - - - 14 changed files: - compiler/GHC/Builtin.hs - compiler/GHC/Builtin/KnownKeys.hs - docs/users_guide/exts/rebindable_syntax.rst - libraries/base/src/Data/Array/Byte.hs - libraries/base/src/Data/Bool.hs - libraries/base/src/Data/Fixed.hs - libraries/base/src/Data/Functor/Compose.hs - libraries/base/src/Data/Functor/Product.hs - libraries/base/src/Data/Functor/Sum.hs - libraries/base/src/Prelude.hs - libraries/base/src/System/Console/GetOpt.hs - libraries/ghc-internal/src/GHC/Internal/Base.hs - libraries/ghc-internal/src/GHC/Internal/OverloadedLabels.hs - utils/genprimopcode/Main.hs Changes: ===================================== compiler/GHC/Builtin.hs ===================================== @@ -112,9 +112,9 @@ Here are more details. A "wired-in" entity: * Defined in GHC.Builtin.WiredIn.* - * Its Unique, OccName - * Its defining module - * Its data constructors etc + * GHC knows its Unique, OccName + * GHC knows its defining module + * GHC knows its data constructors etc So GHC knows /everything/ about it. See Note [Overview of wired-in things]. We try hard to avoid wired-in things; it's tricky to ensure that GHC's static @@ -123,20 +123,22 @@ A "wired-in" entity: A "known-key" entity: * Defined in GHC.Builtin.KnownKeys * Its Unique and OccName are baked into GHC. Its Unique is called a KnownKey. - * It is exported by base:GHC.KnownKeyNames + * It is exported by GHC.KnownKeyNames * But that's all that GHC knows about it In particular, GHC does /not/ know in which module the entity is defined. Example: the `Eq` class has OccName "Eq" and unique `eqClassKey`. It happens to be defined in ghc-internal:GHC.Internal.Classes, but GHC does not know that. + Every known-key entity is also a known-occ entity, but not vice versa. + See Note [Recipe for adding a known-key name] for how to add a known-key name to GHC. It's not hard. A "known-occ" entity: * Defined in GHC.Builtin.KnownOccs * Its OccName is baked into GHC -- we call it a KnownOcc - * It is exported by base:GHC.KnownKeyNames + * It is exported by GHC.KnownKeyNames * But that's all that GHC knows about it In particular, GHC does /not/ know in which module the entity is defined, nor its Unique. @@ -189,14 +191,22 @@ When do we use each of these? and then renames and typechecks them. These bindings refer to a myriad of identifiers, such as `(==)`, `(>)`, `inRange`, and so on. Again GHC does not need to know a statically-known unique for them, but it does need to find them - so it uses known + so it uses known-occ names for them. See lots ant lots of definitions like + gunfold_RDR :: RdrName + gunfold_RDR = knownVarOccRdrName "gunfold" + in GHC.Builtin.KnownOccs. This definition constructs a known-occ RdrName; sse + knownOccRdrName :: KnownOcc -> RdrName + in GHC.Types.Name.Reader * When desugaring, the desugarer wants to refer to a particular class, type, or function. It does this via (e.g.) + dsLookupKnownOccTyCon eitherTyConOcc + where dsLookupKnownOccTyCon :: KnownOcc -> DsM TyCon - or + + For known-key entities you can also use dsLookupKnownKeyTyCon :: KnownKey -> DsM TyCon - (It doesn't really matter which we use.) + by giving it the known key of the entity. To implement all this, here are the moving parts. @@ -209,7 +219,7 @@ How known-occ entities work this is not an onerous restriction. But see Note [Tricky known-occ cases] in GHC.Builtin.KnownOccs for some awkward cases. -* A special module `base:GHC.KnownKeyNames` exports all the known-key and known-occ +* A distinguished module `GHC.KnownKeyNames` exports all the known-key and known-occ entities names. There is nothing special about this module except that GHC knows its name and can import it. @@ -220,6 +230,9 @@ How known-occ entities work This is a big reason for (KnownEntityInvariant): an export list cannot have two entities with the same OccName. + When GHC wants to find GHC.KnownKeyNames, it just looks for it in the same + way as any other import. + * There are three flags that control the treatment of known entities: -frebindable-known-names -fdefines-known-names @@ -307,10 +320,10 @@ Known-key entities are * DEFINING. In the module that /defines/ a known-key name, such as the `Num` class in ghc-internal:GHC.Internal.Num - we must assign the correct Unique. So in GHC.Rename.Env.newTopVanillaSrcBinder - if -fdefines-known-key-names is set (Opt_DefinesKnownKeyNames), we check the - OccName against the list in `knownKeyTable`; if it appears there, we use the - Unique from the table. + we must assign the correct Unique at its definitino site. So in + `GHC.Rename.Env.newTopVanillaSrcBinder`, if -fdefines-known-key-names is set + (Opt_DefinesKnownKeyNames), we check the OccName against the list in `knownKeyTable`; + if it appears there, we use the Unique from the table. * SERIALISING. - When we serialise a known-key name into an interface file, we mark it as such. @@ -343,7 +356,7 @@ Wrinkles So we compile GHC.Internal.Data.Foldable with -fexclude-known-define=toList -(KN3) We don't need need to export wired-in entities from GHC.KnownKeyNames +(KN3) We don't need to export wired-in entities from GHC.KnownKeyNames because we (should) never look up a wired-in name via its key. That is, `GHC.Iface.Load.lookupKnownKeyName` should never be called on the key of a wired-in name. @@ -355,9 +368,9 @@ Wrinkles Note [Recipe for adding a known-occ name] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -To make `wombat` into a known-occ name, you must ensure that: +To make `wombat` into a known-occ name, you do the following: -* The module `GHC.KnownKeyNames` must export `wombat`. +* Ensure that the module `GHC.KnownKeyNames` exports `wombat`. * In any module in `base` or `ghc-internal` (which are compiled with -frebindable-known-names), in which `wombat` is needed, you must ensure @@ -373,14 +386,14 @@ To make `wombat` into a known-occ name, you must ensure that: Note [Recipe for adding a known-key name] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -To make `wombat` into a known-key name, you must ensure that: +To make `wombat` into a known-key name, do the following. -* The module M that defines `wombat` is compiled with `-fdefines-known-names`. +* Ensure that the module M that defines `wombat` is compiled with `-fdefines-known-names`. -* If M.hs has an `M.hs-boot` file, it too must be compiled +* If M.hs has an `M.hs-boot` file, ensure that it too must be compiled with `-fdefines-known-names`. -* The module `GHC.KnownKeyNames` must export `wombat`. +* Ensure that the module `GHC.KnownKeyNames` exports `wombat`. * In GHC.Builtin.KnownKeys you must define a static unique wombatKey :: KnownKey ===================================== compiler/GHC/Builtin/KnownKeys.hs ===================================== @@ -1,9 +1,6 @@ {- (c) The GRASP/AQUA Project, Glasgow University, 1992-1998 -\section[GHC.Builtin.KnownKeys]{Definitions of prelude modules and names} - - Nota Bene: all Names defined in here should come from the base package, the big-num package or (for plugins) the ghc package. ===================================== docs/users_guide/exts/rebindable_syntax.rst ===================================== @@ -78,16 +78,6 @@ not the Prelude versions: - An overloaded label "``#foo``" means "``fromLabel @"foo"``", rather than "``GHC.OverloadedLabels.fromLabel @"foo"``" (see :ref:`overloaded-labels`). -.. extension:: ImplicitKnownKeyNames - :shortdesc: Use module ``KnownKeyNames`` to find known-key names - - ToDo: needs proper documentation - -.. extension:: DefinesKnownKeyNames - :shortdesc: This modules defines one or more known-key names - - ToDo: needs proper documentation - :extension:`RebindableSyntax` implies :extension:`NoImplicitPrelude`. In all cases (apart from arrow notation), the static semantics should be ===================================== libraries/base/src/Data/Array/Byte.hs ===================================== @@ -15,15 +15,13 @@ {-# LANGUAGE UnboxedTuples #-} {-# LANGUAGE TemplateHaskellQuotes #-} -{-# OPTIONS_GHC -fno-rebindable-known-names #-} - -- We import Prelude, hence GHC.KnownKeyNames is available - module Data.Array.Byte ( ByteArray(..), MutableByteArray(..), ) where import Prelude +import qualified GHC.KnownKeyNames as Rebindable import GHC.Internal.Data.Bits ((.&.), unsafeShiftR) import GHC.Internal.Data.Data (mkNoRepType, Data(..)) import GHC.Internal.Data.Typeable (Typeable) ===================================== libraries/base/src/Data/Bool.hs ===================================== @@ -1,8 +1,5 @@ {-# LANGUAGE Safe #-} -{-# OPTIONS_GHC -fno-rebindable-known-names #-} - -- We import Prelude, hence GHC.KnownKeyNames is available - -- | -- -- Module : Data.Bool @@ -28,6 +25,7 @@ module Data.Bool ) where import Prelude ( Bool(..), (&&), (||), not, otherwise ) +import qualified GHC.KnownKeyNames as Rebindable -- $setup -- >>> import Prelude ===================================== libraries/base/src/Data/Fixed.hs ===================================== @@ -4,9 +4,6 @@ {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TemplateHaskellQuotes #-} -{-# OPTIONS_GHC -fno-rebindable-known-names #-} - -- We are importing Prelude, hence GHC.KnownKeyNames is available - ----------------------------------------------------------------------------- -- | -- Module : Data.Fixed @@ -90,6 +87,7 @@ module Data.Fixed ) where import Prelude +import qualified GHC.KnownKeyNames as Rebindable import GHC.Internal.Data.Data import GHC.Internal.TypeLits (KnownNat, natVal) import GHC.Internal.Read ===================================== libraries/base/src/Data/Functor/Compose.hs ===================================== @@ -7,9 +7,6 @@ {-# LANGUAGE Trustworthy #-} {-# LANGUAGE StandaloneDeriving #-} -{-# OPTIONS_GHC -fno-rebindable-known-names #-} - -- We import Prelude, hence GHC.KnownKeyNames is available - ----------------------------------------------------------------------------- -- | -- Module : Data.Functor.Compose @@ -30,6 +27,7 @@ module Data.Functor.Compose ( ) where import Prelude +import qualified GHC.KnownKeyNames as Rebindable import Data.Functor.Classes import Control.Applicative import GHC.Internal.Data.Coerce (coerce) ===================================== libraries/base/src/Data/Functor/Product.hs ===================================== @@ -4,9 +4,6 @@ {-# LANGUAGE Safe #-} {-# LANGUAGE StandaloneDeriving #-} -{-# OPTIONS_GHC -fno-rebindable-known-names #-} - -- We import Prelude, hence GHC.KnownKeyNames is available - ----------------------------------------------------------------------------- -- | -- Module : Data.Functor.Product @@ -27,6 +24,7 @@ module Data.Functor.Product ( ) where import Prelude +import qualified GHC.KnownKeyNames as Rebindable import Control.Applicative import GHC.Internal.Control.Monad (MonadPlus(..)) import GHC.Internal.Control.Monad.Fix (MonadFix(..)) ===================================== libraries/base/src/Data/Functor/Sum.hs ===================================== @@ -4,9 +4,6 @@ {-# LANGUAGE Safe #-} {-# LANGUAGE StandaloneDeriving #-} -{-# OPTIONS_GHC -fno-rebindable-known-names #-} - -- We are importing Prelude, hence GHC.KnownKeyNames is available - ----------------------------------------------------------------------------- -- | -- Module : Data.Functor.Sum @@ -27,6 +24,7 @@ module Data.Functor.Sum ( ) where import Prelude +import qualified GHC.KnownKeyNames as Rebindable import Control.Applicative ((<|>)) import GHC.Internal.Data.Data (Data) import Data.Functor.Classes ===================================== libraries/base/src/Prelude.hs ===================================== @@ -164,11 +164,6 @@ module Prelude ( type (~) ) where -import GHC.KnownKeyNames () - -- Force a dependency on KnownKeyNames, so that any module that - -- imports Prelude can rely on KnownKeyNames existing, and hence - -- can be compiled without -frebindable-known-names - import GHC.Internal.Control.Monad import GHC.Internal.System.IO import GHC.Internal.System.IO.Error ===================================== libraries/base/src/System/Console/GetOpt.hs ===================================== @@ -1,7 +1,5 @@ {-# LANGUAGE Safe #-} -{-# OPTIONS_GHC -fno-rebindable-known-names #-} - -- We are importing Prelude, hence GHC.KnownKeyNames is available ----------------------------------------------------------------------------- -- | -- Module : System.Console.GetOpt @@ -65,6 +63,7 @@ module System.Console.GetOpt ( ) where import Prelude +import qualified GHC.KnownKeyNames as Rebindable import GHC.Internal.Data.List ( isPrefixOf, find ) -- |What to do with options following non-options ===================================== libraries/ghc-internal/src/GHC/Internal/Base.hs ===================================== @@ -468,10 +468,12 @@ Wrinkles: For modules high up in the hierarchy of `base`, a convenient way to do this is to say - import GHC.KnownKeyNames + import qualified GHC.KnownKeyNames as Rebindable + (Why `qualified` and `as Rebindable`? See (KN1) in + Note [Overview of known entities] in GHC.Builtin.) For modules not so high up, you can say - import GHC.Internal.Base + import qualified GHC.Internal.Base as Rebindable though you may also need GHC.Internal.Num when numerics are concerned. For `ghc-internal` modules below GHC.Internal.Base we have to be more selective. ===================================== libraries/ghc-internal/src/GHC/Internal/OverloadedLabels.hs ===================================== @@ -49,7 +49,8 @@ module GHC.Internal.OverloadedLabels ( IsLabel(..) ) where -import GHC.Internal.Base +import GHC.Internal.Types (Symbol) +import qualified GHC.Internal.Base as Rebindable class IsLabel (x :: Symbol) a where fromLabel :: a ===================================== utils/genprimopcode/Main.hs ===================================== @@ -313,11 +313,9 @@ gen_wrappers (Info _ entries) -- don't need the Prelude here so we add NoImplicitPrelude. ++ "{-# OPTIONS_GHC -Wno-deprecations -O0 -fno-do-eta-reduction #-}\n" -- Very important OPTIONS_GHC! See Note [OPTIONS_GHC in GHC.PrimopWrappers] - ++ "{-# OPTIONS_GHC -Wno-unused-imports #-}\n" - -- Don't warn about unused import of GHC.Internal.Base; needed for Typeable bindings ++ "module GHC.Internal.PrimopWrappers where\n" ++ "import qualified GHC.Internal.Prim\n" - ++ "import GHC.Internal.Base -- For Typeable bindings\n" + ++ "import qualified GHC.Internal.Base as Rebindable -- For Typeable bindings\n" ++ "import GHC.Internal.Tuple ()\n" ++ "import GHC.Internal.Prim (" ++ types ++ ")\n" ++ unlines (concatMap mk_wrapper wrappers) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/c256a1075f20f6a83d0cda49843d2529... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/c256a1075f20f6a83d0cda49843d2529... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Simon Peyton Jones (@simonpj)