Skip to content

mkGADTVars panic with a GADT data family instance and unused variable in forall

{-# LANGUAGE DataKinds                  #-}
{-# LANGUAGE GADTs                      #-}
{-# LANGUAGE PolyKinds                  #-}
{-# LANGUAGE ScopedTypeVariables        #-}
{-# LANGUAGE StandaloneKindSignatures   #-}
{-# LANGUAGE TypeApplications           #-}
{-# LANGUAGE TypeFamilies               #-}

module T23784 where

import Data.Kind

type Sing :: forall k. k -> Type
data family Sing a

data instance forall a b. Sing a where
  S :: Sing '()
<no location info>: error:
    panic! (the 'impossible' happened)
  GHC version 9.9.20230724:
        mkGADTVars
  [k_awT[sk:1], b_awF[sk:1], k_awU[sk:1], a_awE[sk:1]]
  <InScope = {a_awE[sk:1] k_awU[sk:1]}
   IdSubst   = []
   TvSubst   = [awE :-> '(), awU :-> ()]
   CvSubst   = []>
  Call stack:
      CallStack (from HasCallStack):
        callStackDoc, called at compiler\GHC\Utils\Panic.hs:191:37 in ghc-9.9-inplace:GHC.Utils.Panic
        pprPanic, called at compiler\GHC\Tc\TyCl.hs:4244:9 in ghc-9.9-inplace:GHC.Tc.TyCl
  CallStack (from HasCallStack):
    panic, called at compiler\GHC\Utils\Error.hs:503:29 in ghc-9.9-inplace:GHC.Utils.Error


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

I think this is to do with the way we create a representation TyCon for a data family instance using the full set of bound variables in the forall (see the use of ty_binders in mkAlgTyCon in GHC.Tc.TyCl.Instance.tcDataFamInstDecl and Note [Data type families] in GHC.Core.TyCon).

I ran into this when working on error messages for unused type variables in family instances in !11032 (closed). I will investigate, but pinging @rae as the authority on this code (in case anything obvious springs to mind).

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