Skip to content

Commit b8ad6cd

Browse files
archiecobbsVicente Romero
authored and
Vicente Romero
committed
8294461: wrong effectively final determination by javac
Reviewed-by: vromero
1 parent d667895 commit b8ad6cd

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -2006,14 +2006,13 @@ void newVar(JCVariableDecl varDecl) {
20062006
void letInit(DiagnosticPosition pos, VarSymbol sym) {
20072007
if (sym.adr >= firstadr && trackable(sym)) {
20082008
if ((sym.flags() & EFFECTIVELY_FINAL) != 0) {
2009-
if (!uninits.isMember(sym.adr)) {
2010-
//assignment targeting an effectively final variable
2011-
//makes the variable lose its status of effectively final
2012-
//if the variable is _not_ definitively unassigned
2009+
if (inits.isMember(sym.adr) || !uninits.isMember(sym.adr)) {
2010+
//assignment targeting an effectively final variable makes the
2011+
//variable lose its status of effectively final if the variable
2012+
//is definitely assigned or _not_ definitively unassigned
20132013
sym.flags_field &= ~EFFECTIVELY_FINAL;
2014-
} else {
2015-
uninit(sym);
20162014
}
2015+
uninit(sym);
20172016
}
20182017
else if ((sym.flags() & FINAL) != 0) {
20192018
if ((sym.flags() & PARAMETER) != 0) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @test /nodynamiccopyright/
3+
* @summary Verify for() loop variable not effectively final even if loop never increments
4+
* @bug 8294461
5+
* @compile/fail/ref=EffectivelyFinalLoopIncrement.out -XDrawDiagnostics EffectivelyFinalLoopIncrement.java
6+
*/
7+
class EffectivelyFinalLoopIncrement {
8+
EffectivelyFinalLoopIncrement() {
9+
for (int i = 0; i < 10; i++) {
10+
Runnable r = () -> System.out.println(i); // variable i is NOT effectively final
11+
break; // even though "i++" is never reached
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
EffectivelyFinalLoopIncrement.java:10:51: compiler.err.cant.ref.non.effectively.final.var: i, (compiler.misc.lambda)
2+
1 error

0 commit comments

Comments
 (0)