JVM IR: copy property instead of field in MoveOrCopyCompanionObjectFieldsLowering

This way we're making sure that the copied field has some associated
property, where we can get the value of isConst flag from. That flag is
later used in StaticInitializersLowering to determine whether we need to
erase initializer of a field. The tests are unmuted because now the
initializer is correctly _not_ erased. (They were passing before
switching master to 1.4 because without
NoConstantValueAttributeForNonConstVals, we treated all static fields
with primitive/string values as const, and never erased initializers
because of that.)
This commit is contained in:
Alexander Udalov
2020-01-23 18:08:52 +01:00
parent e42a4b2fac
commit 00de5dae32
3 changed files with 23 additions and 14 deletions
@@ -12,6 +12,8 @@ import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.ir.replaceThisByStaticReference
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.ir.builders.declarations.addField
import org.jetbrains.kotlin.ir.builders.declarations.addProperty
import org.jetbrains.kotlin.ir.builders.declarations.buildField
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrAnonymousInitializerImpl
import org.jetbrains.kotlin.ir.expressions.*
@@ -70,7 +72,7 @@ private class MoveOrCopyCompanionObjectFieldsLowering(val context: JvmBackendCon
if (companionParent != null) {
for (declaration in declarations) {
if (declaration is IrProperty && declaration.isConst && declaration.hasPublicVisibility) {
copyConstField(declaration.backingField!!, companionParent)
copyConstProperty(declaration, companionParent)
}
}
}
@@ -102,15 +104,25 @@ private class MoveOrCopyCompanionObjectFieldsLowering(val context: JvmBackendCon
}
}
private fun copyConstField(oldField: IrField, newParent: IrClass) =
newParent.addField {
updateFrom(oldField)
name = oldField.name
isStatic = true
}.apply {
parent = newParent
annotations += oldField.annotations
initializer = with(oldField.initializer!!) { IrExpressionBodyImpl(startOffset, endOffset, (expression as IrConst<*>).copy()) }
private fun copyConstProperty(oldProperty: IrProperty, newParent: IrClass): IrProperty =
newParent.addProperty {
updateFrom(oldProperty)
name = oldProperty.name
isConst = true
}.also { property ->
val oldField = oldProperty.backingField!!
property.backingField = buildField {
updateFrom(oldField)
name = oldField.name
isStatic = true
}.apply {
parent = newParent
correspondingPropertySymbol = property.symbol
annotations += oldField.annotations
initializer = with(oldField.initializer!!) {
IrExpressionBodyImpl(startOffset, endOffset, (expression as IrConst<*>).copy())
}
}
}
}
@@ -1,6 +1,5 @@
// IGNORE_BACKEND_FIR: JVM_IR
// TARGET_BACKEND: JVM
// IGNORE_BACKEND: JVM_IR
// FILE: JavaClass.java
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
package test
annotation class AString(val value: String)
@@ -22,4 +20,4 @@ interface Test {
const val vfloat: Float = 1.3.toFloat()
}
}
}