diff --git a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestWithStdlibGenerated.java b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestWithStdlibGenerated.java index 7c55f2442d7..050e76224e7 100644 --- a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestWithStdlibGenerated.java +++ b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestWithStdlibGenerated.java @@ -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") diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/ResolutionResultsHandler.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/ResolutionResultsHandler.java index 9ed0af58e2c..bb89699a00a 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/ResolutionResultsHandler.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/ResolutionResultsHandler.java @@ -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> 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()); } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/KotlinCallResolver.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/KotlinCallResolver.kt index f40ca24c11f..c878e366a39 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/KotlinCallResolver.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/KotlinCallResolver.kt @@ -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( diff --git a/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/resolutionInOldInference.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/resolutionInOldInference.fir.kt new file mode 100644 index 00000000000..ea9e10330bc --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/resolutionInOldInference.fir.kt @@ -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("") +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/resolutionInOldInference.kt b/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/resolutionInOldInference.kt new file mode 100644 index 00000000000..1da656d5b7c --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/resolutionInOldInference.kt @@ -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("") +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/resolutionInOldInference.txt b/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/resolutionInOldInference.txt new file mode 100644 index 00000000000..d7aa414e20d --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/resolutionInOldInference.txt @@ -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 + } +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java index afdb479d6ec..34d19643ff9 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java @@ -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") diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java index cf122df09bf..7fa263e5198 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java @@ -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")