Using default methods with (r ~ LiftedRep) leads to Iface Lint failure
I originally spotted this bug when trying to minimize #17722 (closed), but I believe this is an entirely different bug.
See also #19519 (closed), which makes this bug show up much more often: every use of liftTyped
.
To reproduce the issue, you'll need these two files:
-- Lib.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeInType #-}
module Lib (C(..)) where
import GHC.Exts
-- Essentially a stripped-down version of the `Lift` type class from
-- Language.Haskell.TH.Syntax. I have redefined it here both to strip away
-- inessential details and to ensure that this test case will not change if
-- the API of Lift were to change in the future.
class C (a :: TYPE (r :: RuntimeRep)) where
m :: a -> ()
default m :: (r ~ LiftedRep) => a -> ()
m = const ()
-- Bug.hs
module Bug where
import Lib
data Foo
instance C Foo
Next, compile them with a recent version of GHC like so:
$ /opt/ghc/8.8.2/bin/ghc -c Lib.hs -O -dcore-lint && /opt/ghc/8.8.2/bin/ghc -c Bug.hs -O -dcore-lint
ghc: panic! (the 'impossible' happened)
(GHC version 8.8.2 for x86_64-unknown-linux):
Iface Lint failure
In interface for Lib
Unfolding of $dmm
<no location info>: warning:
In the type ‘(a_a1a9 |> Sym (TYPE (Sym co_a1ac))_N) -> ()’
co_a1ac :: r_a1a8 ~# 'LiftedRep
[LclId[CoVarId]] is out of scope
$dmm = \ (@ (r_a1a8 :: RuntimeRep))
(@ (a_a1a9 :: TYPE r_a1a8))
($dC_a1aa :: C a_a1a9)
($d~_a1ab :: r_a1a8 ~ 'LiftedRep) ->
case eq_sel @ RuntimeRep @ r_a1a8 @ 'LiftedRep $d~_a1ab of co_a1ac
{ __DEFAULT ->
\ (ds_a1ad :: (a_a1a9 |> Sym (TYPE (Sym co_a1ac))_N)) -> ()
}
Iface expr = \ @ r :: RuntimeRep
@ a :: TYPE r
($dC :: C a)
($d~ :: r ~ 'LiftedRep) ->
case eq_sel @ RuntimeRep @ r @ 'LiftedRep $d~ of co { DEFAULT ->
\ (ds :: (a |> Sym (TYPE (Sym co))_N)) -> () }
Call stack:
CallStack (from HasCallStack):
callStackDoc, called at compiler/utils/Outputable.hs:1159:37 in ghc:Outputable
pprPanic, called at compiler/iface/TcIface.hs:1546:33 in ghc:TcIface
Please report this as a GHC bug: https://www.haskell.org/ghc/reportabug
I have observed this panic on every version of GHC from 8.2.2 to HEAD.
Another strange thing about this program is that I have to write m = const ()
. If I instead try to define it like m _ = ()
, I get the following error:
$ /opt/ghc/8.8.2/bin/ghc -c Lib.hs -O -dcore-lint && /opt/ghc/8.8.2/bin/ghc -c Bug.hs -O -dcore-lint
Lib.hs:19:5: error:
A levity-polymorphic type is not allowed here:
Type: a
Kind: TYPE r
In a wildcard pattern
|
19 | m _ = ()
| ^
This seems suspicious, does it not? Why should that binder get rejected for being levity polymorphic if r ~ LiftedRep
? Shouldn't that make the binder levity monomorphic?