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

Type inference problem. Ambiguous method

XMLWordPrintable

    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.8.0_60"
      Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
      Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      All latest updates

      A DESCRIPTION OF THE PROBLEM :
      The following case does not compile using Javac tool. It compiles and works using eclipse incremental compiler.

      package test;

      import java.util.function.Supplier;

      class X {
          public X() {}
      }

      public class Main {
          static <T extends X> T foo( T reg ) { return reg; }

          static <T extends X> T foo( Supplier<T> sup ) { return sup.get(); }

          public static void main(String[] args) throws Exception {
              X l = foo( X::new );
              System.out.println(l);
          }
      }

      Compilation with javac.exe yelds:
      Main.java:24: error: reference to foo is ambiguous
              X l = foo( X::new );
                    ^
        both method <T#1>foo(T#1) in Main and method <T#2>foo(Supplier<T#2>) in Main match
        where T#1,T#2 are type-variables:
          T#1 extends X declared in method <T#1>foo(T#1)
          T#2 extends X declared in method <T#2>foo(Supplier<T#2>)
      Main.java:24: error: incompatible types: cannot infer type-variable(s) T
              X l = foo( X::new );
                       ^
          (argument mismatch; X is not a functional interface)
        where T is a type-variable:
          T extends X declared in method <T>foo(T)
      2 errors



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Copy-paste example from description section and compile using javac.exe

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Compile and work as in eclipse compiler. In Eclipse no ambiguity is reported. Type inference works correctly.
      ACTUAL -
      See the description section.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package test;

      import java.util.function.Supplier;

      class X {
          public X() {}
      }

      public class Main {
          static <T extends X> T foo( T reg ) { return reg; }

          static <T extends X> T foo( Supplier<T> sup ) { return sup.get(); }

          public static void main(String[] args) throws Exception {
              X l = foo( X::new );
              System.out.println(l);
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      package test;

      import java.util.function.Supplier;

      class X {
          public X() {}
      }

      public class Main {
          static <T extends X> T foo( T reg ) { return reg; }

          static <T extends X> T foo( Supplier<T> sup ) { return sup.get(); }

          public static void main(String[] args) throws Exception {
              Supplier<X> sup = X::new;
              X l = foo( sup );
              System.out.println(l);
          }
      }

      The difference in this code is that there is a local variable added which explicitly states that X::new method reference should be converted to Supplier<T>


            dlsmith Dan Smith
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: