Bug 316956 - [compiler] Private superclass and enclosing scope field names incorrectly reported as conflicting
Summary: [compiler] Private superclass and enclosing scope field names incorrectly rep...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.5.2   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.7 M1   Edit
Assignee: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-06-15 14:13 EDT by vassili CLA
Modified: 2010-08-04 16:06 EDT (History)
3 users (show)

See Also:


Attachments
junit from submitter's report. (1.97 KB, patch)
2010-06-24 23:50 EDT, Srikanth Sankaran CLA
no flags Details | Diff
Patch under consideration (6.67 KB, patch)
2010-07-05 07:24 EDT, Srikanth Sankaran CLA
no flags Details | Diff
Same patch after CVS synchronization. (6.68 KB, patch)
2010-07-12 23:54 EDT, Srikanth Sankaran CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description vassili CLA 2010-06-15 14:13:49 EDT
Build Identifier: M20100211-1343

The following code compiles cleanly in javac and shows an error in Eclipse.

class A {
  private int x;
  static class B {
    private int x;
    private C c = new C() {
      void foo() {
        x = 3;  // error: "The field x is defined in an inherited type and an enclosing scope"
      }
    };
  }
  static class C {
    private int x;
  }
}

See JLS 8.1.6, last two paragraphs, and JLS 8.2 "Members of a class that are declared private are not inherited by subclasses of that class". C.x is not in scope in the anonymous subclass of C and the only possible meaning of the name x is B.x.

Interesting fact #1: if C.x is changed to public or protected the compiler correctly shadows B.x with C.x and the error goes away.

Interesting fact #2: the following variation with the anonymous class pulled one level up compiles correctly:

class A {
  private int x;
  private C c = new C() {
    void foo() {
      x = 3;
    }
  };
  static class C {
    private int x;
  }
 }


Reproducible: Always

Steps to Reproduce:
Paste the snippet above into a Java editor.
Comment 1 Srikanth Sankaran CLA 2010-06-24 23:50:52 EDT
Created attachment 172705 [details]
junit from submitter's report.

This codifies the current behavior and will have to change
suitably as the source code changes.
Comment 2 Srikanth Sankaran CLA 2010-07-05 07:24:52 EDT
Created attachment 173410 [details]
Patch under consideration
Comment 3 Srikanth Sankaran CLA 2010-07-05 09:59:33 EDT
All tests pass. Jay, please review. Thanks in advance.
Comment 4 Jay Arthanareeswaran CLA 2010-07-12 08:11:27 EDT
Patch looks good to me.

One observation, though. When I changed the specifier to public in the super class as in the example below, I get the compiler error as mentioned in the comment #0. However, this error doesn't appear if the compiler level is set to >= 1.4. 

class A {
  static class B {
    private int x;
    public C c = new C() {
      void foo() {
        x = 3;  // Error only when compiler is set to 1.3
      }
    };
  }
  static class C {
    public int x;
  }
}

Looking at the code, this might be the expected behavior after all. One concern, though, is that javac doesn't report any error at all no matter what -source I set.
Comment 5 Srikanth Sankaran CLA 2010-07-12 23:45:42 EDT
(In reply to comment #4)
> Patch looks good to me.
> 
> One observation, though. When I changed the specifier to public in the super
> class as in the example below, I get the compiler error as mentioned in the
> comment #0. However, this error doesn't appear if the compiler level is set to
> >= 1.4. 

[...]

> Looking at the code, this might be the expected behavior after all.

Yes, Please see http://dev.eclipse.org/mhonarc/lists/jdt-core-dev/msg00071.html
(The JLS sections mentioned there is not w.r.t JLS3). So this is a deliberate
choice.

See also: https://bugs.eclipse.org/bugs/show_bug.cgi?id=3350 and
https://bugs.eclipse.org/bugs/show_bug.cgi?id=3361
Comment 6 Srikanth Sankaran CLA 2010-07-12 23:54:01 EDT
Created attachment 174113 [details]
Same patch after CVS synchronization.
Comment 7 Srikanth Sankaran CLA 2010-07-12 23:58:41 EDT
Released in HEAD for 3.7 M1.
Comment 8 Olivier Thomann CLA 2010-08-04 16:06:38 EDT
Verified for 3.7M1.