aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2017-04-05 22:45:23 +0200
committerliu fengyun <liu@fengy.me>2017-04-06 15:38:04 +0200
commit52521ea6070d907f77ceab0692f804f52c29ffca (patch)
tree3e358f5806fb65423b5e16e9aeabb62bdd721add
parent09cc23726069fa04cbfeec55a9fa4bb8e4a02ff9 (diff)
downloaddotty-52521ea6070d907f77ceab0692f804f52c29ffca.tar.gz
dotty-52521ea6070d907f77ceab0692f804f52c29ffca.tar.bz2
dotty-52521ea6070d907f77ceab0692f804f52c29ffca.zip
Add child annotations for enum values
A new kind of child annotation that points to the term symbol representing an enum value.
-rw-r--r--compiler/src/dotty/tools/dotc/transform/IsInstanceOfEvaluator.scala3
-rw-r--r--compiler/src/dotty/tools/dotc/transform/PostTyper.scala11
-rw-r--r--tests/run/enum-Color.scala4
3 files changed, 14 insertions, 4 deletions
diff --git a/compiler/src/dotty/tools/dotc/transform/IsInstanceOfEvaluator.scala b/compiler/src/dotty/tools/dotc/transform/IsInstanceOfEvaluator.scala
index ebd2ae436..b48d219d6 100644
--- a/compiler/src/dotty/tools/dotc/transform/IsInstanceOfEvaluator.scala
+++ b/compiler/src/dotty/tools/dotc/transform/IsInstanceOfEvaluator.scala
@@ -147,6 +147,9 @@ class IsInstanceOfEvaluator extends MiniPhaseTransform { thisTransformer =>
(scTrait && selTrait)
val inMatch = s.qualifier.symbol is Case
+ // FIXME: This will misclassify case objects! We need to find another way to characterize
+ // isInstanceOfs generated by matches.
+ // Probably the most robust way is to use another symbol for the isInstanceOf method.
if (valueClassesOrAny) tree
else if (knownStatically)
diff --git a/compiler/src/dotty/tools/dotc/transform/PostTyper.scala b/compiler/src/dotty/tools/dotc/transform/PostTyper.scala
index 9821757e8..a0fe55a5b 100644
--- a/compiler/src/dotty/tools/dotc/transform/PostTyper.scala
+++ b/compiler/src/dotty/tools/dotc/transform/PostTyper.scala
@@ -104,8 +104,14 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisTran
private def transformAnnot(annot: Annotation)(implicit ctx: Context): Annotation =
annot.derivedAnnotation(transformAnnot(annot.tree))
+ private def registerChild(sym: Symbol, tp: Type)(implicit ctx: Context) = {
+ val cls = tp.classSymbol
+ if (cls.is(Sealed)) cls.addAnnotation(Annotation.makeChild(sym))
+ }
+
private def transformMemberDef(tree: MemberDef)(implicit ctx: Context): Unit = {
val sym = tree.symbol
+ if (sym.is(CaseVal, butNot = Module | Method)) registerChild(sym, sym.info)
sym.transformAnnotations(transformAnnot)
}
@@ -227,10 +233,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisTran
// Add Child annotation to sealed parents unless current class is anonymous
if (!sym.isAnonymousClass) // ignore anonymous class
- for (parent <- sym.asClass.classInfo.classParents) {
- val pclazz = parent.classSymbol
- if (pclazz.is(Sealed)) pclazz.addAnnotation(Annotation.makeChild(sym))
- }
+ sym.asClass.classInfo.classParents.foreach(registerChild(sym, _))
tree
}
diff --git a/tests/run/enum-Color.scala b/tests/run/enum-Color.scala
index 683d18d9e..f4f6aaef8 100644
--- a/tests/run/enum-Color.scala
+++ b/tests/run/enum-Color.scala
@@ -7,5 +7,9 @@ object Test {
for (color <- Color.enumValues) {
println(s"$color: ${color.enumTag}")
assert(Color.enumValue(color.enumTag) eq color)
+ import Color._
+ color match {
+ case Red | Green | Blue =>
+ }
}
}