Skip to content

Commit e18eb0f

Browse files
committed
Fix handling of arrays in java annotation arguments
Previously, the array literal argument `{ "foo", "bar" }` was typed as an `Array["foo" | "bar"]`, which is not a subtype of the corresponding parameter type (`Array[String]`). This lead to a crash when forcing the annotation. This is a serious problem because in Java 9+, JFrame contains such an annotation, therefore this affects basically any code using Swing/AWT. Similary an empty array `{}` was always typed as `Array[Object]` which does not always match the parameter type, this one is trickier to fix since the serialized annotation does not keep track of the element type of an empty array and we don't want to force the serialization at this point, so we use a WildcardType.
1 parent 228e593 commit e18eb0f

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,8 @@ class ClassfileParser(
518518
else {
519519
val elems = arr.toList
520520
val elemType =
521-
if (elems.isEmpty) defn.ObjectType
522-
else ctx.typeComparer.lub(elems.tpes).widen
521+
if (elems.isEmpty) WildcardType // No way to figure out the element type without forcing the annotation constructor
522+
else elems.head.tpe.widen
523523
Some(JavaSeqLiteral(elems, TypeTree(elemType)))
524524
}
525525
case ANNOTATION_TAG =>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public @interface Annot_1 {
2+
String[] values() default {};
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class Class_1 {
2+
@Annot_1(values = { "foo", "bar" })
3+
void foo() {}
4+
5+
@Annot_1(values = {})
6+
void foo2() {}
7+
}
8+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class Test extends Class_1

0 commit comments

Comments
 (0)