Bug 110030 - [plan][compiler] Provide support for null reference analysis
Summary: [plan][compiler] Provide support for null reference analysis
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.2 M5   Edit
Assignee: Maxime Daniel CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 80252 107804 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-09-20 11:06 EDT by Jerome Lanneluc CLA
Modified: 2007-06-01 21:57 EDT (History)
9 users (show)

See Also:


Attachments
Null checks stage1: provides checks upon locals. (200.49 KB, patch)
2005-11-28 07:15 EST, Maxime Daniel CLA
no flags Details | Diff
Experimental version for checks upon locals (621.29 KB, patch)
2006-01-24 06:04 EST, Maxime Daniel CLA
no flags Details | Diff
Patch to change the behavior on second error - applies to v_634a (18.69 KB, patch)
2006-01-27 07:58 EST, Maxime Daniel CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jerome Lanneluc CLA 2005-09-20 11:06:25 EDT
I20050919

Compiler should provide support for null reference analysis, coupled with use of
@NonNull annotation.
Comment 1 Jerome Lanneluc CLA 2005-09-20 11:07:05 EDT
See also bug 96063
Comment 2 Maxime Daniel CLA 2005-11-28 07:15:33 EST
Created attachment 30700 [details]
Null checks stage1: provides checks upon locals.

No way to selectively suppress the warnings yet.
User preferences should include the following line to get the warnings activated within Eclipse:
org.eclipse.jdt.core.compiler.problem.nullReference=warning
Batch compiler added a new warning toggle option, which name is 'null'.
Comment 3 Maxime Daniel CLA 2005-11-28 07:32:33 EST
*** Bug 80252 has been marked as a duplicate of this bug. ***
Comment 4 Maxime Daniel CLA 2006-01-24 06:04:29 EST
Created attachment 33507 [details]
Experimental version for checks upon locals

Released as v_634a. May have to roll back depending on the results of an experimentation run within the team.
Comment 5 Maxime Daniel CLA 2006-01-27 07:58:05 EST
Created attachment 33699 [details]
Patch to change the behavior on second error - applies to v_634a

Following a remark from Frédéric about the fact that the warning on line 2 below was annoying, prototyped a version in which in case of error in a test against null, the null status of a variable is no more changed.
if (strings != null) {
  int length = strings == null ? 0 : strings.length; // 1 - complain
  for (int i = 0; i < length; i++) {
    System.out.println(strings[i]);                  // 2 - used to complain
  }
}
By the new behavior, we complain on line 1 and no more modify strings status, which means that it remains known as being non null (and 2 is OK).
This changes quite a few test cases diagnostics, which can be seen in the patch. But this only affects second and following situations (never the first diagnostic for a given variable) and the results seem rather OK.
Feedback will be appreciated.
Comment 6 Maxime Daniel CLA 2006-01-30 11:53:55 EST
Committed a couple of cleanup changes in preparation for integration build of tomorrow.
Did not release the suggestion of comment #5. Feedback welcome.
Comment 7 Tom Hofmann CLA 2006-02-03 04:30:44 EST
I understand that this request may go over the aim of analyzing local variables, but here it comes anyway:

A frequent pattern is to rely on methods throwing an exception if a null check fails, but this is not recognized by the analysis; the following code triggers a warning:

void m(Object o) {
  Assert.isNotNull(o);
  o.toString(); // warning
}

To solve this, the analysis would have to find out under what circumstances the exception is thrown...
Comment 8 Maxime Daniel CLA 2006-02-06 07:45:07 EST
(In reply to comment #7)
Thanks for your feedback.
What  is due to stretch what we've done so far quite a bit indeed.
This is part of inter procedural analysis, for which we may consider a separate effort. Moreover, this calls for something we had not thought much about yet: tell the system that a given method warranties that one of its operands is non null or else  the normal control flow gets broken (here it is an exception, but it could be a call to System#exit).
Would you please file a separate bug?
Comment 9 Tom Hofmann CLA 2006-02-06 08:24:00 EST
(In reply to comment #8)
> Would you please file a separate bug?

Bug 126551 

Comment 10 Maxime Daniel CLA 2006-02-10 05:47:59 EST
(In reply to comment #9)
> (In reply to comment #8)
> > Would you please file a separate bug?
> 
> Bug 126551 
> 
thx
Comment 11 Maxime Daniel CLA 2006-02-10 05:59:48 EST
Released in HEAD.
NullReferenceTest provides lots of examples of the feature behavior.
Comment 12 Axel Rauschmayer CLA 2006-02-13 14:02:11 EST
The following paper might be relevant for this bug:
P. Chalin and F. Rioux, "Non-null References by Default in the Java Modeling Language."
Can be downloaded at: http://www.cs.concordia.ca/~chalin/main.html

The authors argue (correctly, I think) that by default, one should assume that a method argument is meant to *not* be null. If not, you tag it (say, with @AllowNull). This would be the opposite of how the suggested @NonNull annotation would be used. 
Comment 13 Adam Kiezun CLA 2006-02-13 14:10:21 EST
this paper seems even more relevant (no JML):
www.cs.umd.edu/~jspacco/marmoset/papers/hovemeyer-paste2005.pdf

this last one is also on the subject:
research.microsoft.com/~leino/papers/krml109.pdf
Comment 14 Adam Kiezun CLA 2006-02-13 14:11:25 EST
(repeating for easier clicking)

this paper seems even more relevant (no JML):
http://www.cs.umd.edu/~jspacco/marmoset/papers/hovemeyer-paste2005.pdf

this last one is also on the subject:
http://research.microsoft.com/~leino/papers/krml109.pdf
Comment 15 Maxime Daniel CLA 2006-02-14 03:10:11 EST
Thanks for the input.
I suggest that further comments regarding null reference analysis be added to one of the following bugs, or to new ones if appropriate:
bug 126551: [compiler] null reference analysis: add interprocedural analysis
bug  96063: [compiler] Null pointer analysis needed in autoboxing/autounboxing
bug 123399: [compiler] missing null ref warning upon specific if/do while case
bug 127244: [compiler] Null reference analysis doesn't understand assertions
Comment 16 Jerome Lanneluc CLA 2006-02-14 09:20:07 EST
Verified for 3.2 M5 using build I20060214-0010
Comment 17 Stefan Matthias Aust CLA 2006-02-15 05:23:26 EST
Is this the right bug report to lobby for a @NotNull annotation? If not, please excuse my bold attempt to comment a "fixed" bug.

IDEA has recently introduced @NotNull and @Nullable annotations which come quite handy as the help to fix the "static" type system of Java and help to combat NPEs. While the solution is not perfect, it adds value and it would be nice, if Eclipse would also recognise the same annotation because that would help to make code more portable between IDEs.

See http://www.jetbrains.com/idea/documentation/howto.html for details on the semantics of IDEAs annotations.
Comment 18 Maxime Daniel CLA 2006-02-15 11:25:50 EST
(In reply to comment #17)
> Is this the right bug report to lobby for a @NotNull annotation?
I would suggest that you use https://bugs.eclipse.org/bugs/show_bug.cgi?id=126551 instead.
Comment 19 Maxime Daniel CLA 2006-10-10 01:32:13 EDT
*** Bug 107804 has been marked as a duplicate of this bug. ***