Regression Bug for Record patterns in Switch in JDK 20

Blaise B. blaisebass at gmail.com
Thu Feb 9 16:12:57 UTC 2023


Hello team,
I would like to report a behavior I stumbled upon when a record pattern in
switch is unconditional.

public class Main {
>       record SomeRecord(Number num) {}
>       public static void main(String[] args) {
>            //var rec = new SomeRecord(null);
>            var rec = new SomeRecord(1.0f);
>             switch (rec) {
>                    case SomeRecord(Double d) -> System.out.println("got a
> double: " + d);
>                    case SomeRecord(Number n) -> System.out.println("value
> is " + n);
>             }
>        }
>   }


Here, I expect variable "n" to be of type Number, but instead it displays
"value is SomeRecord[num=1.0]". When I comment out the first case label, it
works as expected.
The issue is not present on JDK 19. I've observed it only on EA builds of
jdk-20 and jdk-21.

I also tried a record with two components:

   public class Main {
>           record Foo(Number num, String s) {}
>           public static void main(String[] args) {
>                 Foo myfoo = new Foo(2, "me");
>                  switch (myfoo) {
>                        case Foo(Double d, String s) ->
> System.out.println("component values: " + d + ", " + s);
>                        case Foo(Number n, String s) ->
> System.out.println("component values: " + n + ", " + s);
>                  }
>             }
>      }


This time, the code compiles but fails to run with error message:

>           Error: Unable to initialize main class Main
>           Caused by: java.lang.VerifyError: Bad local variable type


Again it works fine on jdk-19.

Hope this helps.
Regards,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230209/57f008ea/attachment.htm>


More information about the amber-dev mailing list