Expand the possibilities of object's interpretation

It is possible to create object in interpreter even if its
receiver symbol came from function parameter instead of object's class.
#KT-48174 Fixed
This commit is contained in:
Ivan Kylchik
2021-08-10 17:32:56 +03:00
committed by TeamCityServer
parent 492b8acf93
commit 2f2e608502
6 changed files with 40 additions and 10 deletions
@@ -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 {
@@ -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))
@@ -289,4 +289,11 @@ internal fun IrFunction.hasFunInterfaceParent(): Boolean {
internal fun IrClass.getSingleAbstractMethod(): IrFunction {
return declarations.filterIsInstance<IrSimpleFunction>().single { it.modality == Modality.ABSTRACT }
}
}
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() == "<this>"
}
@@ -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 {
@@ -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()
}
@@ -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 {