Javac NPE Crash - Local class constructor reference in static context & closure

XMLWordPrintable

    • b26
    • 16
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Ubuntu 24.04.3 LTS
      Kernel version 6.14.0-35-generic
      javac 21.0.9

      A DESCRIPTION OF THE PROBLEM :
      Compiler is not accusing illegal use of local class constructor in a static context when using method reference. The compiler fails with a NPE crash if this happens within a closure.
      This is related to https://bugs.openjdk.org/browse/JDK-8333313, and fix was confirmed in JDK 24

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the provided source code


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Javac reports a compile error when trying to reference a constructor of a Local class in a static context, such as:
      ```
      error: local class Local cannot be instantiated from a static context
      ```
      ACTUAL -
      Javac crashes with the exception:
      ```
      An exception has occurred in the compiler (21.0.9). 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:568)
      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:2368)
      at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCIdent.accept(JCTree.java:2715)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:885)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genArgs(Gen.java:910)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitNewClass(Gen.java:2053)
      at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCNewClass.accept(JCTree.java:1871)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:885)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitReturn(Gen.java:1909)
      at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCReturn.accept(JCTree.java:1728)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:614)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:649)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:635)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStats(Gen.java:686)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.doVisitBlock(Gen.java:1138)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitBlock(Gen.java:1117)
      at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1092)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:614)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:649)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genMethod(Gen.java:975)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitMethodDef(Gen.java:938)
      at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:916)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:614)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genClass(Gen.java:2506)
      at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.genCode(JavaCompiler.java:769)
      at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1710)
      at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1678)
      at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:976)
      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:64)
      at jdk.compiler/com.sun.tools.javac.Main.main(Main.java:50)
      ```

      ---------- BEGIN SOURCE ----------
      import java.util.function.Supplier;

      class TestCaseJavac21NpeCrash {
          public static void main(final String[] args) {
              class Local {
                  static Supplier<Local> staticFunction() {
                      return Local::new;
                  }

                  public String[] closure() {
                      return args;
                  }
              }
          }
      }
      ---------- END SOURCE ----------

            Assignee:
            Vicente Arturo Romero Zaldivar
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: