RFR: 8352621: MatchException from backwards incompatible change to switch expressions

Jan Lahoda jlahoda at openjdk.org
Wed Mar 26 07:59:56 UTC 2025


Considering code like:

record R2(int i1, int i2) {}
...
        case R2(int i1, int i2) when i1 == 0 -> 2;
        case R2(int i1, int i2) -> 3;
....


javac will compile this along these lines:

   case R2 $temp: int $c1 = $temp.i1();
                               switch (i1) {
                                   case int i1 when ...
                                   ...
...


The nested switch is a pattern matching switch, and uses `SwitchBootstraps.typeSwitch`, passing the case label types (classes) as static arguments. The problem is that since primitive type patterns have been introduced, `typeSwitch` accepts primitive types as case labels, and javac will use such types in this case. Which works OK on JDK 23+.

But, on JDK 21 (and 22), `typeSwitch` cannot handle case labels of primitive types correctly, and the matching fails.

The solution proposed herein is to not use the primitive type case labels when targeting JDK 21 and 22, which makes the behavior similar to what javac did for this code in JDK 21.

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

Commit messages:
 - Removing trailing whitespace.
 - 8352621: MatchException from backwards incompatible change to switch expressions

Changes: https://git.openjdk.org/jdk/pull/24241/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=24241&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8352621
  Stats: 181 lines in 3 files changed: 178 ins; 0 del; 3 mod
  Patch: https://git.openjdk.org/jdk/pull/24241.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/24241/head:pull/24241

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


More information about the compiler-dev mailing list