Bug 83616

Summary: Unclear result of calling isAssigmentCompatible for wildcard types
Product: [Eclipse Project] JDT Reporter: Dirk Baeumer <dirk_baeumer>
Component: CoreAssignee: Maxime Daniel <maxime_daniel>
Status: RESOLVED WORKSFORME QA Contact:
Severity: normal    
Priority: P3 CC: markus.kell.r, martinae
Version: 3.1   
Target Milestone: 3.2 M6   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Dirk Baeumer CLA 2005-01-25 10:31:34 EST
Below a couple of unclear result of assigmnemt checks with wildcards. 

import java.util.List;

public class A<U extends Number> {
	List<? extends Number> lhs;
	List<? extends U> rhs;
	{
		lhs.add(rhs.get(0));
	}
}

produces a compile error. However

"? extends U".isAssignmentCompatible("? extends Number") answer true.
Comment 1 Dirk Baeumer CLA 2005-01-25 10:50:24 EST
Another case: 

	List<? extends Object> lhs;
	List<? extends Number> rhs;
	{
		lhs.add(rhs.get(0));
	}

procudes compile error. However

"? extends Number".isAssignmentCompatible("? extends Object") produces true. 

This should be false IMO since ? in ? extends Number can be bound to Integer and
? in ? extends Object can be bound to String and
Number.isAssignmentCompatible(String) == false; 
Comment 2 Dirk Baeumer CLA 2005-01-25 12:09:20 EST
Regarding the case in comment #1. Philippe, I tried your idea:

public class A<LHS, RHS> {
	LHS lhs;
	RHS rhs;
	{
		A<? extends Object, ? extends Number> a= null;
		a.lhs= a.rhs;
	}
}

This produces a compile error saying: 

Bound mismatch: Cannot assign expression of type ? extends Number to wildcard
type ? extends Object. The wildcard type has no lower bound, and may actually be
more restrictive than expression type.

So "? extends Number".isAssignmentCompatible("? extends Object") should return
false then.


Comment 3 Philipe Mulet CLA 2005-01-25 12:13:58 EST
Indeed.

I cannot reproduce the "true" answers from isAssignmentCompatible; using source
code. I suspect our API is buggy, since it isn't the true semantics played by
the compiler. Can you provide the right testcase you used ?

import java.util.List;
public class X<U extends Number> {
	U u;
	void foo(X<? extends Number> xn, X<? extends U> xu) {
		xu.u = xn.u; // ko
		xn.u = xu.u; // ko
	}
}
Comment 4 Dirk Baeumer CLA 2005-01-25 13:06:28 EST
Philippe,

the test I use is TypeEnvironmentTests in jdt.ui.tests.refactoring. To enable
the wildcard assignment tests go to method
TypeEnvironmentTests#performGenericAssignmentTest uncomment the line testAssignment.

This will print the result of the comparison to the console together with the
result the context wide implementation in the UI computes.

You need the code from HEAD to test this.
Comment 5 Philipe Mulet CLA 2005-06-10 06:53:18 EDT
Please reopen if this is still an issue in latest.
Comment 6 Philipe Mulet CLA 2006-03-27 06:56:54 EST
reopening for closing properly
Comment 7 Philipe Mulet CLA 2006-03-27 06:57:42 EST
closing, since didn't hear back in a long time.