JDK-6468354 : Type inference fails on method w/type parm "T extends Comparable"
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6
  • Priority: P5
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: generic
  • CPU: generic
  • Submitted: 2006-09-07
  • Updated: 2010-08-18
  • Resolved: 2010-08-18
Related Reports
Duplicate :  
Relates :  
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 Not reproducible after 6369605 has been fixed.
18-08-2010

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
13-03-2009

EVALUATION The program should compile.
16-10-2006

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.
07-09-2006