[klib] Fix exception for clashing signatures from different modules

If we encounter a declaration in the current module whose signature
is the same as that of a declaration in another module which we happen
to also reference from the current module, don't report any errors,
just like we don't do it in Kotlin/JVM. This leaves the user in the KLIB
hell situation, but this is intentional, because otherwise a legitimate
change like moving a declaration to another module and marking
the original one as `@Deprecated("", level = DeprecationLevel.HIDDEN)`
would lead to a error, and we don't want that.

Also, don't try to show the diagnostics on a declaration that doesn't
have an IrFile.

^KT-65063 Fixed
This commit is contained in:
Sergej Jaskiewicz
2024-01-18 15:58:50 +01:00
committed by Space Team
parent a5c8ecae0b
commit 6900e20096
8 changed files with 62 additions and 14 deletions
@@ -23,7 +23,7 @@ class IdSignatureClashDetector : SignatureClashDetector<IdSignature, IrDeclarati
SerializationErrors.CONFLICTING_KLIB_SIGNATURES_ERROR,
declarations,
ConflictingKlibSignaturesData(signature, declarations),
reportOnIfSynthetic = { it.parentClassOrNull ?: it.file },
reportOnIfSynthetic = { it.parentClassOrNull },
)
}
}
@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.ir.IrBuiltIns
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrSymbolOwner
import org.jetbrains.kotlin.ir.declarations.lazy.IrLazyDeclarationBase
import org.jetbrains.kotlin.ir.util.IdSignature
import org.jetbrains.kotlin.ir.util.KotlinMangler
import org.jetbrains.kotlin.ir.util.render
@@ -25,14 +26,14 @@ abstract class GlobalDeclarationTable(private val mangler: KotlinMangler.IrMangl
protected fun loadKnownBuiltins(builtIns: IrBuiltIns) {
builtIns.knownBuiltins.forEach {
val symbol = (it as IrSymbolOwner).symbol
table[it] = symbol.signature!!.also { id -> clashDetector.trackDeclaration(it, id) }
table[it] = symbol.signature!!.also { id -> clashDetector.trackDeclarationIfInCurrentModule(it, id) }
}
}
open fun computeSignatureByDeclaration(declaration: IrDeclaration, compatibleMode: Boolean): IdSignature {
return table.getOrPut(declaration) {
publicIdSignatureComputer.composePublicIdSignature(declaration, compatibleMode).also {
clashDetector.trackDeclaration(declaration, it)
clashDetector.trackDeclarationIfInCurrentModule(declaration, it)
}
}
}
@@ -40,6 +41,17 @@ abstract class GlobalDeclarationTable(private val mangler: KotlinMangler.IrMangl
fun isExportedDeclaration(declaration: IrDeclaration, compatibleMode: Boolean): Boolean = with(mangler) { declaration.isExported(compatibleMode) }
}
private fun IdSignatureClashDetector.trackDeclarationIfInCurrentModule(declaration: IrDeclaration, signature: IdSignature) {
// Only count signature clashes on the declarations declared in the module being serialized.
// If there is a declaration in an external module with the same signature as some declaration in the current module,
// so be it (for now).
// Note that when we are serializing a module into a KLIB, the declarations from other modules always come in the form of Lazy IR,
// so this check is enough to ensure that this declaration is declared in the module currently being serialized.
if (declaration !is IrLazyDeclarationBase) {
trackDeclaration(declaration, signature)
}
}
open class DeclarationTable(globalTable: GlobalDeclarationTable) {
protected val table = hashMapOf<IrDeclaration, IdSignature>()
protected open val globalDeclarationTable: GlobalDeclarationTable = globalTable