From 541fba15315f715d6c355fd9e3ee30dd2d20cc43 Mon Sep 17 00:00:00 2001 From: Ivan Kochurkin Date: Wed, 6 Apr 2022 23:30:25 +0300 Subject: [PATCH] [FIR] Type argument instead of unexpected type parameter for TypeMismatch error ^KT-49035 ^KT-51201 Fixed --- ...CompilerTestFE10TestdataTestGenerated.java | 6 +++++ ...irOldFrontendDiagnosticsTestGenerated.java | 6 +++++ ...DiagnosticsWithLightTreeTestGenerated.java | 6 +++++ .../kotlin/fir/resolve/calls/Arguments.kt | 12 +++++++-- .../fir/resolve/calls/ResolutionStages.kt | 3 +-- ...cTypeArgumentInsteadOfTypeParameter.fir.kt | 17 ++++++++++++ ...nericTypeArgumentInsteadOfTypeParameter.kt | 17 ++++++++++++ ...ericTypeArgumentInsteadOfTypeParameter.txt | 27 +++++++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 6 +++++ 9 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.fir.kt create mode 100644 compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.kt create mode 100644 compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.txt 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 7c0fdd793ad..18a97200799 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 @@ -31508,6 +31508,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/typeParameters/starProjectionInsteadOutCaptured.kt"); } + @Test + @TestMetadata("typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.kt") + public void testTypeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.kt"); + } + @Test @TestMetadata("upperBoundCannotBeArray.kt") public void testUpperBoundCannotBeArray() 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 77c35e2c266..7f32ae1fdb8 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 @@ -31508,6 +31508,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/typeParameters/starProjectionInsteadOutCaptured.kt"); } + @Test + @TestMetadata("typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.kt") + public void testTypeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.kt"); + } + @Test @TestMetadata("upperBoundCannotBeArray.kt") public void testUpperBoundCannotBeArray() 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 51a73a5d311..59280f20b75 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 @@ -31508,6 +31508,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/typeParameters/starProjectionInsteadOutCaptured.kt"); } + @Test + @TestMetadata("typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.kt") + public void testTypeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.kt"); + } + @Test @TestMetadata("upperBoundCannotBeArray.kt") public void testUpperBoundCannotBeArray() throws Exception { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Arguments.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Arguments.kt index 65aefb0a4eb..b8fe97e4185 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Arguments.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Arguments.kt @@ -29,6 +29,7 @@ import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.StandardClassIds import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder import org.jetbrains.kotlin.resolve.calls.inference.addSubtypeConstraintIfCompatible +import org.jetbrains.kotlin.resolve.calls.inference.components.VariableFixationFinder import org.jetbrains.kotlin.resolve.calls.inference.model.SimpleConstraintSystemConstraintPosition import org.jetbrains.kotlin.types.AbstractTypeChecker import org.jetbrains.kotlin.types.model.CaptureStatus @@ -363,8 +364,16 @@ private fun checkApplicabilityForArgumentType( fun tryGetConeTypeThatCompatibleWithKtType(type: ConeKotlinType): ConeKotlinType { if (type is ConeTypeVariableType) { + val lookupTag = type.lookupTag + + val constraints = (csBuilder as VariableFixationFinder.Context).notFixedTypeVariables[lookupTag]?.constraints + val constraintTypes = constraints?.mapNotNull { it.type as? ConeKotlinType } + if (constraintTypes != null && constraintTypes.isNotEmpty()) { + return ConeTypeIntersector.intersectTypes(context.session.typeContext, constraintTypes) + } + val originalTypeParameter = - (type.lookupTag as? ConeTypeVariableTypeConstructor)?.originalTypeParameter as? ConeTypeParameterLookupTag + (lookupTag as? ConeTypeVariableTypeConstructor)?.originalTypeParameter as? ConeTypeParameterLookupTag if (originalTypeParameter != null) return ConeTypeParameterTypeImpl(originalTypeParameter, type.isNullable, type.attributes) @@ -375,7 +384,6 @@ private fun checkApplicabilityForArgumentType( return type } - return ArgumentTypeMismatch( tryGetConeTypeThatCompatibleWithKtType(actualExpectedType), tryGetConeTypeThatCompatibleWithKtType(argumentType), diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt index d6329b7b6fd..0e3c113bfcf 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt @@ -445,11 +445,10 @@ internal object CheckArguments : CheckerStage() { val argumentMapping = candidate.argumentMapping ?: error("Argument should be already mapped while checking arguments!") for (argument in callInfo.arguments) { - val parameter = argumentMapping[argument] candidate.resolveArgument( callInfo, argument, - parameter, + argumentMapping[argument], isReceiver = false, sink = sink, context = context diff --git a/compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.fir.kt b/compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.fir.kt new file mode 100644 index 00000000000..11876d7c4b6 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.fir.kt @@ -0,0 +1,17 @@ +// ISSUE: KT-49035, KT-51201 + +@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") + +fun foo(it: @kotlin.internal.Exact T) {} + +fun main() { + foo("") +} + +interface I +class Foo : I +class Bar + +fun myRun(action: () -> MY_TYPE_PARAM): MY_TYPE_PARAM = action() + +val a = myRun { Bar() } diff --git a/compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.kt b/compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.kt new file mode 100644 index 00000000000..5d95112044a --- /dev/null +++ b/compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.kt @@ -0,0 +1,17 @@ +// ISSUE: KT-49035, KT-51201 + +@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") + +fun foo(it: @kotlin.internal.Exact T) {} + +fun main() { + foo("") +} + +interface I +class Foo : I +class Bar + +fun myRun(action: () -> MY_TYPE_PARAM): MY_TYPE_PARAM = action() + +val a = myRun { Bar() } diff --git a/compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.txt b/compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.txt new file mode 100644 index 00000000000..10040f6c685 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.txt @@ -0,0 +1,27 @@ +package + +public val a: Foo +public fun foo(/*0*/ it: T): kotlin.Unit +public fun main(): kotlin.Unit +public fun myRun(/*0*/ action: () -> MY_TYPE_PARAM): MY_TYPE_PARAM + +public final class Bar { + public constructor Bar() + 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 Foo : I { + public constructor Foo() + 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 I { + 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-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 decbca339ee..3cc6e693919 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 @@ -31598,6 +31598,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/typeParameters/starProjectionInsteadOutCaptured.kt"); } + @Test + @TestMetadata("typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.kt") + public void testTypeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.kt"); + } + @Test @TestMetadata("upperBoundCannotBeArray.kt") public void testUpperBoundCannotBeArray() throws Exception {