Skip to content

DerivedLoggableScala3Suite testcase for sealed trait enum are based on bug in Scala compiler #1422

@WojciechMazur

Description

@WojciechMazur

"SealedTraitEnum SingletonEnumLoggable" should "correctly log" in:
import SealedTraitEnum.*
json(Container(A, B, C)) shouldBe """{"a":"A","b":"B","c":"C"}"""

compiles only until Scala 3.4.0 due to a bug in compiler see scala/scala3#23558

The issue was spotted by Scala 3 Open Community Build, it was discussed by the Scala core team and we agreed it now works as expected.
That's becouse when we define:

  enum Scala3Enum derives SingletonEnumLoggable:
    case A, B, C

it's being emitted as (with big simplification)

class Scala3Enum
object Scala3Enum: 
  given SingletonEnumLoggable[Scala3Enum] = ??? 
  val A: Scala3Enum = new Scala3Enum()
  val B: Scala3Enum = new Scala3Enum() 
  val C: Scala3Enum = new Scala3Enum() 

So the type of enumerated instances is always narrowed to the enum base type.

In case of sealed trait

  sealed trait SealedTraitEnum derives SingletonEnumLoggable
  object SealedTraitEnum:
    case object A extends SealedTraitEnum
    case object B extends SealedTraitEnum
    case object C extends SealedTraitEnum

there exists type SealedTraitEnum.A.type that is a more specfic subtype of SealedTraitEnum (same for other subtypes)

Before bugfix introduced in 3.4.1 the type requested for implicit resolution was narrowed from more case object subtype, to more generic SealedTraitEnum

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions