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

Patterns: Fix specification for creation time of pattern variables

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Withdrawn
    • Affects Version/s: 16
    • Fix Version/s: 16
    • Component/s: specification
    • Labels:

      Description

      The specification states that a pattern variable is created when the pattern matches. This is incorrect - its creation time is determined by its scope.

      Example:


          if (e instanceof String s)
              System.out.println(s);

      In this example the pattern variable `s` is created when the statement is executed. (The compiler actually translate the conditional into a new block containing a declaration of a local variable `String s` before the translated conditional. This ensures that it is not in scope beyond the conditional statement.) However, in this case:

          if (!(e instanceof String s))
              return;

      As the pattern variable is in scope for all the statements that follow the `if-then` statement, the pattern variable is actually created at the start of the enclosing block.

      The specification needs to be reverted to an earlier version that was more explicit. (This bug is the result of an incorrect simplification to the specification for pattern variables.)

        Attachments

          Activity

            People

            • Assignee:
              gbierman Gavin Bierman
              Reporter:
              gbierman Gavin Bierman
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: