Skip to content

Commit 8aeaa8a

Browse files
method added to enums to carry the ordinal number unconditionally
1 parent 0a0fa3e commit 8aeaa8a

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ object DesugarEnums {
132132
* }
133133
*/
134134
private def enumValueCreator(implicit ctx: Context) = {
135-
val ordinalDef = ordinalMeth(Ident(nme.tagDollar))
135+
val ordinalDef = ordinalMeth(Ident(nme.ordinalDollar_))
136136
val toStringDef = toStringMeth(Ident(nme.nameDollar))
137137
val creator = New(Template(
138138
constr = emptyConstructor,
@@ -142,9 +142,9 @@ object DesugarEnums {
142142
body = List(ordinalDef, toStringDef) ++ registerCall
143143
).withAttachment(ExtendsSingletonMirror, ()))
144144
DefDef(nme.DOLLAR_NEW, Nil,
145-
List(List(param(nme.tagDollar, defn.IntType), param(nme.nameDollar, defn.StringType))),
145+
List(List(param(nme.nameDollar, defn.StringType), param(nme.ordinalDollar_, defn.IntType))),
146146
TypeTree(), creator).withFlags(Private | Synthetic)
147-
}
147+
}.reporting(e => s"marker\n${e.show}")
148148

149149
/** The return type of an enum case apply method and any widening methods in which
150150
* the apply's right hand side will be wrapped. For parents of the form
@@ -266,7 +266,7 @@ object DesugarEnums {
266266
ValDef(name, TypeTree(typ), EmptyTree).withFlags(Param)
267267

268268
def ordinalMeth(body: Tree)(implicit ctx: Context): DefDef =
269-
DefDef(nme.ordinal, Nil, Nil, TypeTree(defn.IntType), body).withFlags(Override)
269+
DefDef(nme.ordinalDollar, Nil, Nil, TypeTree(defn.IntType), body)
270270

271271
def toStringMeth(body: Tree)(implicit ctx: Context): DefDef =
272272
DefDef(nme.toString_, Nil, Nil, TypeTree(defn.StringType), body).withFlags(Override)
@@ -304,7 +304,7 @@ object DesugarEnums {
304304
}
305305
else {
306306
val (tag, scaffolding) = nextOrdinal(CaseKind.Simple)
307-
val creator = Apply(Ident(nme.DOLLAR_NEW), List(Literal(Constant(tag)), Literal(Constant(name.toString))))
307+
val creator = Apply(Ident(nme.DOLLAR_NEW), List(Literal(Constant(name.toString)), Literal(Constant(tag))))
308308
val vdef = ValDef(name, enumClassRef, creator).withMods(mods | Final)
309309
flatTree(scaffolding ::: vdef :: Nil).withSpan(span)
310310
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,8 @@ object StdNames {
497497
val ofDim: N = "ofDim"
498498
val opaque: N = "opaque"
499499
val ordinal: N = "ordinal"
500+
val ordinalDollar: N = "$ordinal"
501+
val ordinalDollar_ : N = "_$ordinal"
500502
val origin: N = "origin"
501503
val prefix : N = "prefix"
502504
val productArity: N = "productArity"
@@ -536,7 +538,6 @@ object StdNames {
536538
val strictEquality: N = "strictEquality"
537539
val synchronized_ : N = "synchronized"
538540
val tag: N = "tag"
539-
val tagDollar: N = "$tag"
540541
val tail: N = "tail"
541542
val `then` : N = "then"
542543
val this_ : N = "this"

library/src/scala/Enum.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ trait Enum {
55

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

0 commit comments

Comments
 (0)