Summary: | Wrong warning: The assignment to variable has no effect | ||||||||
---|---|---|---|---|---|---|---|---|---|
Product: | [Eclipse Project] JDT | Reporter: | Steffen Heil <bugs.eclipse.org> | ||||||
Component: | Core | Assignee: | Srikanth Sankaran <srikanth_sankaran> | ||||||
Status: | VERIFIED FIXED | QA Contact: | |||||||
Severity: | minor | ||||||||
Priority: | P3 | CC: | Olivier_Thomann, satyam.kandula | ||||||
Version: | 3.6 | ||||||||
Target Milestone: | 3.7 M1 | ||||||||
Hardware: | PC | ||||||||
OS: | Windows 7 | ||||||||
Whiteboard: | |||||||||
Attachments: |
|
Description
Steffen Heil
2010-04-23 07:16:29 EDT
> The java memory model enforces that every thread reading x and y in that order
> is guaranteed to see the value 3 for y. This is because assigning to x in one
> thread and reading x in the other establishes a happens-before-ordering.
>
> Yet eclipse also warns, if this assignment is done with a volatile variable. I
> think that it should not warn for volatiles or have an option to disable
> warnings in that special case.
Per initial reading, this does sound correct. I'll look at suppressing
this warning for volatiles for 3.7 M1.
One additional question: In the meantime I get a little worried that the problem is bigger than just a warning: Does the compiler emmit any bytecode for such statements? Or is this optimized away? (In that case not only the warning was anoying, but also the resulting classes were broken.) (In reply to comment #2) > One additional question: > > In the meantime I get a little worried that the problem is bigger than just a > warning: Does the compiler emmit any bytecode for such statements? Or is this > optimized away? (In that case not only the warning was anoying, but also the > resulting classes were broken.) All the compiler seems to do with the volatile modifier is to faithfully expose it to downstream components and do some error reporting as needed (e.g. illegal combination of "final" and "volatile" etc) I don't see the compiler doing any load avoidance in the first place for volatile to have an effect: (1) public class FetchEach { int field; public int thrice() { return field + field + field; } } Code generated: public int thrice(); Code: Stack=2, Locals=1, Args_size=1 0: aload_0 1: getfield #18; //Field field:I 4: aload_0 5: getfield #18; //Field field:I 8: iadd 9: aload_0 10: getfield #18; //Field field:I 13: iadd 14: ireturn (2) public class FetchEach { volatile int field; public int thrice() { return field + field + field; } } Code generated: public int thrice(); Code: Stack=2, Locals=1, Args_size=1 0: aload_0 1: getfield #18; //Field field:I 4: aload_0 5: getfield #18; //Field field:I 8: iadd 9: aload_0 10: getfield #18; //Field field:I 13: iadd 14: ireturn So we should be good. (3) Returning to your original question, no this warning has no effect on the code generated: public class FetchEach { volatile int field; public int thrice() { field = field; return 0; } } produces the warning and generates the following: public int thrice(); Code: Stack=2, Locals=1, Args_size=1 0: aload_0 1: aload_0 2: getfield #18; //Field field:I 5: putfield #18; //Field field:I 8: iconst_0 9: ireturn Created attachment 174162 [details]
Patch under consideration
(In reply to comment #4) > Created an attachment (id=174162) [details] > Patch under consideration This patch (intentionally) leaves the warning in place for cases like: class X { volatile int x = this.x;\n" + int nvx = this.nvx;\n" + } however unlikely it is to be useful. Created attachment 174166 [details]
Full Patch
Earlier patch was missing some changes.
Satyam, please review, TIA. (In reply to comment #7) > Satyam, please review, TIA. The changes look good. +1 Released in HEAD for 3.7 M1. Verified for 3.7M1. |