JDK-8047797 : Incorrect error: incompatible types: cannot infer type-variable(s) T#1,CAP#1,T#2
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 9
  • Priority: P3
  • Status: Resolved
  • Resolution: Cannot Reproduce
  • Submitted: 2014-06-23
  • Updated: 2015-08-03
  • Resolved: 2015-08-03
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 9
9Resolved
Related Reports
Blocks :  
Relates :  
Description
Consider this code:
-----
import java.util.Collection;
import java.util.List;

public final class ModelUtils {

    public static void getNamespaceScope(Collection<? extends CharSequence> l) {
        getFirst(filter(l));
    }

    public static <T> T getFirst(Collection<? extends T> all) {
        return null;
    }

    public static <T extends CharSequence> T getFirst(Collection<T> allElements, String... elementName) {
        return null;
    }

    public static <T extends CharSequence> List<? extends T> filter(Collection<T> allElements) {
        return null;
    }

}
-----

Compiling using langtools with fix for JDK-8044546 yields:
-----
$ javac ModelUtils.java 
ModelUtils.java:7: error: incompatible types: cannot infer type-variable(s) T#1,CAP#1,T#2
        getFirst(filter(l));
                ^
    (argument mismatch; List<CAP#2> cannot be converted to Collection<? extends CAP#2>)
  where T#1,T#2 are type-variables:
    T#1 extends Object declared in method <T#1>getFirst(Collection<? extends T#1>)
    T#2 extends CharSequence declared in method <T#2>filter(Collection<T#2>)
  where CAP#1,CAP#2,CAP#3 are fresh type-variables:
    CAP#1 extends T#2 from capture of ? extends T#2
    CAP#2 extends CAP#3 from capture of ? extends T#2
    CAP#3 extends CharSequence from capture of ? extends CharSequence
1 error
-----

Compiling using langtools before the fix for JDK-8044546 passes:

Comments
The issue has been fixed in current 9 repo
03-08-2015

I believe the fix is right - what is making this example fail to compile is the usual issue: JDK-8039214. It is possible that, before the fix, as the compiler was (erroneously) applying capture on partially inferred types, this particular example used to work as the subtyping being used there is less strict than the one used for proper types. Nevertheless, the compiler ends up with a very simple check: Collection<#CAP1> <: Collection<? extends #CAP1> Which fails, because, during type-containment the compiler is erroneously calling upperBound on the LHS capture var, generating the following check: UB(#CAP1) <: #CAP1 --> CharSequence <: #CAP1, false If the upper bound call is removed, then the code compiles correctly.
23-06-2014