Skip to content

Commit a4e6cb7

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 7f0cecd commit a4e6cb7

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
@@ -394,6 +394,10 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
394394
}
395395
}
396396

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

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

572576
def normalizedFun = myNormalizedFun
573577

574-
private def isJavaAnnotConstr(sym: Symbol) =
575-
sym.is(JavaDefined) && sym.isConstructor && sym.owner.derivesFrom(defn.AnnotationClass)
576-
577578
override def liftFun(): Unit =
578-
if (liftedDefs == null && !isJavaAnnotConstr(methRef.symbol)) {
579+
if (liftedDefs == null) {
579580
liftedDefs = new mutable.ListBuffer[Tree]
580581
myNormalizedFun = liftApp(liftedDefs, myNormalizedFun)
581582
}
@@ -606,7 +607,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
606607
val app1 =
607608
if (!success) app0.withType(UnspecifiedErrorType)
608609
else {
609-
if (!sameSeq(args, orderedArgs)) {
610+
if (!sameSeq(args, orderedArgs) && !isJavaAnnotConstr(methRef.symbol)) {
610611
// need to lift arguments to maintain evaluation order in the
611612
// presence of argument reorderings.
612613
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)