Skip to content

Commit 3204c29

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 c5d46e2 commit 3204c29

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
@@ -791,10 +791,14 @@ class Namer { typer: Typer =>
791791
lazy val annotCtx = annotContext(original, sym)
792792
for (annotTree <- untpd.modsDeco(original).mods.annotations) {
793793
val cls = typedAheadAnnotationClass(annotTree)(annotCtx)
794-
val ann = Annotation.deferred(cls, implicit ctx => typedAnnotation(annotTree))
795-
sym.addAnnotation(ann)
796-
if (cls == defn.ForceInlineAnnot && sym.is(Method, butNot = Accessor))
797-
sym.setFlag(Inline)
794+
if (cls eq sym)
795+
ctx.error("An annotation class cannot be annotated with iself", annotTree.sourcePos)
796+
else {
797+
val ann = Annotation.deferred(cls, implicit ctx => typedAnnotation(annotTree))
798+
sym.addAnnotation(ann)
799+
if (cls == defn.ForceInlineAnnot && sym.is(Method, butNot = Accessor))
800+
sym.setFlag(Inline)
801+
}
798802
}
799803
case _ =>
800804
}

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)