IrInterpreter: interpret IrGetField properly

This commit is contained in:
Mikhail Glukhikh
2021-01-25 15:58:09 +03:00
parent b311e95487
commit 76c2288e21
7 changed files with 51 additions and 0 deletions
@@ -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 {
@@ -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())
@@ -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)
+18
View File
@@ -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
@@ -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 {
@@ -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 {
@@ -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");