RFR: 8310133: Effectivelly final condition not enforced in guards for binding variables from the same case

Jan Lahoda jlahoda at openjdk.org
Thu Jun 15 16:32:49 UTC 2023


Consider code containing snippet like:

...
case String s when s.isEmpty() -> {
s = null;
}
...


The `s` binding variable is obviously not effectively final, but the specification currently requires variables used inside the guard to be effectively final or final.

The cause here is in a position check - for error reporting we need to sent a shared variable `currentTree` to the case, but the effectively final check will only check variables declared before the start of `currentTree` (to avoid reporting issues for variables inside a lambda, for example). The proposed fix is to keep the case inside `currentTree`, but ensure the guard's starting position is used, to allow a proper effectively final check.

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

Commit messages:
 - 8310133: Effectivelly final condition not enforced in guards for binding variables from the same case

Changes: https://git.openjdk.org/jdk/pull/14498/files
 Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=14498&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8310133
  Stats: 13 lines in 3 files changed: 9 ins; 0 del; 4 mod
  Patch: https://git.openjdk.org/jdk/pull/14498.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/14498/head:pull/14498

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


More information about the compiler-dev mailing list