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

javac 21 crashes with AssertionError when compiling a pattern-matching switch

XMLWordPrintable

    • 21
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      javac 21.0.8

      A DESCRIPTION OF THE PROBLEM :
      javac (JDK 21) throws an internal java.lang.AssertionError when compiling following valid code that uses a pattern-matching switch over a sealed interface with record implementations, within a lambda in a stream pipeline.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compiling following code using javac 21:
      ```
      javac App.java
      ```

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The code should compile successfully.
      ACTUAL -
      javac throws java.lang.AssertionError:
      ```
      An exception has occurred in the compiler (21.0.8). 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.checkNull(Assert.java:54)
              at jdk.compiler/com.sun.tools.javac.jvm.Code.addLocalVar(Code.java:2005)
              at jdk.compiler/com.sun.tools.javac.jvm.Code.newLocal(Code.java:2221)
              at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitVarDef(Gen.java:1088)
              at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:1022)
              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.visitLetExpr(Gen.java:2439)
              at jdk.compiler/com.sun.tools.javac.tree.JCTree$LetExpr.accept(JCTree.java:3418)
              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:1936)
              at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1816)
              at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:885)
              at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitVarDef(Gen.java:1089)
              at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:1022)
              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: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.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:1110)
              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:2482)
              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.*;

      sealed interface Result permits Success, Failure {
      }

      record Success(String msg) implements Result {
          @Override
          public String toString() {
              return "Success: " + msg;
          }
      }

      record Failure(String err) implements Result {
          @Override
          public String toString() {
              return "Failure: " + err;
          }
      }

      public class App {
          public static void main(String[] args) {
              List<String> data = List.of("123", "abc", "456", "789");

              data.stream()
                      .map(s -> {
                          if (s.matches("\\d+")) {
                              return new Success(s);
                          } else {
                              return new Failure(s);
                          }
                      })
                      .forEach(r -> System.out.println(
                              switch (r) {
                                  case Success(String msg) -> msg;
                                  case Failure(String err) -> err;
                                  default -> "Unknown result";
                              }));
          }
      }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: