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

Eclipse compiles code that shouldn't compile, when resolving overloads from inferred type

Open
#99 7 comments 0 reactions 0 assignees View on GitHub
compiler javac
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

Take this file:

```java
package p;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

public class Test {

void m() {
set(x(() -> map()));
set(y(y -> map()));
}

Map, Object> map() {
return null;
}

T x(Supplier f) {
return f.get();
}

T y(Function f) {
return f.apply(null);
}

void set(List a) {
}

void set(Map map) {
}
}
```

Eclipse compiles it just fine with language level 17, when javac produces this output:

```
$ javac p/Test.java
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8
p\Test.java:12: error: reference to set is ambiguous
set(y(y -> map()));
^
both method set(List) in Test and method set(Map) in Test match
p\Test.java:12: error: incompatible types: inference variable T has incompatible bounds
set(y(y -> map()));
^
lower bounds: List,Object
lower bounds: Map,Object>
where T is a type-variable:
T extends Object declared in method y(Function)
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
2 errors
```

I'm personally not convinced that this is how the language should work, but I guess the JLS specifies things this way? Note that if `set(List)` tries to capture the wildcard in a type variable, then Eclipse also complains about the same error. With this code:

```java
package p;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

public class Test {

void m() {
set(x(() -> map()));
set(y(y -> map()));
}

Map, Object> map() {
return null;
}

T x(Supplier f) {
return f.get();
}

T y(Function f) {
return f.apply(null);
}

void set(List a) {
}

void set(Map map) {
}
}
```

Eclipse now produces these two errors:

```
The method set(List) is ambiguous for the type Test
```

And

```
Type mismatch: cannot convert from Map,Object> to List
```

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.