[klib] Don't forget to set signatures' descriptions before serialization

Ensure that in IdSignatureBuilder hashId/hashIdAcc is only set
together with the description.

In 6142d75bb4 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
This commit is contained in:
Sergej Jaskiewicz
2023-07-24 13:02:59 +02:00
committed by Space Team
parent 2818957689
commit ce6e904b70
3 changed files with 49 additions and 40 deletions
@@ -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<D> {
abstract class IdSignatureBuilder<Declaration : Any, Mangler : KotlinMangler<Declaration>> {
protected var packageFqn: FqName = FqName.ROOT
protected val classFqnSegments = mutableListOf<String>()
protected var hashId: Long? = null
protected var hashIdAcc: Long? = null
protected var overridden: List<D>? = null
private var hashId: Long? = null
private var hashIdAcc: Long? = null
protected var overridden: List<Declaration>? = null
protected var mask = 0L
protected var container: IdSignature? = null
protected var description: String? = null
@@ -23,7 +24,26 @@ abstract class IdSignatureBuilder<D> {
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<D> {
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)
@@ -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<DeclarationDescriptor>(),
IdSignatureBuilder<DeclarationDescriptor, KotlinMangler.DescriptorMangler>(),
DeclarationDescriptorVisitor<Unit, Nothing?> {
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)
@@ -60,9 +60,12 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
scopeCounter = 0
}
private inner class PublicIdSigBuilder : IdSignatureBuilder<IrDeclaration>(),
private inner class PublicIdSigBuilder : IdSignatureBuilder<IrDeclaration, KotlinMangler.IrMangler>(),
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)
}
}
}