RFR: 8305671: javac rejects semicolons in compilation units with no imports
Jonathan Gibbons
jjg at openjdk.org
Mon Apr 17 23:21:45 UTC 2023
On Wed, 5 Apr 2023 18:37:05 GMT, Archie L. Cobbs <duke at openjdk.org> wrote:
> The fix for [JDK-8027682](https://bugs.openjdk.org/browse/JDK-8027682) created a new bug in which a program like this:
>
> package p;;
>
> would be rejected.
>
> This is due to the previous fix swallowing all the semicolons. Instead, in the case of semicolons followed by EOF, it needs to swallow all but the last one.
test/langtools/tools/javac/parser/ExtraPackageSemicolon.java line 42:
> 40:
> 41: public static void runTest(String filename, String source) throws Exception {
> 42: final File sourceFile = new File(filename);
As a matter of style, writing source code to `java.io.File` objects, this feels very old-school, but for a small test program like this, it's not awful.
If this were any bigger, I'd suggest looking at the test `ToolBox` library and/or using `SimpleJavaFileObject` to use more common code idioms and support.
As it is, I'd still suggest using `Files.writeString` to write each source.
https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/nio/file/Files.html#writeString(java.nio.file.Path,java.lang.CharSequence,java.nio.charset.Charset,java.nio.file.OpenOption...)
test/langtools/tools/javac/parser/ExtraPackageSemicolon.java line 45:
> 43: System.err.println("writing: " + sourceFile);
> 44: try (PrintStream output = new PrintStream(new FileOutputStream(sourceFile))) {
> 45: output.println(source);
This would be somewhat overly simplistic (but not wrong) if any source had newlines in it.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/13361#discussion_r1169345681
PR Review Comment: https://git.openjdk.org/jdk/pull/13361#discussion_r1169346313
More information about the compiler-dev
mailing list