Skip to content

Tc trace of checkValidType panics in tcTypeKind for T16391b

In !2218 (comment 335959), I triggered a panic in tcTypeKind. I thought that my branch was responsible, but then figured out that without -ddump-tc-trace, there was no panic. Then I tried -ddump-tc-trace on T16391b:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
module T16391b where

import GHC.Exts

type family T (r :: RuntimeRep) :: TYPE r

foo :: T r
foo = foo

And sure enough, that panics on HEAD:

checkValidTypeghc: panic! (the 'impossible' happened)
  (GHC version 9.1.20210305:
        tcTypeKind
  forall (r :: RuntimeRep). T r
  [r_aw2[sk:2]]
  T r_aw2[sk:2] :: TYPE r_aw2[sk:2]
  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/Type.hs:2660:18 in ghc:GHC.Core.Type

... when checkValidType (I think) ultimately would have printed the following expected error:

T16391b.hs:10:8: error:
    • Quantified type's kind mentions quantified type variable
        type: ‘T 'LiftedRep’
      where the body of the forall has this kind: ‘TYPE r’
    • In the type signature: foo :: T r
   |
10 | foo :: T r
   |        ^^^

It's a matter of printing the error message before calling tcTypeKind:

checkValidType ctxt ty
  = do { traceTc "checkValidType" (ppr ty <+> text "::" <+> ppr (tcTypeKind ty))
       ; rankn_flag  <- xoptM LangExt.RankNTypes
...
       ; checkNoErrs $
         do { check_type ve ty
            ; checkUserTypeError ty
            ; traceTc "done ct" (ppr ty) }
...
       ; traceTc "checkValidType done" (ppr ty <+> text "::" <+> ppr (tcTypeKind ty)) }

I suggest we simply delete the <+> text "::" <+> ppr (tcTypeKind ty) of the first traceTc.

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