Skip to content

Commit 09e39f9

Browse files
committed
Represent Java annotations as interfaces so they can be extended
Previously we treated Java annotations as if they were classes, like Scala annotations. For example, given @interface Ann { int foo(); } we pretended it was defined as: abstract class Ann(foo: Int) extends java.lang.annotation.Annotation { def foo(): Int } We take advantage of this to type annotation trees as if they were new calls, for example `@Ann(1)` is typed as `new Ann(1)`. Pretending that annotations are classes is fine most of the time and matches what Scala 2.12 did, but it's problematic because the JVM treats annotations as interfaces. In practice this was only an issue with code trying to extend Java annotations, which would either be rejected at compile-time or miscompiled before this commit. This commit switches our representation of annotations to be trait-based instead: trait Ann(foo: Int) extends java.lang.annotation.Annotation { def foo(): Int } Classes are then free to extend annotations using the same pattern as in Scala 2.13: class Foo extends Ann {val annotationType = classOf[Retention]; def foo(): Int = 1} Notice that we still pretend these traits have constructors, this lets us type annotation trees in much the same way as before, and crucially it means that macros that depended on the exact tree shape of annotation trees can continue to work, as demonstrated by the annot-java-tree test extracted from wartremover. To prevent miscompilation issues, we disallow passing arguments to the annotation constructor in `extends` clause. The treatment of default arguments to annotations stays unchanged from 85cd1cf. Fixes #5690. Fixes #12840. Fixes #14199.
1 parent 9ff325c commit 09e39f9

File tree

23 files changed

+163
-36
lines changed

23 files changed

+163
-36
lines changed

compiler/src/dotty/tools/dotc/core/classfile/ClassfileConstants.scala

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -353,18 +353,15 @@ object ClassfileConstants {
353353
if (jflag == 0) base else base | translateFlag(jflag)
354354

355355
private def translateFlags(jflags: Int, baseFlags: FlagSet): FlagSet = {
356-
val nflags =
357-
if ((jflags & JAVA_ACC_ANNOTATION) == 0) jflags
358-
else jflags & ~(JAVA_ACC_ABSTRACT | JAVA_ACC_INTERFACE) // annotations are neither abstract nor interfaces
359356
var res: FlagSet = baseFlags | JavaDefined
360-
res = addFlag(res, nflags & JAVA_ACC_PRIVATE)
361-
res = addFlag(res, nflags & JAVA_ACC_PROTECTED)
362-
res = addFlag(res, nflags & JAVA_ACC_FINAL)
363-
res = addFlag(res, nflags & JAVA_ACC_SYNTHETIC)
364-
res = addFlag(res, nflags & JAVA_ACC_STATIC)
365-
res = addFlag(res, nflags & JAVA_ACC_ENUM)
366-
res = addFlag(res, nflags & JAVA_ACC_ABSTRACT)
367-
res = addFlag(res, nflags & JAVA_ACC_INTERFACE)
357+
res = addFlag(res, jflags & JAVA_ACC_PRIVATE)
358+
res = addFlag(res, jflags & JAVA_ACC_PROTECTED)
359+
res = addFlag(res, jflags & JAVA_ACC_FINAL)
360+
res = addFlag(res, jflags & JAVA_ACC_SYNTHETIC)
361+
res = addFlag(res, jflags & JAVA_ACC_STATIC)
362+
res = addFlag(res, jflags & JAVA_ACC_ENUM)
363+
res = addFlag(res, jflags & JAVA_ACC_ABSTRACT)
364+
res = addFlag(res, jflags & JAVA_ACC_INTERFACE)
368365
res
369366
}
370367

compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,7 @@ class ClassfileParser(
165165
* Updates the read pointer of 'in'. */
166166
def parseParents: List[Type] = {
167167
val superType =
168-
if (isAnnotation) {
169-
in.nextChar
170-
defn.ObjectType
171-
}
172-
else if (classRoot.symbol == defn.ComparableClass ||
168+
if (classRoot.symbol == defn.ComparableClass ||
173169
classRoot.symbol == defn.JavaCloneableClass ||
174170
classRoot.symbol == defn.JavaSerializableClass) {
175171
// Treat these interfaces as universal traits
@@ -844,7 +840,7 @@ class ClassfileParser(
844840

845841
class AnnotConstructorCompleter(classInfo: TempClassInfoType) extends LazyType {
846842
def complete(denot: SymDenotation)(using Context): Unit = {
847-
val attrs = classInfo.decls.toList.filter(sym => sym.isTerm && sym != denot.symbol)
843+
val attrs = classInfo.decls.toList.filter(sym => sym.isTerm && sym != denot.symbol && sym.name != nme.CONSTRUCTOR)
848844
val paramNames = attrs.map(_.name.asTermName)
849845
val paramTypes = attrs.map(_.info.resultType)
850846
denot.info = MethodType(paramNames, paramTypes, classRoot.typeRef)

compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ object JavaParsers {
876876
List(constructorParams), TypeTree(), EmptyTree).withMods(Modifiers(Flags.JavaDefined))
877877
val templ = makeTemplate(annotationParents, constr :: body, List(), true)
878878
val annot = atSpan(start, nameOffset) {
879-
TypeDef(name, templ).withMods(mods | Flags.Abstract)
879+
TypeDef(name, templ).withMods(mods | Flags.JavaInterface)
880880
}
881881
addCompanionObject(statics, annot)
882882
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,8 @@ trait Checking {
11091109
def checkParentCall(call: Tree, caller: ClassSymbol)(using Context): Unit =
11101110
if (!ctx.isAfterTyper) {
11111111
val called = call.tpe.classSymbol
1112+
if (called.derivesFrom(defn.JavaAnnotationClass))
1113+
report.error(i"${called.name} must appear without any argument to be a valid class parent because it is a Java annotation", call.srcPos)
11121114
if (caller.is(Trait))
11131115
report.error(i"$caller may not call constructor of $called", call.srcPos)
11141116
else if (called.is(Trait) && !caller.mixins.contains(called))

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,6 +2595,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
25952595
*/
25962596
def ensureConstrCall(cls: ClassSymbol, parent: Tree, psym: Symbol)(using Context): Tree =
25972597
if parent.isType && !cls.is(Trait) && !cls.is(JavaDefined) && psym.isClass
2598+
// Annotations are represented as traits with constructors, but should
2599+
// never be called as such outside of annotation trees.
2600+
&& !psym.derivesFrom(defn.JavaAnnotationClass)
25982601
&& (!psym.is(Trait)
25992602
|| psym.primaryConstructor.info.takesParams && !cls.superClass.isSubClass(psym))
26002603
then typed(untpd.New(untpd.TypedSplice(parent), Nil))
File renamed without changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public @interface Ann {
2+
int value();
3+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Bar extends Ann(1) { // error
2+
def value = 1
3+
def annotationType = classOf[Ann]
4+
}
5+
6+
def test =
7+
// Typer errors
8+
new Ann // error
9+
new Ann(1) {} // error
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public @interface Ann {
2+
int value();
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def test =
2+
// Posttyper errors
3+
new Ann(1) // error

0 commit comments

Comments
 (0)