RFR: 8027682: javac wrongly accepts semicolons in package and import decls

Joe Darcy darcy at openjdk.org
Mon Feb 6 21:32:47 UTC 2023


On Mon, 6 Feb 2023 20:50:34 GMT, Liam Miller-Cushon <cushon at openjdk.org> wrote:

>> JLS [§7.3](https://docs.oracle.com/javase/specs/jls/se19/html/jls-7.html#jls-7.3) specifies that while a lone semi-colon is a valid _TopLevelClassOrInterfaceDeclaration_, it is not a valid _ImportDeclaration_. Therefore, if we see a lone semi-colon while looking for the next import statement we have to advance and accept a class declaration, and we can therefore no longer accept any further `import` statements.
>> 
>> However, the compiler was allowing this, for example:
>> 
>> package p; import X; ;;; import Y; class Foo {}
>> 
>> The bug is that the parser was switching out of "look for imports" mode after parsing a valid class declaration, but it was not switching out of "look for imports" mode after parsing a lone semi-colon.
>> 
>> The fix to `JavacParser.java` is easy, however it also requires these adjustments to unit tests:
>> 
>> * Test `tree/T6963934.java` must go away, because it is verifying a bug that only happens when the above bogus input is successfully parsed, and this can no longer happen.
>> * A bug in `lib/types/TypeHarness.java` was uncovered and fixed; it was inserting an extra semi-colon.
>> * The following bugs, which check invalid syntax within import statements, now generate different parse errors and therefor needed their "golden output" updated:
>>   * annotations/typeAnnotations/failures/AnnotatedImport.java
>>   * annotations/typeAnnotations/failures/AnnotatedPackage1.java
>>   * annotations/typeAnnotations/failures/AnnotatedPackage2.java
>
> I would be happy to see this fixed, but note that there's going to be some compatibility impact from this to existing programs containing extra `;`, and it probably warrants a CSR.
> 
> There was some discussion in [the original compiler-dev thread](https://mail.openjdk.org/pipermail/compiler-dev/2013-August/006973.html) about whether the right fix was to change javac, or change the spec.

If javac were to start rejecting previously accepted programs, even programs that violate the letter of the JLS, that would require a CSR, as @cushon noted.

-------------

PR: https://git.openjdk.org/jdk/pull/12448


More information about the compiler-dev mailing list