[PL] Halt compilation when there are linkage issues logged with the ERROR severity

^KT-61097
This commit is contained in:
Dmitriy Dolovov
2023-08-09 14:59:26 +02:00
committed by Space Team
parent 288163437d
commit 8be98da1d3
5 changed files with 33 additions and 8 deletions
@@ -135,7 +135,7 @@ object PartialLinkageUtils {
}
// A workaround for KT-58837 until KT-58904 is fixed. TODO: Merge with IrMessageLogger.
class PartialLinkageLogger(private val irLogger: IrMessageLogger, logLevel: PartialLinkageLogLevel) {
class PartialLinkageLogger(val irLogger: IrMessageLogger, val logLevel: PartialLinkageLogLevel) {
class Location(val moduleName: String, val filePath: String, val lineNumber: Int, val columnNumber: Int) {
fun render(): StringBuilder = StringBuilder().apply {
append(moduleName)
@@ -29,6 +29,10 @@ abstract class KotlinIrLinkerIssue {
}
}
object PartialLinkageErrorsLogged : KotlinIrLinkerIssue() {
override val errorMessage = "There are linkage errors reported by the partial linkage engine"
}
class UnexpectedUnboundIrSymbols(unboundSymbols: Set<IrSymbol>, whenDetected: String) : KotlinIrLinkerIssue() {
override val errorMessage = buildString {
// cause:
@@ -5,12 +5,14 @@
package org.jetbrains.kotlin.backend.common.linkage.partial
import org.jetbrains.kotlin.backend.common.linkage.issues.PartialLinkageErrorsLogged
import org.jetbrains.kotlin.backend.common.overrides.FakeOverrideBuilder
import org.jetbrains.kotlin.ir.IrBuiltIns
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.linkage.partial.PartialLinkageConfig
import org.jetbrains.kotlin.ir.linkage.partial.PartialLinkageLogLevel
import org.jetbrains.kotlin.ir.linkage.partial.PartialLinkageLogger
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.util.IrMessageLogger
@@ -29,7 +31,7 @@ else
internal class PartialLinkageSupportForLinkerImpl(
builtIns: IrBuiltIns,
allowErrorTypes: Boolean,
logger: PartialLinkageLogger
private val logger: PartialLinkageLogger
) : PartialLinkageSupportForLinker {
private val stubGenerator = MissingDeclarationStubGenerator(builtIns)
private val classifierExplorer = ClassifierExplorer(builtIns, stubGenerator, allowErrorTypes)
@@ -81,5 +83,10 @@ internal class PartialLinkageSupportForLinkerImpl(
// Patch the stubs which were not patched yet.
patcher.patchDeclarations(stubGenerator.grabDeclarationsToPatch())
// Make sure that there are no linkage issues that have been reported with the 'error' severity.
// If there are, abort the current compilation.
if (logger.logLevel == PartialLinkageLogLevel.ERROR && patcher.linkageIssuesLogged > 0)
PartialLinkageErrorsLogged.raiseIssue(logger.irLogger)
}
}
@@ -33,11 +33,22 @@ internal class PartialLinkageSupportForLoweringsImpl(
) : PartialLinkageSupportForLowerings {
override val isEnabled get() = true
// N.B. errorMessagesRendered is always >= than throwExpressionsGenerated.
var throwExpressionsGenerated = 0 // Track each generate `throw` expression.
/** To track the amount of rendered linkage issues. */
var linkageIssuesRendered = 0
private set
var errorMessagesRendered = 0 // Track each rendered error message.
/**
* To track the amount of logged linkage issues.
* Note that the following condition is always true: [linkageIssuesLogged] <= [linkageIssuesRendered].
*/
var linkageIssuesLogged = 0
private set
/**
* To track the amount of generated `throw` expressions.
* Note that the following condition is always true: [throwExpressionsGenerated] <= [linkageIssuesRendered].
*/
var throwExpressionsGenerated = 0
private set
override fun throwLinkageError(
@@ -51,7 +62,7 @@ internal class PartialLinkageSupportForLoweringsImpl(
else
renderAndLogLinkageError(partialLinkageCase, element, file) // Render + log with the appropriate severity.
throwExpressionsGenerated++ // Track each generate `throw` expression.
throwExpressionsGenerated++ // Track each generated `throw` expression.
return IrCallImpl(
startOffset = element.startOffset,
@@ -70,13 +81,14 @@ internal class PartialLinkageSupportForLoweringsImpl(
val errorMessage = renderLinkageError(partialLinkageCase)
val locationInSourceCode = file.computeLocationForOffset(element.startOffsetOfFirstDenotableIrElement())
linkageIssuesLogged++ // Track each logged linkage issue.
logger.log(errorMessage, locationInSourceCode)
return errorMessage
}
private fun renderLinkageError(partialLinkageCase: PartialLinkageCase): String {
errorMessagesRendered++ // Track each rendered error message.
linkageIssuesRendered++ // Track each rendered linkage issue.
return partialLinkageCase.renderLinkageError()
}
@@ -58,6 +58,8 @@ internal class PartiallyLinkedIrTreePatcher(
// Used only to generate IR expressions that throw linkage errors.
private val supportForLowerings by lazy { PartialLinkageSupportForLoweringsImpl(builtIns, logger) }
val linkageIssuesLogged get() = supportForLowerings.linkageIssuesLogged
fun shouldBeSkipped(declaration: IrDeclaration): Boolean = PLModule.determineModuleFor(declaration).shouldBeSkipped
fun patchModuleFragments(roots: Sequence<IrModuleFragment>) {
@@ -887,7 +889,7 @@ internal class PartiallyLinkedIrTreePatcher(
}
private inner class AnnotationChecker(currentFile: PLFile) : ExpressionTransformer(currentFile) {
private val currentErrorMessagesCount get() = supportForLowerings.errorMessagesRendered
private val currentErrorMessagesCount get() = supportForLowerings.linkageIssuesRendered
private val initialErrorMessagesCount = currentErrorMessagesCount // Memoize the number of PL errors generated to this moment.
var isUsableAnnotation = true