From a0510f4a675b63c04a68a8935cedffebb491b572 Mon Sep 17 00:00:00 2001 From: Ivan Kochurkin Date: Fri, 28 Jan 2022 22:48:52 +0300 Subject: [PATCH] [FIR] Fixed Replace ConeTypeVariable on ConeTypeParameter ^KT-51017 in AbstractConeCallConflictResolver.computeParameterTypes --- ...CompilerTestFE10TestdataTestGenerated.java | 6 ++++ ...irOldFrontendDiagnosticsTestGenerated.java | 6 ++++ ...DiagnosticsWithLightTreeTestGenerated.java | 6 ++++ .../calls/AbstractConeCallConflictResolver.kt | 30 +++++++++++++++++-- ...nceWithMostSpecificGenericTypeParameter.kt | 12 ++++++++ ...ceWithMostSpecificGenericTypeParameter.txt | 17 +++++++++++ .../callableReference/generic/kt11075.fir.kt | 10 ------- .../callableReference/generic/kt11075.kt | 1 + .../test/runners/DiagnosticTestGenerated.java | 6 ++++ 9 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/callableReference/callableReferenceWithMostSpecificGenericTypeParameter.kt create mode 100644 compiler/testData/diagnostics/tests/callableReference/callableReferenceWithMostSpecificGenericTypeParameter.txt delete mode 100644 compiler/testData/diagnostics/tests/callableReference/generic/kt11075.fir.kt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index 99eb2393744..5bf92f55d36 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -2646,6 +2646,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/callableReference/callableReferenceAssignmentToVar.kt"); } + @Test + @TestMetadata("callableReferenceWithMostSpecificGenericTypeParameter.kt") + public void testCallableReferenceWithMostSpecificGenericTypeParameter() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/callableReferenceWithMostSpecificGenericTypeParameter.kt"); + } + @Test @TestMetadata("classVsPackage.kt") public void testClassVsPackage() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index 0e95e8502d6..c21f3c21aa6 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -2646,6 +2646,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/callableReference/callableReferenceAssignmentToVar.kt"); } + @Test + @TestMetadata("callableReferenceWithMostSpecificGenericTypeParameter.kt") + public void testCallableReferenceWithMostSpecificGenericTypeParameter() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/callableReferenceWithMostSpecificGenericTypeParameter.kt"); + } + @Test @TestMetadata("classVsPackage.kt") public void testClassVsPackage() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index 26df67d766d..440e9ab1c37 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -2646,6 +2646,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/callableReference/callableReferenceAssignmentToVar.kt"); } + @Test + @TestMetadata("callableReferenceWithMostSpecificGenericTypeParameter.kt") + public void testCallableReferenceWithMostSpecificGenericTypeParameter() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/callableReferenceWithMostSpecificGenericTypeParameter.kt"); + } + @Test @TestMetadata("classVsPackage.kt") public void testClassVsPackage() throws Exception { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/AbstractConeCallConflictResolver.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/AbstractConeCallConflictResolver.kt index 32f21d1ba8d..0179ba6b12c 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/AbstractConeCallConflictResolver.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/AbstractConeCallConflictResolver.kt @@ -7,12 +7,17 @@ package org.jetbrains.kotlin.fir.resolve.calls import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.utils.isExpect +import org.jetbrains.kotlin.fir.diagnostics.ConeSimpleDiagnostic +import org.jetbrains.kotlin.fir.diagnostics.DiagnosticKind import org.jetbrains.kotlin.fir.resolve.inference.InferenceComponents +import org.jetbrains.kotlin.fir.symbols.ConeTypeParameterLookupTag import org.jetbrains.kotlin.fir.types.* +import org.jetbrains.kotlin.fir.types.impl.ConeTypeParameterTypeImpl import org.jetbrains.kotlin.name.StandardClassIds import org.jetbrains.kotlin.resolve.calls.results.* import org.jetbrains.kotlin.types.model.KotlinTypeMarker import org.jetbrains.kotlin.types.model.requireOrDescribe +import org.jetbrains.kotlin.utils.addIfNotNull abstract class AbstractConeCallConflictResolver( private val specificityComparator: TypeSpecificityComparator, @@ -160,9 +165,28 @@ abstract class AbstractConeCallConflictResolver( call: Candidate, function: FirFunction ): List { - return listOfNotNull(function.receiverTypeRef?.coneType) + - (call.resultingTypeForCallableReference?.typeArguments?.map { it as ConeKotlinType } - ?: call.argumentMapping?.map { it.value.argumentType() }.orEmpty()) + return buildList { + addIfNotNull(function.receiverTypeRef?.coneType) + val typeForCallableReference = call.resultingTypeForCallableReference + if (typeForCallableReference != null) { + typeForCallableReference.typeArguments + .mapTo(this) { + when (it) { + is ConeTypeVariableType -> { + val typeParameterLookupTag = it.lookupTag.originalTypeParameter as ConeTypeParameterLookupTag? + if (typeParameterLookupTag == null) { + ConeErrorType(ConeSimpleDiagnostic("no type parameter for type variable", DiagnosticKind.Other)) + } else { + ConeTypeParameterTypeImpl(typeParameterLookupTag, it.isNullable) + } + } + else -> it as ConeKotlinType + } + } + } else { + call.argumentMapping?.mapTo(this) { it.value.argumentType() } + } + } } private fun createFlatSignature(call: Candidate, klass: FirClassLikeDeclaration): FlatSignature { diff --git a/compiler/testData/diagnostics/tests/callableReference/callableReferenceWithMostSpecificGenericTypeParameter.kt b/compiler/testData/diagnostics/tests/callableReference/callableReferenceWithMostSpecificGenericTypeParameter.kt new file mode 100644 index 00000000000..1693861cf96 --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/callableReferenceWithMostSpecificGenericTypeParameter.kt @@ -0,0 +1,12 @@ +// FIR_IDENTICAL +// ISSUE: KT-51017 + +interface A +interface B : A + +fun V.foo(): V = this +fun T.foo(): T = this + +fun test(list: List) { + B::foo // No ambiguity, T.foo wins +} diff --git a/compiler/testData/diagnostics/tests/callableReference/callableReferenceWithMostSpecificGenericTypeParameter.txt b/compiler/testData/diagnostics/tests/callableReference/callableReferenceWithMostSpecificGenericTypeParameter.txt new file mode 100644 index 00000000000..655836cb7f8 --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/callableReferenceWithMostSpecificGenericTypeParameter.txt @@ -0,0 +1,17 @@ +package + +public fun test(/*0*/ list: kotlin.collections.List): kotlin.Unit +public fun T.foo(): T +public fun V.foo(): V + +public interface 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 interface B : 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 +} diff --git a/compiler/testData/diagnostics/tests/callableReference/generic/kt11075.fir.kt b/compiler/testData/diagnostics/tests/callableReference/generic/kt11075.fir.kt deleted file mode 100644 index 23bfd874a21..00000000000 --- a/compiler/testData/diagnostics/tests/callableReference/generic/kt11075.fir.kt +++ /dev/null @@ -1,10 +0,0 @@ -// KT-11075 NONE_APPLICABLE reported for callable reference to an overloaded generic function with expected type provided - -object TestCallableReferences { - fun foo(x: A) = x - fun foo(x: List) = x - - fun test0(): (String) -> String = TestCallableReferences::foo - - fun test1(): (List) -> List = TestCallableReferences::foo -} diff --git a/compiler/testData/diagnostics/tests/callableReference/generic/kt11075.kt b/compiler/testData/diagnostics/tests/callableReference/generic/kt11075.kt index 8648a002e0b..c28e4644a2f 100644 --- a/compiler/testData/diagnostics/tests/callableReference/generic/kt11075.kt +++ b/compiler/testData/diagnostics/tests/callableReference/generic/kt11075.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // KT-11075 NONE_APPLICABLE reported for callable reference to an overloaded generic function with expected type provided object TestCallableReferences { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index 3530ae808f6..56586a3dc84 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -2652,6 +2652,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/callableReference/callableReferenceAssignmentToVar.kt"); } + @Test + @TestMetadata("callableReferenceWithMostSpecificGenericTypeParameter.kt") + public void testCallableReferenceWithMostSpecificGenericTypeParameter() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/callableReferenceWithMostSpecificGenericTypeParameter.kt"); + } + @Test @TestMetadata("classVsPackage.kt") public void testClassVsPackage() throws Exception {