From edf4e80165113963a4f701c1a5b543842e2bcd93 Mon Sep 17 00:00:00 2001 From: Igor Yakovlev Date: Tue, 11 Apr 2023 15:32:36 +0200 Subject: [PATCH] [Wasm] Fix for ReturnableBlockLowering invalid ir type for converted inlined blocks --- ...LightTreeBlackBoxCodegenTestGenerated.java | 6 +++++ .../FirPsiBlackBoxCodegenTestGenerated.java | 6 +++++ .../common/lower/ReturnableBlockLowering.kt | 19 +++++++++----- .../box/coroutines/inlineCallWithReturns.kt | 25 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 +++++ .../IrBlackBoxCodegenTestGenerated.java | 6 +++++ ...kBoxCodegenWithIrInlinerTestGenerated.java | 6 +++++ .../LightAnalysisModeTestGenerated.java | 5 ++++ .../js/test/JsCodegenBoxTestGenerated.java | 6 +++++ .../fir/FirJsCodegenBoxTestGenerated.java | 6 +++++ .../test/ir/IrJsCodegenBoxTestGenerated.java | 6 +++++ .../ir/IrJsES6CodegenBoxTestGenerated.java | 6 +++++ .../FirNativeCodegenBoxTestGenerated.java | 6 +++++ .../FirNativeCodegenBoxTestNoPLGenerated.java | 6 +++++ .../NativeCodegenBoxTestGenerated.java | 6 +++++ .../NativeCodegenBoxTestNoPLGenerated.java | 6 +++++ .../test/IrCodegenBoxWasmTestGenerated.java | 5 ++++ 17 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java index 8cd868b951a..c75c08bc762 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java @@ -10375,6 +10375,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); } + @Test + @TestMetadata("inlineCallWithReturns.kt") + public void testInlineCallWithReturns() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java index e6bb7e93397..1365f2e577e 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java @@ -10375,6 +10375,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); } + @Test + @TestMetadata("inlineCallWithReturns.kt") + public void testInlineCallWithReturns() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ReturnableBlockLowering.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ReturnableBlockLowering.kt index e9dbdba0fb8..4051b25365f 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ReturnableBlockLowering.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ReturnableBlockLowering.kt @@ -127,14 +127,20 @@ class ReturnableBlockTransformer(val context: CommonBackendContext, val containe } } - val newStatements = expression.statements.mapIndexed { i, s -> - if (expression.statements.size == 1 && s is IrInlinedFunctionBlock) { - for ((j, statement) in s.statements.withIndex()) { - s.statements[j] = transformSingleStatement(statement, j == s.statements.lastIndex) + val newStatements = expression.statements.mapIndexed { i, currentStatement -> + if (expression.statements.size == 1 && currentStatement is IrInlinedFunctionBlock) { + val lastIndex = currentStatement.statements.lastIndex + for ((j, statement) in currentStatement.statements.withIndex()) { + val lastInList = j == lastIndex + val transformedStatement = transformSingleStatement(statement, lastInList) + currentStatement.statements[j] = transformedStatement + if (lastInList) { + currentStatement.type = (transformedStatement as? IrExpression)?.type ?: context.irBuiltIns.unitType + } } - s + currentStatement } else { - transformSingleStatement(s, i == expression.statements.lastIndex) + transformSingleStatement(currentStatement, i == expression.statements.lastIndex) } } @@ -164,6 +170,7 @@ class ReturnableBlockTransformer(val context: CommonBackendContext, val containe // In case of Unit return type we don't need to return an explicit value. This will not be optimized by JVM backend and // may result in exceptions in `MethodVerifier` before optimizations. // Also note that `UNDEFINED_OFFSET` is needed to make proper line number for JVM. + expression.type = context.irBuiltIns.unitType +at(UNDEFINED_OFFSET, UNDEFINED_OFFSET).irGet(variable) } } diff --git a/compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt b/compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt new file mode 100644 index 00000000000..ffe54141079 --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt @@ -0,0 +1,25 @@ +// WITH_STDLIB + +import kotlin.coroutines.* + +suspend fun withCorutine(block: suspend () -> Unit): Unit { + block() +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(Continuation(EmptyCoroutineContext) { + it.getOrThrow() + }) +} + +inline fun f(): Int { + if (42 != 42) return 12345 + return 67890 +} + +fun box(): String { + builder { + check(f() == 67890) + } + 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 615958f4fa4..4a7771a602d 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 @@ -10117,6 +10117,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); } + @Test + @TestMetadata("inlineCallWithReturns.kt") + public void testInlineCallWithReturns() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() 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 162d2395828..bfc24955645 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 @@ -10375,6 +10375,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); } + @Test + @TestMetadata("inlineCallWithReturns.kt") + public void testInlineCallWithReturns() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java index 1cda44c3e43..70209a29bf3 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java @@ -10375,6 +10375,12 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); } + @Test + @TestMetadata("inlineCallWithReturns.kt") + public void testInlineCallWithReturns() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() 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 402f84fb89e..3ea758be8c4 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -7967,6 +7967,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); } + @TestMetadata("inlineCallWithReturns.kt") + public void testInlineCallWithReturns() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt"); + } + @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { runTest("compiler/testData/codegen/box/coroutines/inlineFunInGenericClass.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 800bf91db1c..0a37cd11bda 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 @@ -7125,6 +7125,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); } + @Test + @TestMetadata("inlineCallWithReturns.kt") + public void testInlineCallWithReturns() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java index fd64dc94b61..d87b97ee47c 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java @@ -7221,6 +7221,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest { runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); } + @Test + @TestMetadata("inlineCallWithReturns.kt") + public void testInlineCallWithReturns() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() 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 7e06fbf1530..c1faf432503 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 @@ -7221,6 +7221,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); } + @Test + @TestMetadata("inlineCallWithReturns.kt") + public void testInlineCallWithReturns() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java index 16f3f8fcf45..64bd90ab2d7 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java @@ -7221,6 +7221,12 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); } + @Test + @TestMetadata("inlineCallWithReturns.kt") + public void testInlineCallWithReturns() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/FirNativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/FirNativeCodegenBoxTestGenerated.java index 10a97197879..a424caf279d 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/FirNativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/FirNativeCodegenBoxTestGenerated.java @@ -8266,6 +8266,12 @@ public class FirNativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTe runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); } + @Test + @TestMetadata("inlineCallWithReturns.kt") + public void testInlineCallWithReturns() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/FirNativeCodegenBoxTestNoPLGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/FirNativeCodegenBoxTestNoPLGenerated.java index be8246dae9d..79fa09c74d1 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/FirNativeCodegenBoxTestNoPLGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/FirNativeCodegenBoxTestNoPLGenerated.java @@ -8444,6 +8444,12 @@ public class FirNativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenB runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); } + @Test + @TestMetadata("inlineCallWithReturns.kt") + public void testInlineCallWithReturns() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { 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 950ed478604..28532765b53 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 @@ -8177,6 +8177,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); } + @Test + @TestMetadata("inlineCallWithReturns.kt") + public void testInlineCallWithReturns() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestNoPLGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestNoPLGenerated.java index 16e7e8b09ed..7655cdb2a88 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestNoPLGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestNoPLGenerated.java @@ -8355,6 +8355,12 @@ public class NativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenBoxT runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); } + @Test + @TestMetadata("inlineCallWithReturns.kt") + public void testInlineCallWithReturns() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { diff --git a/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/IrCodegenBoxWasmTestGenerated.java b/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/IrCodegenBoxWasmTestGenerated.java index 81131b31f06..6a1bf8be1ce 100644 --- a/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/IrCodegenBoxWasmTestGenerated.java +++ b/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/IrCodegenBoxWasmTestGenerated.java @@ -6352,6 +6352,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); } + @TestMetadata("inlineCallWithReturns.kt") + public void testInlineCallWithReturns() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/inlineCallWithReturns.kt"); + } + @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { runTest("compiler/testData/codegen/box/coroutines/inlineFunInGenericClass.kt");