Bug 154270 - [compiler][null][correlation] the array length should control the array null status, beyond the simpler final/final case (bug 125319)
Summary: [compiler][null][correlation] the array length should control the array null ...
Status: CLOSED DUPLICATE of bug 538421
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3   Edit
Hardware: All All
: P5 enhancement (vote)
Target Milestone: 4.7 M1   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords: helpwanted
Depends on:
Blocks:
 
Reported: 2006-08-17 14:06 EDT by John Arthorne CLA
Modified: 2018-08-30 10:39 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description John Arthorne CLA 2006-08-17 14:06:02 EDT
Build: I20060815

There are several pieces of code in OSGi that have a pattern like the one below.  These appear as potential null reference warnings, but from flow analysis you can see that it is impossible for the reference to be null:

public void doit(Object[] array) {
	int length = array == null ? 0 : array.length;
	if (length == 0)
		return;
	System.out.println(array[0]);//null reference warning
}
Comment 1 Maxime Daniel CLA 2006-08-21 07:34:57 EDT
This adds some complexity to bug 125319:
- the considered length and array are not final;
- the protection involves a different control flow (deep return instead of 
  embedded block).
Comment 2 Maxime Daniel CLA 2007-06-19 08:08:18 EDT
Reopening as P5 (since RESOLVED LATER is deprecated).
Comment 3 Srikanth Sankaran CLA 2012-01-21 20:31:53 EST
Ayush & Stephan,

Where has the resolution of bug 247564 left us with vis-à-vis
the current one ?
Comment 4 Stephan Herrmann CLA 2012-01-22 07:09:54 EST
(In reply to comment #3)
> Ayush & Stephan,
> 
> Where has the resolution of bug 247564 left us with vis-à-vis
> the current one ?

Bug 247564 has not introduced any correlation analysis.
Nor do I have plans to implement such analysis, because I believe the complexity of such analysis would be way beyond all that we have today. I expect the costs both in development time and probably also performance / memory consumption to outweigh the benefit.

That said, the reference to bug 125319 is new for me and I'm curious to investigate if any of the work that has been started there looks promising. Yet,  _this_ bug "adds some complexity to bug 125319" so I don't want to grow any expectations here. Looking at the NullPatternsHandler from bug 125319 any solution in that direction will certainly be limited to a narrow range of code patterns (it may avoid the pot. performance issues, though).

OTOH, I see no reason why the null analysis should jump through hoops to accommodate this code which so easily could be rephrased, e.g., as

public void doit(Object[] array) {
    if (array == null || array.length == 0)
        return;
    System.out.println(array[0]); // known to be safe
}

this version can be easily understood by both human *and* compiler. 
It is actually a message from bug 247564 that certain guarantees can only be given if the code is rephrased into a safer variant, e.g.:

class X {
   Object o;
   @NonNull Object foo() {
       if (o != null) {
           possiblySomeStuffHere...
           return o; // (1)
       }
       Object local = o;
       if (local != null) {
           possiblySomeStuffHere...
           return local; // (2)
       }
   }
}

(1) violates the null specification (due to concurrency or field updates
    in methods called from possiblySomeStuffHere)
(2) is a fully safe alternative, understood by the analysis

Saying: wrt fields our analysis should actually direct the user to reduce the number of direct field accesses and use more local variables inside a method.
A similar message regarding correlation might be: "don't rely on correlation, protect array access by a direct check for null, not by a check on a derived value."

As an aside, in bug 335118 I had started to refactor the JDT/Core code in the same vein as illustrated above. Maybe by continuing that work we will get a better idea about the practicality of such refactoring.
Comment 5 Stephan Herrmann CLA 2016-06-28 17:17:40 EDT
Bulk closing all compiler bugs tagged [null][correlation], because we have no plans to add such a feature: it would be a tremendous implementation effort, beyond our current man power, and may be impossible to achieve within the desired performance bounds.

If s.o. has a viable strategy or even implementation for such a feature, I'm all ears.
Comment 6 Sasikanth Bharadwaj CLA 2016-08-02 04:28:00 EDT
Verified for 4.7 M1
Comment 7 Stephan Herrmann CLA 2018-08-30 10:39:39 EDT
I created a new umbrella RFE outlining what would be needed to address this issue.

*** This bug has been marked as a duplicate of bug 538421 ***