Skip to content

Parse Java annotations on array dimensions (Scala 3 backport)#11251

Open
mkurz wants to merge 1 commit into
scala:2.13.xfrom
mkurz:backport-java-array-type-annotations
Open

Parse Java annotations on array dimensions (Scala 3 backport)#11251
mkurz wants to merge 1 commit into
scala:2.13.xfrom
mkurz:backport-java-array-type-annotations

Conversation

@mkurz
Copy link
Copy Markdown
Contributor

@mkurz mkurz commented May 29, 2026

Backport the Scala 3 Java parser fix for annotations that appear before array brackets, such as String @Nullable [] and nested annotated arrays.

Scala 3 fixed the same parser gap in scala/scala3#22391. This applies the corresponding optArrayBrackets change to the Scala 2 Java parser and extends the existing Java type annotation positive test.

Scala 3 related issues/PRs/commits:


To reproduce:

J.java

public interface J {
  String @Nullable [] values();
}

Nullable.java

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(ElementType.TYPE_USE)
public @interface Nullable {}

Compiling with Java 8 works and produces class files:

$ java -version
openjdk version "1.8.0_492"
OpenJDK Runtime Environment (Temurin)(build 1.8.0_492-b09)
OpenJDK 64-Bit Server VM (Temurin)(build 25.492-b09, mixed mode)

$ javac Nullable.java J.java
$ ls -1 *.class
J.class
Nullable.class

The Scala 3.3 compiler accepts the files (does not create the class files however which I guess is expected):

$ scala -version
Scala code runner version 3.3.7 -- Copyright 2002-2025, LAMP/EPFL

$ scalac -usejavacp Nullable.java J.java
$ echo $?
0

Scala 2.13 fails:

$ scala -version
Scala code runner version 2.13.18 -- Copyright 2002-2025, LAMP/EPFL and Lightbend, Inc. dba Akka

$ scalac -usejavacp Nullable.java J.java
J.java:2: error: identifier expected but `@` found.
  String @Nullable [] values();
         ^
1 error

Backport the Scala 3 Java parser fix for annotations that appear
before array brackets, such as String @nullable [] and nested annotated
arrays.

Scala 3 fixed the same parser gap in scala/scala3#22391. This applies
the corresponding optArrayBrackets change to the Scala 2 Java parser
and extends the existing Java type annotation positive test.
@mkurz mkurz force-pushed the backport-java-array-type-annotations branch from f249e76 to a14ea6a Compare May 30, 2026 13:47
@mkurz
Copy link
Copy Markdown
Contributor Author

mkurz commented May 30, 2026

I amended the commit with one more test case for Java type-use annotations before varargs:

void varargs(String @NotNull ... values) {}

This mirrors the Scala 3 test added in

This PR is ready now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants