RFR: 8341070: javac fails with an exception when compiling import module under source level 8
Jan Lahoda
jlahoda at openjdk.org
Mon Sep 30 11:46:04 UTC 2024
Consider code like:
package test;
import module java.base;
public class Test {
List<String> l;
}
Compiling this may cause javac to fail with an exception under some circumstances:
$ javac --source 8 -XDshould-stop.at=FLOW -XDdev /tmp/Test.java
warning: [options] bootstrap class path is not set in conjunction with -source 8
not setting the bootstrap class path may lead to class files that cannot run on JDK 8
--release 8 is recommended instead of -source 8 because it sets the bootstrap class path automatically
warning: [options] source value 8 is obsolete and will be removed in a future release
warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.
/tmp/Test.java:3: error: module imports are a preview feature and are disabled by default.
import module java.base;
^
(use --enable-preview to enable module imports)
1 error
3 warnings
An exception has occurred in the compiler (23-internal). Please file a bug against the Java compiler via the Java bug reporting page (https://bugreport.java.com/) after checking the Bug Database (https://bugs.java.com/) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
java.lang.NullPointerException: Cannot invoke "java.util.Set.contains(Object)" because "this.env.toplevel.modle.readModules" is null
at jdk.compiler/com.sun.tools.javac.comp.TypeEnter$ImportsPhase.doModuleImport(TypeEnter.java:477)
at jdk.compiler/com.sun.tools.javac.comp.TypeEnter$ImportsPhase.handleImports(TypeEnter.java:414)
at jdk.compiler/com.sun.tools.javac.comp.TypeEnter$ImportsPhase.resolveImports(TypeEnter.java:389)
at jdk.compiler/com.sun.tools.javac.comp.TypeEnter$ImportsPhase.runPhase(TypeEnter.java:318)
at jdk.compiler/com.sun.tools.javac.comp.TypeEnter$Phase.doCompleteEnvs(TypeEnter.java:279)
at jdk.compiler/com.sun.tools.javac.comp.TypeEnter$Phase.completeEnvs(TypeEnter.java:248)
at jdk.compiler/com.sun.tools.javac.comp.TypeEnter.complete(TypeEnter.java:195)
at jdk.compiler/com.sun.tools.javac.code.Symbol.complete(Symbol.java:687)
at jdk.compiler/com.sun.tools.javac.code.Symbol$ClassSymbol.complete(Symbol.java:1455)
at jdk.compiler/com.sun.tools.javac.comp.Enter.complete(Enter.java:632)
at jdk.compiler/com.sun.tools.javac.comp.Enter.main(Enter.java:599)
at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.enterTrees(JavaCompiler.java:1077)
at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:948)
at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:319)
at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:178)
at jdk.compiler/com.sun.tools.javac.Main.compile(Main.java:66)
at jdk.compiler/com.sun.tools.javac.Main.main(Main.java:52)
printing javac parameters to: /tmp/javac.20240927_093418.args
The reason is that while there are no modules under `--source 8`, the javac's `Symtab` still has a partial `java.base` module as a consequence of the initialization order. This partial modules is then found while evaluating `import module java.base;`, and leads to the subsequent crash.
The proposal herein is to reorganize the `Symtab` initialization a little, to ensure there's no partial `java.base` module.
-------------
Commit messages:
- Simplifying test.
- Preventing a NPE when --source 8 + -XDshould-stop.at=FLOW is used in combination with import module java.base;
Changes: https://git.openjdk.org/jdk/pull/21262/files
Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=21262&range=00
Issue: https://bugs.openjdk.org/browse/JDK-8341070
Stats: 82 lines in 2 files changed: 63 ins; 19 del; 0 mod
Patch: https://git.openjdk.org/jdk/pull/21262.diff
Fetch: git fetch https://git.openjdk.org/jdk.git pull/21262/head:pull/21262
PR: https://git.openjdk.org/jdk/pull/21262
More information about the compiler-dev
mailing list