United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6650759 : Inference of formal type parameter (unused in formal parameters) is not performed

Details
Type:
Enhancement
Submit Date:
2008-01-15
Status:
Closed
Updated Date:
2014-06-19
Project Name:
JDK
Resolved Date:
2012-01-13
Component:
tools
OS:
linux,generic
Sub-Component:
javac
CPU:
x86,generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
7
Fixed Versions:

Related Reports
Backport:
Duplicate:
Relates:
Relates:
Relates:
Relates:
Relates:
Relates:
Relates:
Relates:
Relates:
Relates:

Sub Tasks

Description
This source code:
-----------------------------------------------------------------------------
public class GenericTest {
    
    public static interface Interface<T> { }
    public static class IntegerInterface implements Interface<Integer> { }
    
    public static <I extends Interface<T>, T> T getGenericValue(I test) {
        return null;
    }
    
    public static void testSet(Integer test) { }
    
    public static void main(String args[]) {
        
        Integer test = getGenericValue(new IntegerInterface());
        testSet(getGenericValue(new IntegerInterface()));  //flagged as an error
    }
}
---------------------------------------------------------------------------------------

Produces the following compilation error:
---------------------------------------------------------------------------------------
GenericTest.java:15: testSet(java.lang.Integer) in GenericTest cannot be applied to (java.lang.Object)
         testSet(getGenericValue(new IntegerInterface()));  //flagged as an error
         ^
1 error
---------------------------------------------------------------------------------------

On:
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b22)
Java HotSpot(TM) Server VM (build 11.0-b08, mixed mode)

But not on:
java version "1.6.0_05-ea"
Java(TM) SE Runtime Environment (build 1.6.0_05-ea-b04)
Java HotSpot(TM) Tiered VM (build 1.6.0_05-ea-b04, mixed mode)

                                    

Comments
EVALUATION

This situatioin can be regarded as a consequence of the interplay between the JLS bug described in 6640435 and a limitation of section 15.12.2.8 of the JLS. Here, a constraint of the kind T == Integer is found during the first step of type inference (the one taking into account actual vs. formal parameter types - JLS 15.12.2.7). However, since this constraint does NOT involve the type variable I being inferred, this constraint is simply discarded by javac. 

During the second step of type inference (the one taking into account return type along with uninferred type variables' bounds) start from scatch. Moreover, since the the return type in the second call to the method getGenericValue() is not subject to an assigment conversion, no further constraints on T is derived in 15.12.2.8. The only constraint is thus Object >> Integer (being Object the bound of T), so that the inferred type of T is indeed glb(Object) = Object. This choice is not compliant with the signature of the method testSet, thus triggering the error.

Resuming, there are two possible solutions to this bug:

1) Propagate the constraint T==Integer from 15.12.2.7 to 15.12.2.8
2) Generalizing 15.12.2.8 in order to infer a constraint S >> T if the type variable T appear in return-type position of a generic method m1() whose evaluation is passed to another method m2() accepting an argument of type S.
                                     
2008-02-11
EVALUATION

Update: this bug has been caused by 6278587 - the bug is not present in JDK6/5.
As such it should be regarded as a regression (as it was suggested by the last three lines of 'descriptions' - the submitter itself suggested that this code used to work with elder compilers). See also 6811943, a JLS RFE that summarizes the difference between javac's and JLS.
                                     
2009-03-03
SUGGESTED FIX

A webrev of this fix is available at the following URL:
http://hg.openjdk.java.net/jdk7/tl/langtools/rev/dda7e13f09fb
                                     
2009-09-07



Hardware and Software, Engineered to Work Together