Skip to content

Commit d3af2e1

Browse files
committed
remove dollarordinal from Enum, reduce bytecode
1 parent 493f192 commit d3af2e1

File tree

6 files changed

+16
-16
lines changed

6 files changed

+16
-16
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ object DesugarEnums {
125125
/** A creation method for a value of enum type `E`, which is defined as follows:
126126
*
127127
* private def $new(_$ordinal: Int, $name: String) = new E with scala.runtime.EnumValue {
128-
* def $ordinal = $tag
128+
* private[this] def $ordinal = _$ordinal // if deriving from jl.Enum
129+
* def ordinal = _$ordinal // if not deriving from jl.Enum
129130
* override def toString = $name
130131
* $values.register(this)
131132
* }
@@ -265,7 +266,9 @@ object DesugarEnums {
265266
ValDef(name, TypeTree(typ), EmptyTree).withFlags(Param)
266267

267268
def ordinalMeth(body: Tree)(using Context): DefDef =
268-
DefDef(nme.ordinalDollar, Nil, Nil, TypeTree(defn.IntType), body)
269+
val isJEnum = ctx.owner.linkedClass.derivesFrom(defn.JavaEnumClass)
270+
val method = DefDef(if isJEnum then nme.ordinalDollar else nme.ordinal, Nil, Nil, TypeTree(defn.IntType), body)
271+
if isJEnum then method.withMods(Modifiers(Private)) else method
269272

270273
def toStringMeth(body: Tree)(using Context): DefDef =
271274
DefDef(nme.toString_, Nil, Nil, TypeTree(defn.StringType), body).withFlags(Override)

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
4040
sym == defn.JavaEnumClass.primaryConstructor ||
4141
sym.owner.derivesFromJavaEnum))
4242
addConstrParams(sym.info)
43+
// TODO: remove $ordinal and toString override from decls
4344
else tp
4445

4546
/** Add constructor parameters `$name: String` and `$ordinal: Int` to the end of
@@ -63,8 +64,8 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
6364
* with given flags (either `Param` or `ParamAccessor`)
6465
*/
6566
private def addedParams(owner: Symbol, flag: FlagSet)(using Context): List[ValDef] = {
66-
val nameParam = newSymbol(owner, nameParamName, flag | Synthetic, defn.StringType, coord = owner.span)
67-
val ordinalParam = newSymbol(owner, ordinalParamName, flag | Synthetic, defn.IntType, coord = owner.span)
67+
val nameParam = newSymbol(owner, nameParamName, flag | Synthetic | Private | Local, defn.StringType, coord = owner.span)
68+
val ordinalParam = newSymbol(owner, ordinalParamName, flag | Synthetic | Private | Local, defn.IntType, coord = owner.span)
6869
List(ValDef(nameParam), ValDef(ordinalParam))
6970
}
7071

@@ -146,7 +147,11 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
146147
}.head
147148
val args = List(rhsOf(nme.toString_), rhsOf(nme.ordinalDollar))
148149
cpy.Template(templ)(
149-
parents = addEnumConstrArgs(cls.owner.owner.linkedClass, templ.parents, args))
150+
parents = addEnumConstrArgs(cls.owner.owner.linkedClass, templ.parents, args),
151+
body = templ.body.filterNot {
152+
case ddef: NamedDefTree => ddef.name == nme.toString_ || ddef.name == nme.ordinalDollar
153+
case _ => false
154+
})
150155
}
151156
else templ
152157
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
5757
private var myValueSymbols: List[Symbol] = Nil
5858
private var myCaseSymbols: List[Symbol] = Nil
5959
private var myCaseModuleSymbols: List[Symbol] = Nil
60-
private var myEnumCaseSymbols: List[Symbol] = Nil
6160

6261
private def initSymbols(using Context) =
6362
if (myValueSymbols.isEmpty) {
@@ -66,13 +65,11 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
6665
defn.Product_productArity, defn.Product_productPrefix, defn.Product_productElement,
6766
defn.Product_productElementName)
6867
myCaseModuleSymbols = myCaseSymbols.filter(_ ne defn.Any_equals)
69-
myEnumCaseSymbols = List(defn.Enum_ordinal)
7068
}
7169

7270
def valueSymbols(using Context): List[Symbol] = { initSymbols; myValueSymbols }
7371
def caseSymbols(using Context): List[Symbol] = { initSymbols; myCaseSymbols }
7472
def caseModuleSymbols(using Context): List[Symbol] = { initSymbols; myCaseModuleSymbols }
75-
def enumCaseSymbols(using Context): List[Symbol] = { initSymbols; myEnumCaseSymbols }
7673

7774
private def existingDef(sym: Symbol, clazz: ClassSymbol)(using Context): Symbol = {
7875
val existing = sym.matchingMember(clazz.thisType)
@@ -96,9 +93,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
9693
val symbolsToSynthesize: List[Symbol] =
9794
if (clazz.is(Case))
9895
if (clazz.is(Module)) caseModuleSymbols
99-
else if (isEnumCase) caseSymbols ++ enumCaseSymbols
10096
else caseSymbols
101-
else if (isEnumCase) enumCaseSymbols
10297
else if (isDerivedValueClass(clazz)) valueSymbols
10398
else Nil
10499

@@ -128,7 +123,6 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
128123
case nme.productPrefix => ownName
129124
case nme.productElement => productElementBody(accessors.length, vrefss.head.head)
130125
case nme.productElementName => productElementNameBody(accessors.length, vrefss.head.head)
131-
case nme.ordinal => Select(This(clazz), nme.ordinalDollar)
132126
}
133127
report.log(s"adding $synthetic to $clazz at ${ctx.phase}")
134128
synthesizeDef(synthetic, syntheticRHS)

docs/docs/reference/enums/desugarEnums.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ map into `case class`es or `val`s.
3636
```
3737
expands to a `sealed abstract` class that extends the `scala.Enum` trait and
3838
an associated companion object that contains the defined cases, expanded according
39-
to rules (2 - 8). The enum trait starts with a compiler-generated import that imports
40-
the names `<caseIds>` of all cases so that they can be used without prefix in the trait.
39+
to rules (2 - 8). The enum class starts with a compiler-generated import that imports
40+
the names `<caseIds>` of all cases so that they can be used without prefix in the class.
4141
```scala
4242
sealed abstract class E ... extends <parents> with scala.Enum {
4343
import E.{ <caseIds> }

library/src-bootstrapped/scala/Enum.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,3 @@ trait Enum extends Product, Serializable:
55

66
/** A number uniquely identifying a case of an enum */
77
def ordinal: Int
8-
protected def $ordinal: Int
9-

library/src-non-bootstrapped/scala/Enum.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package scala
22

33
/** A base trait of all enum classes */
4-
trait Enum:
4+
trait Enum extends Product, Serializable:
55

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

0 commit comments

Comments
 (0)