Final field update in unreachable for loop update section

Thanks for the interesting test case. You're pushing on this part of the language spec: https://docs.oracle.com/javase/specs/jls/se9/html/jls-16.html#jls-16.2.12 <https://docs.oracle.com/javase/specs/jls/se9/html/jls-16.html> https://docs.oracle.com/javase/specs/jls/se9/html/jls-16.html#jls-16.2.13 <https://docs.oracle.com/javase/specs/jls/se9/html/jls-16.html#jls-16.2.13> > On Feb 7, 2018, at 7:49 PM, Tagir Valeev <amaembo at gmail.com> wrote: > > public class Sample1 { > final int x; > > Sample1() { > for(;;x=1) { > x=2; > break; > } > } > } An error should only occur if 'x' is assigned to where it is definitely unassigned ("DU"). The rules say that: - x is DU before the for loop - x is DU after the (empty) initialization - assuming x is DU before the (empty) condition: - x is DU before the loop body* - x is DU after the 'break' (because this is unreachable) - x is DU after the loop body - thus, x is DU before the (empty) condition - x is DU before 'x=2' - x is DU after the 'break' - x is DU after the loop body - x is DU before the incrementation statement - x is DU before 'x=1' (*The connection between the start of the condition and the start of the loop body is a little confusing in the spec, because when the condition is empty, it refers to the initialization instead. But you get the same answer either way.) So, the compiler is wrong: there is no error in Sample1. > public class Sample2 { > final int x; > > Sample2() { > x=2; > for(;;x=1) { > break; > } > } > } No error here, which is fine. 'x=1' is unreachable, so it doesn't matter that it performs an assignment. (You get the same behavior if you do 'if (false) x = 1'.) —Dan -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20180215/ddbc8b72/attachment.html>