Final field update in unreachable for loop update section

Vicente Romero vicente.romero at oracle.com
Thu Feb 15 20:39:04 UTC 2018


Thanks Dan for evaluating this one, certainly this set of rules is 
tricky to follow. I have created bug [1] to track this issue,

Vicente

[1] https://bugs.openjdk.java.net/browse/JDK-8198245

On 02/15/2018 03:16 PM, Dan Smith wrote:
> 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
>
>> On Feb 7, 2018, at 7:49 PM, Tagir Valeev <amaembo at gmail.com 
>> <mailto: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/cf92db0a/attachment-0001.html>


More information about the compiler-dev mailing list