diff --git a/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala b/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala index aa1fbf371fec..54f5de520a6b 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala @@ -668,6 +668,7 @@ class TreeUnpickler(reader: TastyReader, else newSymbol(ctx.owner, name, flags, completer, privateWithin, coord) } + registerSym(start, sym) val annotOwner = if sym.owner.isClass then newLocalDummy(sym.owner) else sym.owner sym.annotations = annotFns.map(_(annotOwner)) @@ -685,7 +686,6 @@ class TreeUnpickler(reader: TastyReader, cls.enter(sym) case _ => } - registerSym(start, sym) if (isClass) { if sym.owner.is(Package) && withCaptureChecks then sym.setFlag(CaptureChecked) @@ -768,7 +768,10 @@ class TreeUnpickler(reader: TastyReader, addFlag(Protected) privateWithin = readWithin case ANNOTATION => - annotFns = readAnnot :: annotFns + val annotFn = + val annot = readAnnot + (sym: Symbol) => annot.complete(sym) + annotFns = annotFn :: annotFns case tag => assert(false, s"illegal modifier tag $tag at $currentAddr, end = $end") } @@ -778,21 +781,23 @@ class TreeUnpickler(reader: TastyReader, private def readWithin(using Context): Symbol = readType().typeSymbol - private def readAnnot(using Context): Symbol => Annotation = + private def readAnnot(using Context): Trees.Lazy[Symbol => Annotation] = readByte() val end = readEnd() - val tp = readType() - val lazyAnnotTree = readLaterWithOwner(end, _.readTree()) - owner => - new DeferredSymAndTree(tp.typeSymbol, lazyAnnotTree(owner).complete): - // Only force computation of symbol if it has the right name. This added - // amount of laziness is sometimes necessary to avid cycles. Test case pos/i15980. - override def hasSymbol(sym: Symbol)(using Context) = tp match - case tp: TypeRef => - tp.designator match - case name: Name => name == sym.name && tp.symbol == sym - case _ => tp.symbol == sym - case _ => this.symbol == sym + readLater(end, reader => + val tp = reader.readType() + val lazyAnnotTree = reader.readLaterWithOwner(end, _.readTree()) + owner => + new DeferredSymAndTree(tp.typeSymbol, lazyAnnotTree(owner).complete): + // Only force computation of symbol if it has the right name. This added + // amount of laziness is sometimes necessary to avoid cycles. Test case pos/i15980. + override def hasSymbol(sym: Symbol)(using Context) = tp match + case tp: TypeRef => + tp.designator match + case name: Name => name == sym.name && tp.symbol == sym + case _ => tp.symbol == sym + case _ => this.symbol == sym + ) /** Create symbols for the definitions in the statement sequence between * current address and `end`. diff --git a/compiler/src/dotty/tools/dotc/typer/Namer.scala b/compiler/src/dotty/tools/dotc/typer/Namer.scala index 02a55be9ea5a..2f8182dd7da0 100644 --- a/compiler/src/dotty/tools/dotc/typer/Namer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Namer.scala @@ -896,16 +896,12 @@ class Namer { typer: Typer => original.mods.withAnnotations: original.mods.annotations.mapConserve: annotTree => val cls = typedAheadAnnotationClass(annotTree)(using annotCtx) - if (cls eq sym) - report.error(em"An annotation class cannot be annotated with iself", annotTree.srcPos) - annotTree - else - val ann = - if cls.is(JavaDefined) then Checking.checkNamedArgumentForJavaAnnotation(annotTree, cls.asClass) - else annotTree - val ann1 = Annotation.deferred(cls)(typedAheadExpr(ann)(using annotCtx)) - sym.addAnnotation(ann1) - ann + val ann = + if cls.is(JavaDefined) then Checking.checkNamedArgumentForJavaAnnotation(annotTree, cls.asClass) + else annotTree + val ann1 = Annotation.deferred(cls)(typedAheadExpr(ann)(using annotCtx)) + sym.addAnnotation(ann1) + ann case _ => } diff --git a/tests/neg/i1212.scala b/tests/neg/i1212.scala deleted file mode 100644 index e2c98219c390..000000000000 --- a/tests/neg/i1212.scala +++ /dev/null @@ -1 +0,0 @@ -@ann class ann extends scala.annotation.Annotation // error: An annotation class cannot be annotated with iself diff --git a/tests/pos/i1212.scala b/tests/pos/i1212.scala new file mode 100644 index 000000000000..b975afe37015 --- /dev/null +++ b/tests/pos/i1212.scala @@ -0,0 +1 @@ +@ann class ann extends scala.annotation.Annotation