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

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 17
    • Fix Version/s: 17
    • Component/s: tools
    • Labels:
      None
    • Subcomponent:
    • Resolved In Build:
      b09

      Description

      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.

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: