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

Reviewed-by: vromero
This commit is contained in:
Jan Lahoda 2023-06-21 09:16:12 +00:00
parent a15db1a56c
commit 01623f6a57
3 changed files with 13 additions and 4 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac/comp
test/langtools/tools/javac/patterns

@ -3202,7 +3202,7 @@ public class Flow {
void checkEffectivelyFinal(DiagnosticPosition pos, VarSymbol sym) {
if (currentTree != null &&
sym.owner.kind == MTH &&
sym.pos < currentTree.getStartPosition()) {
sym.pos < getCurrentTreeStartPosition()) {
switch (currentTree.getTag()) {
case CLASSDEF:
case CASE:
@ -3214,6 +3214,11 @@ public class Flow {
}
}
int getCurrentTreeStartPosition() {
return currentTree instanceof JCCase cse ? cse.guard.getStartPosition()
: currentTree.getStartPosition();
}
@SuppressWarnings("fallthrough")
void letInit(JCTree tree) {
tree = TreeInfo.skipParens(tree);

@ -23,7 +23,7 @@
/*
* @test
* @bug 8262891
* @bug 8262891 8310133
* @summary Check errors reported for guarded patterns.
* @compile/fail/ref=GuardsErrors.out -XDrawDiagnostics GuardsErrors.java
*/
@ -54,6 +54,9 @@ public class GuardsErrors {
return i == 2;
}
}.test() -> {}
case Integer v when v != null -> {
v = null;
}
case Number v1 when v1 instanceof Integer v2 && (v2 = 0) == 0 -> {}
default -> {}
}

@ -2,5 +2,6 @@ GuardsErrors.java:36:38: compiler.err.cant.ref.non.effectively.final.var: check,
GuardsErrors.java:47:34: compiler.err.cannot.assign.not.declared.guard: i1
GuardsErrors.java:48:33: compiler.err.cant.ref.non.effectively.final.var: i2, (compiler.misc.guard)
GuardsErrors.java:49:35: compiler.err.cant.ref.non.effectively.final.var: i2, (compiler.misc.guard)
GuardsErrors.java:64:34: compiler.err.cannot.assign.not.declared.guard: f
5 errors
GuardsErrors.java:57:33: compiler.err.cant.ref.non.effectively.final.var: v, (compiler.misc.guard)
GuardsErrors.java:67:34: compiler.err.cannot.assign.not.declared.guard: f
6 errors