Bug 250185 - The Eclipse Compiler incorrectly reports the field is defined in an inherited type and an enclosing scope.
Summary: The Eclipse Compiler incorrectly reports the field is defined in an inherited...
Status: VERIFIED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: 3.5 M6   Edit
Assignee: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-10-08 17:03 EDT by Raj Gunaratnam CLA
Modified: 2009-03-10 07:39 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Raj Gunaratnam CLA 2008-10-08 17:03:34 EDT
Build ID:  I20080617-2000

Steps To Reproduce:

The Eclipse compiler incorrectly reports the field is defined in an inherited type and an enclosing scope when the JDK Compliance level is set to JDK 1.3. In the below code fragment, the eclipse java compiler will report "The field _state is defined in an inherited type and an enclosing scope". This error will disappear if the JDK Compliance level is set to 1.4 or above.

Since compiling for CLDC requires a JDK 1.3 Compliance level target, This will be a continuous issue for any JavaME developers.

public class ClassA {
      ClassB _state;

      public class ClassB {
          int _state;
      }

      public class ClassD extends ClassB {
          public void myMethod() {
             _state = 0; //This gives a compilation error 
                         //when compiler compliance level 
                         //is set to JDK 1.3
          }
      }//end of ClassD
}//end of ClassA

More information:
The JRE version I'm using is 1.5.0_14.
Comment 1 Philipe Mulet CLA 2008-10-09 03:39:56 EDT
The compliance level (1.3) emulates the corresponding JDK level.
If you feed javac 1.3.1 with your testcase, you will also get an error:

ClassA.java:10: _state is inherited from ClassA.ClassB and hides variable in outer class ClassA.  An explicit 'this' qualifier must be used to select the d
esired instance.
             _state = 0; //This gives a compilation error
             ^

Basically, the language evolved between 1.3 and 1.4 to privilege inherited over enclosing. This explains why a Javac 1.5 would compile it clean. Same for Eclipse compiler, if you set compliance above 1.3.

If you look closely to the compliance preference page, you can see that there are actually 3 settings:
1. compliance
2. source level
3. target level

1. emulates the corresponding JDK
2. is equivalent to option -source for javac
3. is equivalent to option -target for javac

So if all you care is to have a target JRE <= 1.3, then you can use:
1. Compliance = 1.4
2. Source = 1.3
3. Target = 1.2
(which are the defaults when toggling the Compliance to 1.4, and keep default source/target levels).


Comment 2 Philipe Mulet CLA 2009-01-22 03:59:03 EST
Srikanth - pls add some regression test (FieldAccessTest) and close as invalid (since the expectation does not match the spec)

The test should be using compliance check to set different expectation based on compliance level (e.g. LookupTest#test074)
Comment 3 Srikanth Sankaran CLA 2009-02-09 04:17:51 EST
(In reply to comment #2)
> Srikanth - pls add some regression test (FieldAccessTest) and close as invalid
> (since the expectation does not match the spec)
> The test should be using compliance check to set different expectation based on
> compliance level (e.g. LookupTest#test074)

There are numerous tests that elicit this diagnostic. In particular,

org.eclipse.jdt.core.tests.compiler.regression.SuperTypeTest.test002()
org.eclipse.jdt.core.tests.compiler.regression.SuperTypeTest.test003()
org.eclipse.jdt.core.tests.compiler.regression.SuperTypeTest.test004()

seem to test both the negative test (1.3) and conform test (1.4, 1.4+).

These tests look adequate to me. Did you have any additional scenarios in mind ? 




Comment 4 Kent Johnson CLA 2009-02-12 14:49:28 EST
Closing as Invalid for Srikanth

Existing regression tests are sufficient
Comment 5 Frederic Fusier CLA 2009-03-10 07:39:22 EDT
Verified for 3.5M6