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

Check for case label validity are misbehaving when binding patterns with unnamed bindings are present

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P3 P3
    • 25
    • 24, 25
    • tools
    • None

      Reported here:
      https://mail.openjdk.org/pipermail/compiler-dev/2025-January/029309.html

      Consider code like:
      ---
          public class X {
              record Point (int x, int y) {}
              static void foo(Object o) {
              switch (o) {
                  case Integer _, Point(int x, int _), String _ :
                      System.out.println("Integer");
                  default:
                      System.out.println("Object");
              }
              }
              public static void main(String [] args) {
                  foo("");
              }
          }
      ---

      javac will compile this code, even though JLS 14.11.1 specifies a compile-time error should be reported:
      It is a compile-time error for a case label to have more than one case pattern and declare any pattern variables (other than those declared by a guard associated with the case label).

            jlahoda Jan Lahoda
            jlahoda Jan Lahoda
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: