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 db4ae21d82b..d67531aaafa 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 @@ -262,6 +262,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/annotations/nestedClassesInAnnotations.kt"); } + @Test + @TestMetadata("objectConstValInAnnotationArgument.kt") + public void testObjectConstValInAnnotationArgument() throws Exception { + runTest("compiler/testData/codegen/box/annotations/objectConstValInAnnotationArgument.kt"); + } + @Test @TestMetadata("parameterAnnotationInDefaultImpls.kt") public void testParameterAnnotationInDefaultImpls() throws Exception { diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/InstructionsUnfolder.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/InstructionsUnfolder.kt index 876a73299c9..703991cdb62 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/InstructionsUnfolder.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/InstructionsUnfolder.kt @@ -235,11 +235,10 @@ private fun unfoldSetField(expression: IrSetField, callStack: CallStack) { } private fun unfoldGetValue(expression: IrGetValue, environment: IrInterpreterEnvironment) { - val expectedClass = expression.type.classOrNull?.owner - // used to evaluate constants inside object - if (expectedClass != null && expectedClass.isObject && expression.symbol.owner.origin == IrDeclarationOrigin.INSTANCE_RECEIVER) { + if (expression.isAccessToObject()) { + // used to evaluate constants inside object // TODO is this correct behaviour? - val irGetObject = expectedClass.createGetObject() + val irGetObject = expression.type.classOrNull?.owner!!.createGetObject() return unfoldGetObjectValue(irGetObject, environment) } environment.callStack.pushState(environment.callStack.loadState(expression.symbol)) diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/Utils.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/Utils.kt index c574804ac27..d2dc248ebf6 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/Utils.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/Utils.kt @@ -289,4 +289,11 @@ internal fun IrFunction.hasFunInterfaceParent(): Boolean { internal fun IrClass.getSingleAbstractMethod(): IrFunction { return declarations.filterIsInstance().single { it.modality == Modality.ABSTRACT } -} \ No newline at end of file +} + +fun IrGetValue.isAccessToObject(): Boolean { + val owner = this.symbol.owner + val expectedClass = this.type.classOrNull?.owner + if (expectedClass == null || !expectedClass.isObject) return false + return owner.origin == IrDeclarationOrigin.INSTANCE_RECEIVER || owner.name.asString() == "" +} diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/checker/IrCompileTimeChecker.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/checker/IrCompileTimeChecker.kt index 9daf80b403a..341bd8b63fe 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/checker/IrCompileTimeChecker.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/checker/IrCompileTimeChecker.kt @@ -11,6 +11,7 @@ import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.interpreter.accessesTopLevelOrObjectField import org.jetbrains.kotlin.ir.interpreter.fqName +import org.jetbrains.kotlin.ir.interpreter.isAccessToObject import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.visitors.IrElementVisitor @@ -111,7 +112,7 @@ class IrCompileTimeChecker( } override fun visitGetObjectValue(expression: IrGetObjectValue, data: Nothing?): Boolean { - // to get object value we need nothing but it will contain only fields with compile time annotation + // to get object value we need nothing, but it will contain only fields with compile time annotation return true } @@ -120,10 +121,7 @@ class IrCompileTimeChecker( } override fun visitGetValue(expression: IrGetValue, data: Nothing?): Boolean { - val owner = expression.symbol.owner - val parent = owner.parent as IrSymbolOwner - val isObjectReceiver = (parent as? IrClass)?.isObject == true && owner.origin == IrDeclarationOrigin.INSTANCE_RECEIVER - return visitedStack.contains(parent) || isObjectReceiver + return visitedStack.contains(expression.symbol.owner.parent) || expression.isAccessToObject() } override fun visitSetValue(expression: IrSetValue, data: Nothing?): Boolean { diff --git a/compiler/testData/codegen/box/annotations/objectConstValInAnnotationArgument.kt b/compiler/testData/codegen/box/annotations/objectConstValInAnnotationArgument.kt new file mode 100644 index 00000000000..f19920aab7d --- /dev/null +++ b/compiler/testData/codegen/box/annotations/objectConstValInAnnotationArgument.kt @@ -0,0 +1,14 @@ +// TARGET_BACKEND: JVM_IR +annotation class Key(val value: String) + +object Messanger { + const val DEFAULT_TEXT = "OK" + + fun message(@Key(value = DEFAULT_TEXT) text: String = DEFAULT_TEXT): String { + return text + } +} + +fun box(): String { + return Messanger.message() +} 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 875cb8a38d0..79c1fca84c4 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 @@ -262,6 +262,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/annotations/nestedClassesInAnnotations.kt"); } + @Test + @TestMetadata("objectConstValInAnnotationArgument.kt") + public void testObjectConstValInAnnotationArgument() throws Exception { + runTest("compiler/testData/codegen/box/annotations/objectConstValInAnnotationArgument.kt"); + } + @Test @TestMetadata("parameterAnnotationInDefaultImpls.kt") public void testParameterAnnotationInDefaultImpls() throws Exception {