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

javac can skip a temporary local variable when pattern matching over a local variable

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 17
    • 17
    • tools
    • None
    • b09

      Consider this code:
      ---
      public class PatternLocal {
          public static boolean test(Object o) {
              return o instanceof String s && s.length() > 0;
          }
      }
      ---

      javac desugars the pattern test into:
      ---
      public class PatternLocal {
          
          public PatternLocal() {
              super();
          }
          
          public static boolean test(Object o) {
              return (let String s in (let /*synthetic*/ Object s$temp = o in s$temp instanceof String && (let s = (String)s$temp; in true)) && s.length() > 0);
          }
      }
      ---

      The temporary variable s$temp is unnecessary here, as o is already a local variable and its value cannot change inside the pattern matching expression.

            jlahoda Jan Lahoda
            jlahoda Jan Lahoda
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: