From 194a1d1c6af550a69fd90dbc1f410ccf79d23e7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Sch=C3=A4fer?= Date: Thu, 9 Jun 2022 14:05:50 +0200 Subject: [PATCH] JVM IR: Start fake variables for default lambdas after initialization KT-52702 --- .../kotlin/codegen/inline/LambdaInfo.kt | 7 +++--- .../FirBlackBoxCodegenTestGenerated.java | 6 +++++ .../codegen/box/defaultArguments/kt52702.kt | 24 +++++++++++++++++++ .../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, 73 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/codegen/box/defaultArguments/kt52702.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/LambdaInfo.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/LambdaInfo.kt index 4dec3c14a7a..f11ddd9f51c 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/LambdaInfo.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/LambdaInfo.kt @@ -140,12 +140,11 @@ class DefaultLambda(info: ExtractedDefaultLambda, sourceCompiler: SourceCompiler val withFakeVariable = MethodNode(originNode.access, originNode.name, originNode.desc, originNode.signature, originNode.exceptions?.toTypedArray()) val fakeVarIndex = originNode.maxLocals + withFakeVariable.instructions.add(LdcInsnNode(0)) + withFakeVariable.instructions.add(VarInsnNode(Opcodes.ISTORE, fakeVarIndex)) + val startLabel = LabelNode().also { withFakeVariable.instructions.add(it) } originNode.accept(withFakeVariable) - val startLabel = - withFakeVariable.instructions.first as? LabelNode ?: LabelNode().apply { withFakeVariable.instructions.insert(this) } val endLabel = withFakeVariable.instructions.last as? LabelNode ?: LabelNode().apply { withFakeVariable.instructions.add(this) } - withFakeVariable.instructions.insert(startLabel, VarInsnNode(Opcodes.ISTORE, fakeVarIndex)) - withFakeVariable.instructions.insert(startLabel, LdcInsnNode(0)) withFakeVariable.localVariables.add( LocalVariableNode( 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 385885d69be..a25b44f8b4f 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 @@ -13735,6 +13735,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/defaultArguments/kt48391.kt"); } + @Test + @TestMetadata("kt52702.kt") + public void testKt52702() throws Exception { + runTest("compiler/testData/codegen/box/defaultArguments/kt52702.kt"); + } + @Test @TestMetadata("kt6382.kt") public void testKt6382() throws Exception { diff --git a/compiler/testData/codegen/box/defaultArguments/kt52702.kt b/compiler/testData/codegen/box/defaultArguments/kt52702.kt new file mode 100644 index 00000000000..21f5bfd3705 --- /dev/null +++ b/compiler/testData/codegen/box/defaultArguments/kt52702.kt @@ -0,0 +1,24 @@ +// IGNORE_BACKEND: JS +// Check that local variables for inline functions and inline default lambdas start +// after they are initialized. + +inline fun spray() { + val a = Any() + val b = Any() + val c = Any() + val d = Any() + val e = Any() +} + +inline fun f(block: () -> String = { "OK" }): String = block() + +fun box(): String { + // On the JVM, this call adds some locals with reference types to the LVT + // which end after the call returns. + spray() + // When inlining `f` we'll reuse the same slots that previously contained + // locals with reference types for the $i$f$f and $i$a$-f-... variables. + // Since these locals have integer types D8 would produce a warning if they + // started before being initialized, which would cause the test to fail. + return f() +} 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 2bcdb11a04b..f579a6f6aa3 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 @@ -13615,6 +13615,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/defaultArguments/kt48391.kt"); } + @Test + @TestMetadata("kt52702.kt") + public void testKt52702() throws Exception { + runTest("compiler/testData/codegen/box/defaultArguments/kt52702.kt"); + } + @Test @TestMetadata("kt6382.kt") public void testKt6382() 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 f2a6fd9034a..053110711a4 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 @@ -13735,6 +13735,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/defaultArguments/kt48391.kt"); } + @Test + @TestMetadata("kt52702.kt") + public void testKt52702() throws Exception { + runTest("compiler/testData/codegen/box/defaultArguments/kt52702.kt"); + } + @Test @TestMetadata("kt6382.kt") public void testKt6382() 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 1a442a40b6f..48fb4ac18d8 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -11063,6 +11063,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/defaultArguments/kt48391.kt"); } + @TestMetadata("kt52702.kt") + public void testKt52702() throws Exception { + runTest("compiler/testData/codegen/box/defaultArguments/kt52702.kt"); + } + @TestMetadata("kt6382.kt") public void testKt6382() throws Exception { runTest("compiler/testData/codegen/box/defaultArguments/kt6382.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 f92959777f2..1d9c3c2c2ce 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 @@ -10239,6 +10239,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/defaultArguments/kt48391.kt"); } + @Test + @TestMetadata("kt52702.kt") + public void testKt52702() throws Exception { + runTest("compiler/testData/codegen/box/defaultArguments/kt52702.kt"); + } + @Test @TestMetadata("kt6382.kt") public void testKt6382() 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 14a7315868b..39cf2a36f9a 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 @@ -10281,6 +10281,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/defaultArguments/kt48391.kt"); } + @Test + @TestMetadata("kt52702.kt") + public void testKt52702() throws Exception { + runTest("compiler/testData/codegen/box/defaultArguments/kt52702.kt"); + } + @Test @TestMetadata("kt6382.kt") public void testKt6382() 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 3df9e8fbd72..78a5e816344 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 @@ -9103,6 +9103,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/defaultArguments/kt48391.kt"); } + @TestMetadata("kt52702.kt") + public void testKt52702() throws Exception { + runTest("compiler/testData/codegen/box/defaultArguments/kt52702.kt"); + } + @TestMetadata("kt6382.kt") public void testKt6382() throws Exception { runTest("compiler/testData/codegen/box/defaultArguments/kt6382.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 39b1d580b0d..7538ced8f67 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 @@ -11227,6 +11227,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest runTest("compiler/testData/codegen/box/defaultArguments/kt48391.kt"); } + @Test + @TestMetadata("kt52702.kt") + public void testKt52702() throws Exception { + runTest("compiler/testData/codegen/box/defaultArguments/kt52702.kt"); + } + @Test @TestMetadata("kt6382.kt") public void testKt6382() throws Exception {