From e468a347b5423295b6717c8d398946cdc87aa722 Mon Sep 17 00:00:00 2001 From: Igor Chevdar Date: Thu, 13 Aug 2020 15:16:53 +0500 Subject: [PATCH] [box-tests] Tests on field init optimization Added tests on all primitive types and a test when the field's type is an inline class --- .../ir/FirBlackBoxCodegenTestGenerated.java | 5 +++ .../fieldInitializerOptimization.kt | 39 +++++++++++++++++-- ...ieldInitializerOptimization_inlineClass.kt | 32 +++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 5 +++ .../LightAnalysisModeTestGenerated.java | 5 +++ .../ir/IrBlackBoxCodegenTestGenerated.java | 5 +++ .../IrJsCodegenBoxES6TestGenerated.java | 5 +++ .../IrJsCodegenBoxTestGenerated.java | 5 +++ .../semantics/JsCodegenBoxTestGenerated.java | 5 +++ 9 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization_inlineClass.kt diff --git a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index ffadf13e879..4b00197fa3b 100644 --- a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -28815,6 +28815,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization.kt"); } + @TestMetadata("fieldInitializerOptimization_inlineClass.kt") + public void testFieldInitializerOptimization_inlineClass() throws Exception { + runTest("compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization_inlineClass.kt"); + } + @TestMetadata("generics.kt") public void testGenerics() throws Exception { runTest("compiler/testData/codegen/box/secondaryConstructors/generics.kt"); diff --git a/compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization.kt b/compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization.kt index 98bf5b4c7d4..395fcfb46a1 100644 --- a/compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization.kt +++ b/compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: NATIVE // IGNORE_BACKEND: JS // IGNORE_BACKEND: JS_IR // IGNORE_BACKEND: JS_IR_ES6 @@ -8,15 +7,47 @@ open class Base { init { setup() } } +val placeHolder = Any() + class Derived : Base { constructor() : super() - override fun setup() { x = 1 } + override fun setup() { + xBool = true + xByte = 1.toByte() + xChar = 2.toChar() + xShort = 3.toShort() + xInt = 4 + xLong = 5L + xFloat = 6.0f + xDouble = 7.0 + xRef = placeHolder + } // Technically, this field initializer comes after the superclass // constructor is called. However, we optimize away field initializers // which set fields to their default value, which is why x ends up with // value 1 after the constructor call. - var x = 0 + var xBool = false + var xByte = 0.toByte() + var xChar = 0.toChar() + var xShort = 0.toShort() + var xInt = 0 + var xLong = 0L + var xFloat = 0.0f + var xDouble = 0.0 + var xRef: Any? = null } -fun box(): String = if (Derived().x == 1) "OK" else "Fail" +fun box(): String { + val d = Derived() + if (d.xBool != true) return "fail Bool" + if (d.xByte != 1.toByte()) return "fail Byte" + if (d.xChar != 2.toChar()) return "fail Char" + if (d.xShort != 3.toShort()) return "fail Short" + if (d.xInt != 4) return "fail Int" + if (d.xLong != 5L) return "fail Long" + if (d.xFloat != 6.0f) return "fail Float" + if (d.xDouble != 7.0) return "fail Double" + if (d.xRef != placeHolder) return "fail Ref" + return "OK" +} diff --git a/compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization_inlineClass.kt b/compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization_inlineClass.kt new file mode 100644 index 00000000000..4d82bd9a2d9 --- /dev/null +++ b/compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization_inlineClass.kt @@ -0,0 +1,32 @@ +// IGNORE_BACKEND: JS +// IGNORE_BACKEND: JS_IR +// IGNORE_BACKEND: JS_IR_ES6 +// IGNORE_BACKEND: JVM +// IGNORE_BACKEND: JVM_IR +// IGNORE_BACKEND_FIR: JVM_IR + +open class Base { + open fun setup() {} + init { setup() } +} + +inline class Z(val y: Int) + +class Derived : Base { + constructor() : super() + override fun setup() { + x = Z(1) + } + + // Technically, this field initializer comes after the superclass + // constructor is called. However, we optimize away field initializers + // which set fields to their default value, which is why x ends up with + // value 1 after the constructor call. + var x = Z(0) +} + +fun box(): String { + val d = Derived() + if (d.x.y != 1) return "fail" + return "OK" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 57bcda6cfe9..7d4deb9400c 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -30411,6 +30411,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization.kt"); } + @TestMetadata("fieldInitializerOptimization_inlineClass.kt") + public void testFieldInitializerOptimization_inlineClass() throws Exception { + runTest("compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization_inlineClass.kt"); + } + @TestMetadata("generics.kt") public void testGenerics() throws Exception { runTest("compiler/testData/codegen/box/secondaryConstructors/generics.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 11560793ad7..b0740b40f59 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -27947,6 +27947,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) public static class SecondaryConstructors extends AbstractLightAnalysisModeTest { + @TestMetadata("fieldInitializerOptimization_inlineClass.kt") + public void ignoreFieldInitializerOptimization_inlineClass() throws Exception { + runTest("compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization_inlineClass.kt"); + } + private void runTest(String testDataFilePath) throws Exception { KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath); } diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index fa427f62639..3d0153e8f26 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -28815,6 +28815,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization.kt"); } + @TestMetadata("fieldInitializerOptimization_inlineClass.kt") + public void testFieldInitializerOptimization_inlineClass() throws Exception { + runTest("compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization_inlineClass.kt"); + } + @TestMetadata("generics.kt") public void testGenerics() throws Exception { runTest("compiler/testData/codegen/box/secondaryConstructors/generics.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index b273e41a0e8..e07ea919ad8 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -23411,6 +23411,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization.kt"); } + @TestMetadata("fieldInitializerOptimization_inlineClass.kt") + public void testFieldInitializerOptimization_inlineClass() throws Exception { + runTest("compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization_inlineClass.kt"); + } + @TestMetadata("generics.kt") public void testGenerics() throws Exception { runTest("compiler/testData/codegen/box/secondaryConstructors/generics.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index 26862eeb4f7..6916004b760 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -23411,6 +23411,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization.kt"); } + @TestMetadata("fieldInitializerOptimization_inlineClass.kt") + public void testFieldInitializerOptimization_inlineClass() throws Exception { + runTest("compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization_inlineClass.kt"); + } + @TestMetadata("generics.kt") public void testGenerics() throws Exception { runTest("compiler/testData/codegen/box/secondaryConstructors/generics.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index de5dd479158..129610bc9f8 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -23426,6 +23426,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization.kt"); } + @TestMetadata("fieldInitializerOptimization_inlineClass.kt") + public void testFieldInitializerOptimization_inlineClass() throws Exception { + runTest("compiler/testData/codegen/box/secondaryConstructors/fieldInitializerOptimization_inlineClass.kt"); + } + @TestMetadata("generics.kt") public void testGenerics() throws Exception { runTest("compiler/testData/codegen/box/secondaryConstructors/generics.kt");