Bug 147118

Summary: [compiler][null] Incorrect null analysis involving do_while loop
Product: [Eclipse Project] JDT Reporter: Gordon Henriksen <malanon>
Component: CoreAssignee: Maxime Daniel <maxime_daniel>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: eclipse, mlists
Version: 3.2   
Target Milestone: 3.3 M3   
Hardware: Macintosh   
OS: Mac OS X - Carbon (unsup.)   
Whiteboard:
Bug Depends on:    
Bug Blocks: 157154    
Attachments:
Description Flags
Suggested fix plus activated test case none

Description Gordon Henriksen CLA 2006-06-14 17:04:44 EDT
This snippet causes null analysis to raise an incorrect warning. It could probably be slightly more minimal, but this actually makes sense.

	private final static class Node {
		public Node parent;
		public Node sibling;
		public Node child;
	}
	
	public void nullAnalysisBug(Node node) {
		Node cur = node;
		do {
			if (null != cur.child)  // INCORRECT -> warning: The variable cur may be null
				cur = cur.child;
			else if (null != cur.sibling)
				cur = cur.sibling;
			else if (null != cur.parent)
				cur = cur.parent.sibling;
			else
				cur = null; // B
		} while (cur != null);
	}

There are two workarounds:
A. Transforming the loop into a while loop silences the warning, at the cost of an additional comparison at runtime.
B. Transforming the final else statement into a 'break' also averts the warning:
Comment 1 Maxime Daniel CLA 2006-06-15 04:21:19 EDT
Agreed. Following shorter test case should remove any doubt about it:

public class X {
  String f;
  void foo (boolean b) {
    X x = new X();
    do {
      System.out.println(x.f); // yields may be null, whereas it is not first 
                               // time, and is protected by the test thereafter
      if (b) {
        x = null;
      }
    } while (x != null);
  }
}

Added (inactive) test NullReferenceTest#613.
Comment 2 Maxime Daniel CLA 2006-09-19 02:58:26 EDT
Created attachment 50447 [details]
Suggested fix plus activated test case

Mistakenly used the action flow info in input for the looping context late check. Must use the upstream flow instead.
Waiting for 3.3 M2 to be declared before committing.
Comment 3 Maxime Daniel CLA 2006-09-25 06:25:20 EDT
Released for 3.3M3.
Comment 4 Olivier Thomann CLA 2006-10-30 14:00:34 EST
Verified for 3.3 M3 using warm-up build I20061030-0010
Comment 5 Eclipse Webmaster CLA 2007-07-29 09:20:21 EDT
Changing OS from Mac OS to Mac OS X as per bug 185991