Skip to content
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

Cannot parse Java file with type annotations on array or path selection #19642

Closed
bishabosha opened this issue Feb 7, 2024 · 0 comments · Fixed by #22391
Closed

Cannot parse Java file with type annotations on array or path selection #19642

bishabosha opened this issue Feb 7, 2024 · 0 comments · Fixed by #22391

Comments

@bishabosha
Copy link
Member

bishabosha commented Feb 7, 2024

Compiler version

3.4.0-RC4

Minimized code

// TpeAnnot.java

package lib;

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

@Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})
public @interface TpeAnnot {}
// TpeAnnotated.java

package lib;

public class TpeAnnotated {

  private static <T> T unimplemented() {
    return null;
  }

  public class C {
    public class D {}
  }

  public static int @TpeAnnot [] g() { return unimplemented(); } // error

  public static int @TpeAnnot [][] h() { return unimplemented(); } // error

  public static int[] @TpeAnnot [] i() { return unimplemented(); } // error

  public static C.@TpeAnnot D foo2() { return unimplemented(); } // error

}

Output

these errors do not appear all at the same time, but by commenting out the appropriate lines of one error to reveal another one.

-- Error: TpeAnnotated.java:11:20 ----------------------------------------------
11 |  public static int @TpeAnnot [] g() { return unimplemented(); } // error
   |                    ^^^
   |                    identifier expected but @ found.
-- Error: TpeAnnotated.java:13:20 ----------------------------------------------
13 |  public static int @TpeAnnot [][] h() { return unimplemented(); } // error
   |                    ^^^
   |                    identifier expected but @ found.
-- Error: TpeAnnotated.java:15:22 ----------------------------------------------
15 |  public static int[] @TpeAnnot [] i() { return unimplemented(); } // error
   |                      ^^^
   |                      identifier expected but @ found.
-- Error: TpeAnnotated.java:23:18 ----------------------------------------------
23 |  public static C.@TpeAnnot D foo2() { return unimplemented(); } // error
   |                  ^
   |                  identifier expected but @ found.
-- Error: TpeAnnotated.java:26:0 -----------------------------------------------
26 |}
   |^
   |identifier expected but '}' found.
2 errors found

Expectation

This is valid java accepted by Javac, see JLS 4.11. Note that this fails to parse in Scala 2.13 also

@bishabosha bishabosha changed the title Cannot parse Java file with type annotations Cannot parse Java file with type annotations on array Feb 7, 2024
@hamzaremmal hamzaremmal self-assigned this Feb 7, 2024
@bishabosha bishabosha changed the title Cannot parse Java file with type annotations on array Cannot parse Java file with type annotations on array or path selection Feb 7, 2024
fan-tom added a commit to fan-tom/scala3 that referenced this issue Jan 16, 2025
…ava parser

Java parser doesn't expect an annotation in the next types, while it should:
- `Object @my.Ann []`
- `Object @my.Ann [] @my.OtherAnn []`

closes scala#19642
hamzaremmal added a commit that referenced this issue Jan 21, 2025
…java parser (#22391)

Closes #19642

Currently Java parser doesn't expect annotation between type name and
square brackets in an array type, or between pairs of square brackets in
nested array types.

I added a call to corresponding parser method `annotations` when parsing
array types.

I also changed an unnecessary `var` to `val`, as IDEA suggested.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants