magicDict crashes runghc, but not compiled code
Consider the following program, which uses GHC.Exts.magicDict
:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RankNTypes #-}
module Main (main) where
import Data.Proxy (Proxy(..))
import GHC.Exts (magicDict)
import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
data WrapS a b = WrapS (KnownSymbol a => Proxy a -> b)
reifySymbol :: forall r. String -> (forall (n :: Symbol). KnownSymbol n => Proxy n -> r) -> r
reifySymbol n k = magicDict (WrapS k) n Proxy
main :: IO ()
main = print $ reifySymbol "Hello World" symbolVal
This works if I compile the program and run it:
$ /opt/ghc/9.0.1/bin/ghc Bug.hs && ./Bug
[1 of 1] Compiling Main ( Bug.hs, Bug.o )
Linking Bug ...
"Hello World"
However, it crashes if I invoke it with runghc
:
$ /opt/ghc/9.0.1/bin/runghc Bug.hs
ghc: ^^ Could not load 'ghczmprim_GHCziPrim_magicDict_closure', dependency unresolved. See top entry above.
Bug.hs:
GHC.ByteCode.Linker.lookupCE
During interactive linking, GHCi couldn't find the following symbol:
ghczmprim_GHCziPrim_magicDict_closure
This may be due to you not asking GHCi to load extra object files,
archives or DLLs needed by your current session. Restart GHCi, specifying
the missing library using the -L/path/to/object/dir and -lmissinglibname
flags, or simply by naming the relevant files on the GHCi command line.
Alternatively, this link failure might indicate a bug in GHCi.
If you suspect the latter, please report this as a GHC bug:
https://www.haskell.org/ghc/reportabug
I would expect both versions to work.