Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 336934

Summary: [compiler] NPE in Scope.getTypeOrPackage
Product: [Eclipse Project] JDT Reporter: Szymon Ptaszkiewicz <sptaszkiewicz>
Component: CoreAssignee: Srikanth Sankaran <srikanth_sankaran>
Status: RESOLVED FIXED QA Contact:
Severity: major    
Priority: P3 CC: daniel_megert, jarthana, Olivier_Thomann
Version: 3.1Flags: john.arthorne: pmc_approved+
Target Milestone: 3.6.2+   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
.log
none
Project for reproduction
none
Proposed fix for 3.6. maintenance
none
Proposed fix + regression test
none
Proposed fix + regression test
none
Revised patch for HEAD.
none
Updated patch
none
Updated patch none

Description Szymon Ptaszkiewicz CLA 2011-02-11 08:35:36 EST
Created attachment 188771 [details]
.log

Build id: I20110118-0800

If package name collides with a type name and there is static import inside, then you get NPE. If the static import is commented out, then NPE is not thrown. Log is attached. You can reproduce the problem using the following code:

//------------------------------
package a;

public class B {
}
//------------------------------
package external;

public class Lib {
	public static void m() {
	}
}
//------------------------------
package a.B;

import static external.Lib.m;

public class C {
	public void main() {
		m();
	}
}
//------------------------------
Comment 1 Szymon Ptaszkiewicz CLA 2011-02-11 08:44:27 EST
Created attachment 188773 [details]
Project for reproduction
Comment 2 Dani Megert CLA 2011-02-15 10:47:15 EST
Simple step to reproduce: paste the following into the Package Explorer:
----------%<----------
package a;

public class B {
}
package external;

public class Lib {
    public static void m() {
    }
}
package a.B;

import static external.Lib.m;

public class C {
    public void main() {
        m();
    }
}
----------%<----------
Comment 3 Dani Megert CLA 2011-02-15 10:59:41 EST
Broken since 3.1.
Comment 4 Olivier Thomann CLA 2011-02-15 11:17:40 EST
This is an error case. That code should not compile and we detect properly the name collision cases.
I think when the current package cannot be se because the package name collides with a type, the import of the corresponding unit declaration should not be resolved.
I'll investigate a fix for this.
Comment 5 Olivier Thomann CLA 2011-02-15 11:48:43 EST
I see two ways to fix this issue:
1) When a package name collides with a type name, we report the error and we initialize the package of the unit scope to the default package to prevent further secondary errors like this NPE.
2) We don't call:
CompilationUnitScope#faultInTypes();
if the corresponding unit is tagged with errors (ignoreFurtherInvestigation is set to true).

Both ways will fix this issue (NPE). The actual code should not compile.

I would favor (1) as we have many cases where we don't check if the unit scope package binding is null.

Srikanth, your call.
Comment 6 Olivier Thomann CLA 2011-02-15 11:51:45 EST
Created attachment 189015 [details]
Proposed fix for 3.6. maintenance
Comment 7 Olivier Thomann CLA 2011-02-15 12:29:56 EST
Created attachment 189022 [details]
Proposed fix + regression test

Same patch for HEAD with some cleanup.
Comment 8 Olivier Thomann CLA 2011-02-15 12:56:40 EST
Created attachment 189028 [details]
Proposed fix + regression test

Updated patch for 3.6 maintenance with a regression test.
Comment 9 Olivier Thomann CLA 2011-02-15 13:49:42 EST
Note that whatever we do, the unit is not resolved as the package binding resolution caused it to be tagged as ignoreFurtherInvestigation. So what can be done with this unit is very limited.
Comment 10 Srikanth Sankaran CLA 2011-02-16 00:44:07 EST
Created attachment 189068 [details]
Revised patch for HEAD.

Agree with the fix.

Slightly deeper changes for HEAD eliminating a few more redundant null checks.
For 3.6.x stream, we can retain the patch already posted.
Comment 11 Srikanth Sankaran CLA 2011-02-16 02:57:57 EST
Released in HEAD for 3.7 M6.
Comment 12 Jay Arthanareeswaran CLA 2011-03-07 06:38:00 EST
Srikanth, do you intend to release this for 3.6.x as well?
Comment 13 Jay Arthanareeswaran CLA 2011-03-07 06:51:49 EST
Verified for 3.7M6 using build I20110301-1537.
Please reopen if the fix is required in 3.6.x.
Comment 14 Szymon Ptaszkiewicz CLA 2011-03-07 06:57:34 EST
Yes, it is required for 3.6.x
Comment 15 Olivier Thomann CLA 2011-03-07 08:40:26 EST
I'll release in 3.6 maintenance.
Comment 16 Olivier Thomann CLA 2011-03-07 10:40:40 EST
John, we need PMC approval to release this to 3.6.2+.
Thanks.
Comment 17 Olivier Thomann CLA 2011-03-07 10:42:55 EST
Created attachment 190559 [details]
Updated patch

Revised patch for 3.6 maintenance with appropriate bundle version changes.
Comment 18 Olivier Thomann CLA 2011-03-07 10:44:27 EST
Created attachment 190560 [details]
Updated patch

Same patch with copyrights update.
Comment 19 John Arthorne CLA 2011-03-07 10:47:41 EST
FYI, I have added the 3.6.2+ target milestone to bugzilla.
Comment 20 Olivier Thomann CLA 2011-03-07 10:48:56 EST
Released last patch in 3.6 maintenance.