RFR: 8302865: Illegal bytecode for break from if with instanceof pattern matching condition

Jan Lahoda jlahoda at openjdk.org
Fri Jun 16 13:13:06 UTC 2023


Please see the CSR for the exact wording of the specification change:
https://bugs.openjdk.org/browse/JDK-8310016

There are two changes to flow scoping under this PR:

a) for code like:


X: if (!(o instanceof String s)) {
    break X;
}
System.err.println(s);


`s` won't be in the scope for `System.err.println` anymore (as that leads to an invalid bytecode anyway). I was experimenting with several possibilities to fix this, and the final proposal is (when we are about to add some bindings to the scope), look for an immediately enclosing labelled statement(s), and there's some, look for a break to this statement(s).

b) for code like:


X: {
         while (!(o instanceof String s)) {
             break X;
         }
         System.err.println(s);
}


javac implements a strict interpretation of the specification, which does not include `s` in the scope for the `System.err.println` (due to the `break` out of the `while`). The specification is clarified, so that only breaks to the loop are considered (together with breaks to the enclosing labelled statements), so flow scoping handling for loops is adjusted to only consider breaks to the loop.

-------------

Commit messages:
 - Fixing search for immediately enclosing labelled statements.
 - Fixing whitespaces.
 - 8302865: Illegal bytecode for break from if with instanceof pattern matching condition

Changes: https://git.openjdk.org/jdk/pull/14517/files
 Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=14517&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8302865
  Stats: 244 lines in 6 files changed: 176 ins; 49 del; 19 mod
  Patch: https://git.openjdk.org/jdk/pull/14517.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/14517/head:pull/14517

PR: https://git.openjdk.org/jdk/pull/14517


More information about the compiler-dev mailing list