Skip to content

Commit

Permalink
8296171: Compiler incorrectly rejects code with variadic method refer…
Browse files Browse the repository at this point in the history
…ences

Reviewed-by: mcimadamore
  • Loading branch information
Vicente Romero committed Nov 14, 2022
1 parent 749335d commit 3eb789a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
Expand Up @@ -3111,7 +3111,8 @@ Pair<Symbol, ReferenceLookupHelper> resolveMemberReference(Env<AttrContext> env,
boundSearchResolveContext.methodCheck = methodCheck;
Symbol boundSym = lookupMethod(boundEnv, env.tree.pos(),
site.tsym, boundSearchResolveContext, boundLookupHelper);
ReferenceLookupResult boundRes = new ReferenceLookupResult(boundSym, boundSearchResolveContext);
boolean isStaticSelector = TreeInfo.isStaticSelector(referenceTree.expr, names);
ReferenceLookupResult boundRes = new ReferenceLookupResult(boundSym, boundSearchResolveContext, isStaticSelector);
if (dumpMethodReferenceSearchResults) {
dumpMethodReferenceSearchResults(referenceTree, boundSearchResolveContext, boundSym, true);
}
Expand All @@ -3127,7 +3128,7 @@ Pair<Symbol, ReferenceLookupHelper> resolveMemberReference(Env<AttrContext> env,
unboundSearchResolveContext.methodCheck = methodCheck;
unboundSym = lookupMethod(unboundEnv, env.tree.pos(),
site.tsym, unboundSearchResolveContext, unboundLookupHelper);
unboundRes = new ReferenceLookupResult(unboundSym, unboundSearchResolveContext);
unboundRes = new ReferenceLookupResult(unboundSym, unboundSearchResolveContext, isStaticSelector);
if (dumpMethodReferenceSearchResults) {
dumpMethodReferenceSearchResults(referenceTree, unboundSearchResolveContext, unboundSym, false);
}
Expand Down Expand Up @@ -3238,26 +3239,26 @@ static StaticKind reduce(StaticKind sk1, StaticKind sk2) {
/** The lookup result. */
Symbol sym;

ReferenceLookupResult(Symbol sym, MethodResolutionContext resolutionContext) {
this(sym, staticKind(sym, resolutionContext));
ReferenceLookupResult(Symbol sym, MethodResolutionContext resolutionContext, boolean isStaticSelector) {
this(sym, staticKind(sym, resolutionContext, isStaticSelector));
}

private ReferenceLookupResult(Symbol sym, StaticKind staticKind) {
this.staticKind = staticKind;
this.sym = sym;
}

private static StaticKind staticKind(Symbol sym, MethodResolutionContext resolutionContext) {
switch (sym.kind) {
case MTH:
case AMBIGUOUS:
return resolutionContext.candidates.stream()
.filter(c -> c.isApplicable() && c.step == resolutionContext.step)
.map(c -> StaticKind.from(c.sym))
.reduce(StaticKind::reduce)
.orElse(StaticKind.UNDEFINED);
default:
return StaticKind.UNDEFINED;
private static StaticKind staticKind(Symbol sym, MethodResolutionContext resolutionContext, boolean isStaticSelector) {
if (sym.kind == MTH && !isStaticSelector) {
return StaticKind.from(sym);
} else if (sym.kind == MTH || sym.kind == AMBIGUOUS) {
return resolutionContext.candidates.stream()
.filter(c -> c.isApplicable() && c.step == resolutionContext.step)
.map(c -> StaticKind.from(c.sym))
.reduce(StaticKind::reduce)
.orElse(StaticKind.UNDEFINED);
} else {
return StaticKind.UNDEFINED;
}
}

Expand Down
Expand Up @@ -132,5 +132,25 @@ public void test() {
}
"""
);

assertOK(
getDiagConsumer(0, -1),
"""
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;
}
}
"""
);
}
}

1 comment on commit 3eb789a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.