Skip to content

Backport "Fix annotations being not expected in the middle of an array type by java parser" to 3.3 LTS #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,16 @@ object JavaParsers {
t
}

def optArrayBrackets(tpt: Tree): Tree =
def optArrayBrackets(tpt: Tree): Tree = {
annotations()
if (in.token == LBRACKET) {
val tpt1 = atSpan(tpt.span.start, in.offset) { arrayOf(tpt) }
in.nextToken()
accept(RBRACKET)
optArrayBrackets(tpt1)
}
else tpt
}

def basicType(): Tree =
atSpan(in.offset) {
Expand Down Expand Up @@ -335,7 +337,7 @@ object JavaParsers {
}

def annotations(): List[Tree] = {
var annots = new ListBuffer[Tree]
val annots = new ListBuffer[Tree]
while (in.token == AT) {
in.nextToken()
annotation() match {
Expand Down
17 changes: 17 additions & 0 deletions tests/pos/i19642/Valid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package lib;

import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Target({ METHOD, FIELD, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
@Documented
public @interface Valid {}
9 changes: 9 additions & 0 deletions tests/pos/i19642/i19642.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package app;

import java.util.Optional;
import lib.*;

public class i19642 {
private String @lib.Valid [] flatArray;
private String @lib.Valid [] @lib.Valid [] nestedArray;
}