
Zubin pushed to branch wip/26135 at Glasgow Haskell Compiler / GHC Commits: 6997abfc by Zubin Duggal at 2025-06-24T16:32:03+05:30 compiler: Export a version of `newNameCache` that is not prone to footguns. `newNameCache` must be initialized with both a non-"reserved" unique tag, as well as a list of known key names. Failing to do so results in hard to debug unique conflicts. It is difficult for API users to tell which unique tags are safe to use. So instead of leaving this up to the user to decide, we now export a version of `newNameCache` which uses a guaranteed non-reserved unique tag. In fact, this is now the way the unique tag is initialized for all invocations of the compiler. The original version of `newNameCache` is now exported as `newNameCache'` for advanced users. We also deprecate `initNameCache` as it is also prone to footguns and is completely subsumed in functionality by `newNameCache` and `newNameCache'`. Fixes #26135 and #26055 - - - - - 3 changed files: - compiler/GHC/Driver/Main.hs - compiler/GHC/Types/Name/Cache.hs - testsuite/tests/hiefile/should_run/TestUtils.hs Changes: ===================================== compiler/GHC/Driver/Main.hs ===================================== @@ -245,7 +245,7 @@ import GHC.Types.IPE import GHC.Types.SourceFile import GHC.Types.SrcLoc import GHC.Types.Name -import GHC.Types.Name.Cache ( newNameCache, knownKeysOrigNameCache ) +import GHC.Types.Name.Cache ( newNameCache ) import GHC.Types.Name.Reader import GHC.Types.Name.Ppr import GHC.Types.TyThing @@ -322,7 +322,7 @@ newHscEnv top_dir dflags = do newHscEnvWithHUG :: FilePath -> DynFlags -> UnitId -> HomeUnitGraph -> IO HscEnv newHscEnvWithHUG top_dir top_dynflags cur_unit home_unit_graph = do - nc_var <- newNameCache 'r' knownKeysOrigNameCache + nc_var <- newNameCache fc_var <- initFinderCache logger <- initLogger tmpfs <- initTmpFs ===================================== compiler/GHC/Types/Name/Cache.hs ===================================== @@ -4,6 +4,7 @@ module GHC.Types.Name.Cache ( NameCache (..) , newNameCache + , newNameCache' , initNameCache , takeUniqFromNameCache , updateNameCache' @@ -140,11 +141,27 @@ extendOrigNameCache nc mod occ name where combine _ occ_env = extendOccEnv occ_env occ name -newNameCache :: Char -> OrigNameCache -> IO NameCache -newNameCache c nc = NameCache c <$> newMVar nc +-- | Initialize a new name cache +newNameCache :: IO NameCache +newNameCache = newNameCache' 'r' knownKeysOrigNameCache +-- | This is a version of `newNameCache` that lets you supply your +-- own unique tag and set of known key names. This can go wrong if the tag +-- supplied is one reserved by GHC for internal purposes. See #26055 for +-- an example. +-- +-- Use `newNameCache` when possible. +newNameCache' :: Char -> OrigNameCache -> IO NameCache +newNameCache' c nc = NameCache c <$> newMVar nc + +-- | This takes a tag for uniques to be generated and the list of knownKeyNames +-- These must be initialized properly to ensure that names generated from this +-- NameCache do not conflict with known key names. +-- +-- Use `newNameCache` or `newNameCache'` instead +{-# DEPRECATED initNameCache "Use newNameCache or newNameCache' instead" #-} initNameCache :: Char -> [Name] -> IO NameCache -initNameCache c names = newNameCache c (initOrigNames names) +initNameCache c names = newNameCache' c (initOrigNames names) initOrigNames :: [Name] -> OrigNameCache initOrigNames names = foldl' extendOrigNameCache' emptyModuleEnv names ===================================== testsuite/tests/hiefile/should_run/TestUtils.hs ===================================== @@ -25,9 +25,6 @@ import GHC.Iface.Ext.Utils import GHC.Driver.Session import GHC.SysTools -makeNc :: IO NameCache -makeNc = initNameCache 'z' [] - dynFlagsForPrinting :: String -> IO DynFlags dynFlagsForPrinting libdir = do systemSettings <- initSysTools libdir @@ -37,7 +34,7 @@ readTestHie :: FilePath -> IO (DynFlags, HieFile) readTestHie fp = do libdir:_ <- getArgs df <- dynFlagsForPrinting libdir - nc <- makeNc + nc <- newNameCache hfr <- readHieFile nc fp pure (df, hie_file_result hfr) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/6997abfc3ef8622eeee8d69696746bfe... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/6997abfc3ef8622eeee8d69696746bfe... You're receiving this email because of your account on gitlab.haskell.org.