Skip to content

Commit 52521ea

Browse files
oderskyliufengyun
authored andcommitted
Add child annotations for enum values
A new kind of child annotation that points to the term symbol representing an enum value.
1 parent 09cc237 commit 52521ea

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ class IsInstanceOfEvaluator extends MiniPhaseTransform { thisTransformer =>
147147
(scTrait && selTrait)
148148

149149
val inMatch = s.qualifier.symbol is Case
150+
// FIXME: This will misclassify case objects! We need to find another way to characterize
151+
// isInstanceOfs generated by matches.
152+
// Probably the most robust way is to use another symbol for the isInstanceOf method.
150153

151154
if (valueClassesOrAny) tree
152155
else if (knownStatically)

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,14 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisTran
104104
private def transformAnnot(annot: Annotation)(implicit ctx: Context): Annotation =
105105
annot.derivedAnnotation(transformAnnot(annot.tree))
106106

107+
private def registerChild(sym: Symbol, tp: Type)(implicit ctx: Context) = {
108+
val cls = tp.classSymbol
109+
if (cls.is(Sealed)) cls.addAnnotation(Annotation.makeChild(sym))
110+
}
111+
107112
private def transformMemberDef(tree: MemberDef)(implicit ctx: Context): Unit = {
108113
val sym = tree.symbol
114+
if (sym.is(CaseVal, butNot = Module | Method)) registerChild(sym, sym.info)
109115
sym.transformAnnotations(transformAnnot)
110116
}
111117

@@ -227,10 +233,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisTran
227233

228234
// Add Child annotation to sealed parents unless current class is anonymous
229235
if (!sym.isAnonymousClass) // ignore anonymous class
230-
for (parent <- sym.asClass.classInfo.classParents) {
231-
val pclazz = parent.classSymbol
232-
if (pclazz.is(Sealed)) pclazz.addAnnotation(Annotation.makeChild(sym))
233-
}
236+
sym.asClass.classInfo.classParents.foreach(registerChild(sym, _))
234237

235238
tree
236239
}

tests/run/enum-Color.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@ object Test {
77
for (color <- Color.enumValues) {
88
println(s"$color: ${color.enumTag}")
99
assert(Color.enumValue(color.enumTag) eq color)
10+
import Color._
11+
color match {
12+
case Red | Green | Blue =>
13+
}
1014
}
1115
}

0 commit comments

Comments
 (0)