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

Infinite loop in Pattern switch with guarded patterns in JDK 17 ea27/ea26

XMLWordPrintable

    • b26
    • 17
    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      I see an infinite loop with with JDK 17 ea27/ea26 for pattern switch;
      given switch pattern with guarded pattern case:
       
      public class X {
        private static void foo(Object o) {
          switch (o) {
            case String s1 && s1.length()>1: System.out.println("Guarded"); break;
            default : System.out.println("Object");
          }
        }
         public static void main(String[] args) {
          foo(new Object());
        }
      }
      compiles fine with ea27 (and ea26), but while running gets into an infinite loop.

      The disassembled code shows that there is a "goto 9" at 73 (to restart the switch) - I believe that this goto here is an error as switch need not be restarted at default; omitting the generation of this "goto" should remove the infinite loop.
       
       private static void foo(java.lang.Object arg0);
           ...
           9 aload_1
          10 iload_2
          11 invokedynamic 0 typeSwitch(java.lang.Object, int) : int [13]
          16 lookupswitch default: 65
                case 0: 36
          ....
          65 getstatic java.lang.System.out : java.io.PrintStream [23]
          68 ldc <String "Object"> [37]
          70 invokevirtual java.io.PrintStream.println(java.lang.String) : void [31]
          73 goto 9 <== this is an error
          76 return
       


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      javac --enable-preview --release 17 X.java; java --enable-preview X
      where X.java is the code in description and javac/java are from 17 ea+27

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Object
      ACTUAL -
      Infinite loop printing object

      ---------- BEGIN SOURCE ----------
      public class X {
        private static void foo(Object o) {
          switch (o) {
            case String s1 && s1.length()>1: System.out.println("Guarded"); break;
            default : System.out.println("Object");
          }
        }
         public static void main(String[] args) {
          foo(new Object());
        }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


            jlahoda Jan Lahoda
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: