Skip to content

Combination of unboxed sums and unsafeCoerce# causes compiler panics

If you compile this nonsensical program:

{-# LANGUAGE UnboxedSums, UnboxedTuples, MagicHash #-}

import Unsafe.Coerce
import GHC.Prim

main = do
  let x = (# 1 | #) :: (# Int | Int# #)
  let y = unsafeCoerce# x :: (# (# Int, Int #) | Int# #)

  case y of
    (# (# i1, i2 #) | #) -> do print i1; print i2
    (# | i #) -> return ()

GHC HEAD current panics in GHC.Cmm.Liveness:

{-# LANGUAGE UnboxedSums, UnboxedTuples, MagicHash #-}

import Unsafe.Coerce
import GHC.Prim

main = do
  let x = (# 1 | #) :: (# Int | Int# #)
  let y = unsafeCoerce# x :: (# (# Int, Int #) | Int# #)

  case y of
    (# (# i1, i2 #) | #) -> do print i1; print i2
    (# | i #) -> return ()

While this program doesn't make any sense, I'm not sure if GHC should panic this way (instead of with rejecting the program with a friendly error message) when provided a nonsensical program.

I think it's also possible that this is an actual bug in the code generator.

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