From 00de5dae326fde9e67679641ae7d40172d877128 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 23 Jan 2020 18:08:52 +0100 Subject: [PATCH] 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.) --- .../MoveCompanionObjectFieldsLowering.kt | 32 +++++++++++++------ ...ithKotlinPropertyFromInterfaceCompanion.kt | 1 - .../staticFields/AnnotationTrait.kt | 4 +-- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/MoveCompanionObjectFieldsLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/MoveCompanionObjectFieldsLowering.kt index 0caa0217a46..f8aaa8a8bf4 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/MoveCompanionObjectFieldsLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/MoveCompanionObjectFieldsLowering.kt @@ -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()) + } + } } } diff --git a/compiler/testData/codegen/box/annotations/annotationWithKotlinPropertyFromInterfaceCompanion.kt b/compiler/testData/codegen/box/annotations/annotationWithKotlinPropertyFromInterfaceCompanion.kt index 61eee0e1133..155078c10be 100644 --- a/compiler/testData/codegen/box/annotations/annotationWithKotlinPropertyFromInterfaceCompanion.kt +++ b/compiler/testData/codegen/box/annotations/annotationWithKotlinPropertyFromInterfaceCompanion.kt @@ -1,6 +1,5 @@ // IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM -// IGNORE_BACKEND: JVM_IR // FILE: JavaClass.java diff --git a/compiler/testData/compileJavaAgainstKotlin/staticFields/AnnotationTrait.kt b/compiler/testData/compileJavaAgainstKotlin/staticFields/AnnotationTrait.kt index 453a20ea927..e8f1ffac562 100644 --- a/compiler/testData/compileJavaAgainstKotlin/staticFields/AnnotationTrait.kt +++ b/compiler/testData/compileJavaAgainstKotlin/staticFields/AnnotationTrait.kt @@ -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() } -} \ No newline at end of file +}