Template Haskell splice requires foreign symbol that is unneeded
Consider the following (2-module & standalone) program:
```haskell
-- FFI.hs
{-# LANGUAGE GHCForeignImportPrim #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UnliftedFFITypes #-}
module FFI ( D(..), D2(..), tabulate ) where
import GHC.Exts ( Double#, Double(D#) )
import Language.Haskell.TH ( CodeQ )
data D = D Double
data D2 = D2 { x, y :: D }
tabulate :: ( Word -> CodeQ D ) -> CodeQ ( D2 )
tabulate f = [|| D2 $$( f 1 ) $$( f 2 ) ||]
-- Now an unrelated "Num D" instance.
instance Num D where
( D ( D# x ) ) * ( D ( D# y ) ) = D ( D# ( func x y ) )
foreign import prim "prim_func"
func :: Double# -> Double# -> Double#
-- TH.hs
{-# LANGUAGE TemplateHaskell #-}
module TH where
import FFI
th_bug :: D2
th_bug = $$( tabulate $ \ _ -> [|| D 0 ||] )
```
Here we have two modules, `FFI` and `TH`.
In the `FFI` module, we define a data type `D` and a function to be used in Template Haskell, `tabulate`.
Completely unrelatedly, we also include a `Num D` instance which makes use of a foreign import prim declaration, `foreign import prim "prim_func"`.
In the `TH` module, we use `tabulate` and get a linking error:
```
FFI.o: unknown symbol `prim_func'
```
This suggests we are trying to run some code at splice time involving the `Num D` instance, even though `Num D` is not involved in any way. In particular, if we change the use of `prim_func` to something like `error "blah"`, then the error never gets thrown, which shows we don't run that code at splice time.
Note that it is important to use a typeclass to trigger this bug. If we instead use the FFI import in a top-level binding, there are no problems. It doesn't matter that the class is `Num` or some other (possibly user-defined) class.
Reproduces on GHC versions: 9.4, 9.6, 9.8, 9.10, HEAD.
On 9.0 and 9.2, I get a different error:
```
HSghc-prim-0.7.0.o: unknown symbol `atanhf`
```
issue