JDK-6785114 : Return type inference (15.12.2.8) doesn't work with autoboxing
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: other
  • CPU: generic
  • Submitted: 2008-12-15
  • Updated: 2017-05-16
  • Resolved: 2011-07-21
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 rcFixed
Related Reports
Relates :  
Relates :  
Description
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);
    }
}

Comments
EVALUATION JLS3 integrated boxing and unboxing conversion into overload resolution at a high level (the 'applicable by method invocation conversion' step), and did not push them all the way down into the inference algorithm per se. The fix is: "Then, a set of initial constraints consisting of: - the constraint box(S) >> R', provided R is not void; ***(where box(S) is S if S is a reference type, and box(S) is the result of applying boxing conversion (5.1.7) to S if S is a primitive type)*** and - additional constraints ..."
17-12-2008