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

Java classes with annotations fail to parse #20063

Open
noti0na1 opened this issue Apr 1, 2024 · 2 comments
Open

Java classes with annotations fail to parse #20063

noti0na1 opened this issue Apr 1, 2024 · 2 comments

Comments

@noti0na1
Copy link
Member

noti0na1 commented Apr 1, 2024

Compiler version

3.3.1
3.5.0-RC1-bin-SNAPSHOT

Should happen in every version, and in Scala 2 as well

Minimized code

// JFun.java
// Other annotations also fail, for example, @Deprecated
@FunctionalInterface
public interface JFun {
    String f(String s);
}

with an empty scala file in order to trigger the compiling: Stest.scala.

Output

> scala-cli compile JFun.java Stest.scala
Compiling project (Scala 3.3.1, JVM (17))
[error] ./JFun.java:2:1
[error] package expected but public found.
[error] public interface JFun {
[error] ^^^^^^
[error] ./JFun.java:6:1
[error] identifier expected but eof found.
Error compiling project (Scala 3.3.1, JVM (17))
Compilation failed
> scala-cli compile --scala 2 JFun.java Stest.scala
Compiling project (Scala 2.13.13, JVM (17))
[error] ./JFun.java:2:1
[error] `package` expected but `public` found.
[error] public interface JFun {
[error] ^
[error] ./JFun.java:5:1
[error] identifier expected but eof found.
[error] ^
Error compiling project (Scala 2.13.13, JVM (17))
Compilation failed

Expectation

Should compile.

Analyze

In JavaParser, it tries to read annotations with the package.

/** CompilationUnit ::= [package QualId semi] TopStatSeq
  */
def compilationUnit(): Tree = {
  val start = in.offset
  val pkg: RefTree =
    if (in.token == AT || in.token == PACKAGE) {
      annotations()
      accept(PACKAGE)
      val pkg = qualId()
      accept(SEMI)
      pkg
    }
    ...

Scala 2 also has the same issue, since this part is a direct port (and there is a TODO there).

Removing in.token == AT || from the condition and annotations() from the body makes the code compile.

@som-snytt
Copy link
Contributor

Sounds like #20026

@som-snytt
Copy link
Contributor

PR against the other ticket is forward-port of a fix on scala 2. #20064

The other ticket also implicates scala-cli, which I haven't tried yet.

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

No branches or pull requests

2 participants