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

Switch case with pattern matching and guard clause compiles inconsistently

XMLWordPrintable

    • b06
    • generic
    • generic

        ADDITIONAL SYSTEM INFORMATION :
        OS: Windows 11 Pro
        java 22.0.1 2024-04-16

        A DESCRIPTION OF THE PROBLEM :
        When using a switch statement with pattern matching and a guard clause, one specific case label fails to compile, whereas an almost similar case compiles successfully. Both case labels should compile without errors. The only difference between the two labels is a mathematical operator in the guard clause. ( >= instead of > )

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The code should compile successfully
        ACTUAL -
        The code didn't compile and showed errors on a specific line that is almost identical to the previous line that has no errors.

        ---------- BEGIN SOURCE ----------
        public class Test {

            void t1(Object o) {
                switch (o) {
                    case Point(int x, int y) when ((x >= 0) && (y != 0)) -> {} // this compiles by javac
                    case Point(int x, int y) when ((x > 0) && (y != 0)) -> {} // this doesn't compile by javac, only difference is > instead of >=
                    default -> {}
                }
            }
            record Point(int x, int y) { }
        }

        ---------- END SOURCE ----------

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

                Created:
                Updated:
                Resolved: