K2: report INCOMPATIBLE_CLASS in JVM

#KT-60795
This commit is contained in:
Alexander Udalov
2023-08-01 01:12:34 +02:00
committed by Space Team
parent db8fb30343
commit 7d60b5df43
19 changed files with 262 additions and 29 deletions
@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.diagnostics.KtPsiDiagnostic
import org.jetbrains.kotlin.diagnostics.Severity
import org.jetbrains.kotlin.diagnostics.impl.BaseDiagnosticsCollector
import org.jetbrains.kotlin.diagnostics.rendering.RootDiagnosticRendererFactory
import org.jetbrains.kotlin.fir.analysis.diagnostics.jvm.FirJvmErrors
import java.io.Closeable
import java.io.File
import java.io.InputStreamReader
@@ -24,6 +25,14 @@ object FirDiagnosticsCompilerResultsReporter {
): Boolean {
return reportByFile(diagnosticsCollector) { diagnostic, location ->
reportDiagnosticToMessageCollector(diagnostic, location, messageCollector, renderDiagnosticName)
}.also {
AnalyzerWithCompilerReport.reportSpecialErrors(
diagnosticsCollector.diagnostics.any { it.factory == FirJvmErrors.INCOMPATIBLE_CLASS },
hasPrereleaseClasses = false, // TODO (KT-60780): missing PRE_RELEASE_CLASS
hasUnstableClasses = false, // TODO (KT-61598): report FIR_COMPILED_CLASS and IR_WITH_UNSTABLE_ABI_COMPILED_CLASS
hasFirUnstableClasses = false, // TODO (KT-61598): report FIR_COMPILED_CLASS and IR_WITH_UNSTABLE_ABI_COMPILED_CLASS
messageCollector,
)
}
}
@@ -77,22 +86,9 @@ object FirDiagnosticsCompilerResultsReporter {
}
}
}
// TODO: for uncommenting, see comment in reportSpecialErrors
// reportSpecialErrors(diagnostics)
return hasErrors
}
@Suppress("UNUSED_PARAMETER", "unused")
private fun reportSpecialErrors(diagnostics: Collection<KtDiagnostic>) {
/*
* TODO: handle next diagnostics when they will be supported in FIR:
* - INCOMPATIBLE_CLASS
* - PRE_RELEASE_CLASS
* - IR_WITH_UNSTABLE_ABI_COMPILED_CLASS
* - FIR_COMPILED_CLASS
*/
}
private fun reportDiagnosticToMessageCollector(
diagnostic: KtDiagnostic,
location: CompilerMessageSourceLocation,
@@ -180,9 +180,25 @@ class AnalyzerWithCompilerReport(
messageCollector: MessageCollector,
renderInternalDiagnosticName: Boolean
): Boolean {
val hasErrors = reportDiagnostics(diagnostics, DefaultDiagnosticReporter(messageCollector), renderInternalDiagnosticName)
return reportDiagnostics(diagnostics, DefaultDiagnosticReporter(messageCollector), renderInternalDiagnosticName).also {
reportSpecialErrors(
diagnostics.any { it.factory == Errors.INCOMPATIBLE_CLASS },
diagnostics.any { it.factory == Errors.PRE_RELEASE_CLASS },
diagnostics.any { it.factory == Errors.IR_WITH_UNSTABLE_ABI_COMPILED_CLASS },
diagnostics.any { it.factory == Errors.FIR_COMPILED_CLASS },
messageCollector,
)
}
}
if (diagnostics.any { it.factory == Errors.INCOMPATIBLE_CLASS }) {
fun reportSpecialErrors(
hasIncompatibleClasses: Boolean,
hasPrereleaseClasses: Boolean,
hasUnstableClasses: Boolean,
hasFirUnstableClasses: Boolean,
messageCollector: MessageCollector,
) {
if (hasIncompatibleClasses) {
messageCollector.report(
ERROR,
"Incompatible classes were found in dependencies. " +
@@ -190,7 +206,7 @@ class AnalyzerWithCompilerReport(
)
}
if (diagnostics.any { it.factory == Errors.PRE_RELEASE_CLASS }) {
if (hasPrereleaseClasses) {
messageCollector.report(
ERROR,
"Pre-release classes were found in dependencies. " +
@@ -199,7 +215,7 @@ class AnalyzerWithCompilerReport(
)
}
if (diagnostics.any { it.factory == Errors.IR_WITH_UNSTABLE_ABI_COMPILED_CLASS }) {
if (hasUnstableClasses) {
messageCollector.report(
ERROR,
"Classes compiled by an unstable version of the Kotlin compiler were found in dependencies. " +
@@ -207,15 +223,13 @@ class AnalyzerWithCompilerReport(
)
}
if (diagnostics.any { it.factory == Errors.FIR_COMPILED_CLASS }) {
if (hasFirUnstableClasses) {
messageCollector.report(
ERROR,
"Classes compiled by the new Kotlin compiler frontend were found in dependencies. " +
"Remove them from the classpath or use '-Xallow-unstable-dependencies' to suppress errors"
)
}
return hasErrors
}
fun reportSyntaxErrors(file: PsiElement, reporter: DiagnosticMessageReporter): SyntaxErrorReport {