Integrated: 8296171: Compiler incorrectly rejects code with variadic method references
Vicente Romero
vromero at openjdk.org
Mon Nov 14 18:34:55 UTC 2022
On Thu, 10 Nov 2022 22:02:04 GMT, Vicente Romero <vromero at openjdk.org> wrote:
> 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
This pull request has now been integrated.
Changeset: 3eb789af
Author: Vicente Romero <vromero at openjdk.org>
URL: https://git.openjdk.org/jdk/commit/3eb789af74231d37796a5670ffab935cabbf3b09
Stats: 36 lines in 2 files changed: 21 ins; 0 del; 15 mod
8296171: Compiler incorrectly rejects code with variadic method references
Reviewed-by: mcimadamore
-------------
PR: https://git.openjdk.org/jdk/pull/11093
More information about the compiler-dev
mailing list