From c1ec1d9d4c7719bf671e72b67b714f0b206aded9 Mon Sep 17 00:00:00 2001 From: Pavel Kunyavskiy Date: Tue, 28 Jun 2022 15:25:34 +0200 Subject: [PATCH] [K/N] Handle Unit? and Nothing? correctly in finally transformation ^KT-52985 --- .../FirBlackBoxCodegenTestGenerated.java | 6 ++++ .../common/lower/FinallyBlocksLowering.kt | 5 ++- .../box/finally/returnNullFromInlined.kt | 32 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 ++++ .../IrBlackBoxCodegenTestGenerated.java | 6 ++++ .../LightAnalysisModeTestGenerated.java | 5 +++ .../js/test/JsCodegenBoxTestGenerated.java | 6 ++++ .../test/ir/IrJsCodegenBoxTestGenerated.java | 6 ++++ .../IrCodegenBoxWasmTestGenerated.java | 5 +++ .../NativeCodegenBoxTestGenerated.java | 6 ++++ 10 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/codegen/box/finally/returnNullFromInlined.kt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 7c0ff190d1d..f80e4304126 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -17669,6 +17669,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/finally/objectInFinally.kt"); } + @Test + @TestMetadata("returnNullFromInlined.kt") + public void testReturnNullFromInlined() throws Exception { + runTest("compiler/testData/codegen/box/finally/returnNullFromInlined.kt"); + } + @Test @TestMetadata("someStuff.kt") public void testSomeStuff() throws Exception { diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/FinallyBlocksLowering.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/FinallyBlocksLowering.kt index f08ae452a49..a618d81b91b 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/FinallyBlocksLowering.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/FinallyBlocksLowering.kt @@ -267,9 +267,8 @@ class FinallyBlocksLowering(val context: CommonBackendContext, private val throw value: IrExpression, finallyExpression: IrExpression ): IrExpression { - val returnTypeClassifier = (type as? IrSimpleType)?.classifier - return when (returnTypeClassifier) { - context.irBuiltIns.unitClass, context.irBuiltIns.nothingClass -> irBlock(value, null, type) { + return when { + type.isUnit() || type.isNothing() -> irBlock(value, null, type) { +irReturnableBlock(symbol, type) { +value } diff --git a/compiler/testData/codegen/box/finally/returnNullFromInlined.kt b/compiler/testData/codegen/box/finally/returnNullFromInlined.kt new file mode 100644 index 00000000000..a2987f9f2f4 --- /dev/null +++ b/compiler/testData/codegen/box/finally/returnNullFromInlined.kt @@ -0,0 +1,32 @@ +inline fun withCatch(block: () -> T?) : T? { + try { + return block() + } catch (e: NullPointerException) { + return null + } finally { + } +} +fun f1() = withCatch { null } +fun f2() = withCatch { null } +fun f3() = withCatch { null } + +inline fun withOutCatch(block: () -> T?) : T? { + try { + return block() + } finally { + } +} +fun f4() = withOutCatch { null } +fun f5() = withOutCatch { null } +fun f6() = withOutCatch { null } + + +fun box() : String { + if (f1() != null) return "FAIL1" + if (f2() != null) return "FAIL2" + if (f3() != null) return "FAIL3" + if (f4() != null) return "FAIL4" + if (f5() != null) return "FAIL5" + if (f6() != null) return "FAIL6" + return "OK" +} \ No newline at end of file diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index c173dc0c0d8..014ef4491ea 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -17213,6 +17213,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/finally/objectInFinally.kt"); } + @Test + @TestMetadata("returnNullFromInlined.kt") + public void testReturnNullFromInlined() throws Exception { + runTest("compiler/testData/codegen/box/finally/returnNullFromInlined.kt"); + } + @Test @TestMetadata("someStuff.kt") public void testSomeStuff() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index d84b05dfb1e..1fe681136c6 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -17669,6 +17669,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/finally/objectInFinally.kt"); } + @Test + @TestMetadata("returnNullFromInlined.kt") + public void testReturnNullFromInlined() throws Exception { + runTest("compiler/testData/codegen/box/finally/returnNullFromInlined.kt"); + } + @Test @TestMetadata("someStuff.kt") public void testSomeStuff() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 3641bc4ed84..9672de2b92f 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -14257,6 +14257,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/finally/objectInFinally.kt"); } + @TestMetadata("returnNullFromInlined.kt") + public void testReturnNullFromInlined() throws Exception { + runTest("compiler/testData/codegen/box/finally/returnNullFromInlined.kt"); + } + @TestMetadata("someStuff.kt") public void testSomeStuff() throws Exception { runTest("compiler/testData/codegen/box/finally/someStuff.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java index a2efb7e4a18..963dd49ee90 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java @@ -13339,6 +13339,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/finally/objectInFinally.kt"); } + @Test + @TestMetadata("returnNullFromInlined.kt") + public void testReturnNullFromInlined() throws Exception { + runTest("compiler/testData/codegen/box/finally/returnNullFromInlined.kt"); + } + @Test @TestMetadata("someStuff.kt") public void testSomeStuff() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java index 7420bfc0110..465b7603890 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java @@ -13381,6 +13381,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/finally/objectInFinally.kt"); } + @Test + @TestMetadata("returnNullFromInlined.kt") + public void testReturnNullFromInlined() throws Exception { + runTest("compiler/testData/codegen/box/finally/returnNullFromInlined.kt"); + } + @Test @TestMetadata("someStuff.kt") public void testSomeStuff() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java index 11b04bdd171..3d9bb07775a 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java @@ -11887,6 +11887,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/finally/objectInFinally.kt"); } + @TestMetadata("returnNullFromInlined.kt") + public void testReturnNullFromInlined() throws Exception { + runTest("compiler/testData/codegen/box/finally/returnNullFromInlined.kt"); + } + @TestMetadata("someStuff.kt") public void testSomeStuff() throws Exception { runTest("compiler/testData/codegen/box/finally/someStuff.kt"); diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java index d3c4f49bd58..0eabd570925 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java @@ -14413,6 +14413,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest runTest("compiler/testData/codegen/box/finally/objectInFinally.kt"); } + @Test + @TestMetadata("returnNullFromInlined.kt") + public void testReturnNullFromInlined() throws Exception { + runTest("compiler/testData/codegen/box/finally/returnNullFromInlined.kt"); + } + @Test @TestMetadata("someStuff.kt") public void testSomeStuff() throws Exception {