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

javac incorrectly complains of "incompatible upper bounds", but ECJ compiles it fine

XMLWordPrintable

    • x86_64
    • linux

      FULL PRODUCT VERSION :
      java version "1.8.0_172-ea"
      Java(TM) SE Runtime Environment (build 1.8.0_172-ea-b03)
      Java HotSpot(TM) 64-Bit Server VM (build 25.172-b03, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Linux Crushinator 4.14.15-gentoo #1 SMP Wed Jan 24 07:55:59 EST 2018 x86_64 Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz GenuineIntel GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      I apologize, I do not know enough of the internal jargon of Java's type system to write a succinct title for this bug report. Please retitle this report as appropriate.

      The attached minimal test case demonstrates a shortcoming of javac in dealing with type inference on method references. Please note that Eclipse's ECJ compiler has no problem with this.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the attached code.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Code compiles.
      ACTUAL -
      javac reports an error.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Demo.java:6: error: no suitable method found for lowerBound(C[],int,int,sought::compareTo)
                      return Demo.lowerBound(array, 0, array.length, sought::compareTo);
                                 ^
          method Demo.<C>lowerBound(C[],C) is not applicable
            (cannot infer type-variable(s) C
              (actual and formal argument lists differ in length))
          method Demo.<T>lowerBound(T[],int,int,ToIntFunction<T>) is not applicable
            (inference variable T has incompatible upper bounds ? super C,Object)
        where C,T are type-variables:
          C extends Comparable<? super C> declared in method <C>lowerBound(C[],C)
          T extends Object declared in method <T>lowerBound(T[],int,int,ToIntFunction<T>)
      1 error


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /* Demo.java */

      import java.util.function.ToIntFunction;

      public final class Demo {

      public static final <C extends Comparable<? super C>> int lowerBound(C[] array, C sought) {
      return lowerBound(array, 0, array.length, sought::compareTo);
      }

      public static <T> int lowerBound(T[] array, int low, int high, ToIntFunction<T> compare) {
      int i;
      for (i = low; i < high && compare.applyAsInt(array[i]) > 0; ++i) {
      }
      return i;
      }

      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Explicitly specifying the type parameter on the method call suppresses the error:

      return Demo.<C>lowerBound(array, 0, array.length, sought::compareTo);

        1. Demo.java
          0.4 kB
          Fairoz Matte

            rpallath Rajendrakumar Pallath
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: