Panic from refineFromInScope when using Typeable with TH
Summary
Using Typeable
in TemplateHaskell doesn't seem to work.
Using Typeable
in TemplateHaskell (as shown below) is useful because as far as I'm aware, there is no other way to determine the Type
of the instantiation of a polymorphic function in the Q
monad.
That is to say, in the below reproducer, Typeable
is really an XY problem; what I really want is to use the Type
corresponding to the monomorphic instantiation of a
in the body of foo
. This is allowed to fail in whatever way you like if a
doesn't end up being instantiated to a monomorphic type. Hence Typeable
seems to have the right semantics, but it doesn't work, as shown.
Steps to reproduce
https://git.tomsmeding.com/ghc-refinefrominscope-panic-repro
Foo.hs:
module Foo where
import Data.Typeable
import Language.Haskell.TH
foo :: Typeable a => Code Q a -> Code Q ()
foo = undefined
Main.hs:
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Foo
data T a = T a deriving (Show)
main :: IO ()
main =
let x = $$(foo [|| T () ||])
in return ()
Compiling:
$ ghc-9.2.2 Main.hs Foo.hs
[1 of 2] Compiling Foo ( Foo.hs, Foo.o, Foo.dyn_o )
[2 of 2] Compiling Main ( Main.hs, Main.o, Main.dyn_o )
ghc: panic! (the 'impossible' happened)
(GHC version 9.2.2:
refineFromInScope
InScope {wild_00 $dQuote_a2Jf}
$tcT
Call stack:
CallStack (from HasCallStack):
callStackDoc, called at compiler/GHC/Utils/Panic.hs:181:37 in ghc:GHC.Utils.Panic
pprPanic, called at compiler/GHC/Core/Opt/Simplify/Env.hs:706:30 in ghc:GHC.Core.Opt.Simplify.Env
Please report this as a GHC bug: https://www.haskell.org/ghc/reportabug
Expected behavior
Don't panic. :)
Environment
- GHC version used: 9.2.2 from ghcup
Optional:
- Operating System: Arch Linux
- System Architecture: amd64