Bug 173800 - [compiler] suboptimal line number attributes for cascading field accesses
Summary: [compiler] suboptimal line number attributes for cascading field accesses
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.3 M6   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-02-11 15:48 EST by Olivier Thomann CLA
Modified: 2007-03-21 09:47 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 Olivier Thomann CLA 2007-02-11 15:48:54 EST
On the following test case, javac 6, 7 and ourselves all generate code that will yield an NPE on line 8, which is a pity since a better indication of the problem would be line 11 (the one where the 'faulty' dot stands):

public class X {
  X next;
  X (X next) {
	this.next = next;
  }
  public static void main(String args[]) {
	X x = new X(new X(new X(null)));
	x. // line 8
		next.
			next.
				next.
					next.
						next.
							toString();
  }
}

Interestingly enough, on the following variant, we get the NPE on line 14, while javac 6 and 7 stubbornly keep line 8:

public class X {
  X next;
  X (X next) {
	this.next = next;
  }
  public static void main(String args[]) {
	X x = new X(new X(new X(null)));
	x.
		next.
			next.
				next.
//					next.
//						next.
							toString(); // line 14
  }
}
Comment 1 Olivier Thomann CLA 2007-02-12 11:22:15 EST
Now I fail on line 12, not 11 though.
The field on line 11 is null, but the access is failing only in line 12 when the field is accessed.
Does this look better?
Comment 2 Olivier Thomann CLA 2007-02-12 11:50:27 EST
The cascading field reference case is also wrong.
public class X {
  X x;
  X next;
  X (X next) {
        this.next = next;
  }
  public static void main(String args[]) {
        X x = new X(new X(new X(null)));
		x.x = x;
		x.foo();
  }
  public void foo() {
        this.
        	x.
                next.
                        next.
                                next.
                                        next.
                                                next.
                                                        toString();
  }
}

Since the field references are sharing the source start, we must use the source end position.
Fix under testing.
Comment 3 Maxime Daniel CLA 2007-02-12 12:26:34 EST
(In reply to comment #1)
> Now I fail on line 12, not 11 though.
> The field on line 11 is null, but the access is failing only in line 12 when
> the field is accessed.
> Does this look better?
> 
Ideally, the line reported should be the one of 'the dot that fails'. But IMHO, the line of the first selector that follows it is OK as well, or the line of the selector that yields the null value (better?), provided that we are consistent and the user can rely on us to be consistent (then a test on a simple case will give her a clue of what to expect).
Comment 4 Olivier Thomann CLA 2007-02-12 13:13:55 EST
(In reply to comment #3)
> Ideally, the line reported should be the one of 'the dot that fails'. But IMHO,
I disagree. The dot itself means nothing. It is the subsequent field access that will fail.
If you write your test case:
x
   .next
       .next

....

I would expect the same line to be returned.

I have one last issue to solve before I can release the fix.
Comment 5 Olivier Thomann CLA 2007-02-12 13:35:13 EST
Released for 3.3M6.
Comment 6 Maxime Daniel CLA 2007-02-13 02:49:51 EST
(In reply to comment #4)
> (In reply to comment #3)
> > Ideally, the line reported should be the one of 'the dot that fails'. But IMHO,
> I disagree. The dot itself means nothing. It is the subsequent field access
> that will fail.
The dot is the common part of the syntax for dereference, for multiple uses (field access and method call). This may not be the best candidate, but it means at least that.
Comment 7 Eric Jodet CLA 2007-03-21 07:19:47 EDT
Verified for 3.3 M6 using build I20070320-0010