JDK-8025138 : Type inference error when reversing a comparator
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8
  • Priority: P3
  • Status: Resolved
  • Resolution: Duplicate
  • OS: windows_7
  • Submitted: 2013-09-15
  • Updated: 2013-09-26
  • Resolved: 2013-09-26
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 8
8Resolved
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0-ea"
Java(TM) SE Runtime Environment (build 1.8.0-ea-b106)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b48, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

EXTRA RELEVANT SYSTEM CONFIGURATION :
Using IntelliJ IDEA 12

A DESCRIPTION OF THE PROBLEM :
When attempting to sort an array of enumerables (in this case, 'TokenType', which contains a length method returning an integer), reversing a Comparator<TokenType> created with the new Comparing class results in the compiler reporting a type-inference error.

Whilst the following code compiles:

  TokenType[] types = TokenType.values();
Arrays.sort(types, Comparator.comparing((TokenType t) -> t.length()));

This does not:

  TokenType[] types = TokenType.values();
Arrays.sort(types, Comparator.comparing((TokenType t) -> t.length()).reversed());

The only difference being the addition of the "reversed" method. This compiled under previous builds (below b106).

REGRESSION.  Last worked in version 8

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create a java file containing the main static method.
2. Create an array of Integer's, like so:
        Integer[] ints = {1, 2, 3, 4};

3. Attempt to sort the array, like so (note this compiles):
        Arrays.sort(ints, Comparator.comparing((Integer i) -> i));

4. Change the sort method, in order to reverse the array:
        Arrays.sort(ints, Comparator.comparing((Integer i) -> i).reversed());

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
A successful compilation, and a program which orders the integer array highest-to-lowest.
ACTUAL -
Failure during compilation.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
java: incompatible types: cannot infer type-variable(s) T,U
    (argument mismatch; java.util.function.Function<java.lang.Integer,capture#1 of ? extends java.lang.Integer> cannot be converted to java.util.function.Function<? super java.lang.Object,? extends java.lang.Integer>)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.Arrays;
import java.util.Comparator;

public class TestBug {

    public static void main(String[] args) {
        Integer[] ints = {1, 2, 3, 4};

        // Error occurs here
        Arrays.sort(ints, Comparator.comparing((Integer i) -> i).reversed());

        for(int i: ints) {
            System.out.println(i);
        }
    }

}

---------- END SOURCE ----------