Skip to content

Commit 797f5f6

Browse files
committed
Use attachment to mark singleton cases
1 parent a8cc076 commit 797f5f6

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ object DesugarEnums {
2222
/** Attachment containing the number of enum cases and the smallest kind that was seen so far. */
2323
val EnumCaseCount: Property.Key[(Int, DesugarEnums.CaseKind.Value)] = new Property.Key
2424

25+
/** Attachment marking an anonymous class as a singleton case. */
26+
val SingletonCase: Property.StickyKey[Unit] = new Property.StickyKey
27+
2528
/** The enumeration class that belongs to an enum case. This works no matter
2629
* whether the case is still in the enum class or it has been transferred to the
2730
* companion object.
@@ -115,13 +118,13 @@ object DesugarEnums {
115118
val toStringDef =
116119
DefDef(nme.toString_, Nil, Nil, TypeTree(), Ident(nme.name))
117120
.withFlags(Override)
118-
def creator = New(Template(
121+
val creator = New(Template(
119122
constr = emptyConstructor,
120123
parents = enumClassRef :: Nil,
121124
derived = Nil,
122125
self = EmptyValDef,
123126
body = List(enumTagDef, toStringDef) ++ registerCall
124-
))
127+
).withAttachment(SingletonCase, ()))
125128
DefDef(nme.DOLLAR_NEW, Nil,
126129
List(List(param(nme.tag, defn.IntType), param(nme.name, defn.StringType))),
127130
TypeTree(), creator).withFlags(Private | Synthetic)
@@ -264,6 +267,7 @@ object DesugarEnums {
264267
.withFlags(Override)
265268
val (tagMeth, scaffolding) = enumTagMeth(CaseKind.Object)
266269
val impl1 = cpy.Template(impl)(body = List(tagMeth, toStringMeth) ++ registerCall)
270+
.withAttachment(SingletonCase, ())
267271
val vdef = ValDef(name, TypeTree(), New(impl1)).withMods(mods | Final)
268272
flatTree(scaffolding ::: vdef :: Nil).withSpan(span)
269273
}

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import NameOps._
1010
import Annotations.Annotation
1111
import typer.ProtoTypes.constrained
1212
import ast.untpd
13+
import ast.DesugarEnums.SingletonCase
1314
import ValueClasses.isDerivedValueClass
1415
import SymUtils._
1516
import config.Printers.derive
@@ -423,9 +424,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
423424
else if (linked.is(Sealed))
424425
derive.println(i"$linked is not a sum because ${linked.whyNotGenericSum}")
425426
}
426-
else if (clazz.isAnonymousClass &&
427-
(clazz.owner.is(EnumCaseVal) ||
428-
clazz.owner.name == nme.DOLLAR_NEW && clazz.owner.is(Synthetic)))
427+
else if (impl.removeAttachment(SingletonCase).isDefined)
429428
addParent(defn.Mirror_SingletonType)
430429
cpy.Template(impl)(parents = newParents, body = newBody)
431430
}

0 commit comments

Comments
 (0)