Undefined symbol linking error when using static pointers (only with optimisations)
See also
* #16981
Attempting to compile the following program into an executable with any recent GHC (9.2, 9.4, 9.6, 9.8) causes a linker error:
```haskell
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StaticPointers #-}
{-# OPTIONS_GHC -O1 #-}
module Main where
-- base
import Control.Monad ( ap )
import GHC.StaticPtr ( StaticPtr )
--------------------------------------------------------------------------------
newtype ReaderIO a = ReaderIO { runReaderIO :: () -> IO a }
deriving stock Functor
instance Applicative ReaderIO where
pure a = ReaderIO $ \ _ -> pure a
(<*>) = ap
instance Monad ReaderIO where
m >>= k =
ReaderIO $ \ r -> do
a <- runReaderIO m r
runReaderIO (k a) r
{-# NOINLINE uhoh #-}
uhoh :: ReaderIO ()
uhoh = do
let ptr :: StaticPtr ( IO () )
ptr = static ( return () )
_ <- ReaderIO $ \ _ -> return ()
ptr `seq` return ()
main :: IO ()
main = putStrLn "Hello, Haskell!"
```
```
ld.lld: error: undefined symbol: r1vI_closure
>>> referenced by Main.o(ghc_3.o):(.refptr.r1vI_closure)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ghc-9.8.2.exe: `clang.exe' failed in phase `Linker'. (Exit code: 1)
```
Note that this bug goes away if one disables optimisations with `-O0`.
issue