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

Case enumConstant, pattern compilation fails

XMLWordPrintable

    • 21
    • b12

        Code:

        package com.example;

        public class Test {
          enum X {A, B}

          void test(Object obj) {
            switch (obj) {
              case X.A, Integer _ -> System.out.println("A or Integer");
              case String _, X.B -> System.out.println("B or String");
              default -> System.out.println("other");
            }
          }

          public static void main(String[] args) {
            new Test().test("ddd");
          }
        }

        Compile with --enable-preview

        Expected: either successful compilation or two compilation errors in both case lines. Actual: one compilation error:

        com\example\Test.java:8:15
        java: <identifier> expected

        Java parses `case String _, X.B` (when pattern goes first) but does not parse `case X.A, Integer _` when enum constant goes first. It should either accept or reject both. According to current spec draft, it should reject both, but probably it's better to rectify the spec draft.

              abimpoudis Angelos Bimpoudis
              tvaleev Tagir Valeev
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: