Skip to content

Commit 546c59f

Browse files
committed
Fix handling of defaults in Java annotations
- Java-defined annotation constructors need to have JavaDefined set. - Change the points where we suppress lifting. One case slipped through before. - Also: Add test.
1 parent a1b6c3e commit 546c59f

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ class ClassfileParser(
637637
val constr = ctx.newSymbol(
638638
owner = classRoot.symbol,
639639
name = nme.CONSTRUCTOR,
640-
flags = Flags.Synthetic,
640+
flags = Flags.Synthetic | Flags.JavaDefined,
641641
info = constrType
642642
).entered
643643
for ((attr, i) <- attrs.zipWithIndex)

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,10 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
395395
}
396396
}
397397

398+
/** Is `sym` a constructor of a Java-defined annotation? */
399+
def isJavaAnnotConstr(sym: Symbol) =
400+
sym.is(JavaDefined) && sym.isConstructor && sym.owner.derivesFrom(defn.AnnotationClass)
401+
398402
/** Match re-ordered arguments against formal parameters
399403
* @param n The position of the first parameter in formals in `methType`.
400404
*/
@@ -420,7 +424,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
420424
}
421425

422426
def tryDefault(n: Int, args1: List[Arg]): Unit = {
423-
liftFun()
427+
if (!isJavaAnnotConstr(methRef.symbol)) liftFun()
424428
val getter = findDefaultGetter(n + numArgs(normalizedFun))
425429
if (getter.isEmpty) missingArg(n)
426430
else {
@@ -575,11 +579,8 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
575579

576580
def normalizedFun = myNormalizedFun
577581

578-
private def isJavaAnnotConstr(sym: Symbol) =
579-
sym.is(JavaDefined) && sym.isConstructor && sym.owner.derivesFrom(defn.AnnotationClass)
580-
581582
override def liftFun(): Unit =
582-
if (liftedDefs == null && !isJavaAnnotConstr(methRef.symbol)) {
583+
if (liftedDefs == null) {
583584
liftedDefs = new mutable.ListBuffer[Tree]
584585
myNormalizedFun = liftApp(liftedDefs, myNormalizedFun)
585586
}
@@ -610,7 +611,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
610611
val app1 =
611612
if (!success) app0.withType(UnspecifiedErrorType)
612613
else {
613-
if (!sameSeq(args, orderedArgs)) {
614+
if (!sameSeq(args, orderedArgs) && !isJavaAnnotConstr(methRef.symbol)) {
614615
// need to lift arguments to maintain evaluation order in the
615616
// presence of argument reorderings.
616617
liftFun()

tests/pos/i2797/Fork.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public @interface Fork {
2+
int value() default -1;
3+
String[] jvmArgs() default { "nope" };
4+
}

tests/pos/i2797/Test.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@Fork(jvmArgs = Array("I'm", "hot"))
2+
class Test

0 commit comments

Comments
 (0)