Open Madjosz opened 2 years ago
Generally, overload resolution happens in phases such when one phase succeeds, the next phase is never consulted. The first phase ("Applicable by Strict Invocation") does not allow an conversions like (un)boxing. At a naive look this could justify why the first method is chosen over the second. However, even the first method is not a "Strict Invocation" but a "Variable Arity Invocation" (the int
must still be converted to int[]
). In that - 3rd - phase there is no more mentioning of "strict". Ergo, both methods are applicable, and it would require step "15.12.2.5. Choosing the Most Specific Method" to prefer one over the other.
Someone would have to spend time to debug the compiler with the corresonding JLS paragraphs on the table. A key piece will probably be https://docs.oracle.com/javase/specs/jls/se19/html/jls-18.html#jls-18.5.4.
OTOH, overloading is one of the best means to not only confuse compilers but also programmers. Overloading with generic methods likely comes with more complexity then what average users of those methods can handle. Add varargs plus boxing to the mix and you've got one of the hardest riddles the language has to offer. Method names are cheap, the complexity of overloading is not - far from it.
Something like this also happens with
class A {
public void method(String a, String... b) {
}
}
class B extends A {
public void method(String a, String a2, String... b) {
}
}
when you call the method with something like
method("a", "b", "c", "d");
Eclipse wants to use the method in class B while javac is not happy.
Eclipse wants to use the method in class B while javac is not happy.
At compliance 1.7 reports the same ambiguity, only since 1.8 the example is accepted.
However, the original issue was about a combination of varargs plus boxing in overloading resolution. Since the newer example has no boxing in the picture please file a new issue. Thanks.
Anyone planning to investigate these issues may want to have a look at https://bugs.eclipse.org/bugs/show_bug.cgi?id=457233#c2 where I collected some links to bug fixes of JLS, that may not yet have found their way into ecj.
Transfer of Bug 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
While
eclipsec
compiles this code without problems, bindind the call tomethod
to theint...
overload,javac
fails with the following error messageThe 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) theneclipsec
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)