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

Return type inference (15.12.2.8) doesn't work with autoboxing

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P3 P3
    • 7
    • 7
    • specification
    • None
    • rc
    • generic
    • other
    • Verified

      I'd expect the following to compile:

      given the generic method

      <T> T getOption();

      the subsequent call will fail to compile with javac

      int i = getOption();

      At first I regarded this as a javac bug, but after a deeper look at the spec, it seems that no autoboxing should be exploited here:

      accordingly to 15.12.2.8, a constraint of the kind int >> T should be derived. However it seems that rules in 15.12.2.7 cannot derive the constraint Integer >> T starting from this initial constraint. So we cannot derive anything useful from this constraint. Maybe 15.12.2.8 should be changed in order replaced the primitive type with the boxed type in case the return type is primitive.

      Here's a test-case:

      public class X {
          public static <T> T getValue(T t1, T t2) {
              return t1;
          }

          public void testGenerics(Comparable<String> s) {
              int i = getValue(0, s);
          }
      }

            abuckley Alex Buckley
            mcimadamore Maurizio Cimadamore
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: