Skip to content

panic in refineFromInScope with 9.2.1

Summary

The Bluespec Compiler (BSC) has been compiling with GHC 9.0.1, 8.10.7, etc, but when attempting to compile with 9.2.1, it encounters a panic in refineFromInScope. I have reduced it to a small example, which compiles fine when GHC is called with no flags, but encounters the panic when called with -O2:

$ ghc -O2 CtxRed.hs
[1 of 1] Compiling CtxRed           ( CtxRed.hs, CtxRed.o )
ghc: panic! (the 'impossible' happened)
  (GHC version 9.2.1:
	refineFromInScope
  InScope {wild_00 wild_X1 s1_X4 d_aF3 incoh_aF4 cpreds_aF5
           $cctxRed_aJC $krep_aKv $krep_aKw $krep_aKx $krep_aKy $krep_aKz
           $krep_aKA $krep_aKB $krep_aKC $krep_aKD $krep_aKE $krep_aKF
           $krep_aKG $krep_aKH $krep_aKI $krep_aKJ $krep_aKK $krep_aKL
           $krep_aKM $krep_aKN $krep_aKO $krep_aKP $krep_aKQ $krep_aKR
           wild_aPf x_aPi s1_aPv wild_aPx a1_aPy s'_aPz ctxRedCQType'
           $tc'CTypeclass $tcCTypeclass $tc'CPred $tcCPred $tc'CQType
           $tcCQType $tc'Cclass $tcCDefn $tcCtxRed $tc'C:CtxRed $fCtxRedCDefn
           $trModule $trModule_sN7 $trModule_sN8 $trModule_sN9 $trModule_sNa
           $krep_sNb $krep_sNc $krep_sNd $krep_sNe $krep_sNf $krep_sNg
           $krep_sNh $krep_sNi $krep_sNj $krep_sNk $tcCTypeclass_sNl
           $tcCTypeclass_sNm $tc'CTypeclass_sNn $tc'CTypeclass_sNo
           $tcCPred_sNp $tcCPred_sNq $tc'CPred_sNr $tc'CPred_sNs $krep_sNt
           $tcCQType_sNu $tcCQType_sNv $tc'CQType_sNw $tc'CQType_sNx
           $tcCDefn_sNy $tcCDefn_sNz $tc'Cclass_sNA $tc'Cclass_sNB
           $tcCtxRed_sNC $tcCtxRed_sND $krep_sNE $tc'C:CtxRed_sNF
           $tc'C:CtxRed_sNG ds_sPV m1_sPW lvl_sQ4 lvl_sQ5 m1_sQ7 lvl_sQ8}
  s'_X7
  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

I do not know if this is related to the two other issues that mention a panic in refineFromInScope (#20419 and #19360 (closed)).

Steps to reproduce

Place the following example in a file called CtxRed.hs:

module CtxRed(ctxRedCQType', CtxRed(..)) where

import Control.Monad.State
import Control.Monad.Except

type TI = StateT Bool (ExceptT [Integer] (State Bool))

data CDefn = Cclass (Maybe Bool) [CPred]
data CQType = CQType [CPred]
data CTypeclass = CTypeclass
data CPred = CPred CTypeclass

class CtxRed a where
    ctxRed :: a -> TI a

instance CtxRed CDefn where
    ctxRed d@(Cclass incoh cpreds) = do
       (CQType _) <- ctxRedCQType' (CQType [])
       return (Cclass incoh cpreds)

ctxRedCQType' :: CQType -> TI (CQType)
ctxRedCQType' cqt = do
    let CQType cqs = cqt
    case [p | p@(CPred CTypeclass) <- cqs] of
      p : _ -> return ()
      [] -> return ()
    return (cqt)

And execute:

ghc -O2 CtxRed.hs

If you would like to reproduce the original error in BSC (which has 226 Haskell source files, imported foreign functions, etc), instructions for building BSC can be found at https://github.com/B-Lang-org/bsc/blob/main/INSTALL.md

Expected behavior

If I execute GHC without the -O2 flag, or if I use a version such as 9.0.1 or 8.10.7, there are no errors or warnings:

$ ghc CtxRed.hs
[1 of 1] Compiling CtxRed           ( CtxRed.hs, CtxRed.o )

This small example has no real meaning; in the BSC project, with the original CtxRed.hs file, the module compiles and GHC continues compiling the rest of the modules and generates an executable, which can then be run against the BSC testsuite.

Environment

As said above, this occurs with GHC 9.2.1.

The small example was developed on macOS 11.6. The original BSC issue can be seen both on GitHub's Ubuntu 18.04/20.04 VMs (in the BSC project's automated testing) and locally for me on macOS 11.6.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information