Skip to content

Regression in newtype coercions between 9.6.0-alpha2 and alpha3

module Lib where

import Data.Coerce

newtype Fix f = Fix (f (Fix f))

data FooF r = A | B r

type Foo = Fix FooF

newtype NewFoo = NewFoo Foo

newFoo :: NewFoo -> Bool
newFoo x = case y of
  Fix A -> True
  _ -> False
  where
    y = coerce x

This program compiles in GHC 9.2, 9.4 and 9.6.0-alpha2, but not in 9.6.0-alpha3.

$ ghc-9.6.0.20230128 NewFoo.hs
[1 of 1] Compiling Lib              ( NewFoo.hs, NewFoo.o )
$ ghc-9.6.0.20230210 NewFoo.hs
[1 of 1] Compiling Lib              ( NewFoo.hs, NewFoo.o )

NewFoo.hs:18:9: error:
    • Reduction stack overflow; size = 201
      When simplifying the following type: Fix FooF
      Use -freduction-depth=0 to disable this check
      (any upper bound you could choose might fail unpredictably with
       minor updates to GHC, so disabling the check is recommended if
       you're sure that type checking should terminate)
    • In the expression: coerce x
      In an equation for ‘y’: y = coerce x
      In an equation for ‘newFoo’:
          newFoo x
            = case y of
                Fix A -> True
                _ -> False
            where
                y = coerce x
   |
18 |     y = coerce x
   |         ^^^^^^
$ ghc-9.6.0.20230210 -freduction-depth=0 NewFoo.hs
[1 of 1] Compiling Lib              ( NewFoo.hs, NewFoo.o )
ghc-9.6.0.20230210: Stack space overflow: current size 33616 bytes.
ghc-9.6.0.20230210: Use `+RTS -Ksize -RTS' to increase it.

I have no particular opinion whether this behaviour is expected and/or desired. However, my concern is twofold:

  • Technical: release notes does not seem to have anything related, I cannot find any mentions of "newtype".
  • Social: breaking changes to the type checker between alpha2 and alpha3 push maintainers to delay releases until the very final GHC 9.6.1, thus causing the ecosystem to lag behind even longer (and a new GHC to be battle tested even later).
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information