Skip to content

Commit b264bf0

Browse files
committed
Add explicit check against self-annotated classes
Following the completions-related changes in this PR, we no longer encounter cyclic reference errors when annotating a class with itself (i1212.scala) as well as in some situation where an annotation class indirectly refers to itself (i3506.scala). While the latter is OK, the former is problematic because when unpickling from tasty, the modifiers and annotations of a symbol are read before the symbol itself is created, so we can't support self-annotation classes, we therefore need to disallow them explicitly.
1 parent d9ade19 commit b264bf0

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -792,10 +792,14 @@ class Namer { typer: Typer =>
792792
lazy val annotCtx = annotContext(original, sym)
793793
for (annotTree <- untpd.modsDeco(original).mods.annotations) {
794794
val cls = typedAheadAnnotationClass(annotTree)(annotCtx)
795-
val ann = Annotation.deferred(cls, implicit ctx => typedAnnotation(annotTree))
796-
sym.addAnnotation(ann)
797-
if (cls == defn.ForceInlineAnnot && sym.is(Method, butNot = Accessor))
798-
sym.setFlag(Inline)
795+
if (cls eq sym)
796+
ctx.error("An annotation class cannot be annotated with iself", annotTree.sourcePos)
797+
else {
798+
val ann = Annotation.deferred(cls, implicit ctx => typedAnnotation(annotTree))
799+
sym.addAnnotation(ann)
800+
if (cls == defn.ForceInlineAnnot && sym.is(Method, butNot = Accessor))
801+
sym.setFlag(Inline)
802+
}
799803
}
800804
case _ =>
801805
}

tests/neg/i1212.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
@ann class ann extends scala.annotation.Annotation // error: cyclic reference
1+
@ann class ann extends scala.annotation.Annotation // error: An annotation class cannot be annotated with iself

tests/neg/i3506.scala

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

tests/pos/i3506.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
trait C2[@specialized A]
2+
class specialized extends scala.annotation.StaticAnnotation with C2[Char]

0 commit comments

Comments
 (0)