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

Switch statement with a pattern, constant and default label elements crash javac

    XMLWordPrintable

    Details

    • Subcomponent:
    • Introduced In Build:
      b26
    • Introduced In Version:
      17
    • Resolved In Build:
      b31
    • CPU:
      generic
    • OS:
      generic

      Backports

        Description

        ADDITIONAL SYSTEM INFORMATION :
        OS: Ubuntu 20.04.1 LTS
        JRE: OpenJDK 17 build 17-ea+27-2476
        JVM parameters: --enable-preview --source 17

        A DESCRIPTION OF THE PROBLEM :
        Trying to compile the code containing a pattern, constant, and default label elements within one switch label leads to the javac compilation error.


        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Try to compile the code:

            public static void main(String[] args) {
                test(1);
            }

            static void test(Integer i) {
                switch (i) {
                    case Integer o && o != null, 1, default:
                        System.out.println("s1");
                }
            }


        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        It's expected to see clear compilation errors according to the spec draft: http://cr.openjdk.java.net/~gbierman/jep406/jep406-20210527/specs/patterns-switch-jls.html#jls-14.30.1

        ACTUAL -
        Compilation error:

        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.Gen.handleSwitch(Gen.java:1310)
                at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitSwitch(Gen.java:1193)
                at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCSwitch.accept(JCTree.java:1294)
                at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:610)
                at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:645)
                at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:631)
                at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStats(Gen.java:682)
                at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitBlock(Gen.java:1097)
                at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1091)
                at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:610)
                at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:645)
                at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:631)
                at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStats(Gen.java:682)
                at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitBlock(Gen.java:1097)
                at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1091)
                at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:610)
                at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:645)
                at jdk.compiler/com.sun.tools.javac.jvm.Gen.genMethod(Gen.java:967)
                at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitMethodDef(Gen.java:930)
                at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:921)
                at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:610)
                at jdk.compiler/com.sun.tools.javac.jvm.Gen.genClass(Gen.java:2415)
                at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.genCode(JavaCompiler.java:737)
                at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1617)
                at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1585)
                at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:946)
                at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.lambda$doCall$0(JavacTaskImpl.java:104)
                at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.invocationHelper(JavacTaskImpl.java:152)
                at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:100)
                at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:94)
                at jdk.compiler/com.sun.tools.javac.launcher.Main.compile(Main.java:383)
                at jdk.compiler/com.sun.tools.javac.launcher.Main.run(Main.java:189)
                at jdk.compiler/com.sun.tools.javac.launcher.Main.main(Main.java:132)
        error: compilation failed

        ---------- BEGIN SOURCE ----------
        public class Switch {

            public static void main(String[] args) {
                test(1);
            }

            static void test(Integer i) {
                switch (i) {
                    case Integer o && o != null, 1, default:
                        System.out.println("s1");
                }
            }
        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        '1' or 'default' removal fixes the issue.

        FREQUENCY : always


          Attachments

            Issue Links

              Activity

                People

                Assignee:
                jlahoda Jan Lahoda
                Reporter:
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved: