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

Match on enum constants with body compiles but fails with MatchException

    XMLWordPrintable

Details

    • 21
    • b23
    • generic
    • generic

    Backports

      Description

        ADDITIONAL SYSTEM INFORMATION :
        java version "21" 2023-09-19 LTS

        A DESCRIPTION OF THE PROBLEM :
        Try running the following code:


        Expected result on stdout: A
        Actual result: JVM throws MatchException

        REGRESSION : Last worked in version 17.0.8

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Run provided example code

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        Stdout should read "A"
        ACTUAL -
        JVM throws MatchException

        ---------- BEGIN SOURCE ----------
        public class Main
        {
            public enum Test {
                A {}, B {}, C {}
            }

            public static void main(String[] args)
            {
                Test x = get();
                switch (x) {
                    case A -> System.out.println("A");
                    case B -> System.out.println("B");
                    case C -> System.out.println("C");
                    case null -> System.out.println("NULL");
                }
            }

            private static Test get() {
                return Test.A;
            }
        }
        ---------- END SOURCE ----------

        FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

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

                Dates

                  Created:
                  Updated:
                  Resolved: