From 9851ff1905ae167c81714ed62a6520809c218009 Mon Sep 17 00:00:00 2001 From: Sergej Jaskiewicz Date: Thu, 14 Mar 2024 16:39:23 +0100 Subject: [PATCH] [FIR] Reproduce KT-66277, KT-66279, KT-66512 and KT-66534 --- ...CompilerTestFE10TestdataTestGenerated.java | 24 ++++ ...sticCompilerFE10TestDataTestGenerated.java | 24 ++++ ...eeOldFrontendDiagnosticsTestGenerated.java | 24 ++++ ...siOldFrontendDiagnosticsTestGenerated.java | 24 ++++ .../functionLiterals/return/kt66277.fir.kt | 93 +++++++++++++ .../tests/functionLiterals/return/kt66277.kt | 93 +++++++++++++ .../functionLiterals/return/kt66279.fir.kt | 114 ++++++++++++++++ .../tests/functionLiterals/return/kt66279.kt | 114 ++++++++++++++++ .../functionLiterals/return/kt66512.fir.kt | 20 +++ .../tests/functionLiterals/return/kt66512.kt | 20 +++ .../functionLiterals/return/kt66534.fir.kt | 125 ++++++++++++++++++ .../tests/functionLiterals/return/kt66534.kt | 125 ++++++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 24 ++++ 13 files changed, 824 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/functionLiterals/return/kt66277.fir.kt create mode 100644 compiler/testData/diagnostics/tests/functionLiterals/return/kt66277.kt create mode 100644 compiler/testData/diagnostics/tests/functionLiterals/return/kt66279.fir.kt create mode 100644 compiler/testData/diagnostics/tests/functionLiterals/return/kt66279.kt create mode 100644 compiler/testData/diagnostics/tests/functionLiterals/return/kt66512.fir.kt create mode 100644 compiler/testData/diagnostics/tests/functionLiterals/return/kt66512.kt create mode 100644 compiler/testData/diagnostics/tests/functionLiterals/return/kt66534.fir.kt create mode 100644 compiler/testData/diagnostics/tests/functionLiterals/return/kt66534.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 a3b8df9bee0..0377361eb47 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 @@ -14199,6 +14199,30 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia runTest("compiler/testData/diagnostics/tests/functionLiterals/return/IfWithoutElseWithExplicitType.kt"); } + @Test + @TestMetadata("kt66277.kt") + public void testKt66277() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66277.kt"); + } + + @Test + @TestMetadata("kt66279.kt") + public void testKt66279() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66279.kt"); + } + + @Test + @TestMetadata("kt66512.kt") + public void testKt66512() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66512.kt"); + } + + @Test + @TestMetadata("kt66534.kt") + public void testKt66534() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66534.kt"); + } + @Test @TestMetadata("LambdaReturnTypeIsUnitIfImplicitReturnExists.kt") public void testLambdaReturnTypeIsUnitIfImplicitReturnExists() { 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 e8afba3848b..cdf6201028a 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 @@ -14199,6 +14199,30 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated runTest("compiler/testData/diagnostics/tests/functionLiterals/return/IfWithoutElseWithExplicitType.kt"); } + @Test + @TestMetadata("kt66277.kt") + public void testKt66277() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66277.kt"); + } + + @Test + @TestMetadata("kt66279.kt") + public void testKt66279() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66279.kt"); + } + + @Test + @TestMetadata("kt66512.kt") + public void testKt66512() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66512.kt"); + } + + @Test + @TestMetadata("kt66534.kt") + public void testKt66534() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66534.kt"); + } + @Test @TestMetadata("LambdaReturnTypeIsUnitIfImplicitReturnExists.kt") public void testLambdaReturnTypeIsUnitIfImplicitReturnExists() { 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 1f0c194bba4..d9ab9ccf5d2 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 @@ -14193,6 +14193,30 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir runTest("compiler/testData/diagnostics/tests/functionLiterals/return/IfWithoutElseWithExplicitType.kt"); } + @Test + @TestMetadata("kt66277.kt") + public void testKt66277() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66277.kt"); + } + + @Test + @TestMetadata("kt66279.kt") + public void testKt66279() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66279.kt"); + } + + @Test + @TestMetadata("kt66512.kt") + public void testKt66512() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66512.kt"); + } + + @Test + @TestMetadata("kt66534.kt") + public void testKt66534() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66534.kt"); + } + @Test @TestMetadata("LambdaReturnTypeIsUnitIfImplicitReturnExists.kt") public void testLambdaReturnTypeIsUnitIfImplicitReturnExists() { 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 7f5588c8cab..42c3c675e14 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 @@ -14199,6 +14199,30 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia runTest("compiler/testData/diagnostics/tests/functionLiterals/return/IfWithoutElseWithExplicitType.kt"); } + @Test + @TestMetadata("kt66277.kt") + public void testKt66277() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66277.kt"); + } + + @Test + @TestMetadata("kt66279.kt") + public void testKt66279() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66279.kt"); + } + + @Test + @TestMetadata("kt66512.kt") + public void testKt66512() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66512.kt"); + } + + @Test + @TestMetadata("kt66534.kt") + public void testKt66534() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66534.kt"); + } + @Test @TestMetadata("LambdaReturnTypeIsUnitIfImplicitReturnExists.kt") public void testLambdaReturnTypeIsUnitIfImplicitReturnExists() { diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/kt66277.fir.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/kt66277.fir.kt new file mode 100644 index 00000000000..ec2e8a86332 --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/kt66277.fir.kt @@ -0,0 +1,93 @@ +// ISSUE: KT-66277 + +fun foo() {} +typealias MyUnit = Unit + +// Note that resolution works differently for lambdas passed as function arguments and lambdas assigned to variables, +// thus we need to test both cases. + +// ================= Lambdas assigned to a variable ================= + +val expectedAnyEmptyReturnAndString: () -> Any = l@ { + if ("0".hashCode() == 42) return@l + "" +} + +val expectedAnyExplicitReturnUnitAndString: () -> Any = l@ { + if ("0".hashCode() == 42) return@l Unit + "" +} + +val expectedAnyExplicitReturnMyUnitAndString: () -> Any = l@ { + if ("0".hashCode() == 42) return@l MyUnit + "" +} + +val expectedAnyExplicitReturnFooAndString: () -> Any = l@ { + if ("0".hashCode() == 42) return@l foo() + "" +} + +val expectedAnyEmptyReturnOnly: () -> Any = l@ { + if ("0".hashCode() == 42) return@l + return@l +} + +val expectedAnyImplicitAndExplicitReturnUnit: () -> Any = l@ { + if ("0".hashCode() == 42) return@l + return@l Unit +} + +val expectedAnyEmptyReturnAndExplicitReturnMyUnit: () -> Any = l@ { + if ("0".hashCode() == 42) return@l + return@l MyUnit +} + +val expectedAnyEmptyReturnAndExplicitReturnFoo: () -> Any = l@ { + if ("0".hashCode() == 42) return@l + return@l foo() +} + +// ============== Lambdas passed as function argument =============== + +fun test() { + run l@ { + if ("0".hashCode() == 42) return@l + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l Unit + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l MyUnit + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l foo() + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l Unit + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l MyUnit + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l foo() + } +} diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/kt66277.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/kt66277.kt new file mode 100644 index 00000000000..d3be1cefc6d --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/kt66277.kt @@ -0,0 +1,93 @@ +// ISSUE: KT-66277 + +fun foo() {} +typealias MyUnit = Unit + +// Note that resolution works differently for lambdas passed as function arguments and lambdas assigned to variables, +// thus we need to test both cases. + +// ================= Lambdas assigned to a variable ================= + +val expectedAnyEmptyReturnAndString: () -> Any = l@ { + if ("0".hashCode() == 42) return@l + "" +} + +val expectedAnyExplicitReturnUnitAndString: () -> Any = l@ { + if ("0".hashCode() == 42) return@l Unit + "" +} + +val expectedAnyExplicitReturnMyUnitAndString: () -> Any = l@ { + if ("0".hashCode() == 42) return@l MyUnit + "" +} + +val expectedAnyExplicitReturnFooAndString: () -> Any = l@ { + if ("0".hashCode() == 42) return@l foo() + "" +} + +val expectedAnyEmptyReturnOnly: () -> Any = l@ { + if ("0".hashCode() == 42) return@l + return@l +} + +val expectedAnyImplicitAndExplicitReturnUnit: () -> Any = l@ { + if ("0".hashCode() == 42) return@l + return@l Unit +} + +val expectedAnyEmptyReturnAndExplicitReturnMyUnit: () -> Any = l@ { + if ("0".hashCode() == 42) return@l + return@l MyUnit +} + +val expectedAnyEmptyReturnAndExplicitReturnFoo: () -> Any = l@ { + if ("0".hashCode() == 42) return@l + return@l foo() +} + +// ============== Lambdas passed as function argument =============== + +fun test() { + run l@ { + if ("0".hashCode() == 42) return@l + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l Unit + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l MyUnit + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l foo() + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l Unit + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l MyUnit + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l foo() + } +} diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/kt66279.fir.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/kt66279.fir.kt new file mode 100644 index 00000000000..6ca002b9e2b --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/kt66279.fir.kt @@ -0,0 +1,114 @@ +// ISSUE: KT-66279 + +fun foo() {} + +typealias MyUnit = Unit + +// Note that resolution works differently for lambdas passed as function arguments and lambdas assigned to variables, +// thus we need to test both cases. + +// ================= Lambdas assigned to a variable ================= + +val expectedUnitEmptyReturnUnitAndString: () -> Unit = l@ { + if ("0".hashCode() == 42) return@l + "" +} + +val expectedMyUnitEmptyReturnUnitAndString: () -> Unit = l@ { + if ("0".hashCode() == 42) return@l + "" +} + +val expectedUnitExplicitReturnUnitAndString: () -> Unit = l@ { + if ("0".hashCode() == 42) return@l Unit + "" +} + +val expectedUnitExplicitReturnMyUniAndString: () -> Unit = l@ { + if ("0".hashCode() == 42) return@l MyUnit + "" +} + +val expectedUnitExplicitReturnFooAndString: () -> Unit = l@ { + if ("0".hashCode() == 42) return@l foo() + "" +} + +val expectedUnitEmptyReturnOnly: () -> Unit = l@ { + if ("0".hashCode() == 42) return@l + return@l +} + +val expectedUnitEmptyReturnAndExplicitReturnUnit: () -> Unit = l@ { + if ("0".hashCode() == 42) return@l + return@l Unit +} + +val expectedUnitEmptyReturnAndExplicitReturnFoo: () -> Unit = l@ { + if ("0".hashCode() == 42) return@l + return@l foo() +} + +val expectedMyUnitEmptyReturnOnly: () -> MyUnit = l@ { + if ("0".hashCode() == 42) return@l + return@l +} + +val expectedMyUnitEmptyReturnAndExplicitReturnMyUnit: () -> MyUnit = l@ { + if ("0".hashCode() == 42) return@l + return@l Unit +} + +// ============== Lambdas passed as function argument =============== + +fun test() { + run l@ { + if ("0".hashCode() == 42) return@l + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l Unit + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l MyUnit + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l foo() + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l Unit + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l foo() + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l Unit + } +} diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/kt66279.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/kt66279.kt new file mode 100644 index 00000000000..882925cfeec --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/kt66279.kt @@ -0,0 +1,114 @@ +// ISSUE: KT-66279 + +fun foo() {} + +typealias MyUnit = Unit + +// Note that resolution works differently for lambdas passed as function arguments and lambdas assigned to variables, +// thus we need to test both cases. + +// ================= Lambdas assigned to a variable ================= + +val expectedUnitEmptyReturnUnitAndString: () -> Unit = l@ { + if ("0".hashCode() == 42) return@l + "" +} + +val expectedMyUnitEmptyReturnUnitAndString: () -> Unit = l@ { + if ("0".hashCode() == 42) return@l + "" +} + +val expectedUnitExplicitReturnUnitAndString: () -> Unit = l@ { + if ("0".hashCode() == 42) return@l Unit + "" +} + +val expectedUnitExplicitReturnMyUniAndString: () -> Unit = l@ { + if ("0".hashCode() == 42) return@l MyUnit + "" +} + +val expectedUnitExplicitReturnFooAndString: () -> Unit = l@ { + if ("0".hashCode() == 42) return@l foo() + "" +} + +val expectedUnitEmptyReturnOnly: () -> Unit = l@ { + if ("0".hashCode() == 42) return@l + return@l +} + +val expectedUnitEmptyReturnAndExplicitReturnUnit: () -> Unit = l@ { + if ("0".hashCode() == 42) return@l + return@l Unit +} + +val expectedUnitEmptyReturnAndExplicitReturnFoo: () -> Unit = l@ { + if ("0".hashCode() == 42) return@l + return@l foo() +} + +val expectedMyUnitEmptyReturnOnly: () -> MyUnit = l@ { + if ("0".hashCode() == 42) return@l + return@l +} + +val expectedMyUnitEmptyReturnAndExplicitReturnMyUnit: () -> MyUnit = l@ { + if ("0".hashCode() == 42) return@l + return@l Unit +} + +// ============== Lambdas passed as function argument =============== + +fun test() { + run l@ { + if ("0".hashCode() == 42) return@l + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l Unit + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l MyUnit + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l foo() + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l Unit + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l foo() + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l Unit + } +} diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/kt66512.fir.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/kt66512.fir.kt new file mode 100644 index 00000000000..808fd85d517 --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/kt66512.fir.kt @@ -0,0 +1,20 @@ +// ISSUE: KT-66512 + +typealias MyUnit = Unit + +// Note that resolution works differently for lambdas passed as function arguments and lambdas assigned to variables, +// thus we need to test both cases. + +// ================= Lambdas assigned to a variable ================= + +val expectedMyUnitExplicitReturnString: () -> MyUnit = l@ { + return@l "" +} + +// ============== Lambdas passed as function argument =============== + +fun test() { + run l@ { + return@l "" + } +} diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/kt66512.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/kt66512.kt new file mode 100644 index 00000000000..efb83f99aa1 --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/kt66512.kt @@ -0,0 +1,20 @@ +// ISSUE: KT-66512 + +typealias MyUnit = Unit + +// Note that resolution works differently for lambdas passed as function arguments and lambdas assigned to variables, +// thus we need to test both cases. + +// ================= Lambdas assigned to a variable ================= + +val expectedMyUnitExplicitReturnString: () -> MyUnit = l@ { + return@l "" +} + +// ============== Lambdas passed as function argument =============== + +fun test() { + run l@ { + return@l "" + } +} diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/kt66534.fir.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/kt66534.fir.kt new file mode 100644 index 00000000000..319567557b4 --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/kt66534.fir.kt @@ -0,0 +1,125 @@ +// ISSUE: KT-66534 +// WITH_STDLIB + +// FILE: A.java +import org.jetbrains.annotations.NotNull; +import kotlin.jvm.functions.Function0; +import kotlin.Unit; + +public class A { + public static @NotNull Function0 foo; + public static void run(@NotNull Function0 foo) {} +} + +// FILE: main.kt + +// Note that resolution works differently for lambdas passed as function arguments and lambdas assigned to variables, +// thus we need to test both cases. + +// ================= Lambdas assigned to a variable ================= + +val expectedNullableUnitEmptyReturnAndString: () -> Unit? = l@ { + if ("0".hashCode() == 42) return@l + "" +} + +fun expectedFlexibleUnitEmptyReturnAndString() { + A.foo = l@ { + if ("0".hashCode() == 42) return@l + "" + } +} + +val expectedNullableUnitEmptyReturnAndExplicitReturnNull: () -> Unit? = l@ { + if ("0".hashCode() == 42) return@l + return@l null +} + +fun expectedFlexibleUnitEmptyReturnAndExplicitReturnNull() { + A.foo = l@ { + if ("0".hashCode() == 42) return@l + return@l null + } +} + +val expectedNullableUnitExplicitReturnUnitAndString: () -> Unit? = l@ { + if ("0".hashCode() == 42) return@l Unit + "" +} + +fun expectedFlexibleUnitExplicitReturnUnitAndString() { + A.foo = l@ { + if ("0".hashCode() == 42) return@l Unit + "" + } +} + +val expectedNullableUnitExplicitReturnString: () -> Unit? = l@ { + return@l "" +} + +fun expectedFlexibleUnitExplicitReturnString() { + A.foo = l@ { + return@l "" + } +} + +val expectedNullableUnitExplicitReturnNull: () -> Unit? = l@ { + return@l null +} + +fun expectedFlexibleUnitExplicitReturnNull() { + A.foo = l@ { + return@l null + } +} + +// ============== Lambdas passed as function argument =============== + +fun test() { + run l@ { + if ("0".hashCode() == 42) return@l + "" + } + + A.run l@ { + if ("0".hashCode() == 42) return@l + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l null + } + + A.run l@ { + if ("0".hashCode() == 42) return@l + return@l null + } + + run l@ { + if ("0".hashCode() == 42) return@l Unit + "" + } + + A.run l@ { + if ("0".hashCode() == 42) return@l Unit + "" + } + + run l@ { + return@l "" + } + + A.run l@ { + return@l "" + } + + run l@ { + return@l null + } + + A.run l@ { + return@l null + } +} diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/kt66534.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/kt66534.kt new file mode 100644 index 00000000000..2ab352542c7 --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/kt66534.kt @@ -0,0 +1,125 @@ +// ISSUE: KT-66534 +// WITH_STDLIB + +// FILE: A.java +import org.jetbrains.annotations.NotNull; +import kotlin.jvm.functions.Function0; +import kotlin.Unit; + +public class A { + public static @NotNull Function0 foo; + public static void run(@NotNull Function0 foo) {} +} + +// FILE: main.kt + +// Note that resolution works differently for lambdas passed as function arguments and lambdas assigned to variables, +// thus we need to test both cases. + +// ================= Lambdas assigned to a variable ================= + +val expectedNullableUnitEmptyReturnAndString: () -> Unit? = l@ { + if ("0".hashCode() == 42) return@l + "" +} + +fun expectedFlexibleUnitEmptyReturnAndString() { + A.foo = l@ { + if ("0".hashCode() == 42) return@l + "" + } +} + +val expectedNullableUnitEmptyReturnAndExplicitReturnNull: () -> Unit? = l@ { + if ("0".hashCode() == 42) return@l + return@l null +} + +fun expectedFlexibleUnitEmptyReturnAndExplicitReturnNull() { + A.foo = l@ { + if ("0".hashCode() == 42) return@l + return@l null + } +} + +val expectedNullableUnitExplicitReturnUnitAndString: () -> Unit? = l@ { + if ("0".hashCode() == 42) return@l Unit + "" +} + +fun expectedFlexibleUnitExplicitReturnUnitAndString() { + A.foo = l@ { + if ("0".hashCode() == 42) return@l Unit + "" + } +} + +val expectedNullableUnitExplicitReturnString: () -> Unit? = l@ { + return@l "" +} + +fun expectedFlexibleUnitExplicitReturnString() { + A.foo = l@ { + return@l "" + } +} + +val expectedNullableUnitExplicitReturnNull: () -> Unit? = l@ { + return@l null +} + +fun expectedFlexibleUnitExplicitReturnNull() { + A.foo = l@ { + return@l null + } +} + +// ============== Lambdas passed as function argument =============== + +fun test() { + run l@ { + if ("0".hashCode() == 42) return@l + "" + } + + A.run l@ { + if ("0".hashCode() == 42) return@l + "" + } + + run l@ { + if ("0".hashCode() == 42) return@l + return@l null + } + + A.run l@ { + if ("0".hashCode() == 42) return@l + return@l null + } + + run l@ { + if ("0".hashCode() == 42) return@l Unit + "" + } + + A.run l@ { + if ("0".hashCode() == 42) return@l Unit + "" + } + + run l@ { + return@l "" + } + + A.run l@ { + return@l "" + } + + run l@ { + return@l null + } + + A.run l@ { + return@l null + } +} 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 360998de346..3f3a18b36d0 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 @@ -14199,6 +14199,30 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/functionLiterals/return/IfWithoutElseWithExplicitType.kt"); } + @Test + @TestMetadata("kt66277.kt") + public void testKt66277() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66277.kt"); + } + + @Test + @TestMetadata("kt66279.kt") + public void testKt66279() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66279.kt"); + } + + @Test + @TestMetadata("kt66512.kt") + public void testKt66512() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66512.kt"); + } + + @Test + @TestMetadata("kt66534.kt") + public void testKt66534() { + runTest("compiler/testData/diagnostics/tests/functionLiterals/return/kt66534.kt"); + } + @Test @TestMetadata("LambdaReturnTypeIsUnitIfImplicitReturnExists.kt") public void testLambdaReturnTypeIsUnitIfImplicitReturnExists() {