[Git][ghc/ghc][wip/spj-reinstallable-base] Wibbles, mainly comments
Simon Peyton Jones pushed to branch wip/spj-reinstallable-base at Glasgow Haskell Compiler / GHC Commits: 93148501 by Simon Peyton Jones at 2026-03-20T20:23:49+00:00 Wibbles, mainly comments - - - - - 5 changed files: - compiler/GHC/Iface/Binary.hs - compiler/GHC/Iface/Env.hs - compiler/GHC/Rename/Env.hs - compiler/GHC/Rename/Names.hs - compiler/GHC/Types/Name.hs Changes: ===================================== compiler/GHC/Iface/Binary.hs ===================================== @@ -654,13 +654,17 @@ initNameWriterTable = do putSymbolTable :: WriteBinHandle -> Int -> UniqFM Name (Int,Name) -> IO () -putSymbolTable bh name_count symtab = do - put_ bh name_count - let names = elems (array (0,name_count-1) (nonDetEltsUFM symtab)) - -- It's OK to use nonDetEltsUFM here because the elements have - -- indices that array uses to create order - mapM_ (\n -> serialiseName bh n symtab) names - +putSymbolTable bh name_count symtab + = do { put_ bh name_count + ; let names = elems (array (0,name_count-1) (nonDetEltsUFM symtab)) + -- It's OK to use nonDetEltsUFM here because the elements + -- have indices that array uses to create order + ; mapM_ serialise_one names } + where + serialise_one :: Name -> IO () + serialise_one name + | (mod, occ, is_known_key) <- extNamePieces name + = put_ bh (moduleUnit mod, moduleName mod, occ, is_known_key) getSymbolTable :: ReadBinHandle -> NameCache -> IO (SymbolTable Name) -- Create an array of Names for the symbols and add them to the NameCache @@ -697,11 +701,6 @@ getSymbolTable bh name_cache ; writeArray mut_arr (fromIntegral i) name ; return new_cache } -serialiseName :: WriteBinHandle -> Name -> UniqFM key (Int,Name) -> IO () -serialiseName bh name _ - | (mod, occ, is_known_key) <- extNamePieces name - = put_ bh (moduleUnit mod, moduleName mod, occ, is_known_key) - -- Note [Symbol table representation of names] -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- ===================================== compiler/GHC/Iface/Env.hs ===================================== @@ -92,7 +92,7 @@ allocateGlobalBinder -> Module -> OccName -> Maybe Unique -> SrcSpan -> IO Name -- See Note [The Name Cache] in GHC.Types.Name.Cache -allocateGlobalBinder nc mod occ mb_uniq loc +oallocateGlobalBinder nc mod occ mb_uniq loc = updateNameCache nc mod occ $ \cache0 -> do case lookupOrigNameCache cache0 mod occ of -- A hit in the cache! We are at the binding site of the name. ===================================== compiler/GHC/Rename/Env.hs ===================================== @@ -250,6 +250,7 @@ newTopVanillaSrcBinder occ loc = do { this_mod <- getModule -- See if this bindings is for a known-key name, and if so get its Unique + -- See 'Defining known-key names' in GHC.Types.Name ; defines_known_keys <- goptM Opt_DefinesKnownKeyNames ; let mb_uniq :: Maybe Unique mb_uniq | defines_known_keys = lookupOccEnv knownKeyOccMap occ ===================================== compiler/GHC/Rename/Names.hs ===================================== @@ -2025,7 +2025,8 @@ findImportUsage rebindable_known_key_names imports used_gres = acc -- -frebindable-known-key-names is on, and `n` is a known-key name - -- Then don't warn about an unused imports. + -- Then don't warn about an unused import. + -- See (UI2) in Note [Unused imports] | rebindable_known_key_names , isKnownKeyName n = acc @@ -2253,31 +2254,6 @@ warnUnusedImport rdr_env (L loc decl, used, unused, unused_wcs) [ UnusedImportWildcard wc | wc <- unused_wcs ] ++ [ possible_field nm | nm <- sortBy (comparing nameOccName) unused ] -{- -Note [Do not warn about Prelude hiding] -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -We do not warn about - import Prelude hiding( x, y ) -because even if nothing else from Prelude is used, it may be essential to hide -x,y to avoid name-shadowing warnings. Example (#9061) - import Prelude hiding( log ) - f x = log where log = () - - - -Note [Printing minimal imports] -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -To print the minimal imports we walk over all import decls (both user-supplied -and generated), trim their import lists, then filter out generated decls. - -NB that - - * We do *not* change the 'qualified' or 'as' parts! - - * We do not discard a decl altogether; we might need instances - from it. Instead we just trim to an empty import list --} - getMinimalImports :: [ImportDeclUsage] -> RnM [LImportDecl GhcRn] getMinimalImports ie_decls = do { rdr_env <- getGlobalRdrEnv @@ -2399,7 +2375,51 @@ to_ie_post_rn (L l n) | otherwise = L l (IEName noExtField (L (l2l l) n)) where occ = occName n -{- +{- Note [Do not warn about Prelude hiding] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +We do not warn about + import Prelude hiding( x, y ) +because even if nothing else from Prelude is used, it may be essential to hide +x,y to avoid name-shadowing warnings. Example (#9061) + import Prelude hiding( log ) + f x = log where log = () + +Note [Unused imports] +~~~~~~~~~~~~~~~~~~~~~ +In `warnUnusedImport`, if we see an import with an explicit list imports, thus + import M( a, b ) +and neither `a` nor `b` is used, we report the entire import decl as unused. We +check this by looking at the names that it brings into scope scope; if there are +no ununused names, don't report. + +This neatly takes into account two things: + +(UI1) We don't want to complain about `import M()`, because that is often used to bring + M's /instances/ into scope. + +(UI2) In base:Data.Enum we see + import GHC.Internal.Num( Num ) -- For -frebindable-known-key-names (defaulting) + 'Num' is not mentioned explicity but the import is still required; see KKNS_InScope + in Note [Overview of known-key names] in GHC.Types.Name. + + We don't want this import reported at an unused. So `findImportUsage`, when looking + at `import M( x )`, we do /not/ record `x` as "unused" (regardless of whether it is + mentioned in M if + (a) -frebindable-known-key-names is on, and + (b) `x` is a known-key name + +Note [Printing minimal imports] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +To print the minimal imports we walk over all import decls (both user-supplied +and generated), trim their import lists, then filter out generated decls. + +NB that + + * We do *not* change the 'qualified' or 'as' parts! + + * We do not discard a decl altogether; we might need instances + from it. Instead we just trim to an empty import list + Note [Partial export] ~~~~~~~~~~~~~~~~~~~~~ Suppose we have ===================================== compiler/GHC/Types/Name.hs ===================================== @@ -190,6 +190,11 @@ To implement all this, here are the moving parts: This is one reason for (KnownKeyInvariant): an export list cannot have two entities with the same OccName. +* There are two flags that control the treatment of known-key names: + -frebindable-known-key-names + -fdefines-known-key-names + Details in the following bullets. + * Known-key name lookup (normal case: KKNS_FromModule) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In normal client code, suppose the desugarer calls `dsLookupKnownKeyTyCon` @@ -218,18 +223,27 @@ To implement all this, here are the moving parts: 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 - -* Suppose the desugarer calls `dsLookupKnownKeyTyCon` on `rationalTyConKey`. - * In client source code, that is, /without/ -frebindable-known-key-names, - we try to import GHC.KnownKeyNames. Assuming this is succesful, +* Defining known-key names + ~~~~~~~~~~~~~~~~~~~~~~~~ + When we /define/ 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 `basicKnownKeyTable`; if it appears there, we use the + Unique from the table. + +* Serialising known-key names + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + - When we serialise a known-key name into an interface file, we mark it as such. + See `serialise_one` in GHC.Iface.Binary.putSymbolTable. + - When deserialising a name from an interface file, we check the known-key bit, + If it is set, we get the Unique from the `basicKnownKeyTable`, + and use `mkKnownKeyName` rather than `mkExternalName` to build the Name. Wrinkles -(KKN1) In +(KKN1) We need some special treatment of unused-import warnings. + See (UI1) in Note [Unused imports] in GHC.Rename.Names Note [About the NameSorts] ~~~~~~~~~~~~~~~~~~~~~~~~~~ View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/93148501b5e0598ba606a083131884d8... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/93148501b5e0598ba606a083131884d8... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Simon Peyton Jones (@simonpj)