8296171: Compiler incorrectly rejects code with variadic method references

Reviewed-by: mcimadamore
This commit is contained in:
Vicente Romero 2022-11-14 18:31:26 +00:00
parent 749335d34a
commit 3eb789af74
2 changed files with 36 additions and 15 deletions

View file

@ -3111,7 +3111,8 @@ public class Resolve {
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);
}
@ -3127,7 +3128,7 @@ public class Resolve {
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);
}
@ -3238,8 +3239,8 @@ public class Resolve {
/** 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) {
@ -3247,16 +3248,16 @@ public class Resolve {
this.sym = sym;
}
private static StaticKind staticKind(Symbol sym, MethodResolutionContext resolutionContext) {
switch (sym.kind) {
case MTH:
case AMBIGUOUS:
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);
default:
} else {
return StaticKind.UNDEFINED;
}
}

View file

@ -132,5 +132,25 @@ public class BoundUnboundSearchTest extends CompilationTestCase {
}
"""
);
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;
}
}
"""
);
}
}