Skip to content

runtimeRepPrimRep panic with a representation-polymorphic newtype

The following program runs into a panic on HEAD:

{-# LANGUAGE DataKinds, DatatypeContexts, MagicHash, UnliftedNewtypes, TypeFamilies #-}

module EtaExpandNewtypeTF where

import Data.Kind
import GHC.Exts

type R :: Type -> RuntimeRep
type family R a where
  R Float  = FloatRep
  R Double = DoubleRep

type F :: forall (a :: Type) -> TYPE (R a)
type family F a where
  F Float  = Float#
  F Double = Double#

type C :: Type -> Constraint
class C a where {}

type N :: forall (a :: Type) -> TYPE (R a)
newtype C a => N a = MkN (F a)

foo1 :: C Float => F Float -> N Float
foo1 = MkN

foo2 :: C Double => () -> F Double -> N Double
foo2 _ = MkN
  GHC version 9.5.20220525:
        runtimeRepPrimRep
  typePrimRep (F Float :: TYPE (R Float))
  R Float
  Call stack:
      CallStack (from HasCallStack):
        callStackDoc, called at compiler\GHC\Utils\Panic.hs:182:37 in ghc:GHC.Utils.Panic
        pprPanic, called at compiler\GHC\Types\RepType.hs:614:5 in ghc:GHC.Types.RepType
        runtimeRepPrimRep, called at compiler\GHC\Types\RepType.hs:582:5 in ghc:GHC.Types.RepType
        kindPrimRep, called at compiler\GHC\Types\RepType.hs:534:18 in ghc:GHC.Types.RepType
        typePrimRep, called at compiler\GHC\Types\RepType.hs:181:22 in ghc:GHC.Types.RepType
        isZeroBitTy, called at compiler\GHC\Core\Unfold.hs:617:43 in ghc:GHC.Core.Unfold

We also get an error with Core Lint:

*** Core Lint errors : in result of Desugar (after optimization) ***
EtaExpandNewtypeTF.hs:25:1: warning:
    Binder does not have a fixed runtime representation: ds_dHN :: (F Float :: TYPE
                                                                                 (R Float))
    In the RHS of foo1 :: C Float =>
                          (F Float |> (TYPE (D:R:R[0]))_N)
                          -> (N Float |> (TYPE (D:R:R[0]))_N)
    In the body of lambda with binder $dC_aH8 :: C Float
    In the body of lambda with binder ds_dHN :: F Float
    Substitution: [TCvSubst
                     In scope: InScope {}
                     Type env: []
                     Co env: []]

This seems to be a different issue than #21544 (closed), as changing newtype constructors to always inline fixes that bug but doesn't fix this one.

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