From b03e50129eea445515b59cf086a80c553e8e8c89 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Sat, 24 Jun 2023 01:05:24 +0200 Subject: [PATCH] Tests: fix handling of diag.txt files in diagnostic tests with backend Also take into account diagnostics reported via GenerationState.collectedExtraJvmDiagnostics, and render them in the similar format to KtDiagnostic. Don't make the test pass if the reported list of diagnostics is empty, because that defeats the purpose of the test. --- .../indirectInlineCycle_ir.diag.txt | 9 ++-- .../inlineCycle_ir.diag.txt | 9 ++-- .../suspendInlineCycle_ir.diag.txt | 9 ++-- .../handlers/JvmBackendDiagnosticsHandler.kt | 54 ++++++++++--------- 4 files changed, 38 insertions(+), 43 deletions(-) diff --git a/compiler/testData/diagnostics/testsWithJvmBackend/indirectInlineCycle_ir.diag.txt b/compiler/testData/diagnostics/testsWithJvmBackend/indirectInlineCycle_ir.diag.txt index fe28b9dec90..470f3ba8e86 100644 --- a/compiler/testData/diagnostics/testsWithJvmBackend/indirectInlineCycle_ir.diag.txt +++ b/compiler/testData/diagnostics/testsWithJvmBackend/indirectInlineCycle_ir.diag.txt @@ -1,6 +1,3 @@ -/indirectInlineCycle_ir.kt:8:24: error: the 'inlineFun2' invocation is a part of inline cycle - fun method() { inlineFun2(p) } - ^ -/indirectInlineCycle_ir.kt:13:5: error: the 'inlineFun1' invocation is a part of inline cycle - inlineFun1(p) - ^ +/indirectInlineCycle_ir.kt:6:24: error: The 'inlineFun2' invocation is a part of inline cycle + +/indirectInlineCycle_ir.kt:11:5: error: The 'inlineFun1' invocation is a part of inline cycle diff --git a/compiler/testData/diagnostics/testsWithJvmBackend/inlineCycle_ir.diag.txt b/compiler/testData/diagnostics/testsWithJvmBackend/inlineCycle_ir.diag.txt index 8c763cd207d..6c9de85d6c2 100644 --- a/compiler/testData/diagnostics/testsWithJvmBackend/inlineCycle_ir.diag.txt +++ b/compiler/testData/diagnostics/testsWithJvmBackend/inlineCycle_ir.diag.txt @@ -1,6 +1,3 @@ -/inlineCycle_ir.kt:8:5: error: the 'inlineFun2' invocation is a part of inline cycle - inlineFun2(p) - ^ -/inlineCycle_ir.kt:13:5: error: the 'inlineFun1' invocation is a part of inline cycle - inlineFun1(p) - ^ +/inlineCycle_ir.kt:6:5: error: The 'inlineFun2' invocation is a part of inline cycle + +/inlineCycle_ir.kt:11:5: error: The 'inlineFun1' invocation is a part of inline cycle diff --git a/compiler/testData/diagnostics/testsWithJvmBackend/suspendInlineCycle_ir.diag.txt b/compiler/testData/diagnostics/testsWithJvmBackend/suspendInlineCycle_ir.diag.txt index aca5206c7fb..696f64fbcd2 100644 --- a/compiler/testData/diagnostics/testsWithJvmBackend/suspendInlineCycle_ir.diag.txt +++ b/compiler/testData/diagnostics/testsWithJvmBackend/suspendInlineCycle_ir.diag.txt @@ -1,6 +1,3 @@ -/suspendInlineCycle_ir.kt:8:5: error: the 'inlineFun2' invocation is a part of inline cycle - inlineFun2(p) - ^ -/suspendInlineCycle_ir.kt:13:5: error: the 'inlineFun1' invocation is a part of inline cycle - inlineFun1(p) - ^ +/suspendInlineCycle_ir.kt:6:5: error: The 'inlineFun2' invocation is a part of inline cycle + +/suspendInlineCycle_ir.kt:11:5: error: The 'inlineFun1' invocation is a part of inline cycle diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/JvmBackendDiagnosticsHandler.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/JvmBackendDiagnosticsHandler.kt index 7185f177692..76bdc4d1c5d 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/JvmBackendDiagnosticsHandler.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/JvmBackendDiagnosticsHandler.kt @@ -5,18 +5,21 @@ package org.jetbrains.kotlin.test.backend.handlers +import com.intellij.openapi.util.TextRange import com.intellij.openapi.util.io.FileUtil import org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport +import org.jetbrains.kotlin.codeMetaInfo.model.DiagnosticCodeMetaInfo import org.jetbrains.kotlin.diagnostics.DiagnosticUtils -import org.jetbrains.kotlin.diagnostics.KtDiagnostic -import org.jetbrains.kotlin.diagnostics.PsiDiagnosticUtils +import org.jetbrains.kotlin.diagnostics.Severity import org.jetbrains.kotlin.diagnostics.impl.BaseDiagnosticsCollector +import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages import org.jetbrains.kotlin.fir.psi import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.resolve.jvm.diagnostics.KtDefaultJvmErrorMessages import org.jetbrains.kotlin.test.directives.DiagnosticsDirectives import org.jetbrains.kotlin.test.frontend.classic.handlers.ClassicDiagnosticReporter import org.jetbrains.kotlin.test.frontend.classic.handlers.withNewInferenceModeEnabled +import org.jetbrains.kotlin.test.frontend.fir.handlers.FirDiagnosticCodeMetaInfo import org.jetbrains.kotlin.test.frontend.fir.handlers.toMetaInfos import org.jetbrains.kotlin.test.model.BinaryArtifacts import org.jetbrains.kotlin.test.model.FrontendKinds @@ -35,7 +38,7 @@ class JvmBackendDiagnosticsHandler(testServices: TestServices) : JvmBinaryArtifa override fun processModule(module: TestModule, info: BinaryArtifacts.Jvm) { reportDiagnostics(module, info) reportKtDiagnostics(module, info) - checkFullDiagnosticRender(module, info) + checkFullDiagnosticRender(module) } private fun getKtFiles(module: TestModule): Map { @@ -49,12 +52,6 @@ class JvmBackendDiagnosticsHandler(testServices: TestServices) : JvmBinaryArtifa override fun processAfterAllModules(someAssertionWasFailed: Boolean) {} - private fun getLineAndColumnRange(diagnostic: KtDiagnostic): PsiDiagnosticUtils.LineAndColumnRange { - val file = diagnostic.psiElement.containingFile - val textRanges = diagnostic.textRanges - return DiagnosticUtils.getLineAndColumnRange(file, textRanges) - } - private fun reportDiagnostics(module: TestModule, info: BinaryArtifacts.Jvm) { val testFileToKtFileMap = getKtFiles(module) val ktFileToTestFileMap = testFileToKtFileMap.entries.associate { it.value to it.key } @@ -86,29 +83,36 @@ class JvmBackendDiagnosticsHandler(testServices: TestServices) : JvmBinaryArtifa } } - private fun checkFullDiagnosticRender(module: TestModule, info: BinaryArtifacts.Jvm) { + private fun checkFullDiagnosticRender(module: TestModule) { if (DiagnosticsDirectives.RENDER_DIAGNOSTICS_FULL_TEXT !in module.directives) return val testFileToKtFileMap = getKtFiles(module) - val generationState = info.classFileFactory.generationState - val ktDiagnosticReporter = generationState.diagnosticReporter as BaseDiagnosticsCollector val reportedDiagnostics = mutableListOf() - for (ktFile in testFileToKtFileMap.values) { - val ktDiagnostics = ktDiagnosticReporter.diagnosticsByFilePath[ktFile.virtualFilePath] ?: continue - ktDiagnostics.forEach { - val severity = AnalyzerWithCompilerReport.convertSeverity(it.severity).toString().toLowerCaseAsciiOnly() - val message = KtDefaultJvmErrorMessages.MAP[it.factory]?.render(it) - val position = getLineAndColumnRange(it).start - reportedDiagnostics += "/${ktFile.name}:${position.line}:${position.column}: $severity: $message" + for ((testFile, ktFile) in testFileToKtFileMap) { + for (metaInfo in testServices.globalMetadataInfoHandler.getReportedMetaInfosForFile(testFile).sortedBy { it.start }) { + when (metaInfo) { + is DiagnosticCodeMetaInfo -> metaInfo.diagnostic.let { + val message = DefaultErrorMessages.render(it) + reportedDiagnostics += renderDiagnosticMessage(ktFile, it.severity, message, it.textRanges) + } + is FirDiagnosticCodeMetaInfo -> metaInfo.diagnostic.let { + val message = KtDefaultJvmErrorMessages.MAP[it.factory]?.render(it) + reportedDiagnostics += renderDiagnosticMessage(ktFile, it.severity, message, it.textRanges) + } + } } } - if (reportedDiagnostics.isNotEmpty()) { - testServices.assertions.assertEqualsToFile( - File(FileUtil.getNameWithoutExtension(testFileToKtFileMap.keys.first().originalFile.absolutePath) + ".diag.txt"), - reportedDiagnostics.joinToString(separator = "\n\n") - ) - } + testServices.assertions.assertEqualsToFile( + File(FileUtil.getNameWithoutExtension(testFileToKtFileMap.keys.first().originalFile.absolutePath) + ".diag.txt"), + reportedDiagnostics.joinToString(separator = "\n\n", postfix = "\n") + ) + } + + private fun renderDiagnosticMessage(file: KtFile, severity: Severity, message: String?, textRanges: List): String { + val severityString = AnalyzerWithCompilerReport.convertSeverity(severity).toString().toLowerCaseAsciiOnly() + val position = DiagnosticUtils.getLineAndColumnRange(file, textRanges).start + return "/${file.name}:${position.line}:${position.column}: $severityString: $message" } }