[jdk23] RFR: 8335766: Switch case with pattern matching and guard clause compiles inconsistently

Jan Lahoda jlahoda at openjdk.org
Wed Jul 10 20:29:15 UTC 2024


Hi all,

This pull request contains a backport of commit [537d20af](https://github.com/openjdk/jdk/commit/537d20afbff255489a7b1bdb0410b9d1aba715b7) from the [openjdk/jdk](https://git.openjdk.org/jdk) repository.

The commit being backported was authored by Jan Lahoda on 10 Jul 2024 and was reviewed by Aggelos Biboudis.

Thanks!

Original description:
When javac parser see a `case` label, it needs to disambiguate between expressions (constant labels) and patterns. But, for code like:

case R(int x) when (x > 0)


the parser will try to parse the label as an expression, which is obviously not correct. The problem is that it does not disambiguate before `when`, and there indeed is an expression after `when`.

The proposal is to disambiguate as a pattern once there is `when` after a closing parenthesis at the top-level. This should prevent the code to even look at the `when` expression

-------------

Commit messages:
 - Backport 537d20afbff255489a7b1bdb0410b9d1aba715b7

Changes: https://git.openjdk.org/jdk/pull/20111/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=20111&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8335766
  Stats: 14 lines in 2 files changed: 12 ins; 0 del; 2 mod
  Patch: https://git.openjdk.org/jdk/pull/20111.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/20111/head:pull/20111

PR: https://git.openjdk.org/jdk/pull/20111


More information about the compiler-dev mailing list