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

Assertion error when trying to compile switch with fallthrough with pattern

XMLWordPrintable

    • b23
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Fedora Linux 39

      A DESCRIPTION OF THE PROBLEM :
      Trying to compile the following code with preview features enabled yields an assertion error:

      ```java

      public class fallthroughPattern {

      public static void main(String... args) {
      Object o = "";
      switch (o) {
      case String s:
      System.out.println("this is the first outcome");
      case R(String _):
      System.out.println("this is the second outcome");
      case R(Integer _):
      System.out.println("this is the third outcome");
      default:
      System.out.println("this is the fourth outcome");
      }

      }

      private static final record R<T>(T a) {}

      }
      ```

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create the file `fallthroughPattern.java` described in the description.

      Then, attempt to compile the file with `javac --source 21 --enable-preview fallthroughPattern.java`

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I don't fully understand the fallthrough semantics when working with patterns, but I'd expect one of the following to occur:
      - An "illegal fallthrough" error is emitted by the compiler and the program fails to compile
      OR
      - The code compiles with a warning that unnamed variables is a preview feature, and when the program runs, it executes all four print statements
      ACTUAL -
      ```
      Note: fallthroughPattern.java uses preview features of Java SE 21.
      Note: Recompile with -Xlint:preview for details.
      An exception has occurred in the compiler (21.0.2). 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:2338)
              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.visitSelect(Gen.java:2378)
              at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCFieldAccess.accept(JCTree.java:2581)
              at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:885)
              at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitApply(Gen.java:1931)
              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.genStats(Gen.java:670)
              at jdk.compiler/com.sun.tools.javac.jvm.Gen.handleSwitch(Gen.java:1414)
              at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitSwitch(Gen.java:1224)
              at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCSwitch.accept(JCTree.java:1299)
              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)
      printing javac parameters to: /home/davthomp/Documents/Projects/TestProjects/java-21/javac.20240220_161514.arg
      ```

      ---------- BEGIN SOURCE ----------
      public class fallthroughPattern {

      public static void main(String... args) {
      Object o = "";
      switch (o) {
      case String s:
      System.out.println("this is the first outcome");
      case R(String _):
      System.out.println("this is the second outcome");
      case R(Integer _):
      System.out.println("this is the third outcome");
      default:
      System.out.println("this is the fourth outcome");
      }

      }

      private static final record R<T>(T a) {}

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Avoid using fallthrough in switch statements

      FREQUENCY : always


            abimpoudis Angelos Bimpoudis
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: