RFR: 8364987: javac fails with an exception when looking for diamond creation

Jan Lahoda jlahoda at openjdk.org
Thu Aug 7 11:28:50 UTC 2025


Consider code like:

$ cat /tmp/Test.java
public class Test {
    void t() {
        L<Object> l = new L<Test>();
    }
    static class L<T> {}
}


Note the code is invalid - `L<Test>` is not assignable to `L<Object>`. The type of `L<Test>` is erroneous in the AST. This may crash with an exception when searching for diamond rewrites:

$ javac -XDfind=diamond -XDshould-stop.at=FLOW -XDdev /tmp/Test.java
/tmp/Test.java:3: error: incompatible types: L<Test> cannot be converted to L<Object>
        L<Object> l = new L<Test>();
                      ^
1 error
An exception has occurred in the compiler (25-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.AssertionError: Analyzer error when processing: L<Object> l = new L<Test>():java.lang.NullPointerException: Cannot invoke "com.sun.tools.javac.code.Type.isPartial()" because "s" is null
jdk.compiler/com.sun.tools.javac.code.Types$5.visitClassType(Types.java:1399)
jdk.compiler/com.sun.tools.javac.code.Types$5.visitClassType(Types.java:1351)
jdk.compiler/com.sun.tools.javac.code.Type$ClassType.accept(Type.java:1052)
jdk.compiler/com.sun.tools.javac.code.Types$DefaultTypeVisitor.visit(Types.java:4936)
jdk.compiler/com.sun.tools.javac.code.Types.isSameType(Types.java:1343)
jdk.compiler/com.sun.tools.javac.comp.Analyzer$DiamondInitializer.process(Analyzer.java:268)
jdk.compiler/com.sun.tools.javac.comp.Analyzer$DiamondInitializer.process(Analyzer.java:228)
jdk.compiler/com.sun.tools.javac.comp.Analyzer.doAnalysis(Analyzer.java:577)
jdk.compiler/com.sun.tools.javac.comp.Analyzer$2.flush(Analyzer.java:547)
jdk.compiler/com.sun.tools.javac.comp.Analyzer.flush(Analyzer.java:591)
jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1412)
jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1380)
jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:968)
jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:319)
jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:178)
jdk.compiler/com.sun.tools.javac.Main.compile(Main.java:66)
jdk.compiler/com.sun.tools.javac.Main.main(Main.java:52)
        at jdk.compiler/com.sun.tools.javac.util.Assert.error(Assert.java:162)
        at jdk.compiler/com.sun.tools.javac.comp.Analyzer.doAnalysis(Analyzer.java:579)
        at jdk.compiler/com.sun.tools.javac.comp.Analyzer$2.flush(Analyzer.java:547)
        at jdk.compiler/com.sun.tools.javac.comp.Analyzer.flush(Analyzer.java:591)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1412)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1380)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:968)
        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.20250807_090036.args


The reason is that while the inferred type (`L<Object>`) has one type argument, the actual type in the AST (error/`<any>`) has no type arguments, leading to getting a `null` from the list, and failing with the NPE.

The proposal is to improve the error resilience by not trying to search for diamond rewrites for erroneous types.

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

Commit messages:
 - Re-enabling accidentally disabled test.
 - 8364987: javac fails with an exception when looking for diamond creation

Changes: https://git.openjdk.org/jdk/pull/26675/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=26675&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8364987
  Stats: 41 lines in 2 files changed: 39 ins; 0 del; 2 mod
  Patch: https://git.openjdk.org/jdk/pull/26675.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/26675/head:pull/26675

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


More information about the compiler-dev mailing list