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

Eclipse compiler can determine intended varargs method while javac complains method ambiguous

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

Description

Transfer of [Bug 578410](https://bugs.eclipse.org/bugs/show_bug.cgi?id=578410)

# Problem

Eclipse compiler can infer primitive varargs overload while javac errors with "reference to method is ambiguous". This leads to nice little surprises when the build pipeline in Jenkins then fails unexpectedly.

# Example

```java
public class NonAmbiguousVarargs {

static void method(int... array) {
System.out.println("int array");
}

static void method(T... array) {
System.out.println("Object array");
}

public static void main(String[] args) {
method(1);
}
}
```

While `eclipsec` compiles this code without problems, bindind the call to `method` to the `int...` overload, `javac` fails with the following error message

```
NonAmbiguousVarargs.java:12: error: reference to method is ambiguous
method(1);
^
both method method(int...) in NonAmbiguousVarargs and method method(T...) in NonAmbiguousVarargs match
where T is a type-variable:
T extends Object declared in method method(T...)
1 error
```

The compiled class will output "int array" in all cases where compilation was successful.

Assuming the behaviour of the `javac` is correct according to the Java language specification (the code does only compile in JDK 1.5 and 1.6; JDK 1.7 - 20 give the above error) then `eclipsec` should also give an error on this line.

# Tested with

* Eclipse SDK
Version: 2022-12 (4.26)
Build id: I20220904-1800

* java version "1.5.0_22"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_22-b03)
Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_22-b03, mixed mode)

* java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)

* openjdk version "1.7.0_75"
OpenJDK Runtime Environment (build 1.7.0_75-b13)
OpenJDK Client VM (build 24.75-b04, mixed mode)

* ... some other versions in between ...

* openjdk version "20-ea" 2023-03-21
OpenJDK Runtime Environment (build 20-ea+13-860)
OpenJDK 64-Bit Server VM (build 20-ea+13-860, mixed mode, sharing)

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.