Skip to content

Panic on particular combination of UndecidableInstances, StandaloneDeriving, TypeFamilies, and ConstraintKinds

Summary

This program:

{-# LANGUAGE UndecidableInstances, StandaloneDeriving, TypeFamilies, ConstraintKinds #-}

module Bug where

data BoxAssocDouble = BoxAssocDouble (BoxAssoc Int)
  deriving (Eq)

type family Assoc a

data BoxAssoc a = BoxAssoc (Assoc a)

deriving instance c Eq a => Eq (BoxAssoc a)

Panics with this error:

[1 of 1] Compiling Bug              ( bug.hs, bug.o )
ghc: panic! (the 'impossible' happened)
  (GHC version 9.2.1:
	solveDerivEqns: probable loop
  DerivSpec
    ds_loc                  = bug.hs:6:13-14
    ds_name                 = $fEqBoxAssocDouble
    ds_tvs                  = []
    ds_cls                  = Eq
    ds_tys                  = [BoxAssocDouble]
    ds_theta                = [ThetaOrigin
                                 to_anyclass_skols  = []
                                 to_anyclass_metas  = []
                                 to_anyclass_givens = []
                                 to_wanted_origins  = [],
                               ThetaOrigin
                                 to_anyclass_skols  = []
                                 to_anyclass_metas  = []
                                 to_anyclass_givens = []
                                 to_wanted_origins  = [],
                               ThetaOrigin
                                 to_anyclass_skols  = []
                                 to_anyclass_metas  = []
                                 to_anyclass_givens = []
                                 to_wanted_origins  = [],
                               ThetaOrigin
                                 to_anyclass_skols  = []
                                 to_anyclass_metas  = []
                                 to_anyclass_givens = []
                                 to_wanted_origins  = [Eq (BoxAssoc Int)]]
    ds_standalone_wildcard  = Nothing
    ds_mechanism            = DerivSpecStock
                                dsm_stock_dit DITTyConHead
                                                dit_cls_tys []
                                                dit_tc BoxAssocDouble
                                                dit_tc_args []
                                                dit_rep_tc BoxAssocDouble
                                                dit_rep_tc_args []
  [[c_aIT[tau:1] Eq Int]]
  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/Tc/Deriv/Infer.hs:665:9 in ghc:GHC.Tc.Deriv.Infer

Please report this as a GHC bug:  https://www.haskell.org/ghc/reportabug

Steps to reproduce

Run the above MWE with a modern-enough version of GHC. (Tested with 8.6.5 and 9.2.1)

Expected behavior

Honestly, I don't know. I discovered this bug by writing

deriving instance stock Eq a => Eq (BoxAssoc a)

intending to combine StandaloneDeriving with DerivingStrategies. It seems, however, that GHC parses stock Eq a as a constraint, and then promptly crashes.

Environment

  • GHC version used: 9.2.1 (also 8.6.5)
  • Operating System: Pop!_OS 20.10
  • System Architecture: 64-bit
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information