8294461: wrong effectively final determination by javac

Reviewed-by: vromero
This commit is contained in:
Archie L. Cobbs 2022-10-27 15:58:46 +00:00 committed by Vicente Romero
parent d6678952a6
commit b8ad6cd98a
3 changed files with 21 additions and 6 deletions

View File

@ -2006,14 +2006,13 @@ public class Flow {
void letInit(DiagnosticPosition pos, VarSymbol sym) {
if (sym.adr >= firstadr && trackable(sym)) {
if ((sym.flags() & EFFECTIVELY_FINAL) != 0) {
if (!uninits.isMember(sym.adr)) {
//assignment targeting an effectively final variable
//makes the variable lose its status of effectively final
//if the variable is _not_ definitively unassigned
if (inits.isMember(sym.adr) || !uninits.isMember(sym.adr)) {
//assignment targeting an effectively final variable makes the
//variable lose its status of effectively final if the variable
//is definitely assigned or _not_ definitively unassigned
sym.flags_field &= ~EFFECTIVELY_FINAL;
} else {
uninit(sym);
}
uninit(sym);
}
else if ((sym.flags() & FINAL) != 0) {
if ((sym.flags() & PARAMETER) != 0) {

View File

@ -0,0 +1,14 @@
/*
* @test /nodynamiccopyright/
* @summary Verify for() loop variable not effectively final even if loop never increments
* @bug 8294461
* @compile/fail/ref=EffectivelyFinalLoopIncrement.out -XDrawDiagnostics EffectivelyFinalLoopIncrement.java
*/
class EffectivelyFinalLoopIncrement {
EffectivelyFinalLoopIncrement() {
for (int i = 0; i < 10; i++) {
Runnable r = () -> System.out.println(i); // variable i is NOT effectively final
break; // even though "i++" is never reached
}
}
}

View File

@ -0,0 +1,2 @@
EffectivelyFinalLoopIncrement.java:10:51: compiler.err.cant.ref.non.effectively.final.var: i, (compiler.misc.lambda)
1 error