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

Handling of static method reference confused by generic class

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • None
    • tools
    • None
    • Confirmed in 8, 11, 20, and 21 (current development version).

    • generic
    • generic

      Input source:

      import java.util.function.Supplier;
      public class MethRefBug2 {

          static class Foo<T> {
              static int zero() {
                  return 0;
              }
          }

          static void consume(int x) { }
          static void consume(boolean x) { }

          static <T> T grabFrom(Supplier<? extends T> supplier) {
              return supplier.get();
          }

          public static void main(String[] args) {
              consume(grabFrom(Foo::zero));
          }
      }

      This should compile, but instead you get this error:

      MethRefBug2.java:18: error: reference to consume is ambiguous
              consume(grabFrom(Foo::zero));
              ^
        both method consume(int) in MethRefBug2 and method consume(boolean) in MethRefBug2 match
      1 error

      Clearly consume(boolean) is not a valid option, so this error is bogus.

      If you change "boolean" to "Boolean" you get a different (wrong) error:

      MethRefBug2.java:18: error: method consume in class MethRefBug2 cannot be applied to given types;
              consume(grabFrom(Foo::zero));
                     ^
        required: Boolean
        found: Integer
        reason: argument mismatch; inference variable T has incompatible bounds
            upper bounds: Boolean,Object
            lower bounds: Integer
        where T is a type-variable:
          T extends Object declared in method <T>grabFrom(Supplier<? extends T>)
      1 error

      All of this goes away if you change "static class Foo<T>" to "static class Foo", even though how the compiler handles a static method reference should (in theory) not be affected at all by whether the class containing that static method is generic or not.

            Unassigned Unassigned
            acobbs Archie Cobbs
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: