"Iface type variable out of scope" with poly-kinded associated type and newtype-deriving
Summary
When a newtype-derived instance involving a kind-polymorphic associated type is called and optimized, ghc-9.4.2 prints the following error:
typecheckIface
Declaration for $fMyClassTYPEMyMaybe
Unfolding of $fMyClassTYPEMyMaybe:
Iface type variable out of scope: k
<no location info>: error:
Cannot continue after interface file error
There are two parts to this error message: the Iface type variable out of scope
part is a regression introduced in ghc-9.0, while the Cannot continue after interface file error
part is a regression introduced in ghc-9.4.
I found several issues from years ago featuring this error message, triggered by increasingly-obscure circumstances. This is yet another obscure circumstance. #9263 (closed) is the most closely related, as it also involves associated types and polykinds, but not newtype-deriving. #2018 (closed) is also related in that it also requires optimizations to be turned on.
Note that some of those tickets say that the error only occurs the second time the code is built. This is not the case here, the error occurs the first time.
Steps to reproduce
The easiest way to reproduce the problem is to clone https://github.com/gelisam/ghc-9.4-bug and to run ./build.sh
. Alternatively, create those two modules:
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module MyModule where
import Data.Kind (Type)
import Data.Proxy (Proxy)
class MyClass (f :: k -> Type) where
type MyTypeFamily f (i :: k) :: Type
myMethod :: MyTypeFamily f i -> Proxy f -> Proxy i -> ()
instance MyClass Maybe where
type MyTypeFamily Maybe i = ()
myMethod = undefined
newtype MyMaybe a = MyMaybe (Maybe a)
deriving MyClass
and
{-# LANGUAGE TypeApplications #-}
module Foo where
import Data.Kind (Type)
import Data.Proxy (Proxy(Proxy))
import MyModule
foo :: ()
foo = myMethod @Type @MyMaybe @() () Proxy Proxy
Then compile those two modules, with optimizations turned on:
$ ghc -O MyModule.hs Foo.hs
[2 of 2] Compiling Foo ( Foo.hs, Foo.o )
typecheckIface
Declaration for $fMyClassTYPEMyMaybe
Unfolding of $fMyClassTYPEMyMaybe:
Iface type variable out of scope: k
<no location info>: error:
Cannot continue after interface file error
Expected behavior
I expect the two modules to compile successfully.
Environment
- GHC version used: ghc-9.4.2, ghc-9.2.4, ghc-9.0.2
Of these, only ghc-9.4 fails to compile.
With ghc-9.2 and ghc-9.0, the Iface type variable out of scope
message is printed but not Cannot continue after interface file error
, and the module still compiles successfully.
I have also tried with ghc-8.10.7, which does not print either message.
Optional:
- Operating System: macOS Ventura 13.3
- System Architecture: arm64