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

Java Record Pattern Match leads to infinite loop

    XMLWordPrintable

Details

    • b17
    • 21
    • b12
    • generic
    • generic

    Description

      ADDITIONAL SYSTEM INFORMATION :
      Mac M1
      openjdk version "21" 2023-09-19
      OpenJDK Runtime Environment (build 21+35-2513)
      OpenJDK 64-Bit Server VM (build 21+35-2513, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      I see an infinite loop with with JDK 21 with this pattern match particularly with guarded match.
      Able to replicate every single time.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run this code.
      ----
      import java.util.Objects;

      public class Replicate {

          record Point(Object a, Object b){}
          public static void main(String[] args) {


              find(new Point(5, null));
              find(new Point(5, 4.5));

          }

          private static void find(Object point){
              switch (point){
                  case Point(Integer a, Integer b) -> System.out.println("both ints");
                  case Point(Integer a, var b) when tester(b) -> System.out.println("not null");
                  case Point(Integer a, var b) -> System.out.println("null");
                  case null, default -> System.out.println("Unexpected value: " + point);
              }
          }

          private static boolean tester(Object b){
              System.out.println("received : " + b);
              return Objects.nonNull(b);
          }

      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The program should print this and terminate.
      ---
          received: null
          null
          received: 4.5
          not null
      ACTUAL -
      Prints below infinitely
      ---
      received: null

      ---------- BEGIN SOURCE ----------
      import java.util.Objects;

      public class Replicate {

          record Point(Object a, Object b){}
          public static void main(String[] args) {


              find(new Point(5, null));
              find(new Point(5, 4.5));

          }

          private static void find(Object point){
              switch (point){
                  case Point(Integer a, Integer b) -> System.out.println("both ints");
                  case Point(Integer a, var b) when tester(b) -> System.out.println("not null");
                  case Point(Integer a, var b) -> System.out.println("null");
                  case null, default -> System.out.println("Unexpected value: " + point);
              }
          }

          private static boolean tester(Object b){
              System.out.println("received : " + b);
              return Objects.nonNull(b);
          }

      }

      ---------- END SOURCE ----------

      FREQUENCY : always


      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved: