From 52521ea6070d907f77ceab0692f804f52c29ffca Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 5 Apr 2017 22:45:23 +0200 Subject: Add child annotations for enum values A new kind of child annotation that points to the term symbol representing an enum value. --- .../dotty/tools/dotc/transform/IsInstanceOfEvaluator.scala | 3 +++ compiler/src/dotty/tools/dotc/transform/PostTyper.scala | 11 +++++++---- tests/run/enum-Color.scala | 4 ++++ 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 => + } } } -- cgit v1.2.3