antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17.11k stars 3.28k forks source link

[Java] - JDK21-EA - possible 'this' escape before subclass is fully initialized Compiler WARNING #4341

Closed khmarbaise closed 1 year ago

khmarbaise commented 1 year ago

During the compilation of the example project it will emit compilation warnings:

[INFO] --- compiler:3.11.0:compile (default-compile) @ issue-antlr-4.13.0 ---
[INFO] Changes detected - recompiling the module! :source
[INFO] Compiling 4 source files with javac [debug release 21] to target/classes
[WARNING] ../issue-antlr-4.13.0/target/generated-sources/antlr4/com/soebes/rpn/grammar/ExprParser.java:[95,50] possible 'this' escape before subclass is fully initialized
[WARNING] ../issue-antlr-4.13.0/target/generated-sources/antlr4/com/soebes/rpn/grammar/ExprParser.java:[174,70] possible 'this' escape before subclass is fully initialized
[WARNING] ../issue-antlr-4.13.0/target/generated-sources/antlr4/com/soebes/rpn/grammar/ExprParser.java:[187,67] possible 'this' escape before subclass is fully initialized
[WARNING] ../issue-antlr-4.13.0/target/generated-sources/antlr4/com/soebes/rpn/grammar/ExprParser.java:[199,69] possible 'this' escape before subclass is fully initialized
[WARNING] ../issue-antlr-4.13.0/target/generated-sources/antlr4/com/soebes/rpn/grammar/ExprParser.java:[217,76] possible 'this' escape before subclass is fully initialized
[WARNING] ../issue-antlr-4.13.0/target/generated-sources/antlr4/com/soebes/rpn/grammar/ExprParser.java:[231,68] possible 'this' escape before subclass is fully initialized
[WARNING] ../issue-antlr-4.13.0/target/generated-sources/antlr4/com/soebes/rpn/grammar/ExprParser.java:[241,68] possible 'this' escape before subclass is fully initialized
[WARNING] ../issue-antlr-4.13.0/target/generated-sources/antlr4/com/soebes/rpn/grammar/ExprParser.java:[253,75] possible 'this' escape before subclass is fully initialized
[WARNING] ../issue-antlr-4.13.0/target/generated-sources/antlr4/com/soebes/rpn/grammar/ExprParser.java:[263,66] possible 'this' escape before subclass is fully initialized
[WARNING] ../issue-antlr-4.13.0/target/generated-sources/antlr4/com/soebes/rpn/grammar/ExprParser.java:[275,72] possible 'this' escape before subclass is fully initialized
[WARNING] ../issue-antlr-4.13.0/target/generated-sources/antlr4/com/soebes/rpn/grammar/ExprParser.java:[287,75] possible 'this' escape before subclass is fully initialized
[WARNING] ../issue-antlr-4.13.0/target/generated-sources/antlr4/com/soebes/rpn/grammar/ExprParser.java:[299,72] possible 'this' escape before subclass is fully initialized
[WARNING] ../issue-antlr-4.13.0/target/generated-sources/antlr4/com/soebes/rpn/grammar/ExprLexer.java:[88,49] possible 'this' escape before subclass is fully initialized

The same code compiles in JDK20.0.1 without WARNING. This implies very likely that it's related to JDK21 itself.

oraluben commented 1 year ago

this-escape is a new linter added in JDK 21, which warns about the constructor calling other methods (with some details).

This is of course a common case in antlr-generated classes. While antlr use @SuppressWarnings("all") for all generated file, this seems only to be a IDE extension, and will be ignored by javac. So compiling program directly with javac will trigger the warning.