From 4c0045b136d794ffd9889b7ae2b44c310816d236 Mon Sep 17 00:00:00 2001 From: Ilya Goncharov Date: Mon, 20 Sep 2021 15:25:43 +0300 Subject: [PATCH] [JS IR] Add test on initialization calling only once --- .../js/lower/PropertyLazyInitLowering.kt | 3 +-- .../properties/eagerInitializationGlobal3.kt | 18 ++++++++++++++++++ .../IrJsCodegenBoxES6TestGenerated.java | 5 +++++ .../semantics/IrJsCodegenBoxTestGenerated.java | 5 +++++ 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/codegen/box/properties/eagerInitializationGlobal3.kt diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/PropertyLazyInitLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/PropertyLazyInitLowering.kt index 6bd261b0db4..df2ff549277 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/PropertyLazyInitLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/PropertyLazyInitLowering.kt @@ -284,8 +284,7 @@ private fun IrDeclaration.withPersistentSafe(transform: IrDeclaration.() -> } else null private fun IrDeclaration.isCompatibleDeclaration(context: JsIrBackendContext) = - (this as? IrField) - ?.correspondingProperty + correspondingProperty ?.hasAnnotation(context.intrinsics.jsEagerInitializationAnnotationSymbol) != true && withPersistentSafe { origin in compatibleOrigins } == true diff --git a/compiler/testData/codegen/box/properties/eagerInitializationGlobal3.kt b/compiler/testData/codegen/box/properties/eagerInitializationGlobal3.kt new file mode 100644 index 00000000000..368fd89c3c5 --- /dev/null +++ b/compiler/testData/codegen/box/properties/eagerInitializationGlobal3.kt @@ -0,0 +1,18 @@ +// TARGET_BACKEND: JS_IR +// DONT_TARGET_EXACT_BACKEND: WASM +// PROPERTY_LAZY_INITIALIZATION + +// FILE: lib.kt +var z1 = false + +// FILE: lib2.kt + +@OptIn(kotlin.ExperimentalStdlibApi::class) +@JsEagerInitialization +val x = run { z1 = !z1; 42 } + +val y = run { 73 } + +fun box(): String { + return if (z1) "OK" else "fail" +} diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index c040c14597c..a64fb3b9672 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -19852,6 +19852,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/properties/eagerInitializationGlobal2.kt"); } + @TestMetadata("eagerInitializationGlobal3.kt") + public void testEagerInitializationGlobal3() throws Exception { + runTest("compiler/testData/codegen/box/properties/eagerInitializationGlobal3.kt"); + } + @TestMetadata("field.kt") public void testField() throws Exception { runTest("compiler/testData/codegen/box/properties/field.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index 14e3ab0accb..8eee15c8b28 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -19258,6 +19258,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/properties/eagerInitializationGlobal2.kt"); } + @TestMetadata("eagerInitializationGlobal3.kt") + public void testEagerInitializationGlobal3() throws Exception { + runTest("compiler/testData/codegen/box/properties/eagerInitializationGlobal3.kt"); + } + @TestMetadata("field.kt") public void testField() throws Exception { runTest("compiler/testData/codegen/box/properties/field.kt");