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

Jan Lahoda jlahoda at openjdk.org
Mon Apr 7 11:59:56 UTC 2025


On Wed, 26 Mar 2025 07:47:19 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

> 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.

This pull request has now been integrated.

Changeset: 26bb1837
Author:    Jan Lahoda <jlahoda at openjdk.org>
URL:       https://git.openjdk.org/jdk/commit/26bb18378737809542b1153f8f34d55d409ea4e2
Stats:     181 lines in 3 files changed: 178 ins; 0 del; 3 mod

8352621: MatchException from backwards incompatible change to switch expressions

Reviewed-by: abimpoudis

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

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


More information about the compiler-dev mailing list