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 0b4635c7cdc..0438cd948c5 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 @@ -8355,6 +8355,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr runTest("compiler/testData/codegen/box/constants/divisionByZero.kt"); } + @Test + @TestMetadata("doNotTriggerInit.kt") + public void testDoNotTriggerInit() throws Exception { + runTest("compiler/testData/codegen/box/constants/doNotTriggerInit.kt"); + } + @Test @TestMetadata("float.kt") public void testFloat() 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 b000264daaf..37e68ac92dd 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 @@ -8355,6 +8355,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo runTest("compiler/testData/codegen/box/constants/divisionByZero.kt"); } + @Test + @TestMetadata("doNotTriggerInit.kt") + public void testDoNotTriggerInit() throws Exception { + runTest("compiler/testData/codegen/box/constants/doNotTriggerInit.kt"); + } + @Test @TestMetadata("float.kt") public void testFloat() throws Exception { diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/InlineConstTransformer.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/InlineConstTransformer.kt index 0f150c3ebc0..adb06fbdb78 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/InlineConstTransformer.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/InlineConstTransformer.kt @@ -33,7 +33,9 @@ abstract class InlineConstTransformer : IrElementTransformerVoid() { abstract fun reportInlineConst(field: IrField, value: IrConst<*>) - abstract fun IrExpression.shouldDropConstReceiver(): Boolean + fun IrExpression.shouldDropConstReceiver(): Boolean { + return this is IrConst<*> || this is IrGetValue || this is IrGetObjectValue + } override fun visitCall(expression: IrCall): IrExpression { val function = (expression.symbol.owner as? IrSimpleFunction) ?: return super.visitCall(expression) diff --git a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/ConstLowering.kt b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/ConstLowering.kt index 3dd6d6d7306..dc6703844c8 100644 --- a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/ConstLowering.kt +++ b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/ConstLowering.kt @@ -50,6 +50,4 @@ private class JvmInlineConstTransformer(val irFile: IrFile, val inlineConstTrack inlineConstTracker.report(path, owner, name, constType) } - - override fun IrExpression.shouldDropConstReceiver() = this is IrConst<*> || this is IrGetValue || this is IrGetObjectValue } \ No newline at end of file diff --git a/compiler/testData/codegen/box/constants/doNotTriggerInit.kt b/compiler/testData/codegen/box/constants/doNotTriggerInit.kt new file mode 100644 index 00000000000..23a09ff3a5e --- /dev/null +++ b/compiler/testData/codegen/box/constants/doNotTriggerInit.kt @@ -0,0 +1,19 @@ +// IGNORE_BACKEND: WASM +// IGNORE_BACKEND_K1: JS_IR, JS_IR_ES6 +var initialized = 0 + +object O { + init { + initialized += 1 + } + const val x = 1 + val y = 2 +} + +fun box() : String { + if (O.x != 1) return "FAIL 1" + if (initialized != 0) return "FAIL 2" + if (O.y != 2) return "FAIL 3" + if (initialized != 1) return "FAIL 4" + 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 7589860964a..801e5268d39 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 @@ -8151,6 +8151,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/constants/divisionByZero.kt"); } + @Test + @TestMetadata("doNotTriggerInit.kt") + public void testDoNotTriggerInit() throws Exception { + runTest("compiler/testData/codegen/box/constants/doNotTriggerInit.kt"); + } + @Test @TestMetadata("float.kt") public void testFloat() 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 3e56f075c8b..6c9db954504 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 @@ -8355,6 +8355,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/constants/divisionByZero.kt"); } + @Test + @TestMetadata("doNotTriggerInit.kt") + public void testDoNotTriggerInit() throws Exception { + runTest("compiler/testData/codegen/box/constants/doNotTriggerInit.kt"); + } + @Test @TestMetadata("float.kt") public void testFloat() 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 da4857101c3..a4017ff24a3 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -6224,6 +6224,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/constants/divisionByZero.kt"); } + @TestMetadata("doNotTriggerInit.kt") + public void testDoNotTriggerInit() throws Exception { + runTest("compiler/testData/codegen/box/constants/doNotTriggerInit.kt"); + } + @TestMetadata("float.kt") public void testFloat() throws Exception { runTest("compiler/testData/codegen/box/constants/float.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 59581019bb8..a8196aefad0 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 @@ -5369,6 +5369,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/constants/divisionByZero.kt"); } + @Test + @TestMetadata("doNotTriggerInit.kt") + public void testDoNotTriggerInit() throws Exception { + runTest("compiler/testData/codegen/box/constants/doNotTriggerInit.kt"); + } + @Test @TestMetadata("float.kt") public void testFloat() 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 2204fed25fe..8cb6bab19fd 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 @@ -5429,6 +5429,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest { runTest("compiler/testData/codegen/box/constants/divisionByZero.kt"); } + @Test + @TestMetadata("doNotTriggerInit.kt") + public void testDoNotTriggerInit() throws Exception { + runTest("compiler/testData/codegen/box/constants/doNotTriggerInit.kt"); + } + @Test @TestMetadata("float.kt") public void testFloat() 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 ec8f4830105..a1b949e96f8 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 @@ -5429,6 +5429,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/constants/divisionByZero.kt"); } + @Test + @TestMetadata("doNotTriggerInit.kt") + public void testDoNotTriggerInit() throws Exception { + runTest("compiler/testData/codegen/box/constants/doNotTriggerInit.kt"); + } + @Test @TestMetadata("float.kt") public void testFloat() 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 80594cd56aa..4d02c3a80a3 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 @@ -5429,6 +5429,12 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes runTest("compiler/testData/codegen/box/constants/divisionByZero.kt"); } + @Test + @TestMetadata("doNotTriggerInit.kt") + public void testDoNotTriggerInit() throws Exception { + runTest("compiler/testData/codegen/box/constants/doNotTriggerInit.kt"); + } + @Test @TestMetadata("float.kt") public void testFloat() throws Exception { diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/Lowerings.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/Lowerings.kt index a523b6b170c..96fe7f2f89a 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/Lowerings.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/Lowerings.kt @@ -552,8 +552,8 @@ private fun PhaseEngine.getAllLowerings() = listOfNotNull coroutinesPhase, typeOperatorPhase, expressionBodyTransformPhase, - objectClassesPhase, constantInliningPhase, + objectClassesPhase, staticInitializersPhase, builtinOperatorPhase, bridgesPhase, diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ConstLowering.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ConstLowering.kt index d813847c582..6e527a39e9a 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ConstLowering.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ConstLowering.kt @@ -32,7 +32,4 @@ private class NativeInlineConstTransformer : InlineConstTransformer() { ?.takeUnless { it.kind == IrConstKind.Float && IrConstKind.Float.valueOf(it).isNaN() } override fun reportInlineConst(field: IrField, value: IrConst<*>) {} - // on jvm IrGetObjectValue is also dropped. This would be breaking change for native. - // Some design work is required to decide what is correct, let just keep current behaviour for now - override fun IrExpression.shouldDropConstReceiver() = this is IrConst<*> || this is IrGetValue } \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/codegen/objectDeclaration/globalConstants.kt b/kotlin-native/backend.native/tests/codegen/objectDeclaration/globalConstants.kt index 989b719a985..8b4fe9b2356 100644 --- a/kotlin-native/backend.native/tests/codegen/objectDeclaration/globalConstants.kt +++ b/kotlin-native/backend.native/tests/codegen/objectDeclaration/globalConstants.kt @@ -57,8 +57,12 @@ object ClassWithConstructor { @Test fun checkConstructor() { assertEquals(0, ClassWithConstructorInitialized) assertEquals(1, ClassWithConstructor.A) + assertEquals(0, ClassWithConstructorInitialized) + val unused1 = ClassWithConstructor + assertEquals(1, unused1.A) assertEquals(1, ClassWithConstructorInitialized) - assertEquals(1, ClassWithConstructor.A) + val unused2 = ClassWithConstructor + assertEquals(1, unused2.A) assertEquals(1, ClassWithConstructorInitialized) assertFalse(ClassWithConstructor.isPermanent()) } diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/K2NativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/K2NativeCodegenBoxTestGenerated.java index a78472e9a63..b4a3ab118d0 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/K2NativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/K2NativeCodegenBoxTestGenerated.java @@ -6338,6 +6338,12 @@ public class K2NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTes runTest("compiler/testData/codegen/box/constants/divisionByZero.kt"); } + @Test + @TestMetadata("doNotTriggerInit.kt") + public void testDoNotTriggerInit() throws Exception { + runTest("compiler/testData/codegen/box/constants/doNotTriggerInit.kt"); + } + @Test @TestMetadata("float.kt") public void testFloat() 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 a554cdc86fe..792fdb65e52 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 @@ -6265,6 +6265,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest runTest("compiler/testData/codegen/box/constants/divisionByZero.kt"); } + @Test + @TestMetadata("doNotTriggerInit.kt") + public void testDoNotTriggerInit() throws Exception { + runTest("compiler/testData/codegen/box/constants/doNotTriggerInit.kt"); + } + @Test @TestMetadata("float.kt") public void testFloat() 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 3cc9875dabb..84c2cfcfd1e 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 @@ -4784,6 +4784,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/constants/divisionByZero.kt"); } + @TestMetadata("doNotTriggerInit.kt") + public void testDoNotTriggerInit() throws Exception { + runTest("compiler/testData/codegen/box/constants/doNotTriggerInit.kt"); + } + @TestMetadata("float.kt") public void testFloat() throws Exception { runTest("compiler/testData/codegen/box/constants/float.kt");