Skip to content

Commit 539756f

Browse files
committed
KT-3772 Invoke and overload resolution ambiguity
#KT-3772 Fixed
1 parent 3f78252 commit 539756f

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/results/OverloadingConflictResolver.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
2525
import org.jetbrains.jet.lang.resolve.OverridingUtil;
2626
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCallWithTrace;
27+
import org.jetbrains.jet.lang.resolve.calls.model.VariableAsFunctionResolvedCall;
2728
import org.jetbrains.jet.lang.types.JetType;
2829
import org.jetbrains.jet.lang.types.TypeUtils;
2930
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
@@ -69,16 +70,38 @@ private <D extends CallableDescriptor> boolean isMaximallySpecific(
6970
boolean discriminateGenericDescriptors
7071
) {
7172
D me = candidateCall.getResultingDescriptor();
73+
74+
boolean isInvoke = candidateCall instanceof VariableAsFunctionResolvedCall;
75+
VariableDescriptor variable;
76+
if (isInvoke) {
77+
variable = ((VariableAsFunctionResolvedCall) candidateCall).getVariableCall().getResultingDescriptor();
78+
}
79+
else {
80+
variable = null;
81+
}
82+
7283
for (ResolvedCallWithTrace<D> otherCall : candidates) {
7384
D other = otherCall.getResultingDescriptor();
7485
if (other == me) continue;
75-
if (!moreSpecific(me, other, discriminateGenericDescriptors) || moreSpecific(other, me, discriminateGenericDescriptors)) {
76-
return false;
86+
87+
if (definitelyNotMaximallySpecific(me, other, discriminateGenericDescriptors)) {
88+
89+
if (!isInvoke) return false;
90+
91+
assert otherCall instanceof VariableAsFunctionResolvedCall : "'invoke' candidate goes with usual one: " + candidateCall + otherCall;
92+
ResolvedCallWithTrace<VariableDescriptor> otherVariableCall = ((VariableAsFunctionResolvedCall) otherCall).getVariableCall();
93+
if (definitelyNotMaximallySpecific(variable, otherVariableCall.getResultingDescriptor(), discriminateGenericDescriptors)) {
94+
return false;
95+
}
7796
}
7897
}
7998
return true;
8099
}
81100

101+
private <D extends CallableDescriptor> boolean definitelyNotMaximallySpecific(D me, D other, boolean discriminateGenericDescriptors) {
102+
return !moreSpecific(me, other, discriminateGenericDescriptors) || moreSpecific(other, me, discriminateGenericDescriptors);
103+
}
104+
82105
/**
83106
* Let < mean "more specific"
84107
* Subtype < supertype
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//KT-3772 Invoke and overload resolution ambiguity
2+
package bar
3+
4+
open class A {
5+
public fun invoke(<!UNUSED_PARAMETER!>f<!>: A.() -> Unit) {}
6+
}
7+
8+
class B {
9+
public fun invoke(<!UNUSED_PARAMETER!>f<!>: B.() -> Unit) {}
10+
}
11+
12+
open class C
13+
val C.attr = A()
14+
15+
open class D: C()
16+
val D.attr = B()
17+
18+
19+
fun main(args: Array<String>) {
20+
val b = D()
21+
b.attr {} // overload resolution ambiguity
22+
23+
val d = b.attr
24+
d {} // no error
25+
}

compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4582,6 +4582,11 @@ public void testInvokeAsMemberExtensionToExplicitReceiver() throws Exception {
45824582
doTest("compiler/testData/diagnostics/tests/resolve/invoke/invokeAsMemberExtensionToExplicitReceiver.kt");
45834583
}
45844584

4585+
@TestMetadata("kt3772.kt")
4586+
public void testKt3772() throws Exception {
4587+
doTest("compiler/testData/diagnostics/tests/resolve/invoke/kt3772.kt");
4588+
}
4589+
45854590
}
45864591

45874592
public static Test innerSuite() {

0 commit comments

Comments
 (0)