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

Ilyas Selimov ilyas.selimov at jetbrains.com
Tue Jun 22 08:37:36 UTC 2021


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?

Thanks,
Ilyas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20210622/5e58e158/attachment.htm>


More information about the compiler-dev mailing list