Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6468354

Type inference fails on method w/type parm "T extends Comparable<? super T>"

XMLWordPrintable

    • generic
    • generic

      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().

            mcimadamore Maurizio Cimadamore
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: