Bug 313720 - [compiler] Self-assignment on declaration (_Can_ reference a field before it is defined)
Summary: [compiler] Self-assignment on declaration (_Can_ reference a field before it ...
Status: VERIFIED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.6   Edit
Hardware: PC Windows 7
: P3 minor (vote)
Target Milestone: 3.6 RC4   Edit
Assignee: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-05-20 09:14 EDT by Mattias CLA
Modified: 2010-06-07 02:43 EDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mattias CLA 2010-05-20 09:14:05 EDT
Build Identifier: 20090920-1017

For example
» static final int A = A;
generates "Cannot reference a field before it is defined" error (CORRECT),
but
» class C
» {
»     static final int A = C.A;
» }
does only generate a "The assignment to variable * has no effect" warning (INCORRECT), and A is give the default value (in the case 0).


Reproducible: Always

Steps to Reproduce:
1. Declare a class, e.g. C
2. In this class declare a static constant, e.g A
3. On declaration, assign C.A to A
Comment 1 Walter Harley CLA 2010-05-20 12:08:14 EDT
This also compiles without error in javac.  I suspect you've hit on a corner case in the Java Language Spec, but I'll leave it to the JDT Core folks to determine.
Comment 2 Olivier Thomann CLA 2010-05-20 12:56:39 EDT
Checking http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.2.3 it appears that qualified references are accepted.
Only simple references are rejected.

Srikanth, last word to you.
Comment 3 Stephan Herrmann CLA 2010-05-20 15:29:02 EDT
(In reply to comment #2)
> Checking
> http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.2.3 it
> appears that qualified references are accepted.
> Only simple references are rejected.

Isn't there a bug even in the JLS?
The first sentence states that checking for this particular problem is
only performed if all four requirements are met. Thus if any of the four
requirements is not met we're good without checking textual order.

The next sentence says:
"A compile-time error occurs if any of the four requirements above are not met."
which to me sounds like a contradiction.

Should we silently ignore that sentence?
Comment 4 Srikanth Sankaran CLA 2010-06-02 01:03:09 EDT
(In reply to comment #2)
> Checking
> http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.2.3 it
> appears that qualified references are accepted.
> Only simple references are rejected.

Indeed, there is even an example that explicitly legitimizes the
qualified name usage:

int z = UseBeforeDeclaration.x * 2;  // ok - not accessed via simple name 

(In reply to comment #3)
> (In reply to comment #2)
> > Checking
> > http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.2.3 it
> > appears that qualified references are accepted.
> > Only simple references are rejected.
> 
> Isn't there a bug even in the JLS?
> The first sentence states that checking for this particular problem is
> only performed if all four requirements are met. Thus if any of the four
> requirements is not met we're good without checking textual order.
> 
> The next sentence says:
> "A compile-time error occurs if any of the four requirements above are not
> met."
> which to me sounds like a contradiction.
> 
> Should we silently ignore that sentence?

I agree that the last sentence does sound contradictory and yes
IMO it is completely superfluous and should be ignored. The right
interpretation is clear from examples and such.
Comment 5 Satyam Kandula CLA 2010-06-07 02:43:51 EDT
Verified for 3.6RC4