Bug 151787 - [compiler] compiler allows assignment to final field in constructor other than through 'this'
Summary: [compiler] compiler allows assignment to final field in constructor other tha...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.3 M7   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 190391 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-07-25 19:30 EDT by Christopher Unkel CLA
Modified: 2007-06-01 04:20 EDT (History)
2 users (show)

See Also:


Attachments
final field initialization test cases (1.73 KB, text/plain)
2006-07-25 19:31 EDT, Christopher Unkel CLA
no flags Details
Proposed patch (7.30 KB, patch)
2007-03-30 08:55 EDT, Philipe Mulet CLA
no flags Details | Diff
Better patch (90.89 KB, patch)
2007-03-30 11:01 EDT, Philipe Mulet CLA
no flags Details | Diff
Proposed patch for 3.2.2 (7.01 KB, patch)
2007-03-30 15:51 EDT, Philipe Mulet CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Christopher Unkel CLA 2006-07-25 19:30:25 EDT
The compiler incorrectly allows writes to final fields, within the constructor for the class declaring a field, when the access is not of the proper form "f = ", or "this.f =".

For example, in the following code:

public class FinalBug {
    // incorrectly passes compilation
    static class Test5 {
        private final Object o;
        
        Test5() {
            Test5 other = this;
            other.o = new Object(); // illegal!  other.o is not assignable
        } // error: this.o is not definitely assigned
    }
    
    // flags wrong statement as error
    static class Test6 {
        private final Object o;
        static Test6 initing;
        
        Test6() {
            initing = this;
            System.out.println("greetings");
            Test6 other = initing;
            other.o = new Object(); // illegal!  other.o is not assignable
            o = new Object(); // legal
        }
    }
}

There should be an error flagged within class Test5, on the marked line, and none is flagged.  Also field o is not "definitely assigned" (in terms of the language specification's meaning of "definitely assigned") at the end of the constructor of Test5, and that is also an error that should be flagged.

Likewise, in Test6, the assigment to other.o is illegal.  Eclipse incorrectly flags the assigment to o as illegal.
Comment 1 Christopher Unkel CLA 2006-07-25 19:31:47 EDT
Created attachment 46788 [details]
final field initialization test cases
Comment 2 Olivier Thomann CLA 2007-03-20 10:38:28 EDT
Reproduced using I20070320-0010.
Comment 3 Philipe Mulet CLA 2007-03-30 08:55:32 EDT
Created attachment 62485 [details]
Proposed patch
Comment 4 Philipe Mulet CLA 2007-03-30 09:02:00 EDT
Raising severity. This is quite bad in the compiler to miss this scenario.
Surprisingly it never got discovered until now... broken since day 1.
Comment 5 Philipe Mulet CLA 2007-03-30 11:01:08 EDT
Created attachment 62503 [details]
Better patch
Comment 6 Philipe Mulet CLA 2007-03-30 15:34:54 EDT
Released for 3.3M7
Comment 7 Philipe Mulet CLA 2007-03-30 15:51:55 EDT
Created attachment 62551 [details]
Proposed patch for 3.2.2
Comment 8 Philipe Mulet CLA 2007-03-30 15:52:34 EDT
Released in 3.2 maintenance stream.
Comment 9 Jerome Lanneluc CLA 2007-04-27 05:16:37 EDT
Verified for 3.3M7 with I20070427-0010
Comment 10 Philipe Mulet CLA 2007-06-01 04:20:47 EDT
*** Bug 190391 has been marked as a duplicate of this bug. ***