Community
Participate
Working Groups
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
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.
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 }; } }
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); } }
Added GenericTypeTest#test0960-0961. Closing as javac bug.
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.
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 ***
Verified for 3.5M5 using build I20090127-0100.