Switch statement containing pattern case label element gets in the loop during execution

Remi Forax forax at univ-mlv.fr
Tue Jun 22 09:10:50 UTC 2021


> De: "Ilyas Selimov" <ilyas.selimov at jetbrains.com>
> À: "compiler-dev" <compiler-dev at openjdk.java.net>
> Envoyé: Mardi 22 Juin 2021 10:37:36
> Objet: Switch statement containing pattern case label element gets in the loop
> during execution

> Hello!
> I tried to execute the code using OpenJDK 17 (build 17-ea+27-2476):

> public static void main(String[] args) {
> test(1);
> }

> static void test(Integer i) {
> switch (i) {
> case Integer o && o != null:
> System.out.println("s1");
> default:
> System.out.println("def");
> }
> }

> and got the next output:
> s1
> def
> s1
> def
> ...

> As a result the switch statement gets in the loop.
> Looking at the bytecode of the method It seems the issue is related to the
> latest goto instruction:

> static void test(java.lang.Integer);
> Code:
> 0: aload_0
> 1: dup
> 2: invokestatic #19 // Method java/util/Objects.requireNonNull:
> 5: pop
> 6: astore_1
> 7: iconst_0
> 8: istore_2
> 9: aload_1
> 10: iload_2
> 11: invokedynamic #25, 0 // InvokeDynamic #0:typeSwitch:(Ljava/lang/Object;I)I
> 16: lookupswitch { // 1
> 0: 36
> default: 55
> }
> 36: aload_1
> 37: astore_3
> 38: aload_3
> 39: ifnonnull 47
> 42: iconst_1
> 43: istore_2
> 44: goto 9
> 47: getstatic #29 // Field java/lang/System.out:Ljava/io/PrintStream;
> 50: ldc #35 // String s1
> 52: invokevirtual #37 // Method
> java/io/PrintStream.println:(Ljava/lang/String;)V
> 55: getstatic #29 // Field java/lang/System.out:Ljava/io/PrintStream;
> 58: ldc #43 // String def
> 60: invokevirtual #37 // Method
> java/io/PrintStream.println:(Ljava/lang/String;)V
> 63: goto 9

> Could somebody take a look?

I agree with your analysis, the last goto should not be there. 

> Thanks,
> Ilyas

Rémi 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20210622/4cabd31e/attachment-0001.htm>


More information about the compiler-dev mailing list