Skip to content

DerivingVia and GND should not need UnboxedTuples

Summary

If class methods involve UnboxedTuples, then UnboxedTuples is required to be enabled in a module to derive that class using DerivingVia or GeneralizedNewtypeDeriving.

Steps to reproduce

-- UT.hs

{-# language UnboxedTuples #-}
module UT where

class Tupleable a where
  tup :: a -> (# a #)

newtype Boring a = Boring a
instance Tupleable (Boring a) where
  tup a = (# a #)

instance Tupleable () where
  tup _ = (# () #)

-- DV.hs

{-# language DerivingVia, GeneralizedNewtypeDeriving #-}
module DV where
import UT

data Foo = Foo
  deriving Tupleable via Boring Foo

newtype Bar a = Bar ()
  deriving newtype Tupleable

GHC says:

[2 of 2] Compiling DV               ( DV.hs, DV.o )

DV.hs:6:12: error:
    • Illegal unboxed tuple type as function argument: (# Foo #)
      Perhaps you intended to use UnboxedTuples
    • In the type signature: tup :: Foo -> (# Foo #)
      When typechecking the code for ‘tup’
        in a derived instance for ‘Tupleable Foo’:
        To see the code I am typechecking, use -ddump-deriv
      In the instance declaration for ‘Tupleable Foo’
  |
6 |   deriving Tupleable via Boring Foo
  |            ^^^^^^^^^

DV.hs:9:20: error:
    • Illegal unboxed tuple type as function argument: (# Bar a #)
      Perhaps you intended to use UnboxedTuples
    • In the type signature: tup :: Bar a -> (# Bar a #)
      When typechecking the code for ‘tup’
        in a derived instance for ‘Tupleable (Bar a)’:
        To see the code I am typechecking, use -ddump-deriv
      In the instance declaration for ‘Tupleable (Bar a)’
  |
9 |   deriving newtype Tupleable
  | 

Expected behavior

I expect it to compile. Note: if UnboxedSums are used instead of UnboxedTuples, then all seems to be well.

Environment

  • GHC version used: 9.2

Optional:

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