[jdk17] RFR: 8269354: javac crashes when processing parenthesized pattern in instanceof

Jan Lahoda jlahoda at openjdk.java.net
Fri Jun 25 14:53:10 UTC 2021


When having code like:

if (o instanceof String s) {
     System.err.println(s);
}


`javac` needs to hoist the `s` binding variable out of the `if` statement. This is done through `BindingContext` in `TransPatterns`. Sometimes, there is no statement out of which the variable would need to be hoisted, like:

boolean b = o instanceof String s && !s.isEmpty();


so some expressions (`&&` in the example) also serve as a place where the binding variables can be hoisted. With the addition of parenthesized and guarded patterns, the `instanceof` expression needs to be one of such expressions so that in expressions like:

boolean b = o instanceof (String s && !s.isEmpty());


the `s.isEmpty()` uses the correct translated/hoisted variable.

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

Commit messages:
 - 8269354: javac crashes when processing parenthesized pattern in instanceof

Changes: https://git.openjdk.java.net/jdk17/pull/147/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk17&pr=147&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8269354
  Stats: 14 lines in 2 files changed: 7 ins; 0 del; 7 mod
  Patch: https://git.openjdk.java.net/jdk17/pull/147.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk17 pull/147/head:pull/147

PR: https://git.openjdk.java.net/jdk17/pull/147


More information about the compiler-dev mailing list