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
This commit is contained in:
Denis.Zharkov
2023-03-28 14:05:14 +02:00
committed by Space Team
parent 0056ac5f5a
commit a8b9e8c44e
7 changed files with 45 additions and 1 deletions
@@ -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
@@ -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
@@ -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
@@ -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
@@ -134,7 +134,7 @@ private fun extractLambdaInfo(
return ResolvedLambdaAtom(
argument,
expectedType,
expectedFunctionTypeKind = null,
expectedFunctionTypeKind = argument.typeRef.coneTypeSafe<ConeKotlinType>()?.lowerBoundIfFlexible()?.functionTypeKind(session),
receiverType,
contextReceivers,
parameters,
@@ -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) {}
@@ -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