Skip to content

Commit 8e2a070

Browse files
Make enum cases static values
1 parent b8426ca commit 8e2a070

File tree

6 files changed

+16
-6
lines changed

6 files changed

+16
-6
lines changed

compiler/src/dotty/tools/backend/jvm/BTypesFromSymbols.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class BTypesFromSymbols[I <: BackendInterface](val int: I) extends BTypes {
224224
if (sym.isVarargsMethod) ACC_VARARGS else 0,
225225
if (sym.isSynchronized) ACC_SYNCHRONIZED else 0,
226226
if (sym.isDeprecated) asm.Opcodes.ACC_DEPRECATED else 0,
227-
if (sym.isJavaEnum) asm.Opcodes.ACC_ENUM else 0
227+
if (sym.isEnum) asm.Opcodes.ACC_ENUM else 0
228228
)
229229
}
230230

compiler/src/dotty/tools/backend/jvm/BackendInterface.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ abstract class BackendInterface extends BackendInterfaceDefinitions {
512512
def isJavaDefaultMethod: Boolean
513513
def isClassConstructor: Boolean
514514
def isSerializable: Boolean
515-
def isJavaEnum: Boolean
515+
def isEnum: Boolean
516516

517517
/**
518518
* True for module classes of modules that are top-level or owned only by objects. Module classes

compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
703703
def shouldEmitForwarders: Boolean =
704704
(sym is Flags.Module) && sym.isStatic
705705
def isJavaEntryPoint: Boolean = CollectEntryPoints.isJavaEntryPoint(sym)
706-
def isJavaEnum = sym.derivesFromJavaEnum
706+
def isEnum = sym.is(Flags.Enum)
707707

708708
def isClassConstructor: Boolean = toDenot(sym).isClassConstructor
709709
def isSerializable: Boolean = toDenot(sym).isSerializable

compiler/src/dotty/tools/dotc/ast/DesugarEnums.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ object DesugarEnums {
8484
/** The following lists of definitions for an enum type E:
8585
*
8686
* private val $values = new EnumValues[E]
87+
* def values = $values.values.toArray
8788
* def valueOf($name: String) =
8889
* try $values.fromName($name) catch
8990
* {
9091
* case ex$:NoSuchElementException =>
9192
* throw new IllegalArgumentException("key not found: ".concat(name))
9293
* }
93-
* def values = $values.values.toArray
9494
*/
9595
private def enumScaffolding(implicit ctx: Context): List[Tree] = {
9696
val valuesDef =
@@ -286,7 +286,7 @@ object DesugarEnums {
286286
val toStringDef = toStringMethLit(name.toString)
287287
val impl1 = cpy.Template(impl)(body = List(ordinalDef, toStringDef) ++ registerCall)
288288
.withAttachment(ExtendsSingletonMirror, ())
289-
val vdef = ValDef(name, TypeTree(), New(impl1)).withMods(mods | Final)
289+
val vdef = ValDef(name, TypeTree(), New(impl1)).withMods(mods | EnumValue)
290290
flatTree(scaffolding ::: vdef :: Nil).withSpan(span)
291291
}
292292
}
@@ -302,7 +302,7 @@ object DesugarEnums {
302302
else {
303303
val (tag, scaffolding) = nextOrdinal(CaseKind.Simple)
304304
val creator = Apply(Ident(nme.DOLLAR_NEW), List(Literal(Constant(tag)), Literal(Constant(name.toString))))
305-
val vdef = ValDef(name, enumClassRef, creator).withMods(mods | Final)
305+
val vdef = ValDef(name, enumClassRef, creator).withMods(mods | EnumValue)
306306
flatTree(scaffolding ::: vdef :: Nil).withSpan(span)
307307
}
308308
}

compiler/src/dotty/tools/dotc/core/Flags.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,9 @@ object Flags {
693693
/** A Java enum value */
694694
final val JavaEnumValue: FlagConjunction = allOf(StableRealizable, JavaStatic, JavaDefined, Enum)
695695

696+
/** An enum value */
697+
final val EnumValue: FlagConjunction = allOf(StableRealizable, JavaStatic, Enum)
698+
696699
/** Labeled private[this] */
697700
final val PrivateLocal: FlagConjunction = allOf(Private, Local)
698701

compiler/src/dotty/tools/dotc/transform/CompleteJavaEnums.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
110110
else tree
111111
}
112112

113+
override def transformValDef(tree: ValDef)(implicit ctx: Context): ValDef = {
114+
val sym = tree.symbol
115+
if ((sym.is(EnumValue) || sym.name == nme.DOLLAR_VALUES) && sym.owner.linkedClass.derivesFromJavaEnum)
116+
sym.addAnnotation(Annotations.Annotation(defn.ScalaStaticAnnot))
117+
tree
118+
}
119+
113120
/** 1. If this is an enum class, add $name and $ordinal parameters to its
114121
* parameter accessors and pass them on to the java.lang.Enum constructor.
115122
*

0 commit comments

Comments
 (0)