KT-9601, KT-10103:

Chose maximally specific function in callable reference
 #KT-9601 Fixed
 #KT-10103 Fixed
This commit is contained in:
Dmitry Petrov
2015-11-24 19:23:15 +03:00
parent 67dd97b918
commit 9ccb1fd506
16 changed files with 203 additions and 30 deletions
@@ -23,6 +23,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.resolve.OverrideResolver;
import org.jetbrains.kotlin.resolve.calls.context.CheckArgumentTypesMode;
import org.jetbrains.kotlin.resolve.calls.model.MutableResolvedCall;
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall;
@@ -43,7 +44,8 @@ public class OverloadingConflictResolver {
@Nullable
public <D extends CallableDescriptor> MutableResolvedCall<D> findMaximallySpecific(
@NotNull Set<MutableResolvedCall<D>> candidates,
boolean discriminateGenericDescriptors
boolean discriminateGenericDescriptors,
@NotNull CheckArgumentTypesMode checkArgumentsMode
) {
// Different smartcasts may lead to the same candidate descriptor wrapped into different ResolvedCallImpl objects
Set<MutableResolvedCall<D>> maximallySpecific = new THashSet<MutableResolvedCall<D>>(new TObjectHashingStrategy<MutableResolvedCall<D>>() {
@@ -58,7 +60,7 @@ public class OverloadingConflictResolver {
}
});
for (MutableResolvedCall<D> candidateCall : candidates) {
if (isMaximallySpecific(candidateCall, candidates, discriminateGenericDescriptors)) {
if (isMaximallySpecific(candidateCall, candidates, discriminateGenericDescriptors, checkArgumentsMode)) {
maximallySpecific.add(candidateCall);
}
}
@@ -68,7 +70,8 @@ public class OverloadingConflictResolver {
private <D extends CallableDescriptor> boolean isMaximallySpecific(
@NotNull MutableResolvedCall<D> candidateCall,
@NotNull Set<MutableResolvedCall<D>> candidates,
boolean discriminateGenericDescriptors
boolean discriminateGenericDescriptors,
@NotNull CheckArgumentTypesMode checkArgumentsMode
) {
D me = candidateCall.getResultingDescriptor();
@@ -85,13 +88,13 @@ public class OverloadingConflictResolver {
D other = otherCall.getResultingDescriptor();
if (other == me) continue;
if (definitelyNotMaximallySpecific(me, other, discriminateGenericDescriptors)) {
if (definitelyNotMaximallySpecific(me, other, discriminateGenericDescriptors, checkArgumentsMode)) {
if (!isInvoke) return false;
assert otherCall instanceof VariableAsFunctionResolvedCall : "'invoke' candidate goes with usual one: " + candidateCall + otherCall;
ResolvedCall<VariableDescriptor> otherVariableCall = ((VariableAsFunctionResolvedCall) otherCall).getVariableCall();
if (definitelyNotMaximallySpecific(variable, otherVariableCall.getResultingDescriptor(), discriminateGenericDescriptors)) {
if (definitelyNotMaximallySpecific(variable, otherVariableCall.getResultingDescriptor(), discriminateGenericDescriptors, checkArgumentsMode)) {
return false;
}
}
@@ -99,8 +102,14 @@ public class OverloadingConflictResolver {
return true;
}
private <D extends CallableDescriptor> boolean definitelyNotMaximallySpecific(D me, D other, boolean discriminateGenericDescriptors) {
return !moreSpecific(me, other, discriminateGenericDescriptors) || moreSpecific(other, me, discriminateGenericDescriptors);
private <D extends CallableDescriptor> boolean definitelyNotMaximallySpecific(
D me,
D other,
boolean discriminateGenericDescriptors,
@NotNull CheckArgumentTypesMode checkArgumentsMode
) {
return !moreSpecific(me, other, discriminateGenericDescriptors, checkArgumentsMode) ||
moreSpecific(other, me, discriminateGenericDescriptors, checkArgumentsMode);
}
/**
@@ -113,8 +122,11 @@ public class OverloadingConflictResolver {
private <Descriptor extends CallableDescriptor> boolean moreSpecific(
Descriptor f,
Descriptor g,
boolean discriminateGenericDescriptors
boolean discriminateGenericDescriptors,
@NotNull CheckArgumentTypesMode checkArgumentsMode
) {
boolean resolvingCallableReference = checkArgumentsMode == CheckArgumentTypesMode.CHECK_CALLABLE_TYPE;
if (f.getContainingDeclaration() instanceof ScriptDescriptor && g.getContainingDeclaration() instanceof ScriptDescriptor) {
ScriptDescriptor fs = (ScriptDescriptor) f.getContainingDeclaration();
ScriptDescriptor gs = (ScriptDescriptor) g.getContainingDeclaration();
@@ -131,11 +143,10 @@ public class OverloadingConflictResolver {
if (isGenericF && !isGenericG) return false;
if (isGenericF && isGenericG) {
return moreSpecific(BoundsSubstitutor.substituteBounds(f), BoundsSubstitutor.substituteBounds(g), false);
return moreSpecific(BoundsSubstitutor.substituteBounds(f), BoundsSubstitutor.substituteBounds(g), false, checkArgumentsMode);
}
}
if (OverrideResolver.overrides(f, g)) return true;
if (OverrideResolver.overrides(g, f)) return false;
@@ -154,11 +165,13 @@ public class OverloadingConflictResolver {
boolean fIsVararg = isVariableArity(fParams);
boolean gIsVararg = isVariableArity(gParams);
if (resolvingCallableReference && fIsVararg != gIsVararg) return false;
if (!fIsVararg && gIsVararg) return true;
if (fIsVararg && !gIsVararg) return false;
if (!fIsVararg && !gIsVararg) {
if (fSize > gSize) return false;
if (resolvingCallableReference && fSize != gSize) return false;
if (!resolvingCallableReference && fSize > gSize) return false;
for (int i = 0; i < fSize; i++) {
ValueParameterDescriptor fParam = fParams.get(i);
@@ -229,12 +242,12 @@ public class OverloadingConflictResolver {
return parameterDescriptor.getType();
}
private boolean isVariableArity(List<ValueParameterDescriptor> fParams) {
private static boolean isVariableArity(List<ValueParameterDescriptor> fParams) {
int fSize = fParams.size();
return fSize > 0 && fParams.get(fSize - 1).getVarargElementType() != null;
}
private boolean isGeneric(CallableDescriptor f) {
private static boolean isGeneric(CallableDescriptor f) {
return !f.getOriginal().getTypeParameters().isEmpty();
}
@@ -48,8 +48,6 @@ public class ResolutionResultsHandler {
@NotNull TracingStrategy tracing,
@NotNull Collection<MutableResolvedCall<D>> candidates
) {
boolean resolveOverloads = context.checkArguments == CheckArgumentTypesMode.CHECK_VALUE_ARGUMENTS; // todo rename CheckArgumentTypesMode
Set<MutableResolvedCall<D>> successfulCandidates = Sets.newLinkedHashSet();
Set<MutableResolvedCall<D>> failedCandidates = Sets.newLinkedHashSet();
Set<MutableResolvedCall<D>> incompleteCandidates = Sets.newLinkedHashSet();
@@ -73,10 +71,10 @@ public class ResolutionResultsHandler {
// TODO : maybe it's better to filter overrides out first, and only then look for the maximally specific
if (!successfulCandidates.isEmpty() || !incompleteCandidates.isEmpty()) {
return computeSuccessfulResult(context, tracing, successfulCandidates, incompleteCandidates, resolveOverloads);
return computeSuccessfulResult(context, tracing, successfulCandidates, incompleteCandidates, context.checkArguments);
}
else if (!failedCandidates.isEmpty()) {
return computeFailedResult(tracing, context.trace, failedCandidates, resolveOverloads);
return computeFailedResult(tracing, context.trace, failedCandidates, context.checkArguments);
}
if (!candidatesWithWrongReceiver.isEmpty()) {
tracing.unresolvedReferenceWrongReceiver(context.trace, candidatesWithWrongReceiver);
@@ -92,12 +90,12 @@ public class ResolutionResultsHandler {
@NotNull TracingStrategy tracing,
@NotNull Set<MutableResolvedCall<D>> successfulCandidates,
@NotNull Set<MutableResolvedCall<D>> incompleteCandidates,
boolean resolveOverloads
@NotNull CheckArgumentTypesMode checkArgumentsMode
) {
Set<MutableResolvedCall<D>> successfulAndIncomplete = Sets.newLinkedHashSet();
successfulAndIncomplete.addAll(successfulCandidates);
successfulAndIncomplete.addAll(incompleteCandidates);
OverloadResolutionResultsImpl<D> results = chooseAndReportMaximallySpecific(successfulAndIncomplete, true, resolveOverloads);
OverloadResolutionResultsImpl<D> results = chooseAndReportMaximallySpecific(successfulAndIncomplete, true, checkArgumentsMode);
if (results.isSingleResult()) {
MutableResolvedCall<D> resultingCall = results.getResultingCall();
resultingCall.getTrace().moveAllMyDataTo(context.trace);
@@ -131,7 +129,7 @@ public class ResolutionResultsHandler {
@NotNull TracingStrategy tracing,
@NotNull BindingTrace trace,
@NotNull Set<MutableResolvedCall<D>> failedCandidates,
boolean resolveOverloads
@NotNull CheckArgumentTypesMode checkArgumentsMode
) {
if (failedCandidates.size() != 1) {
// This is needed when there are several overloads some of which are OK but for nullability of the receiver,
@@ -148,7 +146,7 @@ public class ResolutionResultsHandler {
if (severityLevel.contains(ARGUMENTS_MAPPING_ERROR)) {
return recordFailedInfo(tracing, trace, thisLevel);
}
OverloadResolutionResultsImpl<D> results = chooseAndReportMaximallySpecific(thisLevel, false, resolveOverloads);
OverloadResolutionResultsImpl<D> results = chooseAndReportMaximallySpecific(thisLevel, false, checkArgumentsMode);
return recordFailedInfo(tracing, trace, results.getResultingCalls());
}
}
@@ -189,7 +187,7 @@ public class ResolutionResultsHandler {
private <D extends CallableDescriptor> OverloadResolutionResultsImpl<D> chooseAndReportMaximallySpecific(
@NotNull Set<MutableResolvedCall<D>> candidates,
boolean discriminateGenerics,
boolean resolveOverloads
@NotNull CheckArgumentTypesMode checkArgumentsMode
) {
if (candidates.size() == 1) {
return OverloadResolutionResultsImpl.success(candidates.iterator().next());
@@ -199,18 +197,15 @@ public class ResolutionResultsHandler {
if (noOverrides.size() == 1) {
return OverloadResolutionResultsImpl.success(noOverrides.iterator().next());
}
else if (!resolveOverloads) {
return OverloadResolutionResultsImpl.ambiguity(noOverrides);
}
MutableResolvedCall<D> maximallySpecific = overloadingConflictResolver.findMaximallySpecific(noOverrides, false);
MutableResolvedCall<D> maximallySpecific = overloadingConflictResolver.findMaximallySpecific(noOverrides, false, checkArgumentsMode);
if (maximallySpecific != null) {
return OverloadResolutionResultsImpl.success(maximallySpecific);
}
if (discriminateGenerics) {
MutableResolvedCall<D> maximallySpecificGenericsDiscriminated = overloadingConflictResolver.findMaximallySpecific(
noOverrides, true);
noOverrides, true, checkArgumentsMode);
if (maximallySpecificGenericsDiscriminated != null) {
return OverloadResolutionResultsImpl.success(maximallySpecificGenericsDiscriminated);
}
@@ -0,0 +1,16 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun fun1() {}
fun fun1(x: Int) {}
val ref1 = ::<!OVERLOAD_RESOLUTION_AMBIGUITY!>fun1<!>
fun fun2(vararg x: Int) {}
fun fun2(x: Int) {}
val ref2 = ::<!OVERLOAD_RESOLUTION_AMBIGUITY!>fun2<!>
fun fun3(x0: Int, vararg xs: Int) {}
fun fun3(x0: String, vararg xs: String) {}
val ref3 = ::<!OVERLOAD_RESOLUTION_AMBIGUITY!>fun3<!>
@@ -0,0 +1,11 @@
package
public val ref1: [ERROR : Type for ::fun1]
public val ref2: [ERROR : Type for ::fun2]
public val ref3: [ERROR : Type for ::fun3]
public fun fun1(): kotlin.Unit
public fun fun1(/*0*/ x: kotlin.Int): kotlin.Unit
public fun fun2(/*0*/ x: kotlin.Int): kotlin.Unit
public fun fun2(/*0*/ vararg x: kotlin.Int /*kotlin.IntArray*/): kotlin.Unit
public fun fun3(/*0*/ x0: kotlin.Int, /*1*/ vararg xs: kotlin.Int /*kotlin.IntArray*/): kotlin.Unit
public fun fun3(/*0*/ x0: kotlin.String, /*1*/ vararg xs: kotlin.String /*kotlin.Array<out kotlin.String>*/): kotlin.Unit
@@ -0,0 +1,15 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION
open class A
class B: A()
fun A.foo() {}
fun B.foo() {} // more specific
fun bar(a: Any) {}
fun bar(a: Int) {} // more specific
fun test() {
B::foo
::bar
}
@@ -0,0 +1,21 @@
package
public fun bar(/*0*/ a: kotlin.Any): kotlin.Unit
public fun bar(/*0*/ a: kotlin.Int): kotlin.Unit
public fun test(): kotlin.Unit
public fun A.foo(): kotlin.Unit
public fun B.foo(): kotlin.Unit
public open class A {
public constructor A()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class B : A {
public constructor B()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,21 @@
// !CHECK_TYPE
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE
interface IA
interface IB : IA
fun IA.extFun(x: IB) {}
fun IB.extFun(x: IA) {}
fun test() {
val extFun1 = IA::extFun
val extFun2 = IB::<!OVERLOAD_RESOLUTION_AMBIGUITY!>extFun<!>
}
fun testWithExpectedType() {
val extFun_AB_A: IA.(IB) -> Unit = IA::extFun
val extFun_AA_B: IA.(IA) -> Unit = IB::<!NONE_APPLICABLE!>extFun<!>
val extFun_BB_A: IB.(IB) -> Unit = IA::extFun
val extFun_BA_B: IB.(IA) -> Unit = IB::extFun
val extFun_BB_B: IB.(IB) -> Unit = IB::<!OVERLOAD_RESOLUTION_AMBIGUITY!>extFun<!>
}
@@ -0,0 +1,18 @@
package
public fun test(): kotlin.Unit
public fun testWithExpectedType(): kotlin.Unit
public fun IA.extFun(/*0*/ x: IB): kotlin.Unit
public fun IB.extFun(/*0*/ x: IA): kotlin.Unit
public interface IA {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface IB : IA {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,12 @@
// !CHECK_TYPE
interface IA
interface IB : IA
fun IA.extFun() {}
fun IB.extFun() {}
fun test() {
val extFun = IB::extFun
checkSubtype<IB.() -> Unit>(extFun)
}
@@ -0,0 +1,17 @@
package
public fun test(): kotlin.Unit
public fun IA.extFun(): kotlin.Unit
public fun IB.extFun(): kotlin.Unit
public interface IA {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface IB : IA {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -20,7 +20,7 @@ fun fas() {}
fun fas(i: Int = 1) {}
fun test() {
B::<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!> // todo KT-9601 Chose maximally specific function in callable reference
B::foo // todo KT-9601 Chose maximally specific function in callable reference
B::bar checkType { _<KFunction1<B, Unit>>() }
@@ -11,6 +11,6 @@ class B: A() {
}
fun test() {
B::<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!>
B::<!OVERLOAD_RESOLUTION_AMBIGUITY!>fas<!>
B::foo
B::fas
}
@@ -0,0 +1 @@
val stringReversed = String::reversed
@@ -0,0 +1,3 @@
package
public val stringReversed: kotlin.reflect.KFunction1<kotlin.String, kotlin.String>
@@ -2015,6 +2015,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/callableReference/resolve"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("ambiguousWithVararg.kt")
public void testAmbiguousWithVararg() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/callableReference/resolve/ambiguousWithVararg.kt");
doTest(fileName);
}
@TestMetadata("byArgType.kt")
public void testByArgType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/callableReference/resolve/byArgType.kt");
@@ -2039,6 +2045,24 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("kt9601.kt")
public void testKt9601() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/callableReference/resolve/kt9601.kt");
doTest(fileName);
}
@TestMetadata("moreSpecificAmbiguousExtensions.kt")
public void testMoreSpecificAmbiguousExtensions() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/callableReference/resolve/moreSpecificAmbiguousExtensions.kt");
doTest(fileName);
}
@TestMetadata("moreSpecificSimple.kt")
public void testMoreSpecificSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/callableReference/resolve/moreSpecificSimple.kt");
doTest(fileName);
}
@TestMetadata("overloads.kt")
public void testOverloads() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/callableReference/resolve/overloads.kt");
@@ -994,6 +994,12 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
doTest(fileName);
}
@TestMetadata("kt10103.kt")
public void testKt10103() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/resolve/kt10103.kt");
doTest(fileName);
}
@TestMetadata("kt4711.kt")
public void testKt4711() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/resolve/kt4711.kt");