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] Generated code gives "this escape" warning on Java 21 in Parser class #4485

Closed
nielsbasjes opened this issue Dec 10, 2023 · 1 comment · Fixed by #4486
Closed

[Java] Generated code gives "this escape" warning on Java 21 in Parser class #4485

nielsbasjes opened this issue Dec 10, 2023 · 1 comment · Fixed by #4486

Comments

@nielsbasjes
Copy link
Contributor

The Java code generated by the antlr4-maven-plugin version 4.13.1 still has this escape warnings when compiling with -Xlint:all.

Note that this issue was partially addressed in #4394 where the @SuppressWarnings on the Lexer class was updated. Apparently this problem is still present on the Parser class aswell.

Minimal reproduction:

In an empty directory create
./pom.xml

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>nl.basjes.bugreports</groupId>
  <artifactId>antlr4-java21</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.build.outputEncoding>UTF-8</project.build.outputEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.antlr</groupId>
      <artifactId>antlr4-runtime</artifactId>
      <version>4.13.1</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.antlr</groupId>
        <artifactId>antlr4-maven-plugin</artifactId>
        <version>4.13.1</version>
        <executions>
          <execution>
            <id>antlr</id>
            <goals>
              <goal>antlr4</goal>
            </goals>
            <configuration>
              <visitor>true</visitor>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.11.0</version>
        <configuration>
          <source>11</source>
          <release>11</release>
          <compilerArgs>
            <arg>-Xlint:all</arg>
          </compilerArgs>
          <showWarnings>true</showWarnings>
          <showDeprecation>true</showDeprecation>
        </configuration>
      </plugin>

    </plugins>
  </build>

</project>

and any grammar in ./src/main/antlr4/

I used ./src/main/antlr4/Expression.g4

grammar Expression;

SPACE: ' ' -> skip;

NUMBER: [0-9]+;
PLUS  : '+';
BOPEN : '(';
BCLOSE: ')';

expression
    : num=NUMBER                                #number
    | left=expression PLUS right=expression     #sumNumbers
    | BOPEN expr=expression BCLOSE              #bracedExpression
    ;

Then using Java 21 run mvn clean compile (I used Linux and maven 3.9.5)

The output contains several lines similar to this:

[WARNING] /home/nbasjes/.../Antlr4Java21/target/generated-sources/antlr4/ExpressionParser.java:[88,50] possible 'this' escape before subclass is fully initialized
@nielsbasjes
Copy link
Contributor Author

Additional reproduction: Take the reproduction of #4341 located here https://github.com/khmarbaise/issue-antlr-4.13.0

Clone this and change the version to 4.13.1

When you actually build this with Java 21 you'll see that the reported problem on the Lexer class is gone and the (many) problems in the Parser class remain the same.

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 a pull request may close this issue.

1 participant