[jdk18] RFR: 8278834: Error "Cannot read field "sym" because "this.lvar[od]" is null" when compiling

Jan Lahoda jlahoda at openjdk.java.net
Wed Jan 19 13:40:00 UTC 2022


For code like:

public class TestInstanceOf {

    public static void main(String[] args) {
        Number abc = 1;
        new Object() {
            {
                if (abc instanceof Integer integer) {
                    System.out.println(integer.intValue());
                }
            }
        };
    }

}


`javac` crashes with:

An exception has occurred in the compiler (17-internal). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://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 read field "sym" because "this.lvar[3]" is null
        at jdk.compiler/com.sun.tools.javac.jvm.Code.emitop0(Code.java:577)
        at jdk.compiler/com.sun.tools.javac.jvm.Items$LocalItem.load(Items.java:399)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genArgs(Gen.java:902)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitNewClass(Gen.java:1961)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCNewClass.accept(JCTree.java:1852)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:877)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitExec(Gen.java:1742)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCExpressionStatement.accept(JCTree.java:1584)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:610)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:645)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:631)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStats(Gen.java:682)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitBlock(Gen.java:1097)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1091)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:610)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:645)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genMethod(Gen.java:967)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitMethodDef(Gen.java:930)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:921)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:610)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genClass(Gen.java:2414)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.genCode(JavaCompiler.java:737)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1617)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1585)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:946)
        at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:317)
        at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:176)
        at jdk.compiler/com.sun.tools.javac.Main.compile(Main.java:64)
        at jdk.compiler/com.sun.tools.javac.Main.main(Main.java:50)


The cause is in `TransPatterns` - it tracks the current class and method, so that it can define correct owners for temporary variables. But if there's a class nested in a method, the current method field is not cleared, and consequently the temporary variables get a wrong owner (the method enclosing the class, not a method inside the current class), which then crashes subsequent phases, like Gen.

The proposed fix is to clear `currentMethod` when entering a class AST node.

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

Commit messages:
 - 8278834: Error "Cannot read field "sym" because "this.lvar[od]" is null" when compiling

Changes: https://git.openjdk.java.net/jdk18/pull/110/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk18&pr=110&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8278834
  Stats: 167 lines in 2 files changed: 167 ins; 0 del; 0 mod
  Patch: https://git.openjdk.java.net/jdk18/pull/110.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk18 pull/110/head:pull/110

PR: https://git.openjdk.java.net/jdk18/pull/110


More information about the compiler-dev mailing list