Question regarding exhaustiveness check of record patterns

Tesla Zhang ice1000kotlin at foxmail.com
Thu Nov 10 14:32:52 UTC 2022


Is this bug going to be fixed? 





Regards,
Tesla

---Original---
From: "Remi Forax"<forax at univ-mlv.fr>
Date: Wed, Nov 2, 2022 15:11 PM
To: "Tesla Ice Zhang"<ice1000kotlin at foxmail.com>;
Cc: "amber-dev"<amber-dev at openjdk.org>;
Subject: Re: Question regarding exhaustiveness check of record patterns







From: "Tesla Ice Zhang" <ice1000kotlin at foxmail.com>
To: "amber-dev" <amber-dev at openjdk.org>
Sent: Wednesday, November 2, 2022 7:59:52 PM
Subject: Question regarding exhaustiveness check of record patterns

Hi,
I am confused by a compile error generated by the following code:

sealed interface I<T> {}
record A<T>(T t) implements I<T> {}
public static void main(I<String> args) {
  switch (args) {
    case A<String>(var x) -> System.out.println(x);
  }
}


Compiling the code with the following JDK:

openjdk 19 2022-09-20
OpenJDK Runtime Environment (build 19+37)
OpenJDK 64-Bit Server VM (build 19+37, mixed mode, sharing)


I get:

.\JavaBug.java:5: error: the switch statement does not cover all possible input values
    switch (args) {
    ^
Note: .\JavaBug.java uses preview features of Java SE 19.
Note: Recompile with -Xlint:preview for details.
1 error


What cases could be not covered? It looks like a bug to me.


It is a bug, i'm able to reproduce it.



TeslaSwitchTest.java:6: error: the switch statement does not cover all possible input values
    switch (args) {
    ^
Note: TeslaSwitchTest.java uses preview features of Java SE 20.
Note: Recompile with -Xlint:preview for details.
1 error



As a workaround, using a type parameter works



  static <T> void main(I<T> args) {
    switch (args) {
      case A<T>(var x) -> System.out.println(x);
    }
  }





Also, I believe that showing the particular uncovered cases in the error messages would make very good developer experiences.


yes !




Best regards,
Tesla


Rémi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20221110/f91c1368/attachment.htm>


More information about the amber-dev mailing list