JDK-8143586 : JavaC reports method ambiguity while Eclipse Mars doesn't
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8u60
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2015-08-30
  • Updated: 2015-11-23
  • Resolved: 2015-11-23
Related Reports
Duplicate :  
Duplicate :  
Description
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 [verzi��sz��m: 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
The attached class C doesn't compile with javac but compiles with Eclipse Mars

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Try to compile the given class.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Compilation successful
ACTUAL -
Fails with ambiguity error

ERROR MESSAGES/STACK TRACES THAT OCCUR :
error: reference to f is ambiguous
            f(p1, ArrayList::new);
            ^
  both method f(Consumer<Object>,Function<Object,Object>) in C and method f(Consumer<Object>,Supplier<Object>) in C match
C.java:1909: error: incompatible types: invalid constructor reference
            f(p1, ArrayList::new);
                  ^
    no suitable constructor found for ArrayList(Object)
        constructor ArrayList.ArrayList(int) is not applicable
          (cannot infer type-variable(s) E
            (argument mismatch; Object cannot be converted to int))
        constructor ArrayList.ArrayList() is not applicable
          (cannot infer type-variable(s) E
            (actual and formal argument lists differ in length))
        constructor ArrayList.ArrayList(Collection) is not applicable
          (cannot infer type-variable(s) E
            (argument mismatch; Object cannot be converted to Collection<? extends E>))
        constructor ArrayList.ArrayList(int) is not applicable
          (cannot infer type-variable(s) E
            (argument mismatch; Object cannot be converted to int))
        constructor ArrayList.ArrayList() is not applicable
          (cannot infer type-variable(s) E
            (actual and formal argument lists differ in length))
        constructor ArrayList.ArrayList(Collection) is not applicable
          (cannot infer type-variable(s) E
            (argument mismatch; Object cannot be converted to Collection<? extends E>))
  where E is a type-variable:
    E extends Object declared in class ArrayList
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
class C {
    void f(Consumer<Object> p1, Function<Object, Object> p2) {
        
    }
    void f(Consumer<Object> p1) {
        f(p1, ArrayList::new);
    }
    
    void f(Consumer<Object> p1, int p2) {
        f(p1, () -> new ArrayList<>(p2));
    }
    
    void f(Consumer<Object> f1, Supplier<Object> f2) {
        
    }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Cast ArrayList::new to supplier manually.


Comments
This is a known defect in Eclispe which perhaps over-eagerly applying the congruence rules of 15.13.2 while 15.12.2.1 (Identify Potentially Applicable Methods) speaks of void-value disparity only for lambda expressions. See JDK-8077243 for a detailed analysis on a similar case.
23-11-2015

Workaround: ***************** f(p1, a-> new ArrayList( (int) a)); instead of using f(p1, ArrayList::new);
22-11-2015

Test result: ######### OS: Windows 7-64 bit JAVAC, Netbeans IDE 8.0.2 ----------------------------------- 8u60 : Fail 8u65 : Fail 9ea : Fail Eclipse Luna version 4.4.2: ----------------------------------- 8u60 : Pass 8u65 : Pass 8u60-b27,8u65-b17,9ea-b64 <JAVAC, Netbeans IDE 8.0.2> ################################################## C:\Abhijit\tools>java -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) C:\Abhijit\tools> C:\Abhijit\tools>javac c.java c.java:11: error: reference to f is ambiguous f(p1, ArrayList::new); ^ both method f(Consumer<Object>,Function<Object,Object>) in C and method f(Consumer<Object>,Supplier<Object>) in C match c.java:11: error: incompatible types: invalid constructor reference f(p1, ArrayList::new); ^ no suitable constructor found for ArrayList(Object) constructor ArrayList.ArrayList(int) is not applicable (cannot infer type-variable(s) E (argument mismatch; Object cannot be converted to int)) constructor ArrayList.ArrayList() is not applicable (cannot infer type-variable(s) E (actual and formal argument lists differ in length)) constructor ArrayList.ArrayList(Collection) is not applicable (cannot infer type-variable(s) E (argument mismatch; Object cannot be converted to Collection<? extends E>)) constructor ArrayList.ArrayList(int) is not applicable (cannot infer type-variable(s) E (argument mismatch; Object cannot be converted to int)) constructor ArrayList.ArrayList() is not applicable (cannot infer type-variable(s) E (actual and formal argument lists differ in length)) constructor ArrayList.ArrayList(Collection) is not applicable (cannot infer type-variable(s) E (argument mismatch; Object cannot be converted to Collection<? extends E>)) where E is a type-variable: E extends Object declared in class ArrayList Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 2 errors C:\Abhijit\tools>
18-11-2015