United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6468354 : Type inference fails on method w/type parm "T extends Comparable"

Details
Type:
Bug
Submit Date:
2006-09-07
Status:
Closed
Updated Date:
2010-08-18
Project Name:
JDK
Resolved Date:
2010-08-18
Component:
tools
OS:
generic
Sub-Component:
javac
CPU:
generic
Priority:
P5
Resolution:
Cannot Reproduce
Affected Versions:
6
Fixed Versions:

Related Reports
Duplicate:
Relates:

Sub Tasks

Description
FULL PRODUCT VERSION :
javac 1.6.0-beta2

java version "1.6.0-beta2"
Java(TM) SE Runtime Environment (build 1.6.0-beta2-b86)
Java HotSpot(TM) Client VM (build 1.6.0-beta2-b86, mixed mode, sharing)


ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]


A DESCRIPTION OF THE PROBLEM :
The testcase included with this report yields an error when compiled.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Simply compile the source with javac.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Expected that Integer would have been correctly inferred as the type for the NaturallySortedCollection returned by empty(), as it is-a Comparable<Integer>, which qualifies as a Comparable<? super Integer>.

ACTUAL -
Compile error shown below.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
C:\java\jaggregate-trunk\example-src\foo\InferenceExample.java:12: incompatible types; no instance(s) of type variable(s) T exist so that foo.NaturallySortedCollection<T> conforms to foo.NaturallySortedCollection<java.lang.Integer>
    found   : <T>foo.NaturallySortedCollection<T>
    required: foo.NaturallySortedCollection<java.lang.Integer>
    final NaturallySortedCollection<Integer> digits = empty();
                                                                                                     ^

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package foo;

import static foo.NaturallySortedCollection.empty;

class NaturallySortedCollection<E extends Comparable<? super E>> {
    static <T extends Comparable<? super T>> NaturallySortedCollection<T> empty() {
        return new NaturallySortedCollection<T>();
    }
}
class InferenceExample {
    public static void main( final String[] args ) {
        final NaturallySortedCollection<Integer> digits = empty();
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Provide an explicit type argument of <Integer> to the invocation of empty().

                                    

Comments
EVALUATION

At the very least, the error message is wrong.  Even without a
thorough read of the inference specification, it would appear
that the compiler should be able to infer java.lang.Integer for T.
                                     
2006-09-07
EVALUATION

The program should compile.
                                     
2006-10-16
EVALUATION

This problem is caused by the fact that sometimes javac's implementation of JLS3 15.12.2.8 ignores recursive bounds, sometimes not (as in this case). When recursive bounds contains wildcards, such bounds are included when computing uninferred type variables. This makes subsequent subtyping test (Integer <: Comparable<? super T> where T is a type-variable to be inferred).

Will be fixed after 6369605
                                     
2009-03-13
EVALUATION

Not reproducible after 6369605 has been fixed.
                                     
2010-08-18



Hardware and Software, Engineered to Work Together