[klib] Implement diagnostics for clashing KLIB signatures

Now, we detect clashing signatures during serialization to KLIB and
report a compiler error if two or more declarations have the same
`IdSignature`

For example, for the following code:
```kotlin
@Deprecated("", level = DeprecationLevel.HIDDEN)
fun foo(): String = ""

fun foo(): Int = 0
```

the compiler will produce this diagnostic:
```
e: main.kt:1:1 Platform declaration clash: The following declarations
       have the same KLIB signature (/foo|foo(){}[0]):
    fun foo(): String defined in root package
    fun foo(): Int defined in root package
e: main.kt:4:1 Platform declaration clash: The following declarations
       have the same KLIB signature (/foo|foo(){}[0]):
    fun foo(): String defined in root package
    fun foo(): Int defined in root package
```

Note that we report this diagnostic during serialization and not earlier
(e.g., in fir2ir) for more robustness, so ensure that we check
exactly the signatures that will be written to a KLIB.
If we later introduce some annotation for customizing a declaration's
signature (e.g., for preserving binary compatibility), this
diagnostic will continue to work as expected.

^KT-63670 Fixed
This commit is contained in:
Sergej Jaskiewicz
2023-12-15 20:05:02 +01:00
committed by Space Team
parent e0cb145c6b
commit eda30ff704
27 changed files with 366 additions and 98 deletions
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.klib
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import junit.framework.TestCase
import org.jetbrains.kotlin.KtDiagnosticReporterWithImplicitIrBasedContext
import org.jetbrains.kotlin.backend.common.CommonKLibResolver
import org.jetbrains.kotlin.ir.util.isExpect
import org.jetbrains.kotlin.backend.common.linkage.issues.checkNoUnboundSymbols
@@ -24,6 +25,8 @@ import org.jetbrains.kotlin.config.*
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.diagnostics.DiagnosticReporterFactory
import org.jetbrains.kotlin.incremental.components.LookupTracker
import org.jetbrains.kotlin.ir.AbstractIrGeneratorTestCase
import org.jetbrains.kotlin.ir.IrElement
@@ -139,16 +142,25 @@ abstract class AbstractKlibIrTextTestCase : CodegenTestCase() {
return serializePackageFragment(moduleDescriptor, memberScope, ktFile.packageFqName)
}
protected fun serializeModule(irModuleFragment: IrModuleFragment, bindingContext: BindingContext, stdlib: KotlinLibrary, containsErrorCode: Boolean): String {
private fun serializeModule(
irModuleFragment: IrModuleFragment,
bindingContext: BindingContext,
stdlib: KotlinLibrary,
containsErrorCode: Boolean,
): String {
val ktFiles = myFiles.psiFiles
val serializedIr = JsIrModuleSerializer(
IrMessageLogger.None,
irModuleFragment.irBuiltins,
CompatibilityMode.CURRENT,
false,
emptyList(),
KtDiagnosticReporterWithImplicitIrBasedContext(
DiagnosticReporterFactory.createPendingReporter(),
myEnvironment.configuration.languageVersionSettings,
).serializedIrModule(irModuleFragment)
),
IrMessageLogger.None,
irModuleFragment.irBuiltins,
CompatibilityMode.CURRENT,
false,
emptyList(),
myEnvironment.configuration.languageVersionSettings,
).serializedIrModule(irModuleFragment)
val moduleDescriptor = irModuleFragment.descriptor
val metadataSerializer = klibMetadataIncrementalSerializer(myEnvironment.configuration, myEnvironment.project, containsErrorCode)