RFR: 8296171: Compiler incorrectly rejects code with variadic method references
Vicente Romero
vromero at openjdk.org
Mon Nov 14 18:33:05 UTC 2022
Please review this PR that is syncing javac with the spec. Javac is rejecting this code:
import java.util.function.*;
interface Intf {
Object apply(String... args);
}
public class Test {
public static Object foo(Object o) { return "bar"; }
public final Object foo(Object... o) { return "foo"; }
public void test() {
Intf f = this::foo;
}
}
javac indicates that the method reference is invalid when according to the spec the second `foo` method should be selected by the compiler. The related section of the spec can be found at section `15.13.1 Compile-Time Declaration of a Method Reference`:
– For all other forms of method reference expression, one search for a most
specific applicable method is performed. The search is as specified in
§15.12.2.2 through §15.12.2.5, with the clarifications below.
The method reference is treated as if it were an invocation with argument
expressions of types P 1 , ..., P n ; the type arguments, if any, are given by the
method reference expression.
If the search results in an error as specified in §15.12.2.2 through §15.12.2.5,
or if the most specific applicable method is static , there is no compile-time
declaration.
Otherwise, the compile-time declaration is the most specific applicable
method.
Actually the method search is correct but later on javac is seeing that both methods are applicable and given that they have different staticness, it is making wrong assumptions down the road.
TIA
-------------
Commit messages:
- removing whitespace
- 8296171: Compiler incorrectly rejects code with variadic method references
Changes: https://git.openjdk.org/jdk/pull/11093/files
Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=11093&range=00
Issue: https://bugs.openjdk.org/browse/JDK-8296171
Stats: 36 lines in 2 files changed: 21 ins; 0 del; 15 mod
Patch: https://git.openjdk.org/jdk/pull/11093.diff
Fetch: git fetch https://git.openjdk.org/jdk pull/11093/head:pull/11093
PR: https://git.openjdk.org/jdk/pull/11093
More information about the compiler-dev
mailing list