From 30871fc08f8a2602ee4043e577da4eff1f7c26a2 Mon Sep 17 00:00:00 2001 From: Ivan Kochurkin Date: Fri, 28 Jan 2022 22:56:38 +0300 Subject: [PATCH] [FIR] Infer labeled type for lambda in return in catch ^KT-51009 Fixed --- ...CompilerTestFE10TestdataTestGenerated.java | 6 ++++++ ...irOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ ...DiagnosticsWithLightTreeTestGenerated.java | 6 ++++++ ...rCallCompletionResultsWriterTransformer.kt | 3 ++- ...eledElementTypeForLambdaInReturnInCatch.kt | 20 +++++++++++++++++++ ...ledElementTypeForLambdaInReturnInCatch.txt | 5 +++++ .../test/runners/DiagnosticTestGenerated.java | 6 ++++++ 7 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/tests/inference/completion/inferLabeledElementTypeForLambdaInReturnInCatch.kt create mode 100644 compiler/testData/diagnostics/tests/inference/completion/inferLabeledElementTypeForLambdaInReturnInCatch.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 5bf92f55d36..3f8717dd6c3 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 @@ -14809,6 +14809,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/inference/completion/flexibleType.kt"); } + @Test + @TestMetadata("inferLabeledElementTypeForLambdaInReturnInCatch.kt") + public void testInferLabeledElementTypeForLambdaInReturnInCatch() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/completion/inferLabeledElementTypeForLambdaInReturnInCatch.kt"); + } + @Test @TestMetadata("intersectionType.kt") public void testIntersectionType() 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 c21f3c21aa6..6bab06d6a59 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 @@ -14809,6 +14809,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/inference/completion/flexibleType.kt"); } + @Test + @TestMetadata("inferLabeledElementTypeForLambdaInReturnInCatch.kt") + public void testInferLabeledElementTypeForLambdaInReturnInCatch() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/completion/inferLabeledElementTypeForLambdaInReturnInCatch.kt"); + } + @Test @TestMetadata("intersectionType.kt") public void testIntersectionType() 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 440e9ab1c37..2b0acc4c929 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 @@ -14809,6 +14809,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/inference/completion/flexibleType.kt"); } + @Test + @TestMetadata("inferLabeledElementTypeForLambdaInReturnInCatch.kt") + public void testInferLabeledElementTypeForLambdaInReturnInCatch() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/completion/inferLabeledElementTypeForLambdaInReturnInCatch.kt"); + } + @Test @TestMetadata("intersectionType.kt") public void testIntersectionType() throws Exception { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt index 8928cf2c19c..f97a30ae7ee 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt @@ -702,7 +702,8 @@ class FirCallCompletionResultsWriterTransformer( return returnExpression } - return super.transformReturnExpression(returnExpression, data) + val newData = labeledElement.returnTypeRef.coneTypeSafe()?.toExpectedType() + return super.transformReturnExpression(returnExpression, newData) } override fun transformBlock(block: FirBlock, data: ExpectedArgumentType?): FirStatement { diff --git a/compiler/testData/diagnostics/tests/inference/completion/inferLabeledElementTypeForLambdaInReturnInCatch.kt b/compiler/testData/diagnostics/tests/inference/completion/inferLabeledElementTypeForLambdaInReturnInCatch.kt new file mode 100644 index 00000000000..880e2206f8c --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/completion/inferLabeledElementTypeForLambdaInReturnInCatch.kt @@ -0,0 +1,20 @@ +// FIR_IDENTICAL +// ISSUE: KT-51009 + +fun test(b: Boolean, f: () -> String?): () -> String { + val foo = try { + f + } catch (e: Exception) { + return { "1" } // Infer return type of test () -> String instead of type of `f` + } + return { "2" } +} + +fun test2(b: Boolean, f: () -> String?) = run { // implicit return type + val foo = try { + f + } catch (e: Exception) { + return { "1" } + } + { "2" } +} diff --git a/compiler/testData/diagnostics/tests/inference/completion/inferLabeledElementTypeForLambdaInReturnInCatch.txt b/compiler/testData/diagnostics/tests/inference/completion/inferLabeledElementTypeForLambdaInReturnInCatch.txt new file mode 100644 index 00000000000..6d4d37fa727 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/completion/inferLabeledElementTypeForLambdaInReturnInCatch.txt @@ -0,0 +1,5 @@ +package + +public fun test(/*0*/ b: kotlin.Boolean, /*1*/ f: () -> kotlin.String?): () -> kotlin.String +public fun test2(/*0*/ b: kotlin.Boolean, /*1*/ f: () -> kotlin.String?): () -> 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 56586a3dc84..00b9d718c27 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 @@ -14815,6 +14815,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/inference/completion/flexibleType.kt"); } + @Test + @TestMetadata("inferLabeledElementTypeForLambdaInReturnInCatch.kt") + public void testInferLabeledElementTypeForLambdaInReturnInCatch() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/completion/inferLabeledElementTypeForLambdaInReturnInCatch.kt"); + } + @Test @TestMetadata("intersectionType.kt") public void testIntersectionType() throws Exception {