Bug 264843 - [1.5][compiler] Eclipse compiler fails to reject invalid code with primitives autoboxed to generics
Summary: [1.5][compiler] Eclipse compiler fails to reject invalid code with primitives...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4   Edit
Hardware: All Windows XP
: P3 trivial (vote)
Target Milestone: 3.5 M6   Edit
Assignee: Kent Johnson CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-02-13 09:13 EST by Victor CLA
Modified: 2009-03-09 13:35 EDT (History)
3 users (show)

See Also:


Attachments
Proposed patch and testcase (4.58 KB, patch)
2009-02-13 11:41 EST, Kent Johnson CLA
no flags Details | Diff
Proposed patch and testcase (13.49 KB, patch)
2009-02-17 12:43 EST, Kent Johnson CLA
no flags Details | Diff
Proposed patch and testcase for 3.4.x (16.25 KB, patch)
2009-02-17 14:29 EST, Kent Johnson CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Victor CLA 2009-02-13 09:13:47 EST
Build ID: generics primitive autoboxing

Steps To Reproduce:
In the following code (class Foo), both m1 and m2 are invalid methods and should be equivalent due to autoboxing. Javac reports both as invalid, however eclipse's compiler incorrectly accepts the method m1. If the method m2 is removed, the class may be compiled (by eclipse's compiler) and run normally.

public class Foo {
    public <T extends Integer> T m1() {
        return 35;
    }

    public <T extends Integer> T m2() {
        return new Integer(35);
    }

    public static void main(String[] args) {
        System.out.println(m1());
    }
}

More information:

Originally reported in http://www.guj.com.br/posts/list/118023.java
Comment 1 Olivier Thomann CLA 2009-02-13 09:38:54 EST
Test case:
[public class X {
    public <T extends Integer> T m1() {
        return 35;
    }

    public <T extends Integer> T m2() {
        return new Integer(35);
    }

    public static void main(String[] args) {
        System.out.println(new X().m1());
    }
}]
javac 1.5, 1.6 and 1.7 are properly rejecting the code.

javac 1.6:
X.java:3: incompatible types
found   : int
required: T
        return 35;
               ^
X.java:7: incompatible types
found   : java.lang.Integer
required: T
        return new Integer(35);
               ^
2 errors

javac 1.7 (b44):
X.java:3: incompatible types
found   : int
required: T
        return 35;
               ^
X.java:7: incompatible types
found   : java.lang.Integer
required: T
        return new Integer(35);
               ^
2 errors

javac 1.5:
X.java:3: incompatible types
found   : int
required: T
        return 35;
               ^
X.java:7: incompatible types
found   : java.lang.Integer
required: T
        return new Integer(35);
               ^
2 errors
Comment 2 Kent Johnson CLA 2009-02-13 11:41:18 EST
Created attachment 125658 [details]
Proposed patch and testcase
Comment 3 Kent Johnson CLA 2009-02-13 16:41:23 EST
Released fix and tests for 3.5M6
Comment 4 Philipe Mulet CLA 2009-02-14 04:12:51 EST
I think there is more than the return statement to fix.
e.g.
public class X<T extends Integer> {

	T foo(T t) {
		t = 5;  // we should complain here too
		foo(6); // we do reject it ok already (why?)
		return t;
        }
}

Also I am not convinced this is the right fix. I suspect we have erasure conversion fooling us here...

Reopening
Comment 5 Philipe Mulet CLA 2009-02-14 12:05:19 EST
Looking deeper, I think the fix is reasonable. However, there are more situations to fix in the same way, see test case below:

field decl, local decl, assignment (the rest seems fine since cannot cause grief)

public class X<T extends Integer> {
	T field = 12;
	T foo(T t) {
		t = 5;
		foo(6);
		T t1 = t == null ? t : 8;
		T t2 = 9;
		T[] t3 = { 10 };
		perform(10);
		return 7;
	}
	T bar(T t) {
		int i = 0;
		t = i;
		foo(i);
		T t1 = t == null ? t : i;
		T t2 = i;
		T[] t3 = { i };
		perform(i);
		return i;
	}	
	void baz(T t) {
		int i = t;
		i+= t;
	}
	void perform(T... t) {}
}
Comment 6 Philipe Mulet CLA 2009-02-14 12:06:46 EST
I would also like a 3.4 version of the fix, given it feels an obvious case (for 3.4.x)
Comment 7 Kent Johnson CLA 2009-02-17 12:43:11 EST
Created attachment 125920 [details]
Proposed patch and testcase
Comment 8 Kent Johnson CLA 2009-02-17 14:29:16 EST
Created attachment 125935 [details]
Proposed patch and testcase for 3.4.x
Comment 9 Kent Johnson CLA 2009-02-18 10:33:13 EST
Released fix and test for 3.5M6
Comment 10 Kent Johnson CLA 2009-03-03 13:01:54 EST
Released into 3.4.x branch (post 3.4.2)
Comment 11 Frederic Fusier CLA 2009-03-09 13:35:11 EDT
Verified for 3.5M6 using I20090309-0100.