[OI] Prefer candidate without @OverloadResolutionByLambdaReturnType

This commit is contained in:
Dmitriy Novozhilov
2020-05-21 15:46:40 +03:00
parent a604404bff
commit f76b57d260
8 changed files with 91 additions and 1 deletions
@@ -1829,6 +1829,11 @@ public class FirOldFrontendDiagnosticsTestWithStdlibGenerated extends AbstractFi
public void testOverloadByLambdaReturnType_enabled_no_annotation() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/factoryPattern/overloadByLambdaReturnType_enabled_no_annotation.kt");
}
@TestMetadata("resolutionInOldInference.kt")
public void testResolutionInOldInference() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/factoryPattern/resolutionInOldInference.kt");
}
}
@TestMetadata("compiler/testData/diagnostics/testsWithStdLib/forInArrayLoop")
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.config.LanguageVersionSettings;
import org.jetbrains.kotlin.descriptors.CallableDescriptor;
import org.jetbrains.kotlin.descriptors.ModuleDescriptor;
import org.jetbrains.kotlin.resolve.BindingTrace;
import org.jetbrains.kotlin.resolve.calls.KotlinCallResolver;
import org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt;
import org.jetbrains.kotlin.resolve.calls.context.CallResolutionContext;
import org.jetbrains.kotlin.resolve.calls.context.CheckArgumentTypesMode;
@@ -32,6 +33,7 @@ import org.jetbrains.kotlin.resolve.calls.tower.TowerUtilsKt;
import org.jetbrains.kotlin.util.CancellationChecker;
import java.util.*;
import java.util.stream.Collectors;
import static org.jetbrains.kotlin.resolve.calls.results.ResolutionStatus.*;
@@ -221,6 +223,14 @@ public class ResolutionResultsHandler {
Set<MutableResolvedCall<D>> specificCalls =
myResolver.chooseMaximallySpecificCandidates(refinedCandidates, checkArgumentsMode, discriminateGenerics);
if (specificCalls.size() > 1) {
specificCalls = specificCalls.stream()
.filter((call) ->
!call.getCandidateDescriptor().getAnnotations().hasAnnotation(
KotlinCallResolver.Companion.getOVERLOAD_RESOLUTION_BY_LAMBDA_ANNOTATION())
).collect(Collectors.toSet());
}
if (specificCalls.size() == 1) {
return OverloadResolutionResultsImpl.success(specificCalls.iterator().next());
}
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.resolve.calls
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus
import org.jetbrains.kotlin.resolve.calls.components.*
@@ -25,7 +26,7 @@ class KotlinCallResolver(
private val callComponents: KotlinCallComponents
) {
companion object {
private val OVERLOAD_RESOLUTION_BY_LAMBDA_ANNOTATION = FqName.fromSegments(listOf("kotlin", "OverloadResolutionByLambdaReturnType"))
val OVERLOAD_RESOLUTION_BY_LAMBDA_ANNOTATION = FqName.fromSegments(listOf("kotlin", "OverloadResolutionByLambdaReturnType"))
}
fun resolveCall(
@@ -0,0 +1,24 @@
// !LANGUAGE: -NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_EXPRESSION
// ISSUE: KT-11265
// FILE: OverloadResolutionByLambdaReturnType.kt
package kotlin
annotation class OverloadResolutionByLambdaReturnType
// FILE: main.kt
import kotlin.OverloadResolutionByLambdaReturnType
@OverloadResolutionByLambdaReturnType
fun create(f: (Int) -> Int): Int = 1
fun create(f: (Int) -> String): String = ""
fun takeString(s: String) {}
fun test_1() {
val x = <!AMBIGUITY!>create<!> { "" }
takeString("")
}
@@ -0,0 +1,24 @@
// !LANGUAGE: -NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_EXPRESSION
// ISSUE: KT-11265
// FILE: OverloadResolutionByLambdaReturnType.kt
package kotlin
annotation class OverloadResolutionByLambdaReturnType
// FILE: main.kt
import kotlin.OverloadResolutionByLambdaReturnType
@OverloadResolutionByLambdaReturnType
fun create(f: (Int) -> Int): Int = 1
fun create(f: (Int) -> String): String = ""
fun takeString(s: String) {}
fun test_1() {
val x = create { "" }
takeString("")
}
@@ -0,0 +1,16 @@
package
@kotlin.OverloadResolutionByLambdaReturnType public fun create(/*0*/ f: (kotlin.Int) -> kotlin.Int): kotlin.Int
public fun create(/*0*/ f: (kotlin.Int) -> kotlin.String): kotlin.String
public fun takeString(/*0*/ s: kotlin.String): kotlin.Unit
public fun test_1(): kotlin.Unit
package kotlin {
public final annotation class OverloadResolutionByLambdaReturnType : kotlin.Annotation {
public constructor OverloadResolutionByLambdaReturnType()
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
}
}
@@ -2844,6 +2844,11 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
public void testOverloadByLambdaReturnType_enabled_no_annotation() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/factoryPattern/overloadByLambdaReturnType_enabled_no_annotation.kt");
}
@TestMetadata("resolutionInOldInference.kt")
public void testResolutionInOldInference() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/factoryPattern/resolutionInOldInference.kt");
}
}
@TestMetadata("compiler/testData/diagnostics/testsWithStdLib/forInArrayLoop")
@@ -2844,6 +2844,11 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno
public void testOverloadByLambdaReturnType_enabled_no_annotation() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/factoryPattern/overloadByLambdaReturnType_enabled_no_annotation.kt");
}
@TestMetadata("resolutionInOldInference.kt")
public void testResolutionInOldInference() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/factoryPattern/resolutionInOldInference.kt");
}
}
@TestMetadata("compiler/testData/diagnostics/testsWithStdLib/forInArrayLoop")