Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8314578

Non-verifiable code is emitted when two guards declare pattern variables in colon-switch

    XMLWordPrintable

Details

    • 21
    • b20

    Backports

      Description

        Code:

        class Test {
          public static void main(String[] args) {
            test(1);
            test("hello");
          }

          record R1() {}
          record R2() {}
          
          static void test(Object o) {
            switch (o) {
              case R1() when o instanceof String s:
              case R2() when o instanceof Integer i:
                System.out.println("hello: " + i);
                break;
              default:
                break;
            }
          }
        }

        Expected: should not be compilable: pattern variables declared in guard should not be visible in the switch label body.

        Actual: Compilable (even without preview) and produces VerifyError at runtime:

        Exception in thread "main" java.lang.VerifyError: Bad local variable type
        Exception Details:
          Location:
            Test.test(Ljava/lang/Object;)V @73: aload_2
          Reason:
            Type integer (current frame, locals[2]) is not assignable to reference type
          Current Frame:
            bci: @73
            flags: { }
            locals: { 'java/lang/Object', 'java/lang/Object', integer }
            stack: { 'java/io/PrintStream' }
          Bytecode:
            0000000: 2a59 b800 1557 4c03 3d2b 1cba 001b 0000
            0000010: ab00 0000 0000 0045 0000 0002 0000 0000
            0000020: 0000 001c 0000 0001 0000 0036 2bc0 001f
            0000030: 4e2a c100 2199 000c 2ac0 0021 3a04 a700
            0000040: 0804 3da7 ffc6 b200 232c ba00 2900 00b6
            0000050: 002d a700 06a7 0003 b1
          Stackmap Table:
            append_frame(@9,Object[#2],Integer)
            same_frame(@44)
            append_frame(@65,Object[#31])
            chop_frame(@70,1)
            same_frame(@85)
            chop_frame(@88,2)

        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(Class.java:537)
        at java.base/java.lang.Class.forName(Class.java:516)
        at jdk.compiler/com.sun.tools.javac.launcher.Main.execute(Main.java:432)
        at jdk.compiler/com.sun.tools.javac.launcher.Main.run(Main.java:208)
        at jdk.compiler/com.sun.tools.javac.launcher.Main.main(Main.java:135)

        Attachments

          Issue Links

            Activity

              People

                abimpoudis Angelos Bimpoudis
                tvaleev Tagir Valeev
                Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: