From 758484a01f648bcdf10328051c8cfe697b5808bc Mon Sep 17 00:00:00 2001 From: Igor Yakovlev Date: Fri, 22 Sep 2023 14:19:45 +0200 Subject: [PATCH] [Wasm] Fix generation optimised when expression with Nothing type Fixed #KT-62147 --- ...LightTreeBlackBoxCodegenTestGenerated.java | 6 +++ ...hIrFakeOverrideGeneratorTestGenerated.java | 6 +++ .../FirPsiBlackBoxCodegenTestGenerated.java | 6 +++ .../wasm/ir2wasm/OptimisedWhenGenerator.kt | 9 +++-- .../testData/codegen/box/when/nothingTyped.kt | 39 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 +++ .../IrBlackBoxCodegenTestGenerated.java | 6 +++ ...kBoxCodegenWithIrInlinerTestGenerated.java | 6 +++ .../LightAnalysisModeTestGenerated.java | 5 +++ .../fir/FirJsCodegenBoxTestGenerated.java | 6 +++ .../fir/FirJsES6CodegenBoxTestGenerated.java | 6 +++ .../test/ir/IrJsCodegenBoxTestGenerated.java | 6 +++ .../ir/IrJsES6CodegenBoxTestGenerated.java | 6 +++ .../FirNativeCodegenBoxTestGenerated.java | 6 +++ .../FirNativeCodegenBoxTestNoPLGenerated.java | 6 +++ .../NativeCodegenBoxTestGenerated.java | 6 +++ .../NativeCodegenBoxTestNoPLGenerated.java | 6 +++ .../test/FirWasmCodegenBoxTestGenerated.java | 6 +++ .../test/K1WasmCodegenBoxTestGenerated.java | 6 +++ 19 files changed, 146 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/codegen/box/when/nothingTyped.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 f733224f73d..c8e0e619c4f 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 @@ -54019,6 +54019,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr runTest("compiler/testData/codegen/box/when/noElseNoMatch.kt"); } + @Test + @TestMetadata("nothingTyped.kt") + public void testNothingTyped() throws Exception { + runTest("compiler/testData/codegen/box/when/nothingTyped.kt"); + } + @Test @TestMetadata("nullableWhen.kt") public void testNullableWhen() throws Exception { diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated.java index 5618df12c19..dc56aedf50a 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated.java @@ -54019,6 +54019,12 @@ public class FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated runTest("compiler/testData/codegen/box/when/noElseNoMatch.kt"); } + @Test + @TestMetadata("nothingTyped.kt") + public void testNothingTyped() throws Exception { + runTest("compiler/testData/codegen/box/when/nothingTyped.kt"); + } + @Test @TestMetadata("nullableWhen.kt") public void testNullableWhen() 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 75c560eca88..a72df4c12d7 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 @@ -54019,6 +54019,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo runTest("compiler/testData/codegen/box/when/noElseNoMatch.kt"); } + @Test + @TestMetadata("nothingTyped.kt") + public void testNothingTyped() throws Exception { + runTest("compiler/testData/codegen/box/when/nothingTyped.kt"); + } + @Test @TestMetadata("nullableWhen.kt") public void testNullableWhen() throws Exception { diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/OptimisedWhenGenerator.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/OptimisedWhenGenerator.kt index b4f1ea53ce3..9679b31288e 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/OptimisedWhenGenerator.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/OptimisedWhenGenerator.kt @@ -329,7 +329,10 @@ private fun BodyGenerator.genTableIntSwitch( body.buildBlock(resultType) } - resultType?.let { generateDefaultInitializerForType(it, body) } //stub value + if (resultType != null && resultType !is WasmUnreachableType) { + generateDefaultInitializerForType(resultType, body) //stub value + } + body.buildGetLocal(selectorLocal, location) if (shift != 0) { body.buildConstI32(shift, location) @@ -344,7 +347,7 @@ private fun BodyGenerator.genTableIntSwitch( body.buildEnd() for (expression in branches) { - if (resultType != null) { + if (resultType != null && resultType !is WasmUnreachableType) { body.buildDrop(location) } generateWithExpectedType(expression.expression, expectedType) @@ -354,7 +357,7 @@ private fun BodyGenerator.genTableIntSwitch( } if (elseExpression != null) { - if (resultType != null) { + if (resultType != null && resultType !is WasmUnreachableType) { body.buildDrop(location) } generateWithExpectedType(elseExpression, expectedType) diff --git a/compiler/testData/codegen/box/when/nothingTyped.kt b/compiler/testData/codegen/box/when/nothingTyped.kt new file mode 100644 index 00000000000..a36fb728ef4 --- /dev/null +++ b/compiler/testData/codegen/box/when/nothingTyped.kt @@ -0,0 +1,39 @@ +fun nothingTypedWhenSimple(c: Int): Int { + val nothing = when(c) { + 2 -> return 2 + 3 -> return 5 + 4 -> return 1 + 5 -> return 6 + 6 -> return 9 + else -> return 7746353 + } +} + +fun nothingTypedWhenMixed(c: Int): Int { + val nothing = when(c) { + 2 -> return 2 + 3 -> return 5 + 23 -> return 211 + 44 -> return 666 + 11 -> return 43 + 43 -> return 53 + 55 -> return 2 + 99 -> return 81 + 3 -> return 21 + 212 -> return 5 + 66 -> return 1 + 5 -> return 611 + 6 -> return 12 + 123 -> return 63 + 1 -> return 11 + 5 -> return 19 + 6 -> return 9 + else -> return 7746353 + } +} + +fun box(): String { + if (nothingTypedWhenSimple(4) != 1) return "fail1" + if (nothingTypedWhenMixed(99) != 81) return "fail2" + 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 154255d414c..da270e6d78e 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 @@ -50773,6 +50773,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/when/noElseNoMatch.kt"); } + @Test + @TestMetadata("nothingTyped.kt") + public void testNothingTyped() throws Exception { + runTest("compiler/testData/codegen/box/when/nothingTyped.kt"); + } + @Test @TestMetadata("nullableWhen.kt") public void testNullableWhen() 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 080e45ab097..7191708c6a9 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 @@ -54019,6 +54019,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/when/noElseNoMatch.kt"); } + @Test + @TestMetadata("nothingTyped.kt") + public void testNothingTyped() throws Exception { + runTest("compiler/testData/codegen/box/when/nothingTyped.kt"); + } + @Test @TestMetadata("nullableWhen.kt") public void testNullableWhen() 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 f5912779903..bb488a85499 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 @@ -54019,6 +54019,12 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack runTest("compiler/testData/codegen/box/when/noElseNoMatch.kt"); } + @Test + @TestMetadata("nothingTyped.kt") + public void testNothingTyped() throws Exception { + runTest("compiler/testData/codegen/box/when/nothingTyped.kt"); + } + @Test @TestMetadata("nullableWhen.kt") public void testNullableWhen() 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 81e49be185b..1a9014abf86 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -43689,6 +43689,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/when/noElseNoMatch.kt"); } + @TestMetadata("nothingTyped.kt") + public void testNothingTyped() throws Exception { + runTest("compiler/testData/codegen/box/when/nothingTyped.kt"); + } + @TestMetadata("nullableWhen.kt") public void testNullableWhen() throws Exception { runTest("compiler/testData/codegen/box/when/nullableWhen.kt"); 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 9abdf9e04d6..3754ccfdb71 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 @@ -37471,6 +37471,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest { runTest("compiler/testData/codegen/box/when/noElseNoMatch.kt"); } + @Test + @TestMetadata("nothingTyped.kt") + public void testNothingTyped() throws Exception { + runTest("compiler/testData/codegen/box/when/nothingTyped.kt"); + } + @Test @TestMetadata("nullableWhen.kt") public void testNullableWhen() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsES6CodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsES6CodegenBoxTestGenerated.java index f5252a3779e..7658b87984b 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsES6CodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsES6CodegenBoxTestGenerated.java @@ -37471,6 +37471,12 @@ public class FirJsES6CodegenBoxTestGenerated extends AbstractFirJsES6CodegenBoxT runTest("compiler/testData/codegen/box/when/noElseNoMatch.kt"); } + @Test + @TestMetadata("nothingTyped.kt") + public void testNothingTyped() throws Exception { + runTest("compiler/testData/codegen/box/when/nothingTyped.kt"); + } + @Test @TestMetadata("nullableWhen.kt") public void testNullableWhen() 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 60d7043c1b1..06dbde514e0 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 @@ -37471,6 +37471,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/when/noElseNoMatch.kt"); } + @Test + @TestMetadata("nothingTyped.kt") + public void testNothingTyped() throws Exception { + runTest("compiler/testData/codegen/box/when/nothingTyped.kt"); + } + @Test @TestMetadata("nullableWhen.kt") public void testNullableWhen() 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 d7ea39fad5d..4ea710ae421 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 @@ -37471,6 +37471,12 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes runTest("compiler/testData/codegen/box/when/noElseNoMatch.kt"); } + @Test + @TestMetadata("nothingTyped.kt") + public void testNothingTyped() throws Exception { + runTest("compiler/testData/codegen/box/when/nothingTyped.kt"); + } + @Test @TestMetadata("nullableWhen.kt") public void testNullableWhen() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestGenerated.java index 32f6104a083..acbac2cbff8 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestGenerated.java @@ -41001,6 +41001,12 @@ public class FirNativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTe runTest("compiler/testData/codegen/box/when/noElseNoMatch.kt"); } + @Test + @TestMetadata("nothingTyped.kt") + public void testNothingTyped() throws Exception { + runTest("compiler/testData/codegen/box/when/nothingTyped.kt"); + } + @Test @TestMetadata("nullableWhen.kt") public void testNullableWhen() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestNoPLGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestNoPLGenerated.java index a6e68733f47..84b669ae87e 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestNoPLGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestNoPLGenerated.java @@ -42055,6 +42055,12 @@ public class FirNativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenB runTest("compiler/testData/codegen/box/when/noElseNoMatch.kt"); } + @Test + @TestMetadata("nothingTyped.kt") + public void testNothingTyped() throws Exception { + runTest("compiler/testData/codegen/box/when/nothingTyped.kt"); + } + @Test @TestMetadata("nullableWhen.kt") public void testNullableWhen() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestGenerated.java index 1bf91b773c5..af96ff629aa 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestGenerated.java @@ -40475,6 +40475,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest runTest("compiler/testData/codegen/box/when/noElseNoMatch.kt"); } + @Test + @TestMetadata("nothingTyped.kt") + public void testNothingTyped() throws Exception { + runTest("compiler/testData/codegen/box/when/nothingTyped.kt"); + } + @Test @TestMetadata("nullableWhen.kt") public void testNullableWhen() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestNoPLGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestNoPLGenerated.java index a4a4980cb0a..dcbfca812b7 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestNoPLGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestNoPLGenerated.java @@ -41002,6 +41002,12 @@ public class NativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenBoxT runTest("compiler/testData/codegen/box/when/noElseNoMatch.kt"); } + @Test + @TestMetadata("nothingTyped.kt") + public void testNothingTyped() throws Exception { + runTest("compiler/testData/codegen/box/when/nothingTyped.kt"); + } + @Test @TestMetadata("nullableWhen.kt") public void testNullableWhen() throws Exception { diff --git a/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/FirWasmCodegenBoxTestGenerated.java b/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/FirWasmCodegenBoxTestGenerated.java index dca7fb3e258..9d95455842e 100644 --- a/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/FirWasmCodegenBoxTestGenerated.java +++ b/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/FirWasmCodegenBoxTestGenerated.java @@ -37141,6 +37141,12 @@ public class FirWasmCodegenBoxTestGenerated extends AbstractFirWasmCodegenBoxTes runTest("compiler/testData/codegen/box/when/noElseNoMatch.kt"); } + @Test + @TestMetadata("nothingTyped.kt") + public void testNothingTyped() throws Exception { + runTest("compiler/testData/codegen/box/when/nothingTyped.kt"); + } + @Test @TestMetadata("nullableWhen.kt") public void testNullableWhen() throws Exception { diff --git a/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/K1WasmCodegenBoxTestGenerated.java b/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/K1WasmCodegenBoxTestGenerated.java index b3962955bc5..ecb7cbf3486 100644 --- a/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/K1WasmCodegenBoxTestGenerated.java +++ b/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/K1WasmCodegenBoxTestGenerated.java @@ -37141,6 +37141,12 @@ public class K1WasmCodegenBoxTestGenerated extends AbstractK1WasmCodegenBoxTest runTest("compiler/testData/codegen/box/when/noElseNoMatch.kt"); } + @Test + @TestMetadata("nothingTyped.kt") + public void testNothingTyped() throws Exception { + runTest("compiler/testData/codegen/box/when/nothingTyped.kt"); + } + @Test @TestMetadata("nullableWhen.kt") public void testNullableWhen() throws Exception {