eclipse-jdt / eclipse-jdt/eclipse.jdt.core

ambiguous method in ECJ while javac compiles

Open
#2,304 1 comment 0 reactions 1 assignee Claimed by @stephan-herrmann View on GitHub
javac
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

Consider the following code:
```java
package mwe;

import java.util.function.Consumer;
import java.util.function.Function;

public class ServiceRegistry {

static void registerHandler(Class type, Function handler) { // (1)
handler.apply(null);
}
static void registerHandler(Class type, Consumer handler) {
handler.accept(null);
}

public static void main(String[] args) {
Service service = new Service() {
@Override public Object publish(Request request) {
System.out.println("Function");
return null;
}
@Override public void publish(Object message) {
System.out.println("Consumer");
}
};
registerHandler(Request.class, (Request r) -> service.publish(r)); // (2)
}
}

interface Request {}
interface Service {
void publish(Object message);
Object publish(Request request);
}
```
`ecj` complains on the call to `registerHandler()` in (2)
> The method registerHandler(Class, Function) is ambiguous for the type ServiceRegistry

while `javac` in all versions (8 - 23) compiles this code just fine and takes the `Function` overload as `publish(Request)` is more specific.

Notably the error goes away when the `? super R` in (1) is restricted to just `R`. On the other hand removing the type declaration of the lambda parameter `r` in (2) also lets this error appear in `javac` (which is somehow to expect. I am actually surprised that `javac` makes an educated guess here on which of the `publish` methods I mean)

## Tested with
Eclipse SDK
Version: 2024-06 (4.32)
Build id: I20240408-1800

ECJ v20240215-1558, 3.37.0
ECJ v20240405-1402, 3.38.0

openjdk version "22" 2024-03-19 Temurin-22+36 (build 22+36)
openjdk version "23-ea" 2024-09-17 (build 23-ea+13-981)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.