From ce6e904b7059a0f97216f5a9c5a72fe76d325b7b Mon Sep 17 00:00:00 2001 From: Sergej Jaskiewicz Date: Mon, 24 Jul 2023 13:02:59 +0200 Subject: [PATCH] [klib] Don't forget to set signatures' descriptions before serialization Ensure that in IdSignatureBuilder hashId/hashIdAcc is only set together with the description. In 6142d75bb43de016958239688827a977c9cf90b9 we implemented setting descriptions when building signatures from descriptors, but forgot to do the same for building signatures from IR, resulting in missing descriptions in klibs. See also: KT-59486 --- .../signature/IdSignatureBuilder.kt | 32 ++++++++++++++---- .../signature/IdSignatureDescriptor.kt | 33 ++++++++----------- .../signature/IdSignatureSerializer.kt | 24 ++++++-------- 3 files changed, 49 insertions(+), 40 deletions(-) diff --git a/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/signature/IdSignatureBuilder.kt b/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/signature/IdSignatureBuilder.kt index a66f9a97bc5..7a0b0d99235 100644 --- a/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/signature/IdSignatureBuilder.kt +++ b/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/signature/IdSignatureBuilder.kt @@ -7,14 +7,15 @@ package org.jetbrains.kotlin.backend.common.serialization.signature import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.ir.util.IdSignature +import org.jetbrains.kotlin.ir.util.KotlinMangler import org.jetbrains.kotlin.name.FqName -abstract class IdSignatureBuilder { +abstract class IdSignatureBuilder> { protected var packageFqn: FqName = FqName.ROOT protected val classFqnSegments = mutableListOf() - protected var hashId: Long? = null - protected var hashIdAcc: Long? = null - protected var overridden: List? = null + private var hashId: Long? = null + private var hashIdAcc: Long? = null + protected var overridden: List? = null protected var mask = 0L protected var container: IdSignature? = null protected var description: String? = null @@ -23,7 +24,26 @@ abstract class IdSignatureBuilder { protected abstract val currentFileSignature: IdSignature.FileSignature? - protected abstract fun accept(d: D) + protected abstract val mangler: Mangler + + protected fun setHashIdAndDescriptionFor(declaration: Declaration, isPropertyAccessor: Boolean) { + mangler.run { + val mangledName = declaration.signatureString(compatibleMode = false) + val id = mangledName.hashMangle + setHashIdAndDescription(id, mangledName, isPropertyAccessor) + } + } + + protected fun setHashIdAndDescription(id: Long, description: String, isPropertyAccessor: Boolean) { + if (isPropertyAccessor) { + hashIdAcc = id + } else { + hashId = id + } + this.description = description + } + + protected abstract fun accept(d: Declaration) protected fun reset(resetContainer: Boolean = true) { this.packageFqn = FqName.ROOT @@ -118,7 +138,7 @@ abstract class IdSignatureBuilder { protected open fun platformSpecificAlias(descriptor: TypeAliasDescriptor) {} protected open fun platformSpecificPackage(descriptor: PackageFragmentDescriptor) {} - fun buildSignature(declaration: D): IdSignature { + fun buildSignature(declaration: Declaration): IdSignature { reset() accept(declaration) diff --git a/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/signature/IdSignatureDescriptor.kt b/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/signature/IdSignatureDescriptor.kt index d5adec9fd53..e6e942a3b68 100644 --- a/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/signature/IdSignatureDescriptor.kt +++ b/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/signature/IdSignatureDescriptor.kt @@ -20,21 +20,11 @@ open class IdSignatureDescriptor(override val mangler: KotlinMangler.DescriptorM protected open fun createSignatureBuilder(type: SpecialDeclarationType): DescriptorBasedSignatureBuilder = DescriptorBasedSignatureBuilder(type) protected open inner class DescriptorBasedSignatureBuilder(private val type: SpecialDeclarationType) : - IdSignatureBuilder(), + IdSignatureBuilder(), DeclarationDescriptorVisitor { - private fun setHashIdFor(declaration: DeclarationDescriptor, isPropertyAccessor: Boolean) { - mangler.run { - val mangledName = declaration.signatureString(compatibleMode = false) - val id = mangledName.hashMangle - if (isPropertyAccessor) { - hashIdAcc = id - } else { - hashId = id - } - description = mangledName - } - } + override val mangler: KotlinMangler.DescriptorMangler + get() = this@IdSignatureDescriptor.mangler override fun accept(d: DeclarationDescriptor) { d.accept(this, null) @@ -84,7 +74,7 @@ open class IdSignatureDescriptor(override val mangler: KotlinMangler.DescriptorM override fun visitFunctionDescriptor(descriptor: FunctionDescriptor, data: Nothing?) { collectParents(descriptor) - setHashIdFor(descriptor, isPropertyAccessor = false) + setHashIdAndDescriptionFor(descriptor, isPropertyAccessor = false) isTopLevelPrivate = isTopLevelPrivate or descriptor.isTopLevelPrivate setDescription(descriptor) setExpected(descriptor.isExpect) @@ -96,8 +86,11 @@ open class IdSignatureDescriptor(override val mangler: KotlinMangler.DescriptorM createContainer() classFqnSegments.add(MangleConstant.TYPE_PARAMETER_MARKER_NAME) - hashId = descriptor.index.toLong() - description = DescriptorRenderer.SHORT_NAMES_IN_TYPES.render(descriptor) + setHashIdAndDescription( + descriptor.index.toLong(), + DescriptorRenderer.SHORT_NAMES_IN_TYPES.render(descriptor), + isPropertyAccessor = false, + ) } override fun visitClassDescriptor(descriptor: ClassDescriptor, data: Nothing?) { @@ -128,7 +121,7 @@ open class IdSignatureDescriptor(override val mangler: KotlinMangler.DescriptorM override fun visitConstructorDescriptor(constructorDescriptor: ConstructorDescriptor, data: Nothing?) { collectParents(constructorDescriptor) - setHashIdFor(constructorDescriptor, isPropertyAccessor = false) + setHashIdAndDescriptionFor(constructorDescriptor, isPropertyAccessor = false) platformSpecificConstructor(constructorDescriptor) } @@ -144,7 +137,7 @@ open class IdSignatureDescriptor(override val mangler: KotlinMangler.DescriptorM collectParents(actualDeclaration) isTopLevelPrivate = isTopLevelPrivate or actualDeclaration.isTopLevelPrivate - setHashIdFor(actualDeclaration, isPropertyAccessor = false) + setHashIdAndDescriptionFor(actualDeclaration, isPropertyAccessor = false) setExpected(actualDeclaration.isExpect) platformSpecificProperty(actualDeclaration) if (type == SpecialDeclarationType.BACKING_FIELD) { @@ -161,7 +154,7 @@ open class IdSignatureDescriptor(override val mangler: KotlinMangler.DescriptorM override fun visitPropertyGetterDescriptor(descriptor: PropertyGetterDescriptor, data: Nothing?) { descriptor.correspondingProperty.accept(this, null) - setHashIdFor(descriptor, isPropertyAccessor = true) + setHashIdAndDescriptionFor(descriptor, isPropertyAccessor = true) classFqnSegments.add(descriptor.name.asString()) setExpected(descriptor.isExpect) platformSpecificGetter(descriptor) @@ -169,7 +162,7 @@ open class IdSignatureDescriptor(override val mangler: KotlinMangler.DescriptorM override fun visitPropertySetterDescriptor(descriptor: PropertySetterDescriptor, data: Nothing?) { descriptor.correspondingProperty.accept(this, null) - setHashIdFor(descriptor, isPropertyAccessor = true) + setHashIdAndDescriptionFor(descriptor, isPropertyAccessor = true) classFqnSegments.add(descriptor.name.asString()) setExpected(descriptor.isExpect) platformSpecificSetter(descriptor) diff --git a/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/signature/IdSignatureSerializer.kt b/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/signature/IdSignatureSerializer.kt index 4272e15bb01..344bae44c5c 100644 --- a/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/signature/IdSignatureSerializer.kt +++ b/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/signature/IdSignatureSerializer.kt @@ -60,9 +60,12 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat scopeCounter = 0 } - private inner class PublicIdSigBuilder : IdSignatureBuilder(), + private inner class PublicIdSigBuilder : IdSignatureBuilder(), IrElementVisitorVoid { + override val mangler: KotlinMangler.IrMangler + get() = this@PublicIdSignatureComputer.mangler + override val currentFileSignature: IdSignature.FileSignature? get() = currentFileSignatureX @@ -116,25 +119,19 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat setExpected(declaration.isExpect) } - // Note: `false` because `compatibleMode` is not applied to public signatures - private fun IrDeclarationWithName.hashId(): Long = mangler.run { signatureMangle(compatibleMode = false) } - override fun visitSimpleFunction(declaration: IrSimpleFunction) { val property = declaration.correspondingPropertySymbol if (property != null) { property.owner.acceptVoid(this) - val preservedId = declaration.hashId() if (container != null) { createContainer() - hashId = preservedId - } else { - hashIdAcc = preservedId } + setHashIdAndDescriptionFor(declaration, isPropertyAccessor = container == null) classFqnSegments.add(declaration.name.asString()) } else { collectParents(declaration) isTopLevelPrivate = isTopLevelPrivate || declaration.isTopLevelPrivate - hashId = declaration.hashId() + setHashIdAndDescriptionFor(declaration, isPropertyAccessor = false) setDescription(declaration) } setExpected(declaration.isExpect) @@ -142,7 +139,7 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat override fun visitConstructor(declaration: IrConstructor) { collectParents(declaration) - hashId = declaration.hashId() + setHashIdAndDescriptionFor(declaration, isPropertyAccessor = false) setExpected(declaration.isExpect) } @@ -153,7 +150,7 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat override fun visitProperty(declaration: IrProperty) { collectParents(declaration) isTopLevelPrivate = isTopLevelPrivate || declaration.isTopLevelPrivate - hashId = declaration.hashId() + setHashIdAndDescriptionFor(declaration, isPropertyAccessor = false) setExpected(declaration.isExpect) } @@ -181,8 +178,7 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat } else { classFqnSegments.add(MangleConstant.TYPE_PARAMETER_MARKER_NAME) } - hashId = declaration.index.toLong() - description = declaration.render() + setHashIdAndDescription(declaration.index.toLong(), declaration.render(), isPropertyAccessor = false) } override fun visitField(declaration: IrField) { @@ -196,7 +192,7 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat description = declaration.render() } else { collectParents(declaration) - hashId = declaration.hashId() + setHashIdAndDescriptionFor(declaration, isPropertyAccessor = false) } } }