diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index c20ac61be08..3df80ccb042 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -13700,6 +13700,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/fir"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); } + @Test + @TestMetadata("ConstValAccess.kt") + public void testConstValAccess() throws Exception { + runTest("compiler/testData/codegen/box/fir/ConstValAccess.kt"); + } + @Test @TestMetadata("ExtensionAlias.kt") public void testExtensionAlias() throws Exception { diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrCompileTimeChecker.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrCompileTimeChecker.kt index c7df0ee0685..307bbb79845 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrCompileTimeChecker.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrCompileTimeChecker.kt @@ -168,6 +168,13 @@ class IrCompileTimeChecker( override fun visitGetField(expression: IrGetField, data: Nothing?): Boolean { val owner = expression.symbol.owner + if (owner.origin == IrDeclarationOrigin.PROPERTY_BACKING_FIELD && owner.correspondingPropertySymbol?.owner?.isConst == true) { + val receiverComputable = expression.receiver?.accept(this, null) ?: true + val initializerComputable = owner.initializer?.accept(this, null) ?: false + if (receiverComputable && initializerComputable) { + return true + } + } val parent = owner.parent as IrSymbolOwner val isJavaPrimitiveStatic = owner.origin == IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB && owner.isStatic && (owner.type.isPrimitiveType() || owner.type.isStringClassType()) diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreter.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreter.kt index 1ea11ec77f9..3470a1905c5 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreter.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreter.kt @@ -540,6 +540,9 @@ class IrInterpreter(private val irBuiltIns: IrBuiltIns, private val bodyMap: Map stack.pushReturnValue(Wrapper.getStaticGetter(field)!!.invokeWithArguments().toState(field.type)) return Next } + if (field.origin == IrDeclarationOrigin.PROPERTY_BACKING_FIELD && field.correspondingPropertySymbol?.owner?.isConst == true) { + return field.initializer?.expression?.interpret() ?: Next + } // receiver is null, for example, for top level fields val result = receiver?.let { stack.getVariable(receiver).state.getState(field.correspondingPropertySymbol!!) } ?: return (expression.symbol.owner.initializer?.expression?.interpret() ?: Next) diff --git a/compiler/testData/codegen/box/fir/ConstValAccess.kt b/compiler/testData/codegen/box/fir/ConstValAccess.kt new file mode 100644 index 00000000000..0ba411902f0 --- /dev/null +++ b/compiler/testData/codegen/box/fir/ConstValAccess.kt @@ -0,0 +1,18 @@ +// TARGET_BACKEND: JVM + +// MODULE: lib +// FILE: A.kt + +object Obj { + const val A_CONST = "O" +} + +// MODULE: main(lib) +// FILE: B.kt + +fun box(): String { + val s = B_CONST + return s + "K"; +} + +const val B_CONST = Obj.A_CONST 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 2433ee275cf..c4850eeb1a0 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 @@ -13700,6 +13700,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/fir"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true); } + @Test + @TestMetadata("ConstValAccess.kt") + public void testConstValAccess() throws Exception { + runTest("compiler/testData/codegen/box/fir/ConstValAccess.kt"); + } + @Test @TestMetadata("ExtensionAlias.kt") public void testExtensionAlias() 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 3a4bc06fb9e..d2f59468085 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 @@ -13700,6 +13700,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/fir"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); } + @Test + @TestMetadata("ConstValAccess.kt") + public void testConstValAccess() throws Exception { + runTest("compiler/testData/codegen/box/fir/ConstValAccess.kt"); + } + @Test @TestMetadata("ExtensionAlias.kt") public void testExtensionAlias() 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 5b934ee7745..cf690407596 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -12042,6 +12042,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/fir"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true); } + @TestMetadata("ConstValAccess.kt") + public void testConstValAccess() throws Exception { + runTest("compiler/testData/codegen/box/fir/ConstValAccess.kt"); + } + @TestMetadata("ExtensionAlias.kt") public void testExtensionAlias() throws Exception { runTest("compiler/testData/codegen/box/fir/ExtensionAlias.kt");