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
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:
| ... | ... | @@ -654,13 +654,17 @@ initNameWriterTable = do |
| 654 | 654 | |
| 655 | 655 | |
| 656 | 656 | putSymbolTable :: WriteBinHandle -> Int -> UniqFM Name (Int,Name) -> IO ()
|
| 657 | -putSymbolTable bh name_count symtab = do
|
|
| 658 | - put_ bh name_count
|
|
| 659 | - let names = elems (array (0,name_count-1) (nonDetEltsUFM symtab))
|
|
| 660 | - -- It's OK to use nonDetEltsUFM here because the elements have
|
|
| 661 | - -- indices that array uses to create order
|
|
| 662 | - mapM_ (\n -> serialiseName bh n symtab) names
|
|
| 663 | - |
|
| 657 | +putSymbolTable bh name_count symtab
|
|
| 658 | + = do { put_ bh name_count
|
|
| 659 | + ; let names = elems (array (0,name_count-1) (nonDetEltsUFM symtab))
|
|
| 660 | + -- It's OK to use nonDetEltsUFM here because the elements
|
|
| 661 | + -- have indices that array uses to create order
|
|
| 662 | + ; mapM_ serialise_one names }
|
|
| 663 | + where
|
|
| 664 | + serialise_one :: Name -> IO ()
|
|
| 665 | + serialise_one name
|
|
| 666 | + | (mod, occ, is_known_key) <- extNamePieces name
|
|
| 667 | + = put_ bh (moduleUnit mod, moduleName mod, occ, is_known_key)
|
|
| 664 | 668 | |
| 665 | 669 | getSymbolTable :: ReadBinHandle -> NameCache -> IO (SymbolTable Name)
|
| 666 | 670 | -- Create an array of Names for the symbols and add them to the NameCache
|
| ... | ... | @@ -697,11 +701,6 @@ getSymbolTable bh name_cache |
| 697 | 701 | ; writeArray mut_arr (fromIntegral i) name
|
| 698 | 702 | ; return new_cache }
|
| 699 | 703 | |
| 700 | -serialiseName :: WriteBinHandle -> Name -> UniqFM key (Int,Name) -> IO ()
|
|
| 701 | -serialiseName bh name _
|
|
| 702 | - | (mod, occ, is_known_key) <- extNamePieces name
|
|
| 703 | - = put_ bh (moduleUnit mod, moduleName mod, occ, is_known_key)
|
|
| 704 | - |
|
| 705 | 704 | -- Note [Symbol table representation of names]
|
| 706 | 705 | -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 707 | 706 | --
|
| ... | ... | @@ -92,7 +92,7 @@ allocateGlobalBinder |
| 92 | 92 | -> Module -> OccName -> Maybe Unique -> SrcSpan
|
| 93 | 93 | -> IO Name
|
| 94 | 94 | -- See Note [The Name Cache] in GHC.Types.Name.Cache
|
| 95 | -allocateGlobalBinder nc mod occ mb_uniq loc
|
|
| 95 | +oallocateGlobalBinder nc mod occ mb_uniq loc
|
|
| 96 | 96 | = updateNameCache nc mod occ $ \cache0 -> do
|
| 97 | 97 | case lookupOrigNameCache cache0 mod occ of
|
| 98 | 98 | -- A hit in the cache! We are at the binding site of the name.
|
| ... | ... | @@ -250,6 +250,7 @@ newTopVanillaSrcBinder occ loc |
| 250 | 250 | = do { this_mod <- getModule
|
| 251 | 251 | |
| 252 | 252 | -- See if this bindings is for a known-key name, and if so get its Unique
|
| 253 | + -- See 'Defining known-key names' in GHC.Types.Name
|
|
| 253 | 254 | ; defines_known_keys <- goptM Opt_DefinesKnownKeyNames
|
| 254 | 255 | ; let mb_uniq :: Maybe Unique
|
| 255 | 256 | mb_uniq | defines_known_keys = lookupOccEnv knownKeyOccMap occ
|
| ... | ... | @@ -2025,7 +2025,8 @@ findImportUsage rebindable_known_key_names imports used_gres |
| 2025 | 2025 | = acc
|
| 2026 | 2026 | |
| 2027 | 2027 | -- -frebindable-known-key-names is on, and `n` is a known-key name
|
| 2028 | - -- Then don't warn about an unused imports.
|
|
| 2028 | + -- Then don't warn about an unused import.
|
|
| 2029 | + -- See (UI2) in Note [Unused imports]
|
|
| 2029 | 2030 | | rebindable_known_key_names
|
| 2030 | 2031 | , isKnownKeyName n
|
| 2031 | 2032 | = acc
|
| ... | ... | @@ -2253,31 +2254,6 @@ warnUnusedImport rdr_env (L loc decl, used, unused, unused_wcs) |
| 2253 | 2254 | [ UnusedImportWildcard wc | wc <- unused_wcs ] ++
|
| 2254 | 2255 | [ possible_field nm | nm <- sortBy (comparing nameOccName) unused ]
|
| 2255 | 2256 | |
| 2256 | -{-
|
|
| 2257 | -Note [Do not warn about Prelude hiding]
|
|
| 2258 | -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 2259 | -We do not warn about
|
|
| 2260 | - import Prelude hiding( x, y )
|
|
| 2261 | -because even if nothing else from Prelude is used, it may be essential to hide
|
|
| 2262 | -x,y to avoid name-shadowing warnings. Example (#9061)
|
|
| 2263 | - import Prelude hiding( log )
|
|
| 2264 | - f x = log where log = ()
|
|
| 2265 | - |
|
| 2266 | - |
|
| 2267 | - |
|
| 2268 | -Note [Printing minimal imports]
|
|
| 2269 | -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 2270 | -To print the minimal imports we walk over all import decls (both user-supplied
|
|
| 2271 | -and generated), trim their import lists, then filter out generated decls.
|
|
| 2272 | - |
|
| 2273 | -NB that
|
|
| 2274 | - |
|
| 2275 | - * We do *not* change the 'qualified' or 'as' parts!
|
|
| 2276 | - |
|
| 2277 | - * We do not discard a decl altogether; we might need instances
|
|
| 2278 | - from it. Instead we just trim to an empty import list
|
|
| 2279 | --}
|
|
| 2280 | - |
|
| 2281 | 2257 | getMinimalImports :: [ImportDeclUsage] -> RnM [LImportDecl GhcRn]
|
| 2282 | 2258 | getMinimalImports ie_decls
|
| 2283 | 2259 | = do { rdr_env <- getGlobalRdrEnv
|
| ... | ... | @@ -2399,7 +2375,51 @@ to_ie_post_rn (L l n) |
| 2399 | 2375 | | otherwise = L l (IEName noExtField (L (l2l l) n))
|
| 2400 | 2376 | where occ = occName n
|
| 2401 | 2377 | |
| 2402 | -{-
|
|
| 2378 | +{- Note [Do not warn about Prelude hiding]
|
|
| 2379 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 2380 | +We do not warn about
|
|
| 2381 | + import Prelude hiding( x, y )
|
|
| 2382 | +because even if nothing else from Prelude is used, it may be essential to hide
|
|
| 2383 | +x,y to avoid name-shadowing warnings. Example (#9061)
|
|
| 2384 | + import Prelude hiding( log )
|
|
| 2385 | + f x = log where log = ()
|
|
| 2386 | + |
|
| 2387 | +Note [Unused imports]
|
|
| 2388 | +~~~~~~~~~~~~~~~~~~~~~
|
|
| 2389 | +In `warnUnusedImport`, if we see an import with an explicit list imports, thus
|
|
| 2390 | + import M( a, b )
|
|
| 2391 | +and neither `a` nor `b` is used, we report the entire import decl as unused. We
|
|
| 2392 | +check this by looking at the names that it brings into scope scope; if there are
|
|
| 2393 | +no ununused names, don't report.
|
|
| 2394 | + |
|
| 2395 | +This neatly takes into account two things:
|
|
| 2396 | + |
|
| 2397 | +(UI1) We don't want to complain about `import M()`, because that is often used to bring
|
|
| 2398 | + M's /instances/ into scope.
|
|
| 2399 | + |
|
| 2400 | +(UI2) In base:Data.Enum we see
|
|
| 2401 | + import GHC.Internal.Num( Num ) -- For -frebindable-known-key-names (defaulting)
|
|
| 2402 | + 'Num' is not mentioned explicity but the import is still required; see KKNS_InScope
|
|
| 2403 | + in Note [Overview of known-key names] in GHC.Types.Name.
|
|
| 2404 | + |
|
| 2405 | + We don't want this import reported at an unused. So `findImportUsage`, when looking
|
|
| 2406 | + at `import M( x )`, we do /not/ record `x` as "unused" (regardless of whether it is
|
|
| 2407 | + mentioned in M if
|
|
| 2408 | + (a) -frebindable-known-key-names is on, and
|
|
| 2409 | + (b) `x` is a known-key name
|
|
| 2410 | + |
|
| 2411 | +Note [Printing minimal imports]
|
|
| 2412 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 2413 | +To print the minimal imports we walk over all import decls (both user-supplied
|
|
| 2414 | +and generated), trim their import lists, then filter out generated decls.
|
|
| 2415 | + |
|
| 2416 | +NB that
|
|
| 2417 | + |
|
| 2418 | + * We do *not* change the 'qualified' or 'as' parts!
|
|
| 2419 | + |
|
| 2420 | + * We do not discard a decl altogether; we might need instances
|
|
| 2421 | + from it. Instead we just trim to an empty import list
|
|
| 2422 | + |
|
| 2403 | 2423 | Note [Partial export]
|
| 2404 | 2424 | ~~~~~~~~~~~~~~~~~~~~~
|
| 2405 | 2425 | Suppose we have
|
| ... | ... | @@ -190,6 +190,11 @@ To implement all this, here are the moving parts: |
| 190 | 190 | This is one reason for (KnownKeyInvariant): an export list cannot have two
|
| 191 | 191 | entities with the same OccName.
|
| 192 | 192 | |
| 193 | +* There are two flags that control the treatment of known-key names:
|
|
| 194 | + -frebindable-known-key-names
|
|
| 195 | + -fdefines-known-key-names
|
|
| 196 | + Details in the following bullets.
|
|
| 197 | + |
|
| 193 | 198 | * Known-key name lookup (normal case: KKNS_FromModule)
|
| 194 | 199 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 195 | 200 | In normal client code, suppose the desugarer calls `dsLookupKnownKeyTyCon`
|
| ... | ... | @@ -218,18 +223,27 @@ To implement all this, here are the moving parts: |
| 218 | 223 | to bring into scope some entities that are needed by `dsLookupKnownKeyTyCon` etc.
|
| 219 | 224 | See also wrinkle (KKN1)
|
| 220 | 225 | |
| 221 | -* Defin
|
|
| 222 | -* There are two flags that control the treatment of known-key names:
|
|
| 223 | - -frebindable-known-key-names
|
|
| 224 | - -fdefines-known-key-names
|
|
| 225 | - |
|
| 226 | -* Suppose the desugarer calls `dsLookupKnownKeyTyCon` on `rationalTyConKey`.
|
|
| 227 | - * In client source code, that is, /without/ -frebindable-known-key-names,
|
|
| 228 | - we try to import GHC.KnownKeyNames. Assuming this is succesful,
|
|
| 226 | +* Defining known-key names
|
|
| 227 | + ~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 228 | + When we /define/ a known-key name, such as
|
|
| 229 | + the `Num` class in ghc-internal:GHC.Internal.Num
|
|
| 230 | + we must assign the correct Unique. So in GHC.Rename.Env.newTopVanillaSrcBinder
|
|
| 231 | + if -fdefines-known-key-names is set (Opt_DefinesKnownKeyNames), we check the
|
|
| 232 | + OccName against the list in `basicKnownKeyTable`; if it appears there, we use the
|
|
| 233 | + Unique from the table.
|
|
| 234 | + |
|
| 235 | +* Serialising known-key names
|
|
| 236 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 237 | + - When we serialise a known-key name into an interface file, we mark it as such.
|
|
| 238 | + See `serialise_one` in GHC.Iface.Binary.putSymbolTable.
|
|
| 239 | + - When deserialising a name from an interface file, we check the known-key bit,
|
|
| 240 | + If it is set, we get the Unique from the `basicKnownKeyTable`,
|
|
| 241 | + and use `mkKnownKeyName` rather than `mkExternalName` to build the Name.
|
|
| 229 | 242 | |
| 230 | 243 | Wrinkles
|
| 231 | 244 | |
| 232 | -(KKN1) In
|
|
| 245 | +(KKN1) We need some special treatment of unused-import warnings.
|
|
| 246 | + See (UI1) in Note [Unused imports] in GHC.Rename.Names
|
|
| 233 | 247 | |
| 234 | 248 | Note [About the NameSorts]
|
| 235 | 249 | ~~~~~~~~~~~~~~~~~~~~~~~~~~
|