Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enum companion reflection to scala.reflect #14136

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,19 @@ object desugar {

companionDefs(anyRef, applyMeths ::: unapplyMeth :: toStringMeth :: companionMembers)
}
else if (companionMembers.nonEmpty || companionDerived.nonEmpty || isEnum)
else if (isEnum)
val isSingletonEnum = companionMembers.forall {
case _ : PatDef => true
case _ : ModuleDef => true
case _ => false
}
val enumCompClass =
if (isSingletonEnum) defn.SingletonEnumCompanionClass.typeRef
else defn.EnumCompanionClass.typeRef
val clsWithArgs = appliedTypeTree(Ident(className), impliedTparams.map(_ => WildcardTypeBoundsTree()))
val parent = appliedTypeTree(ref(enumCompClass), clsWithArgs :: Nil)
companionDefs(parent, companionMembers)
else if (companionMembers.nonEmpty || companionDerived.nonEmpty)
companionDefs(anyRef, companionMembers)
else if (isValueClass)
companionDefs(anyRef, Nil)
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,8 @@ class Definitions {

@tu lazy val EnumClass: ClassSymbol = requiredClass("scala.reflect.Enum")
@tu lazy val Enum_ordinal: Symbol = EnumClass.requiredMethod(nme.ordinal)
@tu lazy val EnumCompanionClass: ClassSymbol = requiredClass("scala.reflect.EnumCompanion")
@tu lazy val SingletonEnumCompanionClass: ClassSymbol = requiredClass("scala.reflect.SingletonEnumCompanion")

@tu lazy val EnumValueSerializationProxyClass: ClassSymbol = requiredClass("scala.runtime.EnumValueSerializationProxy")
@tu lazy val EnumValueSerializationProxyConstructor: TermSymbol =
Expand Down
8 changes: 8 additions & 0 deletions library/src/scala/reflect/Enum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ package scala.reflect

/** A number uniquely identifying a case of an enum */
def ordinal: Int

/** A base trait of all Scala enum companion definitions */
@annotation.transparentTrait trait EnumCompanion[E <: Enum] extends AnyRef

/** A base trait of all Scala singleton enum companion definitions */
@annotation.transparentTrait trait SingletonEnumCompanion[E <: Enum] extends EnumCompanion[E]:
def values : Array[E]
def valueOf(name : String) : E
2 changes: 1 addition & 1 deletion tests/init/neg/enum-desugared.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ sealed abstract class ErrorMessageID($name: String, _$ordinal: Int)
def errorNumber: Int = this.ordinal() - 2
}

object ErrorMessageID {
object ErrorMessageID extends scala.reflect.EnumCompanion[ErrorMessageID]{

final val LazyErrorId = $new(0, "LazyErrorId")
final val NoExplanationID = $new(1, "NoExplanationID")
Expand Down
48 changes: 48 additions & 0 deletions tests/run/enum-reflect-companion.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import scala.reflect.{EnumCompanion, SingletonEnumCompanion}
enum Foo1:
case Baz, Bar

val check1 = summon[Foo1.type <:< SingletonEnumCompanion[Foo1]]

enum Foo2[T]:
case Baz extends Foo2[1]
case Bar extends Foo2[2]

val check2 = summon[Foo2.type <:< SingletonEnumCompanion[Foo2[?]]]

enum Foo3[A, B[_]]:
case Baz extends Foo3[Int, List]
case Bar extends Foo3[Int, List]

val check3 = summon[Foo3.type <:< SingletonEnumCompanion[Foo3[?, ?]]]

extension [T <: reflect.Enum](enumCompanion : SingletonEnumCompanion[T])
def check(arg : T) : Unit = assert(enumCompanion.values.contains(arg))

enum Foo4:
case Yes
case No(whyNot: String)
case Skip

val check4 = summon[Foo4.type <:< EnumCompanion[Foo4]]

@main def Test : Unit =
Foo3.check(Foo3.Bar)
(Foo3 : AnyRef) match
case _ : SingletonEnumCompanion[?] =>
case _ : EnumCompanion[?] => assert(false)
case _ => assert(false)

(Foo4 : AnyRef) match
case _ : SingletonEnumCompanion[?] => assert(false)
case _ : EnumCompanion[?] =>
case _ => assert(false)

enum Foo5:
case Baz, Bar

trait Hello
object Foo5 extends Hello

//TODO: fix implementation so this would work
//val check5 = summon[Foo5.type <:< scala.reflect.EnumCompanion[Foo5] with Hello]