Bug 295964 - [1.5][compiler] Wrong method return type during inference.
Summary: [1.5][compiler] Wrong method return type during inference.
Status: VERIFIED WONTFIX
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: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-11-24 03:35 EST by Srikanth Sankaran CLA
Modified: 2010-09-14 05:42 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 Srikanth Sankaran CLA 2009-11-24 03:35:23 EST
3.6M3

While working on bug#277643 I noticed that we actually compute the
return type of generic methods incorrectly in some situations. For
example in org.eclipse.jdt.core.tests.compiler.regression.GenericTypeTest.test1325()
(reproduced below)

public class X<E> {
    <T,U> U foo(X<T> xt) {
        return null;
}
void bar(X x) {
    X<String> xs2 = foo(x);
}
}

if you debug the compiler and break at 
org.eclipse.jdt.internal.compiler.ast.MessageSend.resolveType(BlockScope)
and run until just after the line:

	this.binding = this.receiver.isImplicitThis()
			? scope.getImplicitMethod(this.selector, argumentTypes, this)
			: scope.getMethod(this.actualReceiverType, this.selector, argumentTypes, this);

and then inspect the value of the binding computed, you will get
X<java.lang.String> foo(X<java.lang.Object>)

This is completely bogus and it should be java.lang.Object instead.

Similar problems exist in 
org.eclipse.jdt.core.tests.compiler.regression.GenericTypeTest.test1406()
org.eclipse.jdt.core.tests.compiler.regression.GenericTypeTest.test1407()
org.eclipse.jdt.core.tests.compiler.regression.GenericTypeTest.test1434()
Comment 1 Srikanth Sankaran CLA 2009-11-24 06:00:29 EST
Some background:

Prior to the fix for bug 258798, we would compile the 
code below without any complaints:

import java.util.*; 
public class GenericTest {
    public static void test() {
        Set testList = GenericTest.method1(new Class[] { ArrayList.class });	
    }				
    public static <I> I method1(Class<List>[] params) { 
	return null; 
    }
}

With the proposed fix for bug 277643, this behavior reappears.

But that is incorrect and hence this bug.

javac (7b75) complains: 

[...]

GenericTest.java:4: incompatible types
        Set testList = GenericTest.method1(new Class[] { ArrayList.class });
                                          ^
  required: Set
  found:    Object
1 error
2 warnings


Before the fix for bug 277643 and after the fix for bug 258798, we would
reject this code with the same message as javac, but that was entirely
by luck.
Comment 2 Srikanth Sankaran CLA 2009-11-25 03:57:51 EST
(In reply to comment #0)

> if you debug the compiler and break at 
> org.eclipse.jdt.internal.compiler.ast.MessageSend.resolveType(BlockScope)
> and run until just after the line:
>     this.binding = this.receiver.isImplicitThis()
>             ? scope.getImplicitMethod(this.selector, argumentTypes, this)
>             : scope.getMethod(this.actualReceiverType, this.selector,
> argumentTypes, this);
> and then inspect the value of the binding computed, you will get
> X<java.lang.String> foo(X<java.lang.Object>)
> This is completely bogus and it should be java.lang.Object instead.

This needs more study. See JLS 15.12.2.8 Inferring Unresolved Type Arguments.

Javac has/had many defects here:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6650759
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6640435
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6721089
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6638712
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6721146
Comment 3 Srikanth Sankaran CLA 2010-09-05 23:43:59 EDT
I am closing this internally reported defect as WONTFIX.
This seems to fall in the gray area of the JLS and there
are also many open defects in javac in this space. So until
and unless there is a real and significant customer impact,
it is too risky to change eclipse behavior here.
Comment 4 Ayushman Jain CLA 2010-09-14 05:42:19 EDT
Verified for 3.7M2