Switch statement containing both null and guarded pattern freezes during execution
Jan Lahoda
jan.lahoda at oracle.com
Thu Jul 15 10:24:57 UTC 2021
Hi Ilyas,
Please see:
https://bugs.openjdk.java.net/browse/JDK-8269146
This should report a compile-time error in more recent builds:
$ javac --enable-preview -source 17 T.java
T.java:44: error: illegal fall-through to a pattern
case Integer ii && ii != null:
^
Jan
On 15. 07. 21 12:07, Ilyas Selimov wrote:
> Hello!
>
> I tried to execute the following code using OpenJDK 17 (build
> 17-ea+30-2618) and got the freeze:
>
> public static void main(String[] args) {
> test(null);
> }
>
> static void test(Integer i) {
> switch (i) {
> case null:
> case Integer ii && ii != null:
> System.out.println("int");
> break;
> default:
> System.out.println("def");
> }
> }
>
> Looking at the test method's bytecode, it seems the issue is related
> to the "goto" instruction on the 44's position:
>
> static void test(java.lang.Integer);
> Code:
> 0: aload_0
> 1: astore_1
> 2: iconst_0
> 3: istore_2
> 4: aload_1
> 5: iload_2
> 6: invokedynamic #13, 0 // InvokeDynamic
> #0:typeSwitch:(Ljava/lang/Object;I)I
> 11: lookupswitch { // 2
> -1: 36
> 0: 36
> default: 58
> }
> 36: aload_1
> 37: astore_3
> 38: aload_3
> 39: ifnonnull 47
> 42: iconst_1
> 43: istore_2
> 44: goto 4
> 47: getstatic #17 // Field
> java/lang/System.out:Ljava/io/PrintStream;
> 50: ldc #23 // String int
> 52: invokevirtual #25 // Method
> java/io/PrintStream.println:(Ljava/lang/String;)V
> 55: goto 66
> 58: getstatic #17 // Field
> java/lang/System.out:Ljava/io/PrintStream;
> 61: ldc #31 // String def
> 63: invokevirtual #25 // Method
> java/io/PrintStream.println:(Ljava/lang/String;)V
> 66: return
>
> The third_var = null, as third_var = first_var = null.
> So the result of "ifnonnull" instruction seems false and we meet
> "goto" instruction, jump before the "lookupswitch" position again.
>
> Seems the issue is related to
> https://bugs.openjdk.java.net/browse/JDK-8269141
> <https://bugs.openjdk.java.net/browse/JDK-8269141>.
> Could somebody take a look?
>
> Thanks,
> Ilyas
More information about the compiler-dev
mailing list