switch exhaustiveness check not working within deconstruction pattern.
Jan Lahoda
jan.lahoda at oracle.com
Mon Nov 14 11:41:28 UTC 2022
Hi,
This should indeed be exhaustive, AFAIK, and javac from JDK 19 seems to be happy with the code:
---
$ ./javac --enable-preview -source 19 /tmp/Main.java
Note: /tmp/Main.java uses preview features of Java SE 19.
Note: Recompile with -Xlint:preview for details.
---
What are the precise steps to reproduce/JDK version?
Thanks,
Jan
________________________________
From: amber-dev <amber-dev-retn at openjdk.org> on behalf of redio.development at gmail.com <redio.development at gmail.com>
Sent: Sunday, November 13, 2022 6:48 PM
To: amber-dev at openjdk.org <amber-dev at openjdk.org>
Subject: switch exhaustiveness check not working within deconstruction pattern.
package dev.redio;
import static dev.redio.Main.Activity.*;
public class Main {
public static void main(String[] args) {
record Pair(boolean weekend, Activity activity) {}
var pair = new Pair(true, new Sleeping(8));
var msg = switch (pair) { // <- A switch expression should have a default case Java(1073743531)
case Pair(boolean b, Sleeping s) -> "3";
case Pair(boolean b, Skiing s) -> "4";
case Pair(boolean b, Coding c) -> "5";
};
System.out.println(msg);
}
sealed interface Activity {
record Sleeping(int hours) implements Activity {}
record Skiing(String resort) implements Activity {}
record Coding() implements Activity {}
}
}
This deconstruction should be exhaustive since Pair is a guaranteed match boolean doesn’t have subclasses and Activity is sealed and fully covered.
Null should throw in this case. A default case would never be reached.
(Compiling and running with (dead) default case)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20221114/d6c027ca/attachment-0001.htm>
More information about the amber-dev
mailing list