From a8b9e8c44e2e5c0fc16e92a220cbafee61d9ecf2 Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Tue, 28 Mar 2023 14:05:14 +0200 Subject: [PATCH] K2: Fix false-positive TYPE_MISMATCH for suspend lambdas Why did the problem existed? At first, lambdas were analyzed with suspend function expected type, because it's the WHEN-case and we propagate expected type info to the branches. Then, after the lambdas was introduced to the containing inference system, we're creating ResolvedLambdaAtom using the information from analyzed lambda's shape, but didn't use known lambda resulting type (from which we might infer FunctionTypeKind). So, the fix is just using that already obtained information. ^KT-57446 Fixed --- ...osticCompilerTestFE10TestdataTestGenerated.java | 6 ++++++ ...iagnosticCompilerFE10TestDataTestGenerated.java | 6 ++++++ ...ghtTreeOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ .../FirPsiOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ .../fir/resolve/inference/PostponedArguments.kt | 2 +- .../suspendFunctionExpectedTypeAndWhen.kt | 14 ++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 6 ++++++ 7 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/tests/suspendConversion/suspendFunctionExpectedTypeAndWhen.kt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java index 3e720a6b3f5..ef7670e7a6b 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java @@ -31927,6 +31927,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia public void testSuspendConversionWithReferenceAdaptation() throws Exception { runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendConversionWithReferenceAdaptation.kt"); } + + @Test + @TestMetadata("suspendFunctionExpectedTypeAndWhen.kt") + public void testSuspendFunctionExpectedTypeAndWhen() throws Exception { + runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendFunctionExpectedTypeAndWhen.kt"); + } } @Nested diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java index 3c7a433aa18..d2ad5f4cfe0 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java @@ -31927,6 +31927,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated public void testSuspendConversionWithReferenceAdaptation() throws Exception { runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendConversionWithReferenceAdaptation.kt"); } + + @Test + @TestMetadata("suspendFunctionExpectedTypeAndWhen.kt") + public void testSuspendFunctionExpectedTypeAndWhen() throws Exception { + runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendFunctionExpectedTypeAndWhen.kt"); + } } @Nested diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java index c7378628e93..1c8fe9e1543 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java @@ -31927,6 +31927,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir public void testSuspendConversionWithReferenceAdaptation() throws Exception { runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendConversionWithReferenceAdaptation.kt"); } + + @Test + @TestMetadata("suspendFunctionExpectedTypeAndWhen.kt") + public void testSuspendFunctionExpectedTypeAndWhen() throws Exception { + runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendFunctionExpectedTypeAndWhen.kt"); + } } @Nested diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java index 6078b46b511..af2510ca27b 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java @@ -32023,6 +32023,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia public void testSuspendConversionWithReferenceAdaptation() throws Exception { runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendConversionWithReferenceAdaptation.kt"); } + + @Test + @TestMetadata("suspendFunctionExpectedTypeAndWhen.kt") + public void testSuspendFunctionExpectedTypeAndWhen() throws Exception { + runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendFunctionExpectedTypeAndWhen.kt"); + } } @Nested diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArguments.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArguments.kt index cbb85f689ab..7d508a34a0c 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArguments.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArguments.kt @@ -134,7 +134,7 @@ private fun extractLambdaInfo( return ResolvedLambdaAtom( argument, expectedType, - expectedFunctionTypeKind = null, + expectedFunctionTypeKind = argument.typeRef.coneTypeSafe()?.lowerBoundIfFlexible()?.functionTypeKind(session), receiverType, contextReceivers, parameters, diff --git a/compiler/testData/diagnostics/tests/suspendConversion/suspendFunctionExpectedTypeAndWhen.kt b/compiler/testData/diagnostics/tests/suspendConversion/suspendFunctionExpectedTypeAndWhen.kt new file mode 100644 index 00000000000..467440fe605 --- /dev/null +++ b/compiler/testData/diagnostics/tests/suspendConversion/suspendFunctionExpectedTypeAndWhen.kt @@ -0,0 +1,14 @@ +// FIR_IDENTICAL +// ISSUE: KT-57446 + +fun test1() : suspend (Int) -> Unit = when { + true -> { _ -> } + else -> { _ -> } +} + +fun test2() : suspend (Int) -> Unit = when { + true -> { x -> foo(x) } + else -> { y -> foo(y) } +} + +suspend fun foo(x: Int) {} 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 a4555d1ac09..dd90f1ee713 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 @@ -32771,6 +32771,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { public void testSuspendConversionWithReferenceAdaptation() throws Exception { runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendConversionWithReferenceAdaptation.kt"); } + + @Test + @TestMetadata("suspendFunctionExpectedTypeAndWhen.kt") + public void testSuspendFunctionExpectedTypeAndWhen() throws Exception { + runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendFunctionExpectedTypeAndWhen.kt"); + } } @Nested