GHC panic ("GHC.StgToCmm.Env: variable not found") with lens-style code and RankNTypes
Summary
GHC 8.10.1 panics when compiling a pair of modules that define and use variants of functions from the lens package (including some that require enabling the RankNTypes
extension).
Steps to reproduce
Discovered this panic when upgrading some lens-heavy code from GHC 8.6.4 to GHC 8.10.1. I managed to extract the relevant functions from lens
into their own module, so the test case has no dependencies (other than base
). Unfortunately I wasn't able to reduce it to a single module; putting everything in one module doesn't trigger the panic.
MiniLens.hs
:
{-# LANGUAGE RankNTypes #-}
module MiniLens ((^.), Getting, Lens', lens, view) where
import Data.Functor.Const (Const(..))
type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t
type Lens' s a = Lens s s a a
lens :: (s -> a) -> (s -> b -> t) -> Lens s t a b
lens sa sbt afb s = sbt s <$> afb (sa s)
{-# INLINE lens #-}
type Getting r s a = (a -> Const r a) -> s -> Const r s
view :: Getting a s a -> s -> a
view l = getConst . l Const
{-# INLINE view #-}
(^.) :: s -> Getting a s a -> a
s ^. l = getConst (l Const s)
{-# INLINE (^.) #-}
GHCBug.hs
:
{-# LANGUAGE RankNTypes #-}
module GHCBug (field) where
import MiniLens ((^.), Getting, Lens', lens, view)
t' :: Getting () () ()
t' = lens id const
{-# NOINLINE t' #-}
mlift :: Functor f => Getting b a b -> Lens' (f a) (f b)
mlift l = lens (fmap (^. l)) const
{-# INLINE mlift #-}
newtype Field = F (Maybe () -> Maybe ())
field :: Field
field = F (view (mlift t'))
Command and output:
$ ghc -O GHCBug.hs MiniLens.hs
[1 of 2] Compiling MiniLens ( MiniLens.hs, MiniLens.o )
[2 of 2] Compiling GHCBug ( GHCBug.hs, GHCBug.o )
ghc: panic! (the 'impossible' happened)
(GHC version 8.10.1:
GHC.StgToCmm.Env: variable not found
dk_XM9
local binds for:
dk_sSZ
dk1_sT0
afb_sT1
s1_sT2
sat_sT3
Call stack:
CallStack (from HasCallStack):
callStackDoc, called at compiler/utils/Outputable.hs:1179:37 in ghc:Outputable
pprPanic, called at compiler/GHC/StgToCmm/Env.hs:149:9 in ghc:GHC.StgToCmm.Env
Please report this as a GHC bug: https://www.haskell.org/ghc/reportabug
When I enable -dcore-lint
I get the following core lint error (only the first part, since it's long and this is the part that refers to the variable dk_XM9
):
*** Core Lint errors : in result of Specialise ***
googlex/positron/sw/Model/tests/MiniLens.hs:11:1: warning:
Out of scope: dk_XM9 [Occ=Once!] :: s_aLx -> a_aLy
[LclId]
In the RHS of $slens_sSk :: forall s a b t.
(s -> a) -> (s -> b -> t) -> (a -> Const () b) -> s -> Const () t
In the body of lambda with binder s_XLE :: *
In the body of lambda with binder a_XLG :: *
In the body of lambda with binder b_XLI :: *
In the body of lambda with binder t_XLK :: *
In the body of lambda with binder dk_XMk :: s_XLE -> a_XLG
In the body of lambda with binder dk1_XMh :: s_XLE
-> b_XLI -> t_XLK
In the body of lambda with binder afb_ajg :: a_XLG
-> Const () b_XLI
In the body of lambda with binder s1_ajh :: s_XLE
Substitution: [TCvSubst
In scope: InScope {s_XLE a_XLG b_XLI t_XLK dk1_XMh dk_XMk afb_ajg
s1_ajh dk_aM4 $krep_aRl $krep_aRm $krep_aRn $krep_aRo
$krep_aRp t' field $tc'F $tcField $trModule $trModule_sRP
$trModule_sRQ $trModule_sRR $trModule_sRS $krep_sRT
$tcField_sRU $tcField_sRV $tc'F_sRW $tc'F_sRX $slens_sSk}
Type env: []
Co env: []]
*** Offending Program ***
Rec {
$slens_sSk [InlPrag=INLINE (sat-args=4)]
:: forall s a b t.
(s -> a) -> (s -> b -> t) -> (a -> Const () b) -> s -> Const () t
[LclId,
Arity=4,
Unf=Unf{Src=InlineStable, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True,
Guidance=ALWAYS_IF(arity=3,unsat_ok=False,boring_ok=False)
Tmpl= \ (@ s_aLx)
(@ a_aLy)
(@ b_aLz)
(@ t_aLA)
(dk_XM9 [Occ=Once!] :: s_aLx -> a_aLy)
(dk1_aM5 [Occ=Once!] :: s_aLx -> b_aLz -> t_aLA)
(afb_ajg [Occ=Once!] :: a_aLy -> Const () b_aLz)
(s1_ajh :: s_aLx) ->
fmap
@ (Const ())
($fFunctorConst @ ())
@ b_aLz
@ t_aLA
(dk1_aM5 s1_ajh)
(afb_ajg (dk_XM9 s1_ajh))}]
$slens_sSk
= \ (@ s_XLE)
(@ a_XLG)
(@ b_XLI)
(@ t_XLK)
(dk_XMk [Occ=Once!] :: s_XLE -> a_XLG)
(dk1_XMh [Occ=Once!] :: s_XLE -> b_XLI -> t_XLK)
(afb_ajg [Occ=Once!] :: a_XLG -> Const () b_XLI)
(s1_ajh :: s_XLE) ->
fmap
@ (Const ())
($fFunctorConst @ ())
@ b_XLI
@ t_XLK
(dk1_XMh s1_ajh)
(afb_ajg (dk_XM9 s1_ajh))
end Rec }
... etc.
I found #16887 and #15598 (closed) but I think this may be a different issue because it requires fewer extensions (only RankNTypes
) and the core lint error is after "Specialise" instead of "Desugar".
Expected behavior
It should compile successfully.
Environment
- GHC version used: 8.10.1
Optional:
- Operating System: Unknown/Multiple
- System Architecture: Unknown/Multiple