Calling the constructor of a capturing local class in a static context throws a NPE

Remi Forax forax at univ-mlv.fr
Sun Dec 18 13:25:39 UTC 2022


Hi everybody,
a Christmas bug :)

public class StaticFieldInsideCapturingLocalClass {
  public static void test(int value) {
    class Foo {
      private static final Foo FOO = new Foo();

      public void m() {
        System.out.println(value);
      }
    }
    Foo.FOO.m();
  }
}

For me, "new Foo()" should not be allowed in a static context given that Foo capture the local variable "value".

I was able to reproduce the issue with Java 16, 17, 19 and 20 but not with Java 11 and 15.
This bug seems to have been introduced in Java 16 when Java starts to allow static final field in local class.


~/jdk/jdk-20-valhalla.jdk/Contents/Home/bin/javac StaticFieldInsideCapturingLocalClass.java 
An exception has occurred in the compiler (20-valhalla). 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 read field "sym" because "this.lvar[0]" is null
        at jdk.compiler/com.sun.tools.javac.jvm.Code.emitop0(Code.java:570)
        at jdk.compiler/com.sun.tools.javac.jvm.Items$SelfItem.load(Items.java:369)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitIdent(Gen.java:2354)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCIdent.accept(JCTree.java:2735)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:902)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genArgs(Gen.java:927)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitNewClass(Gen.java:2031)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCNewClass.accept(JCTree.java:1925)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:902)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitAssign(Gen.java:2087)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCAssign.accept(JCTree.java:2114)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:902)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitExec(Gen.java:1812)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCExpressionStatement.accept(JCTree.java:1657)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:631)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:666)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:652)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStats(Gen.java:703)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitBlock(Gen.java:1130)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1097)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:631)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:666)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genMethod(Gen.java:992)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitMethodDef(Gen.java:955)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:923)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:631)
        at jdk.compiler/com.sun.tools.javac.jvm.Gen.genClass(Gen.java:2505)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.genCode(JavaCompiler.java:738)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1636)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1604)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:947)
        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)

happy Christmas,
Rémi


More information about the compiler-dev mailing list