Skip to content

Commit 30317cd

Browse files
authored
Add migration rewrite for non-named arguments in Java annotations (#21397)
Followup to #21329 It might be beneficial for Scala 3.6 migration to allow for an automatic rewrite of unnamed Java annotation parameters to ease the migration. We can assume that already compiling code is already using correct positions for annotation arguments. Based on that, we can use the order of the annotation decls and their indexes to assume the names of annotation arguments
2 parents b64afad + 90eedc5 commit 30317cd

File tree

9 files changed

+42
-2
lines changed

9 files changed

+42
-2
lines changed

compiler/src/dotty/tools/dotc/config/MigrationVersion.scala

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ object MigrationVersion:
4343
val WithOperator = MigrationVersion(`3.4`, future)
4444
val FunctionUnderscore = MigrationVersion(`3.4`, future)
4545

46+
val NonNamedArgumentInJavaAnnotation = MigrationVersion(`3.6`, `3.6`)
47+
4648
val ImportWildcard = MigrationVersion(future, future)
4749
val ImportRename = MigrationVersion(future, future)
4850
val ParameterEnclosedByParenthesis = MigrationVersion(future, future)

compiler/src/dotty/tools/dotc/reporting/messages.scala

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import dotty.tools.dotc.util.Spans.Span
3535
import dotty.tools.dotc.util.SourcePosition
3636
import scala.jdk.CollectionConverters.*
3737
import dotty.tools.dotc.util.SourceFile
38+
import dotty.tools.dotc.config.SourceVersion
3839
import DidYouMean.*
3940

4041
/** Messages
@@ -3293,6 +3294,7 @@ class NonNamedArgumentInJavaAnnotation(using Context) extends SyntaxMsg(NonNamed
32933294

32943295
override protected def msg(using Context): String =
32953296
"Named arguments are required for Java defined annotations"
3297+
+ Message.rewriteNotice("This", version = SourceVersion.`3.6-migration`)
32963298

32973299
override protected def explain(using Context): String =
32983300
i"""Starting from Scala 3.6.0, named arguments are required for Java defined annotations.

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -891,14 +891,26 @@ object Checking {
891891
def annotationHasValueField: Boolean =
892892
sym.info.decls.exists(_.name == nme.value)
893893

894+
lazy val annotationFieldNamesByIdx: Map[Int, TermName] =
895+
sym.info.decls.filter: decl =>
896+
decl.is(Method) && decl.name != nme.CONSTRUCTOR
897+
.map(_.name.toTermName)
898+
.zipWithIndex
899+
.map(_.swap)
900+
.toMap
901+
894902
annot match
895903
case untpd.Apply(fun, List(param)) if !param.isInstanceOf[untpd.NamedArg] && annotationHasValueField =>
896904
untpd.cpy.Apply(annot)(fun, List(untpd.cpy.NamedArg(param)(nme.value, param)))
897905
case untpd.Apply(_, params) =>
898906
for
899-
param <- params
907+
(param, paramIdx) <- params.zipWithIndex
900908
if !param.isInstanceOf[untpd.NamedArg]
901-
do report.error(NonNamedArgumentInJavaAnnotation(), param)
909+
do
910+
report.errorOrMigrationWarning(NonNamedArgumentInJavaAnnotation(), param, MigrationVersion.NonNamedArgumentInJavaAnnotation)
911+
if MigrationVersion.NonNamedArgumentInJavaAnnotation.needsPatch then
912+
annotationFieldNamesByIdx.get(paramIdx).foreach: paramName =>
913+
patch(param.span.startPos, s"$paramName = ")
902914
annot
903915
case _ => annot
904916
end checkNamedArgumentForJavaAnnotation

compiler/test/dotty/tools/dotc/CompilationTests.scala

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class CompilationTests {
7676
compileFile("tests/rewrites/i17187.scala", unindentOptions.and("-rewrite")),
7777
compileFile("tests/rewrites/i17399.scala", unindentOptions.and("-rewrite")),
7878
compileFile("tests/rewrites/i20002.scala", defaultOptions.and("-indent", "-rewrite")),
79+
compileDir("tests/rewrites/annotation-named-pararamters", defaultOptions.and("-rewrite", "-source:3.6-migration")),
7980
).checkRewrites()
8081
}
8182

tests/neg/i20554-a.check

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
3 |@Annotation(3, 4) // error // error : Java defined annotation should be called with named arguments
33
| ^
44
| Named arguments are required for Java defined annotations
5+
| This can be rewritten automatically under -rewrite -source 3.6-migration.
56
|---------------------------------------------------------------------------------------------------------------------
67
| Explanation (enabled by `-explain`)
78
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -23,6 +24,7 @@
2324
3 |@Annotation(3, 4) // error // error : Java defined annotation should be called with named arguments
2425
| ^
2526
| Named arguments are required for Java defined annotations
27+
| This can be rewritten automatically under -rewrite -source 3.6-migration.
2628
|---------------------------------------------------------------------------------------------------------------------
2729
| Explanation (enabled by `-explain`)
2830
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

tests/neg/i20554-b.check

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
3 |@SimpleAnnotation(1) // error: the parameters is not named 'value'
33
| ^
44
| Named arguments are required for Java defined annotations
5+
| This can be rewritten automatically under -rewrite -source 3.6-migration.
56
|---------------------------------------------------------------------------------------------------------------------
67
| Explanation (enabled by `-explain`)
78
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import java.util.concurrent.TimeUnit;
2+
3+
public @interface MyAnnotation {
4+
public TimeUnit D() default TimeUnit.DAYS;
5+
TimeUnit C() default TimeUnit.DAYS;
6+
String A() default "";
7+
public String B() default "";
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import java.util.concurrent.TimeUnit
2+
@MyAnnotation() class Test1
3+
@MyAnnotation(D = TimeUnit.DAYS) class Test2
4+
@MyAnnotation(D = TimeUnit.DAYS, C = TimeUnit.DAYS) class Test3
5+
@MyAnnotation(D = TimeUnit.DAYS, C = TimeUnit.DAYS, A = "foo") class Test4
6+
@MyAnnotation(D = TimeUnit.DAYS, C = TimeUnit.DAYS, A = "foo", B = "bar") class Test5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import java.util.concurrent.TimeUnit
2+
@MyAnnotation() class Test1
3+
@MyAnnotation(TimeUnit.DAYS) class Test2
4+
@MyAnnotation(TimeUnit.DAYS, TimeUnit.DAYS) class Test3
5+
@MyAnnotation(TimeUnit.DAYS, TimeUnit.DAYS, "foo") class Test4
6+
@MyAnnotation(TimeUnit.DAYS, TimeUnit.DAYS, "foo", "bar") class Test5

0 commit comments

Comments
 (0)