JDK-8038485 : Inconsistent ambiguous warning for overloaded methods with functional interfaces
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8
  • Priority: P4
  • Status: Resolved
  • Resolution: Duplicate
  • OS: windows_8
  • CPU: x86
  • Submitted: 2014-03-25
  • Updated: 2014-11-14
  • Resolved: 2014-05-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8Resolved
Related Reports
Duplicate :  
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) Client VM (build 25.0-b70, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [version: 6.0.6002]

A DESCRIPTION OF THE PROBLEM :
The following two methods are reported potentially ambigous:

- Object call1(Function<Object, Object> function)
- void call1(Consumer<Object> consumer)

However, the following two are not:

- void call2(Runnable consumer)
- Object call2(Callable<Object> function)

This seems inconsitent because in both case the argument list is the same for the functional interfaces and the overloaded methods differ only by one of the functional interfaces having a return value while the other does not.

When calling them,

call1((Object arg) -> {})
call2(() -> {})

compiles without any problem (no warning). However,

call1((arg) -> {})

is reported ambigous and does not compile.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the example code to see the warning.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Expected no warnings and also, I expect "call1((arg) -> {})" to compile.
ACTUAL -
Warning is printed for the method "call1" and "call1((arg) -> {})" does not compile (due to reported being ambiguous).

ERROR MESSAGES/STACK TRACES THAT OCCUR :
warning: [overloads] call1(Consumer<Object>) in ExampleMethods is potentially ambiguous with call1(Function<Object,Object>) in ExampleMethods

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.concurrent.Callable;
import java.util.function.Consumer;
import java.util.function.Function;

public final class ExampleMethods {
    public static void call1(Consumer<Object> consumer) {
        consumer.accept(null);
    }

    public static Object call1(Function<Object, Object> function) {
        return function.apply(null);
    }

    public static void call2(Runnable consumer) {
        consumer.run();
    }

    public static Object call2(Callable<Object> function) throws Exception {
        return function.call();
    }

    public static void call1Example() {
        call1((Object arg) -> {});
        // Uncommenting the line below is a compile time error.
        // call1((arg) -> {});
    }

    public static void call2Example() {
        call2(() -> {});
    }
}
---------- END SOURCE ----------


Comments
Thanks for the report, yes we confirm that there is a bug in this area
27-03-2014

Reported webbug. Can the compiler team verify that javac is correctly flagging an issue here.
27-03-2014