RFR: 8352621: MatchException from backwards incompatible change to switch expressions
Aggelos Biboudis
abimpoudis at openjdk.org
Wed Mar 26 14:42:23 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.
LGTM!
test/langtools/tools/javac/patterns/NoPrimitivesAsCaseLabelsFor21.java line 72:
> 70:
> 71: @Test
> 72: public void testExhaustiveSealedClasses(Path base) throws Exception {
small fix about the name of the test is needed here
-------------
Marked as reviewed by abimpoudis (Reviewer).
PR Review: https://git.openjdk.org/jdk/pull/24241#pullrequestreview-2717504119
PR Review Comment: https://git.openjdk.org/jdk/pull/24241#discussion_r2014329443
More information about the compiler-dev
mailing list