Exhaustiveness for record pattern with switch (JEP 440+441)

Jan Lahoda jan.lahoda at oracle.com
Mon Jul 10 15:26:33 UTC 2023


Hi Mikhail,


I've filled:

https://bugs.openjdk.org/browse/JDK-8311815


for this. Thanks for the report!


Jan


On 07. 07. 23 19:51, Mikhail Pyltsin wrote:
> Hi!
> I investigated the algorithm, which Javac 21 (Build 30 (2023/7/6))
> ) uses to check exhaustiveness for record patterns (JEP 440+441), and 
> found the strange behavior:
> - let's take compilable code
> ```
> class Test22 {
>
>   record Pair(I i1, I i2) {}
>
>   sealed interface I {}
>
>   record C() implements I {}
>
>   record D() implements I {}
>
>   void exhaustinvenessWithInterface(Pair pairI) {
>     switch (pairI) {
>       case Pair(C fst, D snd) -> {
>       }
>       case Pair(C fst, C snd) -> {
>       }
>       case Pair(I fst, C snd) -> {
>       }
>       case Pair(D fst, D snd) -> {
>       }
>     }
>   }
> }
> ```
> - If we swap types of components, it starts to produce the error 
> "java: the switch statement does not cover all possible input values":
> ```
>   void exhaustinvenessWithInterface(Pair pairI) {
>     switch (pairI) {
>       case Pair(D fst, C snd) -> {
>       }
>       case Pair(C fst, C snd) -> {
>       }
>       case Pair(C fst, I snd) -> {
>       }
>       case Pair(D fst, D snd) -> {
>       }
>     }
>   }
> ```
> It happens because Javac tries to find the first distinguished type 
> from the beginning, but I couldn't find any mention of it in JEP.
> Is this the expected behavior?
>


More information about the compiler-dev mailing list