[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.descriptors.*
import org.jetbrains.kotlin.ir.util.IdSignature import org.jetbrains.kotlin.ir.util.IdSignature
import org.jetbrains.kotlin.ir.util.KotlinMangler
import org.jetbrains.kotlin.name.FqName 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 var packageFqn: FqName = FqName.ROOT
protected val classFqnSegments = mutableListOf<String>() protected val classFqnSegments = mutableListOf<String>()
protected var hashId: Long? = null private var hashId: Long? = null
protected var hashIdAcc: Long? = null private var hashIdAcc: Long? = null
protected var overridden: List<D>? = null protected var overridden: List<Declaration>? = null
protected var mask = 0L protected var mask = 0L
protected var container: IdSignature? = null protected var container: IdSignature? = null
protected var description: String? = null protected var description: String? = null
@@ -23,7 +24,26 @@ abstract class IdSignatureBuilder<D> {
protected abstract val currentFileSignature: IdSignature.FileSignature? 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) { protected fun reset(resetContainer: Boolean = true) {
this.packageFqn = FqName.ROOT this.packageFqn = FqName.ROOT
@@ -118,7 +138,7 @@ abstract class IdSignatureBuilder<D> {
protected open fun platformSpecificAlias(descriptor: TypeAliasDescriptor) {} protected open fun platformSpecificAlias(descriptor: TypeAliasDescriptor) {}
protected open fun platformSpecificPackage(descriptor: PackageFragmentDescriptor) {} protected open fun platformSpecificPackage(descriptor: PackageFragmentDescriptor) {}
fun buildSignature(declaration: D): IdSignature { fun buildSignature(declaration: Declaration): IdSignature {
reset() reset()
accept(declaration) 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 fun createSignatureBuilder(type: SpecialDeclarationType): DescriptorBasedSignatureBuilder = DescriptorBasedSignatureBuilder(type)
protected open inner class DescriptorBasedSignatureBuilder(private val type: SpecialDeclarationType) : protected open inner class DescriptorBasedSignatureBuilder(private val type: SpecialDeclarationType) :
IdSignatureBuilder<DeclarationDescriptor>(), IdSignatureBuilder<DeclarationDescriptor, KotlinMangler.DescriptorMangler>(),
DeclarationDescriptorVisitor<Unit, Nothing?> { DeclarationDescriptorVisitor<Unit, Nothing?> {
private fun setHashIdFor(declaration: DeclarationDescriptor, isPropertyAccessor: Boolean) { override val mangler: KotlinMangler.DescriptorMangler
mangler.run { get() = this@IdSignatureDescriptor.mangler
val mangledName = declaration.signatureString(compatibleMode = false)
val id = mangledName.hashMangle
if (isPropertyAccessor) {
hashIdAcc = id
} else {
hashId = id
}
description = mangledName
}
}
override fun accept(d: DeclarationDescriptor) { override fun accept(d: DeclarationDescriptor) {
d.accept(this, null) d.accept(this, null)
@@ -84,7 +74,7 @@ open class IdSignatureDescriptor(override val mangler: KotlinMangler.DescriptorM
override fun visitFunctionDescriptor(descriptor: FunctionDescriptor, data: Nothing?) { override fun visitFunctionDescriptor(descriptor: FunctionDescriptor, data: Nothing?) {
collectParents(descriptor) collectParents(descriptor)
setHashIdFor(descriptor, isPropertyAccessor = false) setHashIdAndDescriptionFor(descriptor, isPropertyAccessor = false)
isTopLevelPrivate = isTopLevelPrivate or descriptor.isTopLevelPrivate isTopLevelPrivate = isTopLevelPrivate or descriptor.isTopLevelPrivate
setDescription(descriptor) setDescription(descriptor)
setExpected(descriptor.isExpect) setExpected(descriptor.isExpect)
@@ -96,8 +86,11 @@ open class IdSignatureDescriptor(override val mangler: KotlinMangler.DescriptorM
createContainer() createContainer()
classFqnSegments.add(MangleConstant.TYPE_PARAMETER_MARKER_NAME) classFqnSegments.add(MangleConstant.TYPE_PARAMETER_MARKER_NAME)
hashId = descriptor.index.toLong() setHashIdAndDescription(
description = DescriptorRenderer.SHORT_NAMES_IN_TYPES.render(descriptor) descriptor.index.toLong(),
DescriptorRenderer.SHORT_NAMES_IN_TYPES.render(descriptor),
isPropertyAccessor = false,
)
} }
override fun visitClassDescriptor(descriptor: ClassDescriptor, data: Nothing?) { 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?) { override fun visitConstructorDescriptor(constructorDescriptor: ConstructorDescriptor, data: Nothing?) {
collectParents(constructorDescriptor) collectParents(constructorDescriptor)
setHashIdFor(constructorDescriptor, isPropertyAccessor = false) setHashIdAndDescriptionFor(constructorDescriptor, isPropertyAccessor = false)
platformSpecificConstructor(constructorDescriptor) platformSpecificConstructor(constructorDescriptor)
} }
@@ -144,7 +137,7 @@ open class IdSignatureDescriptor(override val mangler: KotlinMangler.DescriptorM
collectParents(actualDeclaration) collectParents(actualDeclaration)
isTopLevelPrivate = isTopLevelPrivate or actualDeclaration.isTopLevelPrivate isTopLevelPrivate = isTopLevelPrivate or actualDeclaration.isTopLevelPrivate
setHashIdFor(actualDeclaration, isPropertyAccessor = false) setHashIdAndDescriptionFor(actualDeclaration, isPropertyAccessor = false)
setExpected(actualDeclaration.isExpect) setExpected(actualDeclaration.isExpect)
platformSpecificProperty(actualDeclaration) platformSpecificProperty(actualDeclaration)
if (type == SpecialDeclarationType.BACKING_FIELD) { if (type == SpecialDeclarationType.BACKING_FIELD) {
@@ -161,7 +154,7 @@ open class IdSignatureDescriptor(override val mangler: KotlinMangler.DescriptorM
override fun visitPropertyGetterDescriptor(descriptor: PropertyGetterDescriptor, data: Nothing?) { override fun visitPropertyGetterDescriptor(descriptor: PropertyGetterDescriptor, data: Nothing?) {
descriptor.correspondingProperty.accept(this, null) descriptor.correspondingProperty.accept(this, null)
setHashIdFor(descriptor, isPropertyAccessor = true) setHashIdAndDescriptionFor(descriptor, isPropertyAccessor = true)
classFqnSegments.add(descriptor.name.asString()) classFqnSegments.add(descriptor.name.asString())
setExpected(descriptor.isExpect) setExpected(descriptor.isExpect)
platformSpecificGetter(descriptor) platformSpecificGetter(descriptor)
@@ -169,7 +162,7 @@ open class IdSignatureDescriptor(override val mangler: KotlinMangler.DescriptorM
override fun visitPropertySetterDescriptor(descriptor: PropertySetterDescriptor, data: Nothing?) { override fun visitPropertySetterDescriptor(descriptor: PropertySetterDescriptor, data: Nothing?) {
descriptor.correspondingProperty.accept(this, null) descriptor.correspondingProperty.accept(this, null)
setHashIdFor(descriptor, isPropertyAccessor = true) setHashIdAndDescriptionFor(descriptor, isPropertyAccessor = true)
classFqnSegments.add(descriptor.name.asString()) classFqnSegments.add(descriptor.name.asString())
setExpected(descriptor.isExpect) setExpected(descriptor.isExpect)
platformSpecificSetter(descriptor) platformSpecificSetter(descriptor)
@@ -60,9 +60,12 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
scopeCounter = 0 scopeCounter = 0
} }
private inner class PublicIdSigBuilder : IdSignatureBuilder<IrDeclaration>(), private inner class PublicIdSigBuilder : IdSignatureBuilder<IrDeclaration, KotlinMangler.IrMangler>(),
IrElementVisitorVoid { IrElementVisitorVoid {
override val mangler: KotlinMangler.IrMangler
get() = this@PublicIdSignatureComputer.mangler
override val currentFileSignature: IdSignature.FileSignature? override val currentFileSignature: IdSignature.FileSignature?
get() = currentFileSignatureX get() = currentFileSignatureX
@@ -116,25 +119,19 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
setExpected(declaration.isExpect) 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) { override fun visitSimpleFunction(declaration: IrSimpleFunction) {
val property = declaration.correspondingPropertySymbol val property = declaration.correspondingPropertySymbol
if (property != null) { if (property != null) {
property.owner.acceptVoid(this) property.owner.acceptVoid(this)
val preservedId = declaration.hashId()
if (container != null) { if (container != null) {
createContainer() createContainer()
hashId = preservedId
} else {
hashIdAcc = preservedId
} }
setHashIdAndDescriptionFor(declaration, isPropertyAccessor = container == null)
classFqnSegments.add(declaration.name.asString()) classFqnSegments.add(declaration.name.asString())
} else { } else {
collectParents(declaration) collectParents(declaration)
isTopLevelPrivate = isTopLevelPrivate || declaration.isTopLevelPrivate isTopLevelPrivate = isTopLevelPrivate || declaration.isTopLevelPrivate
hashId = declaration.hashId() setHashIdAndDescriptionFor(declaration, isPropertyAccessor = false)
setDescription(declaration) setDescription(declaration)
} }
setExpected(declaration.isExpect) setExpected(declaration.isExpect)
@@ -142,7 +139,7 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
override fun visitConstructor(declaration: IrConstructor) { override fun visitConstructor(declaration: IrConstructor) {
collectParents(declaration) collectParents(declaration)
hashId = declaration.hashId() setHashIdAndDescriptionFor(declaration, isPropertyAccessor = false)
setExpected(declaration.isExpect) setExpected(declaration.isExpect)
} }
@@ -153,7 +150,7 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
override fun visitProperty(declaration: IrProperty) { override fun visitProperty(declaration: IrProperty) {
collectParents(declaration) collectParents(declaration)
isTopLevelPrivate = isTopLevelPrivate || declaration.isTopLevelPrivate isTopLevelPrivate = isTopLevelPrivate || declaration.isTopLevelPrivate
hashId = declaration.hashId() setHashIdAndDescriptionFor(declaration, isPropertyAccessor = false)
setExpected(declaration.isExpect) setExpected(declaration.isExpect)
} }
@@ -181,8 +178,7 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
} else { } else {
classFqnSegments.add(MangleConstant.TYPE_PARAMETER_MARKER_NAME) classFqnSegments.add(MangleConstant.TYPE_PARAMETER_MARKER_NAME)
} }
hashId = declaration.index.toLong() setHashIdAndDescription(declaration.index.toLong(), declaration.render(), isPropertyAccessor = false)
description = declaration.render()
} }
override fun visitField(declaration: IrField) { override fun visitField(declaration: IrField) {
@@ -196,7 +192,7 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
description = declaration.render() description = declaration.render()
} else { } else {
collectParents(declaration) collectParents(declaration)
hashId = declaration.hashId() setHashIdAndDescriptionFor(declaration, isPropertyAccessor = false)
} }
} }
} }