RFR: 8300543 Compiler Implementation for Pattern Matching for switch [v5]
Rémi Forax
forax at openjdk.org
Mon Apr 24 09:09:06 UTC 2023
On Fri, 21 Apr 2023 16:25:04 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:
>> This is the first draft of a patch for JEP 440 and JEP 441. Changes included:
>>
>> - the pattern matching for switch and record patterns features are made final, together with updates to tests.
>> - parenthesized patterns are removed.
>> - qualified enum constants are supported for case labels.
>>
>> This change herein also includes removal record patterns in for each loop, which may be split into a separate PR in the future.
>
> Jan Lahoda has updated the pull request incrementally with two additional commits since the last revision:
>
> - Adding test.
> - Removing redundant continue, as noted on the review.
> I believe the exhaustiveness algorithm needs rules for union types, both in javac and in the JLS:
>
> ...
> I would expect the above switch to be exhaustive, since it covers all possible components of the union type. In other words, the union type should act as a sealed hierarchy which permits Foo and Bar - and the two cases should be merged together (e.g. `case Foo` + `case Bar` should cover `Foo | Bar`). For the records, I get same error on JDK 20.
Precise exception type, here, `Foo | Bar` inside a catch is not an union, it's a way to merge catches when the body of catches are the same. In term of typing, `Foo | Bar` behave as the lub of Foo and Bar, apart in a throw.
So the switch is not exhaustive here because `lub(Foo, Bar) = Exception`.
We may add a special case in JLS for supporting precise exception type in switch given that this issue have been reported several times, but this is not something that should be fixed by this PR.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/13074#issuecomment-1519675744
More information about the core-libs-dev
mailing list