Skip to content

Commit fb35bd3

Browse files
Name removed from enum
1 parent 9973e8d commit fb35bd3

File tree

11 files changed

+70
-86
lines changed

11 files changed

+70
-86
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,7 @@ object desugar {
545545
yield syntheticProperty(nme.selectorName(i), caseParams(i).tpt,
546546
Select(This(EmptyTypeIdent), caseParams(i).name))
547547
}
548-
def ordinalMeths =
549-
if (isEnumCase)
550-
ordinalMethLit(nextOrdinal(CaseKind.Class)._1) ::
551-
nameMethLit(className.toString) :: Nil
552-
else Nil
548+
def ordinalMeths = if (isEnumCase) ordinalMethLit(nextOrdinal(CaseKind.Class)._1) :: Nil else Nil
553549
def copyMeths = {
554550
val hasRepeatedParam = constrVparamss.exists(_.exists {
555551
case ValDef(_, tpt, _) => isRepeated(tpt)

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

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ object DesugarEnums {
8787
/** The following lists of definitions for an enum type E:
8888
*
8989
* private val $values = new EnumValues[E]
90-
* def valueOf(name: String) =
91-
* try $values.fromName(name) catch
90+
* def valueOf($name: String) =
91+
* try $values.fromName($name) catch
9292
* {
9393
* case ex$:NoSuchElementException =>
9494
* throw new IllegalArgumentException("key not found: ".concat(name))
@@ -105,17 +105,17 @@ object DesugarEnums {
105105

106106
val valuesOfExnMessage = Apply(
107107
Select(Literal(Constant("key not found: ")), "concat".toTermName)
108-
, Ident(nme.name) :: Nil)
108+
, Ident(nme.nameDollar) :: Nil)
109109
val valuesOfBody = Try(
110-
expr = Apply(valuesDot("fromName"), Ident(nme.name) :: Nil)
110+
expr = Apply(valuesDot("fromName"), Ident(nme.nameDollar) :: Nil)
111111
, cases = CaseDef(
112112
pat = Typed(Ident(nme.DEFAULT_EXCEPTION_NAME), TypeTree(defn.NoSuchElementExceptionType))
113113
, guard = EmptyTree
114114
, body = Throw(New(TypeTree(defn.IllegalArgumentExceptionType), List(valuesOfExnMessage :: Nil)))
115115
) :: Nil
116116
, finalizer = EmptyTree
117117
)
118-
val valueOfDef = DefDef(nme.valueOf, Nil, List(param(nme.name, defn.StringType) :: Nil),
118+
val valueOfDef = DefDef(nme.valueOf, Nil, List(param(nme.nameDollar, defn.StringType) :: Nil),
119119
TypeTree(), valuesOfBody)
120120

121121
valuesDef ::
@@ -125,25 +125,24 @@ object DesugarEnums {
125125

126126
/** A creation method for a value of enum type `E`, which is defined as follows:
127127
*
128-
* private def $new(tag: Int, name_: String) = new E {
129-
* override def ordinal = tag
130-
* override def name = name_
131-
* override def toString = name
128+
* private def $new($tag: Int, $name: String) = new E {
129+
* override def ordinal = $tag
130+
* override def toString = $name
132131
* $values.register(this)
133132
* }
134133
*/
135134
private def enumValueCreator(implicit ctx: Context) = {
136-
val ordinalDef = ordinalMeth(Ident(nme.tag))
137-
val nameDef = nameMeth(Ident(nme.name_))
135+
val ordinalDef = ordinalMeth(Ident(nme.tagDollar))
136+
val toStringDef = toStringMeth(Ident(nme.nameDollar))
138137
val creator = New(Template(
139138
constr = emptyConstructor,
140139
parents = enumClassRef :: Nil,
141140
derived = Nil,
142141
self = EmptyValDef,
143-
body = List(ordinalDef, nameDef, toStringMethAsName) ++ registerCall
142+
body = List(ordinalDef, toStringDef) ++ registerCall
144143
).withAttachment(ExtendsSingletonMirror, ()))
145144
DefDef(nme.DOLLAR_NEW, Nil,
146-
List(List(param(nme.tag, defn.IntType), param(nme.name_, defn.StringType))),
145+
List(List(param(nme.tagDollar, defn.IntType), param(nme.nameDollar, defn.StringType))),
147146
TypeTree(), creator).withFlags(Private | Synthetic)
148147
}
149148

@@ -269,20 +268,14 @@ object DesugarEnums {
269268
def ordinalMeth(body: Tree)(implicit ctx: Context): DefDef =
270269
DefDef(nme.ordinal, Nil, Nil, TypeTree(defn.IntType), body).withFlags(Override)
271270

272-
def nameMeth(body: Tree)(implicit ctx: Context): DefDef =
273-
DefDef(nme.name, Nil, Nil, TypeTree(defn.StringType), body).withFlags(Override)
274-
275271
def toStringMeth(body: Tree)(implicit ctx: Context): DefDef =
276272
DefDef(nme.toString_, Nil, Nil, TypeTree(defn.StringType), body).withFlags(Override)
277273

278274
def ordinalMethLit(ord: Int)(implicit ctx: Context): DefDef =
279275
ordinalMeth(Literal(Constant(ord)))
280276

281-
def nameMethLit(name: String)(implicit ctx: Context): DefDef =
282-
nameMeth(Literal(Constant(name)))
283-
284-
def toStringMethAsName(implicit ctx: Context): DefDef =
285-
toStringMeth(Ident(nme.name))
277+
def toStringMethLit(name: String)(implicit ctx: Context): DefDef =
278+
toStringMeth(Literal(Constant(name)))
286279

287280
/** Expand a module definition representing a parameterless enum case */
288281
def expandEnumModule(name: TermName, impl: Template, mods: Modifiers, span: Span)(implicit ctx: Context): Tree = {
@@ -293,8 +286,8 @@ object DesugarEnums {
293286
else {
294287
val (tag, scaffolding) = nextOrdinal(CaseKind.Object)
295288
val ordinalDef = ordinalMethLit(tag)
296-
val nameDef = nameMethLit(name.toString)
297-
val impl1 = cpy.Template(impl)(body = List(ordinalDef, nameDef, toStringMethAsName) ++ registerCall)
289+
val toStringDef = toStringMethLit(name.toString)
290+
val impl1 = cpy.Template(impl)(body = List(ordinalDef, toStringDef) ++ registerCall)
298291
.withAttachment(ExtendsSingletonMirror, ())
299292
val vdef = ValDef(name, TypeTree(), New(impl1)).withMods(mods | Final)
300293
flatTree(scaffolding ::: vdef :: Nil).withSpan(span)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ object StdNames {
481481
val mirror : N = "mirror"
482482
val moduleClass : N = "moduleClass"
483483
val name: N = "name"
484-
val name_ : N = "name_"
484+
val nameDollar: N = "$name"
485485
val ne: N = "ne"
486486
val newFreeTerm: N = "newFreeTerm"
487487
val newFreeType: N = "newFreeType"
@@ -536,6 +536,7 @@ object StdNames {
536536
val strictEquality: N = "strictEquality"
537537
val synchronized_ : N = "synchronized"
538538
val tag: N = "tag"
539+
val tagDollar: N = "$tag"
539540
val tail: N = "tail"
540541
val `then` : N = "then"
541542
val this_ : N = "this"

library/src/scala/Enum.scala

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

66
/** A number uniquely identifying a case of an enum */
77
def ordinal: Int
8-
9-
/** Name of the enum's case */
10-
def name: String
118
}

tests/pos-with-compiler/tasty/definitions.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ object definitions {
6565
/** Trees denoting terms */
6666
enum Term extends Statement {
6767
def tpe: Type = ???
68-
case Ident(nme: String, override val tpe: Type)
69-
case Select(prefix: Term, nme: String, signature: Option[Signature])
68+
case Ident(name: String, override val tpe: Type)
69+
case Select(prefix: Term, name: String, signature: Option[Signature])
7070
case Literal(value: Constant)
7171
case This(id: Option[Id])
7272
case New(tpt: TypeTree)
7373
case Throw(expr: Term)
74-
case NamedArg(nme: String, arg: Term)
74+
case NamedArg(name: String, arg: Term)
7575
case Apply(fn: Term, args: List[Term])
7676
case TypeApply(fn: Term, args: List[TypeTree])
7777
case Super(thiz: Term, mixin: Option[Id])
@@ -94,9 +94,9 @@ object definitions {
9494
enum TypeTree extends Positioned {
9595
def tpe: Type = ???
9696
case Synthetic()
97-
case Ident(nme: String, override val tpe: Type)
98-
case TermSelect(prefix: Term, nme: String)
99-
case TypeSelect(prefix: TypeTree, nme: String)
97+
case Ident(name: String, override val tpe: Type)
98+
case TermSelect(prefix: Term, name: String)
99+
case TypeSelect(prefix: TypeTree, name: String)
100100
case Singleton(ref: Term)
101101
case Refined(underlying: TypeTree, refinements: List[Definition])
102102
case Applied(tycon: TypeTree, args: List[TypeTree | TypeBoundsTree])
@@ -105,7 +105,7 @@ object definitions {
105105
case Or(left: TypeTree, right: TypeTree)
106106
case ByName(tpt: TypeTree)
107107
case TypeLambda(tparams: List[TypeDef], body: Type | TypeBoundsTree)
108-
case Bind(nme: String, bounds: TypeBoundsTree)
108+
case Bind(name: String, bounds: TypeBoundsTree)
109109
}
110110

111111
/** Trees denoting type bounds */
@@ -122,7 +122,7 @@ object definitions {
122122
enum Pattern extends Positioned {
123123
def tpe: Type = ???
124124
case Value(v: Term)
125-
case Bind(nme: String, pat: Pattern)
125+
case Bind(name: String, pat: Pattern)
126126
case Unapply(unapply: Term, implicits: List[Term], pats: List[Pattern])
127127
case Alternative(pats: List[Pattern])
128128
case TypeTest(tpt: TypeTree)

tests/pos/enum-List-control.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ abstract sealed class List[T] extends Enum
22
object List {
33
final class Cons[T](x: T, xs: List[T]) extends List[T] {
44
def ordinal = 0
5-
def name = "Cons"
65
}
76
object Cons {
87
def apply[T](x: T, xs: List[T]): List[T] = new Cons(x, xs)
98
}
109
final class Nil[T]() extends List[T] {
1110
def ordinal = 1
12-
def name = "Nil"
1311
}
1412
object Nil {
1513
def apply[T](): List[T] = new Nil()

tests/run/enum-Color.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ enum Color {
66
object Test {
77
def main(args: Array[String]) =
88
for (color <- Color.values) {
9-
println(s"$color: ${color.name}")
10-
assert(Color.valueOf(color.name) eq color)
9+
println(s"$color: ${color.toString}")
10+
assert(Color.valueOf(color.toString) eq color)
1111
import Color._
1212
color match {
1313
case Red | Green | Blue =>

tests/run/enums-java-compat.check

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
name: EARTH
21
ordinal: 0
32
toString: EARTH
43
Values class: class [LA;

tests/run/enums-java-compat.scala

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class JEnum {
2+
def name: String = "Foo"
3+
def ordinal: Int = 10
4+
def action = "fofofo"
5+
}
6+
7+
enum A extends JEnum {
8+
case MONDAY, TUESDAY, SATURDAY
9+
case Stuff
10+
case Someday(x: String)
11+
def report = "Reported"
12+
}
13+
14+
trait Foo1
15+
trait Bar
16+
17+
enum B(val gravity: Double)(val isItGood: Boolean) extends Foo1 {
18+
case EARTH extends B(9.8)(true)
19+
case JUPITER extends B(100)(true)
20+
case MOON extends B(4.3)(true)
21+
case Foo extends B(10)(true) with Bar
22+
}
23+
24+
object Test {
25+
def main(args: Array[String]): Unit = {
26+
val t1 = B.EARTH
27+
val t2 = B.JUPITER
28+
29+
println("ordinal: " + t1.ordinal)
30+
println("toString: " + t1.toString)
31+
32+
val values: Array[A] = A.values
33+
println("Values class: " + values.getClass)
34+
values.foreach(v => println(v.toString + " : " + v.ordinal))
35+
println("By-name value: " + A.valueOf("MONDAY"))
36+
try A.valueOf("stuff")
37+
catch { case e: IllegalArgumentException =>
38+
println("Correctly failed to retrieve illegal name, message: " + e.getMessage)
39+
}
40+
}
41+
}

tests/run/enums-java-compat/Enums.scala

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/run/enums-java-compat/Test.scala

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)