Bug 236707 - [1.5][compiler] Eclipse/javac discrepancy with nested type parameters and raw types in method calls
Summary: [1.5][compiler] Eclipse/javac discrepancy with nested type parameters and raw...
Status: VERIFIED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.5 M1   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-06-11 14:45 EDT by Joe Kearney CLA
Modified: 2008-08-06 12:57 EDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joe Kearney CLA 2008-06-11 14:45:46 EDT
public class Scratch {
	private Y rawObject = new Y();

	public void caller() {
		method(new X<Y<Z>>(), rawObject); // compile error in javac, unchecked conversion in Eclipse
		this.<Y<Z>>method(new X<Y<Z>>(), rawObject); // unchecked warning in both
	}

	public <T> void method(X<T> x, T t) {}
}

class X<S> {}
class Y<S> {}
class Z {}


==========================================================

Using Java 1.6u4, tested in Eclipse 3.3.1.1, 3.4 RC4.

Eclipse implicitly converts the raw object to a Y<Z> compiles the above with a few unchecked cast warnings. Javac gives the following error:
Scratch.java:5: <T>method(X<T>,T) in Scratch cannot be applied to (X<Y<Z>>,Y)
                method(new X<Y<Z>>(), rawObject);

My types knowledge isn't up to knowing which is right. T is constrained as T super Y and X<T> super X<Y<Z>>. The only way I can see that these constraints could be satisfied is if erasure can have an effect far earlier than I thought, so that trivially T=Y. Surely we can't assume that the raw object will be a Y<Z> from the type parameter of X?

Wildcarding the method signature will avoid the problem, though the issue remains for one of the compilers.
	public <T> void method(X<? extends T> x, T t) {}
Comment 1 Philipe Mulet CLA 2008-06-16 08:04:14 EDT
My analysis, based on JLS 15.12.2.7, yields that 'T' is inferred to be 'Y<Z>', as we currently obtain with ECJ.

-------------------------------------------------
15.12.2.7 invoking: #method(X<T>,T) with { X<Y<Z>>, Y }
i.e. Arg1 << Formal1, Arg2 << Formal2

(1) X<<Y<Z>> << X<T>
(2) Y << T


-------------------------------------------------
(1) X<<Y<Z>> << X<T> 

Using p.453: &#10070; If F has the form G<..., Yk-1, U, Yk+1, ...>, where U is a type
expression that involves Tj, then if A has a supertype of the form G<...,
Xk-1, V, Xk+1, ...> where V is a type expression, this algorithm is applied
recursively to the constraint V = U.
[where U is T, V is Y<Z>]

(1a) Y<Z> = T

Using p.456: &#10070; If F = Tj, then the constraint Tj = A is implied.

(1b) T = Y<Z>

-------------------------------------------------
(2) Y << T

Using p.453: &#10070; Otherwise, if F = Tj, then the constraint Tj :> A is implied.

(2a) T :> Y

-------------------------------------------------
p.463 resolving constraints:

{ T = Y<Z>, T :> Y }

Using: "For each implied equality constraint Tj = U or U = Tj:
• If U is not one of the type parameters of the method, then U is the type
inferred for Tj. Then all remaining constraints involving Tj are rewritten such
that Tj is replaced with U. There are necessarily no further equality constraints
involving Tj, and processing continues with the next type parameter, if any."

{ T = Y<Z>, Y<Z> :> Y }

Using: "Then, for each remaining type variable Tj, ..."

There is none, hence the ':>' constraint is not considered; and T = Y<Z>.

Applicability check does pass, with unchecked warning on second argument (raw Y).

Comment 2 Philipe Mulet CLA 2008-06-24 06:09:56 EDT
I got confirmation from spec master that this is the intended behavior by the language spec. Please report the bug to javac.
Comment 3 Philipe Mulet CLA 2008-06-24 06:10:22 EDT
marking as Invalid
Comment 4 Philipe Mulet CLA 2008-06-24 06:16:34 EDT
Added GenericTypeTest#test1349
Comment 5 Philipe Mulet CLA 2008-06-25 13:11:08 EDT
javac bug should soon be avail at http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6718364
Comment 6 Kent Johnson CLA 2008-08-06 12:57:14 EDT
Verified for 3.5M1 using I20080805-1307