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:
+22
-10
@@ -12,6 +12,8 @@ import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
|
|||||||
import org.jetbrains.kotlin.backend.jvm.ir.replaceThisByStaticReference
|
import org.jetbrains.kotlin.backend.jvm.ir.replaceThisByStaticReference
|
||||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||||
import org.jetbrains.kotlin.ir.builders.declarations.addField
|
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.*
|
||||||
import org.jetbrains.kotlin.ir.declarations.impl.IrAnonymousInitializerImpl
|
import org.jetbrains.kotlin.ir.declarations.impl.IrAnonymousInitializerImpl
|
||||||
import org.jetbrains.kotlin.ir.expressions.*
|
import org.jetbrains.kotlin.ir.expressions.*
|
||||||
@@ -70,7 +72,7 @@ private class MoveOrCopyCompanionObjectFieldsLowering(val context: JvmBackendCon
|
|||||||
if (companionParent != null) {
|
if (companionParent != null) {
|
||||||
for (declaration in declarations) {
|
for (declaration in declarations) {
|
||||||
if (declaration is IrProperty && declaration.isConst && declaration.hasPublicVisibility) {
|
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) =
|
private fun copyConstProperty(oldProperty: IrProperty, newParent: IrClass): IrProperty =
|
||||||
newParent.addField {
|
newParent.addProperty {
|
||||||
updateFrom(oldField)
|
updateFrom(oldProperty)
|
||||||
name = oldField.name
|
name = oldProperty.name
|
||||||
isStatic = true
|
isConst = true
|
||||||
}.apply {
|
}.also { property ->
|
||||||
parent = newParent
|
val oldField = oldProperty.backingField!!
|
||||||
annotations += oldField.annotations
|
property.backingField = buildField {
|
||||||
initializer = with(oldField.initializer!!) { IrExpressionBodyImpl(startOffset, endOffset, (expression as IrConst<*>).copy()) }
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Vendored
-1
@@ -1,6 +1,5 @@
|
|||||||
// IGNORE_BACKEND_FIR: JVM_IR
|
// IGNORE_BACKEND_FIR: JVM_IR
|
||||||
// TARGET_BACKEND: JVM
|
// TARGET_BACKEND: JVM
|
||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
|
|
||||||
// FILE: JavaClass.java
|
// FILE: JavaClass.java
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
|
|
||||||
package test
|
package test
|
||||||
|
|
||||||
annotation class AString(val value: String)
|
annotation class AString(val value: String)
|
||||||
|
|||||||
Reference in New Issue
Block a user