RFR: 8300543 Compiler Implementation for Pattern Matching for switch [v4]
Rémi Forax
forax at openjdk.org
Fri Apr 21 15:38:00 UTC 2023
On Fri, 21 Apr 2023 15:30:24 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
>> Also, surprisingly, if I make C and D classes (instead of interfaces):
>>
>> class Test {
>> sealed interface I permits C, D { }
>> final class C implements I { }
>> final class D implements I { }
>>
>> interface F { }
>>
>> <Z extends I & F> int test(Z o) {
>> return switch (o) {
>> case C c -> 1;
>> case D d -> 2;
>> };
>> }
>> }
>>
>> I get errors like:
>>
>>
>> Foo.java:10: error: incompatible types: Z cannot be converted to Test.C
>> case C c -> 1;
>> ^
>> where Z is a type-variable:
>> Z extends I,F declared in method <Z>test(Z)
>>
>>
>> Which seems odd?
>
> Nevermind - these issues are due to a misunderstanding of the rules with intersection types - e.g. given A & B, if a pattern covers _any_ of A, B, then it covers A & B.
>
> Similarly, the second issue I reported is in reality caused by the fact that in the snippet with `class`, I also added `final` which then makes I & F a type with no concrete witnesses (so cast will always fail from there to either C/D).
> Which compiles correctly, but it doesn't look exhaustive to me (because of F) ?
Can you expand a little more, because for me a Z either implement C or D or both C and D.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/13074#discussion_r1173916081
More information about the core-libs-dev
mailing list