Bug 118273

Summary: [1.5][compiler] Generic inference more lenient than JDK compiler
Product: [Eclipse Project] JDT Reporter: David Gates <gatesda>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: VERIFIED DUPLICATE QA Contact:
Severity: minor    
Priority: P3    
Version: 3.1.1   
Target Milestone: 3.5 M5   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description David Gates CLA 2005-11-28 14:11:17 EST
class Generic<A> {
	<B extends Comparable<B>> Generic<B> newInstance() {
		return new Generic<B>();
	}
	
	Generic<String>[] bugDemo() {
		return new Generic[] { newInstance() };
	}
}

Works using Eclipse compiler.  javac 1.5.0_05 compiler gives error message:

BugDemo.java:7: incompatible types; inferred type argument(s) java.lang.Object do not conform to bounds of type variable(s) B
found   : <B>Generic<B>
required: Generic
                return new Generic[] { newInstance() };
                                                  ^
Note: BugDemo.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
Comment 1 Philipe Mulet CLA 2005-11-29 07:59:26 EST
The difference occurs on second pass of inference, where the type expectation is taken into account. Need to check the spec. Assignment conversions are supposed to affect type inference, as it does in our compiler.

Comment 2 Philipe Mulet CLA 2005-12-02 10:31:01 EST
Actually, it behaves the same without any array initializer.

public class X<A> {
        <B extends Comparable<B>> X<B> newInstance() {
                return new X<B>();
        }

        X<String>[] bugDemo() {
       		X x = newInstance();
                return new X[] { x };
        }
}
Comment 3 Philipe Mulet CLA 2005-12-02 10:38:36 EST
Note that the following compiles fine.

public class X<A> {
    <B extends Comparable<B>> B newInstance2(X<B> xb) {
            return null;
    }
    void foo() {
        X x = new X();
        Comparable c = newInstance2(x);
    }
}
Comment 4 Philipe Mulet CLA 2006-03-31 09:03:53 EST
Added GenericTypeTest#test0960-0961.

Closing as javac bug.
Comment 5 Philipe Mulet CLA 2008-12-18 08:53:15 EST
Actually, this may be a bug in ECJ.
JLS 15.12.2.6 seems to mention need for an erasure of return type if any unchecked conversion got performed during inference. 

This would mean javac is actually right.
Reopening for more consideration.
Comment 6 Philipe Mulet CLA 2009-01-08 16:05:39 EST
Given changes for bug 258798, we now report an error:

2. ERROR in X.java (at line 7)
	X x = newInstance();
	      ^^^^^^^^^^^
Bound mismatch: The generic method newInstance() of type X<A> is not applicable for the arguments (). The inferred type Comparable<Comparable<B>> is not a valid substitute for the bounded parameter <B extends Comparable<B>>

Closing as dup

*** This bug has been marked as a duplicate of bug 258798 ***
Comment 7 Frederic Fusier CLA 2009-01-27 05:38:07 EST
Verified for 3.5M5 using build I20090127-0100.