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

Archie L. Cobbs duke at openjdk.org
Tue Feb 7 00:01:47 UTC 2023

> 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

Archie L. Cobbs has updated the pull request incrementally with two additional commits since the last revision:

 - Fix some extraneous semi-colons that are now disallowed.
 - Give a more helpful error for spurious semi-colons before import statements.


  - all: https://git.openjdk.org/jdk/pull/12448/files
  - new: https://git.openjdk.org/jdk/pull/12448/files/adc35713..ac02179a

 - full: https://webrevs.openjdk.org/?repo=jdk&pr=12448&range=01
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=12448&range=00-01

  Stats: 61 lines in 9 files changed: 49 ins; 2 del; 10 mod
  Patch: https://git.openjdk.org/jdk/pull/12448.diff
  Fetch: git fetch https://git.openjdk.org/jdk pull/12448/head:pull/12448

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

More information about the compiler-dev mailing list