Bug 310427 - [content assist] FUP of 236306: Variable proposed before definition.
Summary: [content assist] FUP of 236306: Variable proposed before definition.
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.5   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.7 M2   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-04-26 05:41 EDT by Srikanth Sankaran CLA
Modified: 2010-09-14 13:05 EDT (History)
4 users (show)

See Also:
Olivier_Thomann: review+


Attachments
proposed fix v1.0 + regression tests (9.50 KB, patch)
2010-05-12 08:44 EDT, Ayushman Jain CLA
Olivier_Thomann: iplog+
Details | Diff
Proposed fix + regression test (9.33 KB, patch)
2010-05-13 13:54 EDT, Olivier Thomann CLA
no flags Details | Diff
proposed fix for case 2 + test (3.44 KB, patch)
2010-08-05 14:54 EDT, Ayushman Jain CLA
no flags Details | Diff
proposed fix for case 2 + test with small changes (4.02 KB, patch)
2010-08-09 07:03 EDT, Ayushman Jain CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Srikanth Sankaran CLA 2010-04-26 05:41:28 EDT
I20100424-2000

FUP of 236306

(1) public class X {
	public static void main(String args[]) {
		int xyk = 10;
		int xyz = xy|;
	}
}

Completing at the | symbol proposes xyz, which would immediately
lead to an error (uninitialized)

(2) public class X {
	int xyk = 0;
	int xyz = xy|;
	int xyp = 1;
}

Completing at the | symbol proposes xyz and xyp, which would immediately
lead to an error (field referenced b4 definition)

(3) String newText = String.format(newText, null) proposal
still shows up if newText declaration were to happen as
a field and not as local.
Comment 1 Ayushman Jain CLA 2010-05-12 08:44:13 EDT
Created attachment 168132 [details]
proposed fix v1.0 + regression tests

In the completion engine, we record the fields and local variables inside which completion is taking place in CompletionEngine$uninterestingBindings. We can simply use this to eliminate the recorded fields and locals from being proposed. This is what this patch does.

Remastered tests:
CompletionTests#testCompletionEmptyTypeName2()
CompletionTests#testCompletionEmptyTypeName3()
CompletionTests#testCompletionKeywordFalse5()

Added regression tests:
CompletionTests#testBug310427a()
CompletionTests#testBug310427b()
Comment 2 Ayushman Jain CLA 2010-05-12 08:46:41 EDT
(In reply to comment #0)
Note that this only fixes cases (1) and (2). Case (3) is a different case where we are looking for argument proposals after content assist has added its proposed method and is more like what was fixed in bug 236306. So i'll raise a new bug pertaining to case (3).
Comment 3 Ayushman Jain CLA 2010-05-12 08:52:34 EDT
(In reply to comment #2)
> (In reply to comment #0)
>So i'll raise a new bug pertaining to case (3).

Raised bug 312603
Comment 4 Olivier Thomann CLA 2010-05-13 13:54:27 EDT
Created attachment 168427 [details]
Proposed fix + regression test

I change the patch a little. Otherwise it looks good.
Comment 5 Srikanth Sankaran CLA 2010-05-19 01:59:20 EDT
Targetting 3.7M1
Comment 6 Ayushman Jain CLA 2010-06-21 03:14:53 EDT
Released in HEAD for 3.7M1.
Added tests:
CompletionTests#testBug310427a()
CompletionTests#testBug310427a()
Comment 7 Srikanth Sankaran CLA 2010-08-03 06:03:46 EDT
Reopening as build I20100802-1800 fails the case 2 in comment#0.
I don't also see a regression test that ensures that we don't 
propose a field ahead of its declaration.
Comment 8 Ayushman Jain CLA 2010-08-03 07:04:16 EDT
(In reply to comment #7)
> Reopening as build I20100802-1800 fails the case 2 in comment#0.
> I don't also see a regression test that ensures that we don't 
> propose a field ahead of its declaration.

I'll take a look.
Comment 9 Olivier Thomann CLA 2010-08-05 07:28:03 EDT
Moving to M2.
Comment 10 Ayushman Jain CLA 2010-08-05 14:54:16 EDT
Created attachment 175971 [details]
proposed fix for case 2 + test

Srikanth please review. TIA
Comment 11 Srikanth Sankaran CLA 2010-08-05 22:31:41 EDT
(In reply to comment #10)
> Created an attachment (id=175971) [details]
> proposed fix for case 2 + test
> 
> Srikanth please review. TIA

This patch still allows ahead of declaration use of variables
if the variables are locals to a method.

Try completing at |

public class X {
    int xyk = 0;
    int xyz = xyk;
    int xyp = 1;
    
    public void foo() {
    	int xyk = 0;
        int xyz = xy|;
        int xyp = 1;
    }
}
Comment 12 Ayushman Jain CLA 2010-08-06 05:12:52 EDT
(In reply to comment #11)
[...]
> Try completing at |
> 
> public class X {
>     int xyk = 0;
>     int xyz = xyk;
>     int xyp = 1;
> 
>     public void foo() {
>         int xyk = 0;
>         int xyz = xy|;
>         int xyp = 1;
>     }
> }

On completing at the specified location, we get the following proposals:
FIELDS - xyk, xyz, xyp
LOCAL VARS - xyk

So the proposal you see for xyp is for the field with the same name and hence valid. Also you do get a proposal for the field xyz, but inserting it would give an error since the local var xyz is in the process of declaration. However, this error can be removed by just qualifying it as this.xyz if this was the original intention. So proposal xyz is also valid.
Comment 13 Srikanth Sankaran CLA 2010-08-09 02:31:15 EDT
(In reply to comment #12)
> (In reply to comment #11)
> [...]
> > Try completing at |
> > 
> > public class X {
> >     int xyk = 0;
> >     int xyz = xyk;
> >     int xyp = 1;
> > 
> >     public void foo() {
> >         int xyk = 0;
> >         int xyz = xy|;
> >         int xyp = 1;
> >     }
> > }
> 
> On completing at the specified location, we get the following proposals:
> FIELDS - xyk, xyz, xyp
> LOCAL VARS - xyk

You are right. Apologies for the false alarm.

A couple of comments:

(1) Please comment out the line inside the junit type's static
initializer block.

TESTS_NAMES = new String[] { "testBug310427c"};

(2) You could have used the check field.id >= fieldDeclaration.binding.id
instead and gotten rid of the uninterested bindings approach (at least for
fields). As it is there are two separate techniques used to filter out
the invalid proposals - the fix may be simpler with just one instead.

It is your call to decide whether you want to do something
about (2).
Comment 14 Ayushman Jain CLA 2010-08-09 07:03:53 EDT
Created attachment 176145 [details]
proposed fix for case 2 + test with small changes

>(1) Please comment out the line inside the junit type's static
>initializer block.

Done

(2) You could have used the check field.id >= fieldDeclaration.binding.id
instead and gotten rid of the uninterested bindings approach (at least for
fields). As it is there are two separate techniques used to filter out
the invalid proposals - the fix may be simpler with just one instead.

Done.
Comment 15 Srikanth Sankaran CLA 2010-08-09 09:17:35 EDT
(In reply to comment #14)
> Created an attachment (id=176145) [details]
> proposed fix for case 2 + test with small changes

Patch looks good.
Comment 16 Ayushman Jain CLA 2010-08-18 10:21:49 EDT
Released in HEAD for 3.7M2.
Comment 17 Satyam Kandula CLA 2010-09-14 13:05:26 EDT
Verified for 3.7M2 using build I20100909-1700