Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8305007

Within-lambda subclass of local class using method param causes compiler crash

XMLWordPrintable

    • 12
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Verified on the following Java versions from the OpenJDK site:
       - 21 EA (build 21-ea+15-123)
       - 20 (build 20+36-2344)
       - 19 (build 19.0.2+7-44)
       - 18 (build 18+36-2087)
       - 17 (build 17+35-2724)
       - 16 (build 16+36-2231)
       - 15 (build 15+36-1562)
       - 14 (build 14+36-1461)
       - 13 (build 13+33)
       - 12 (build 12+32)

      Not present on Java 11 (build 11+28) and below.

      A DESCRIPTION OF THE PROBLEM :
      If a local class is defined within a method which uses a parameter of said method (whether within the local class's methods or field initializers), and within a lambda in the same method a subclass of the local class is created, any instantiation of the local class or the subclass causes a compiler crash.

      This issue occurs on versions after and including Java 12. For comparison, the Eclipse Compiler for Java (v20230218-1114, 3.33.0) successfully compiles the same source code.

      REGRESSION : Last worked in version 11

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Put the example source code below into a file `JavacBug.java`.
      2. Attempt to compile the source file using `javac JavacBug.java`.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The source file should compile successfully without any warnings, errors, or crashes.
      ACTUAL -
      The compiler crashes with the following stacktrace (based on 21 EA, all other versions give very similar/exact stacktraces):

      An exception has occurred in the compiler (21-ea). 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
              at jdk.compiler/com.sun.tools.javac.util.Assert.error(Assert.java:155)
              at jdk.compiler/com.sun.tools.javac.util.Assert.check(Assert.java:46)
              at jdk.compiler/com.sun.tools.javac.jvm.Items$LocalItem.<init>(Items.java:392)
              at jdk.compiler/com.sun.tools.javac.jvm.Items.makeLocalItem(Items.java:133)
              at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitIdent(Gen.java:2328)
              at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCIdent.accept(JCTree.java:2681)
              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.visitApply(Gen.java:1926)
              at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1817)
              at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:885)
              at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitExec(Gen.java:1794)
              at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCExpressionStatement.accept(JCTree.java:1604)
              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:1128)
              at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitBlock(Gen.java:1121)
              at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1088)
              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:912)
              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:2472)
              at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.genCode(JavaCompiler.java:755)
              at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1685)
              at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1653)
              at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:964)
              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)

      ---------- BEGIN SOURCE ----------
      public abstract class JavacBug {

          public abstract void consume(Runnable r);

          public void doThing(String parameter) {
              class LocalClass {

                  @Override
                  public String toString() {
                      return parameter;
                  }
              }

              consume(() -> {
                  // if this subclass declaration below is commented out the compiler doesn't crash
                  class LambdaLocalClass extends LocalClass {
                  }

                  new LocalClass();
              });
          }
      }

      ---------- END SOURCE ----------

            vromero Vicente Arturo Romero Zaldivar
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: