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

IncompatibleClassChangeError on empty pattern switch statement case

    XMLWordPrintable

    Details

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

      Backports

        Description

        A DESCRIPTION OF THE PROBLEM :
        When using a patterns (a la JEP 406) in a switch statement and a case is empty, it will compile without error, but at runtime, a IncompatibleClassChangeError is thrown upon encountering this line.

        The same happens if the case only contains statements that don't happen to be executed when it is reached (e.g. "if (false) return;").

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        run

        javac A.java --enable-preview --release 17 && java --enable-preview A

        On the provided source code

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        either successful execution and empty output, or a compile-time error
        ACTUAL -
        Note: A.java uses preview features of Java SE 17.
        Note: Recompile with -Xlint:preview for details.
        Exception in thread "main" java.lang.IncompatibleClassChangeError
                at A.test(A.java:9)
                at A.main(A.java:5)

        ---------- BEGIN SOURCE ----------
        sealed interface A permits A.B {
            record B() implements A {}

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

            static void test(A a) {
                switch (a) {
                    case B b:
                }
            }
        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Writing adding "break;" as last statement in the case

          Attachments

            Issue Links

              Activity

                People

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

                  Dates

                  Created:
                  Updated:
                  Resolved: