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

Type inference error when reversing a comparator

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • 8
    • 8
    • tools
    • b105
    • windows_7

      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 ----------

            vromero Vicente Arturo Romero Zaldivar
            mcastegr Mattis Castegren (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: