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

Wrong switch error: case label is dominated by a preceding case label

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Ubuntu 22.04.2 LTS
      openjdk version "20-internal" 2023-03-21
      OpenJDK Runtime Environment (build 20-internal-adhoc.admin1.jdk-jdk-20-34)
      OpenJDK 64-Bit Server VM (build 20-internal-adhoc.admin1.jdk-jdk-20-34, mixed mode)


      A DESCRIPTION OF THE PROBLEM :
      There seems to be a wrong or misleading error message when a constant label is used after a guarded pattern label in a switch. The error message states that a case label is dominated by a preceding case label, but this error can still be produced when this is not the case as illustrated in the provided example. I am aware that the intention might be that the constant labels should come first before the guarded pattern labels, but the error message does not reflect that. Hence, it would be good to adopt the error handling or at least make the error message more clear.


      ---------- BEGIN SOURCE ----------
      Integer i = 5;
      switch (i) {
          case 1 -> System.out.println("Case 1!");
          case Integer i1 when i1 < 0 -> System.out.println("Case 2!");
          case 42 -> System.out.println("Case 3!");
          default -> System.out.println("Default Case");
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


            adev Anupam Dev
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: