Rodrigo Mesquita pushed to branch wip/spj-reinstallable-base2 at Glasgow Haskell Compiler / GHC Commits: 25497cb4 by Rodrigo Mesquita at 2026-05-06T17:51:42+01:00 unsafeCoercePrimName - - - - - 5 changed files: - compiler/GHC/Builtin/KnownKeys.hs - compiler/GHC/HsToCore.hs - compiler/GHC/Tc/Module.hs - compiler/GHC/Tc/Utils/Concrete.hs - libraries/base/src/GHC/Essentials.hs Changes: ===================================== compiler/GHC/Builtin/KnownKeys.hs ===================================== @@ -271,6 +271,9 @@ knownKeyTable , (mkDataOcc ":$$:", typeErrorVAppendDataConKey) , (mkDataOcc "ShowType", typeErrorShowTypeDataConKey) + -- Unsafe coercion proofs + , (mkVarOcc "unsafeCoerce#", unsafeCoercePrimIdKey) + -- Plugins , (mkTcOcc "Plugin", pluginTyConKey) , (mkTcOcc "FrontendPlugin", frontendPluginTyConKey) @@ -372,10 +375,7 @@ basicKnownKeyNames starKindRepName, starArrStarKindRepName, starArrStarArrStarKindRepName, - constraintKindRepName, - - -- Unsafe coercion proofs - unsafeCoercePrimName + constraintKindRepName ] @@ -437,10 +437,6 @@ constraintKindRepName = varQual gHC_TYPES (fsLit "krep$Constraint") con withDictClassName :: Name withDictClassName = clsQual gHC_MAGIC_DICT (fsLit "WithDict") withDictClassKey --- Unsafe coercion proofs -unsafeCoercePrimName:: Name -unsafeCoercePrimName = varQual gHC_INTERNAL_UNSAFE_COERCE (fsLit "unsafeCoerce#") unsafeCoercePrimIdKey - genericClassKeys :: [KnownKey] genericClassKeys = [genClassKey, gen1ClassKey] ===================================== compiler/GHC/HsToCore.hs ===================================== @@ -86,7 +86,6 @@ import GHC.Types.SourceFile import GHC.Types.TypeEnv import GHC.Types.Name import GHC.Types.Name.Set -import GHC.Types.Name.Env import GHC.Types.Name.Ppr import GHC.Types.HpcInfo @@ -99,6 +98,7 @@ import Data.List (partition) import Data.IORef import GHC.Iface.Make (mkRecompUsageInfo) import GHC.Runtime.Interpreter (interpreterProfiled) +import GHC.Types.Unique.FM {- ************************************************************************ @@ -684,13 +684,14 @@ patchMagicDefns pairs -- optimization: check whether we're in a magic module before looking -- at all the ids = do { this_mod <- getModule + ; magicDefnModules <- mkMagicDefnModules ; if this_mod `elemModuleSet` magicDefnModules then traverse patchMagicDefn pairs else return pairs } patchMagicDefn :: (Id, CoreExpr) -> DsM (Id, CoreExpr) patchMagicDefn orig_pair@(orig_id, orig_rhs) - | Just mk_magic_pair <- lookupNameEnv magicDefnsEnv (getName orig_id) + | Just mk_magic_pair <- lookupUFM magicDefnsEnv (getUnique orig_id) = do { magic_pair@(magic_id, _) <- mk_magic_pair orig_id orig_rhs -- Patching should not change the Name or the type of the Id @@ -701,22 +702,25 @@ patchMagicDefn orig_pair@(orig_id, orig_rhs) | otherwise = return orig_pair -magicDefns :: [(Name, Id -> CoreExpr -- old Id and RHS +magicDefns :: [(KnownKey, Id -> CoreExpr -- old Id and RHS -> DsM (Id, CoreExpr) -- new Id and RHS )] -magicDefns = [ (unsafeCoercePrimName, mkUnsafeCoercePrimPair) ] +magicDefns = [ (unsafeCoercePrimIdKey, mkUnsafeCoercePrimPair) ] -magicDefnsEnv :: NameEnv (Id -> CoreExpr -> DsM (Id, CoreExpr)) -magicDefnsEnv = mkNameEnv magicDefns +magicDefnsEnv :: UniqFM KnownKey (Id -> CoreExpr -> DsM (Id, CoreExpr)) +magicDefnsEnv = listToUFM magicDefns -magicDefnModules :: ModuleSet -magicDefnModules = mkModuleSet $ map (nameModule . getName . fst) magicDefns +mkMagicDefnModules :: DsM ModuleSet +mkMagicDefnModules = do + mods <- mapM (fmap nameModule . dsLookupKnownKeyName . fst) magicDefns + pure $ mkModuleSet mods mkUnsafeCoercePrimPair :: Id -> CoreExpr -> DsM (Id, CoreExpr) -- See Note [Wiring in unsafeCoerce#] for the defn we are creating here mkUnsafeCoercePrimPair _old_id old_expr = do { unsafe_equality_proof_id <- dsLookupKnownKeyId unsafeEqualityProofIdKey ; unsafe_equality_tc <- dsLookupKnownKeyTyCon unsafeEqualityTyConKey + ; unsafeCoercePrimName <- dsLookupKnownKeyName unsafeCoercePrimIdKey ; let [unsafe_refl_data_con] = tyConDataCons unsafe_equality_tc ===================================== compiler/GHC/Tc/Module.hs ===================================== @@ -2566,7 +2566,7 @@ tcGhciStmts stmts -- We use Any rather than a dummy type such as () because of -- the rules of unsafeCoerce#; see Unsafe/Coerce.hs for the details. - ; AnId unsafe_coerce_id <- tcLookupGlobal unsafeCoercePrimName + ; AnId unsafe_coerce_id <- tcLookupKnownKeyGlobal unsafeCoercePrimIdKey -- We use unsafeCoerce# here because of (U11) in -- Note [Implementing unsafeCoerce] in base:Unsafe.Coerce ===================================== compiler/GHC/Tc/Utils/Concrete.hs ===================================== @@ -18,7 +18,7 @@ module GHC.Tc.Utils.Concrete import GHC.Prelude -import GHC.Builtin.KnownKeys ( unsafeCoercePrimName ) +import GHC.Builtin.KnownKeys ( unsafeCoercePrimIdKey ) import GHC.Builtin.WiredIn.Types import GHC.Core.Coercion @@ -45,6 +45,7 @@ import GHC.Utils.Outputable import GHC.Data.FastString ( FastString, fsLit ) import Control.Monad ( void ) +import GHC.Types.Name (hasKnownKey) {- Note [Concrete overview] @@ -857,7 +858,7 @@ idConcreteTvs id -- in the correct information in the desugarer). -- So, for the time being, we manually inspect the type of the original, -- unpatched Id to retrieve which of its outer forall-d tyvars should be concrete. - | idName id == unsafeCoercePrimName + | id `hasKnownKey`unsafeCoercePrimIdKey , (a_rep:_b_rep:a:_b:_, _) <- tcSplitForAllTyVars $ idType id -- NB: only check the argument representation, not the result representation. -- This is because the following is OK: @@ -866,7 +867,7 @@ idConcreteTvs id -- unsafeCoerceWordRep = unsafeCoerce# = mkNameEnv [(tyVarName a_rep, ConcreteFRR $ FixedRuntimeRepOrigin (mkTyVarTy a) - $ FRRRepPolyId unsafeCoercePrimName RepPolyFunction + $ FRRRepPolyId (idName id) RepPolyFunction $ mkArgPos 1 Top)] | otherwise ===================================== libraries/base/src/GHC/Essentials.hs ===================================== @@ -161,7 +161,7 @@ module GHC.Essentials , CS.unpackAppendCStringUtf8#, CS.cstringLength# , eqString, inline - , UnsafeEquality( UnsafeRefl ), unsafeEqualityProof + , UnsafeEquality( UnsafeRefl ), unsafeEqualityProof, unsafeCoerce# -- Typeable and type representations , SomeTypeRep( SomeTypeRep ), TR.Module( Module ) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/25497cb478856fc82cdc5277f85eebb6... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/25497cb478856fc82cdc5277f85eebb6... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Rodrigo Mesquita (@alt-romes)