daml icon indicating copy to clipboard operation
daml copied to clipboard

Bogus error when deriving `Enum` for a Single-constructor enumeration

Open akrmn opened this issue 1 year ago • 0 comments

This program currently fails to build

module Main where

data MyEnum
  = MyEnum
  deriving (Enum)

with the error message

Message: 
error type checking value Main.$con2tag_BJw0GctXQ6lDBoq1ZFgqgC:
  unknown type synonym: 518032f41fd0175461b35ae0c9691e08b4aea55e62915f8360af2cc7a1f2ba6c:GHC.Types:DamlEnum
ERROR: Creation of DAR file failed.

The reason is that during preprocessing we add a DamlEnum "dumb theta" constraint to the type, so it desugars to

data DamlEnum => MyEnum = MyEnum ...

[this constraint is only used to ensure that the type is treated an enum as opposed to a variant or a record during LF conversion. This type is erased so it doesn't exist in LF]

running daml damlc compile daml/Main.daml --ghc-option -ddump-deriv, we see that the derived Enum instance contains references to the erased class, causing the bogus error message

        ....
      
      Main.$con2tag_GQw5e9GrRWNFvxHNe1bHSF :
        GHC.Types.DamlEnum => Main.MyEnum -> GHC.Prim.Int#
      Main.$tag2con_GQw5e9GrRWNFvxHNe1bHSF : GHC.Types.Int -> Main.MyEnum
      Main.$maxtag_GQw5e9GrRWNFvxHNe1bHSF : GHC.Types.Int
      Main.$maxtag_GQw5e9GrRWNFvxHNe1bHSF = GHC.Types.I# 0#
      Main.$tag2con_GQw5e9GrRWNFvxHNe1bHSF (GHC.Types.I# a)
        = GHC.Prim.tagToEnum# a
      ....

akrmn avatar Feb 16 '24 15:02 akrmn