[K2, CLI] Support endOffset in Kotlin CLI

Duplicated messages in testdata appeared because default renderer
renders diagnostic spans ambiguously. It shows only start position.
In fact, there are 3 failures, 2 of them distinguish only by the
diagnostic end offset. See youtrack for more information.

The issue about inconvenient rendering is KT-64989.

#KT-64608
This commit is contained in:
Evgeniy.Zhelenskiy
2024-01-12 03:08:09 +01:00
committed by Space Team
parent 6404cede07
commit f05c972efb
12 changed files with 154 additions and 28 deletions
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
import java.io.Closeable
import java.io.File
import java.io.InputStreamReader
import java.util.TreeSet
object FirDiagnosticsCompilerResultsReporter {
fun reportToMessageCollector(
@@ -54,7 +55,23 @@ object FirDiagnosticsCompilerResultsReporter {
}
try {
for (diagnostic in diagnosticsCollector.diagnosticsByFilePath[filePath].orEmpty().sortedWith(InFileDiagnosticsComparator)) {
val diagnosticList = diagnosticsCollector.diagnosticsByFilePath[filePath].orEmpty()
// Precomputing positions of the offsets in the ascending order of the offsets
val offsetsToPositions = positionFinder.value?.let { finder ->
val sortedOffsets = TreeSet<Int>().apply {
for (diagnostic in diagnosticList) {
if (diagnostic !is KtPsiDiagnostic) {
val range = DiagnosticUtils.firstRange(diagnostic.textRanges)
add(range.startOffset)
add(range.endOffset)
}
}
}
sortedOffsets.associateWith { finder.findNextPosition(it) }
}
for (diagnostic in diagnosticList.sortedWith(InFileDiagnosticsComparator)) {
when (diagnostic) {
is KtPsiDiagnostic -> {
val file = diagnostic.element.psi.containingFile
@@ -68,11 +85,14 @@ object FirDiagnosticsCompilerResultsReporter {
// TODO: bring KtSourceFile and KtSourceFileLinesMapping here and rewrite reporting via it to avoid code duplication
// NOTE: SequentialPositionFinder relies on the ascending order of the input offsets, so the code relies
// on the the appropriate sorting above
// Also the end offset is ignored, as it is irrelevant for the CLI reporting
positionFinder.value?.findNextPosition(DiagnosticUtils.firstRange(diagnostic.textRanges).startOffset)
?.let { pos ->
MessageUtil.createMessageLocation(filePath, pos.lineContent, pos.line, pos.column, -1, -1)
}
offsetsToPositions?.let {
val range = DiagnosticUtils.firstRange(diagnostic.textRanges)
val start = offsetsToPositions[range.startOffset]!!
val end = offsetsToPositions[range.endOffset]!!
MessageUtil.createMessageLocation(
filePath, start.lineContent, start.line, start.column, end.line, end.column
)
}
}
}?.let { location ->
report(diagnostic, location)
@@ -5,4 +5,7 @@ compiler/testData/compileKotlinAgainstCustomBinaries/againstFirWithUnstableAbi/s
compiler/testData/compileKotlinAgainstCustomBinaries/againstFirWithUnstableAbi/source.kt:4:11: error: class 'lib.Box' is compiled by an unstable version of the Kotlin compiler and cannot be loaded by this compiler.
get { Box("OK").value }
^
compiler/testData/compileKotlinAgainstCustomBinaries/againstFirWithUnstableAbi/source.kt:4:11: error: class 'lib.Box' is compiled by an unstable version of the Kotlin compiler and cannot be loaded by this compiler.
get { Box("OK").value }
^
COMPILATION_ERROR
@@ -5,4 +5,7 @@ compiler/testData/compileKotlinAgainstCustomBinaries/againstUnstable/source.kt:4
compiler/testData/compileKotlinAgainstCustomBinaries/againstUnstable/source.kt:4:11: error: class 'lib.Box' is compiled by an unstable version of the Kotlin compiler and cannot be loaded by this compiler.
get { Box("OK").value }
^
compiler/testData/compileKotlinAgainstCustomBinaries/againstUnstable/source.kt:4:11: error: class 'lib.Box' is compiled by an unstable version of the Kotlin compiler and cannot be loaded by this compiler.
get { Box("OK").value }
^
COMPILATION_ERROR
@@ -17,6 +17,10 @@ The class is loaded from $TMP_DIR$/library-after.jar!/a/A$Nested.class
val nested = A.Nested()
^
compiler/testData/compileKotlinAgainstCustomBinaries/wrongMetadataVersion/source.kt:8:22: error: class 'a.A' was compiled with an incompatible version of Kotlin. The actual metadata version is 42.0.0, but the compiler version $ABI_VERSION$ can read versions up to $ABI_VERSION_NEXT$.
The class is loaded from $TMP_DIR$/library-after.jar!/a/A.class
val methodCall = param.method()
^
compiler/testData/compileKotlinAgainstCustomBinaries/wrongMetadataVersion/source.kt:8:22: error: class 'a.A' was compiled with an incompatible version of Kotlin. The actual metadata version is 42.0.0, but the compiler version $ABI_VERSION$ can read versions up to $ABI_VERSION_NEXT$.
The class is loaded from $TMP_DIR$/library-after.jar!/a/A.class
val methodCall = param.method()
^
@@ -17,6 +17,10 @@ The class is loaded from $TMP_DIR$/library-after.jar!/a/A$Nested.class
val nested = A.Nested()
^
compiler/testData/compileKotlinAgainstCustomBinaries/wrongMetadataVersionSkipPrereleaseCheckHasNoEffect/source.kt:8:22: error: class 'a.A' was compiled with an incompatible version of Kotlin. The actual metadata version is 42.0.0, but the compiler version $ABI_VERSION$ can read versions up to $ABI_VERSION_NEXT$.
The class is loaded from $TMP_DIR$/library-after.jar!/a/A.class
val methodCall = param.method()
^
compiler/testData/compileKotlinAgainstCustomBinaries/wrongMetadataVersionSkipPrereleaseCheckHasNoEffect/source.kt:8:22: error: class 'a.A' was compiled with an incompatible version of Kotlin. The actual metadata version is 42.0.0, but the compiler version $ABI_VERSION$ can read versions up to $ABI_VERSION_NEXT$.
The class is loaded from $TMP_DIR$/library-after.jar!/a/A.class
val methodCall = param.method()
^
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.cli.common.CLITool;
import org.jetbrains.kotlin.cli.common.CompilerSystemProperties;
import org.jetbrains.kotlin.cli.common.ExitCode;
import org.jetbrains.kotlin.cli.common.Usage;
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer;
import org.jetbrains.kotlin.cli.js.K2JSCompiler;
import org.jetbrains.kotlin.cli.js.dce.K2JSDce;
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler;
@@ -62,7 +63,18 @@ public abstract class AbstractCliTest extends TestCaseWithTmpdir {
private static final String BUILD_FILE_ARGUMENT_PREFIX = "-Xbuild-file=";
public static Pair<String, ExitCode> executeCompilerGrabOutput(@NotNull CLITool<?> compiler, @NotNull List<String> args) {
public static Pair<String, ExitCode> executeCompilerGrabOutput(
@NotNull CLITool<?> compiler,
@NotNull List<String> args
) {
return executeCompilerGrabOutput(compiler, args, null);
}
public static Pair<String, ExitCode> executeCompilerGrabOutput(
@NotNull CLITool<?> compiler,
@NotNull List<String> args,
@Nullable MessageRenderer messageRenderer
) {
StringBuilder output = new StringBuilder();
int index = 0;
@@ -73,7 +85,7 @@ public abstract class AbstractCliTest extends TestCaseWithTmpdir {
} else {
next = index + next;
}
Pair<String, ExitCode> pair = CompilerTestUtil.executeCompiler(compiler, args.subList(index, next));
Pair<String, ExitCode> pair = CompilerTestUtil.executeCompiler(compiler, args.subList(index, next), messageRenderer);
output.append(pair.getFirst());
if (pair.getSecond() != ExitCode.OK) {
return new Pair<>(output.toString(), pair.getSecond());
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.test
import org.jetbrains.kotlin.cli.common.CLITool
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
import org.jetbrains.kotlin.test.util.KtTestUtil
import java.io.ByteArrayOutputStream
@@ -27,21 +28,22 @@ import kotlin.test.assertEquals
object CompilerTestUtil {
@JvmStatic
fun executeCompilerAssertSuccessful(compiler: CLITool<*>, args: List<String>) {
val (output, exitCode) = executeCompiler(compiler, args)
fun executeCompilerAssertSuccessful(compiler: CLITool<*>, args: List<String>, messageRenderer: MessageRenderer? = null) {
val (output, exitCode) = executeCompiler(compiler, args, messageRenderer)
assertEquals(ExitCode.OK, exitCode, output)
}
@JvmStatic
fun executeCompiler(compiler: CLITool<*>, args: List<String>): Pair<String, ExitCode> {
fun executeCompiler(compiler: CLITool<*>, args: List<String>, messageRenderer: MessageRenderer? = null): Pair<String, ExitCode> {
val bytes = ByteArrayOutputStream()
val origErr = System.err
try {
System.setErr(PrintStream(bytes))
val exitCode = CLITool.doMainNoExit(compiler, args.toTypedArray())
val exitCode =
if (messageRenderer == null) CLITool.doMainNoExit(compiler, args.toTypedArray())
else CLITool.doMainNoExit(compiler, args.toTypedArray(), messageRenderer)
return Pair(String(bytes.toByteArray()), exitCode)
}
finally {
} finally {
System.setErr(origErr)
}
}
@@ -52,7 +54,8 @@ object CompilerTestUtil {
src: File,
libraryName: String = "library",
extraOptions: List<String> = emptyList(),
extraClasspath: List<File> = emptyList()
extraClasspath: List<File> = emptyList(),
messageRenderer: MessageRenderer? = null,
): File {
val destination = File(KtTestUtil.tmpDir("testLibrary"), "$libraryName.jar")
val args = mutableListOf<String>().apply {
@@ -65,7 +68,7 @@ object CompilerTestUtil {
}
addAll(extraOptions)
}
executeCompilerAssertSuccessful(K2JVMCompiler(), args)
executeCompilerAssertSuccessful(K2JVMCompiler(), args, messageRenderer)
return destination
}
}
@@ -12,6 +12,7 @@ import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.analyzer.AnalysisResult
import org.jetbrains.kotlin.cli.common.CLITool
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
import org.jetbrains.kotlin.cli.metadata.K2MetadataCompiler
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
@@ -59,6 +60,7 @@ class AnalysisHandlerExtensionTest : TestCaseWithTmpdir() {
klass: KClass<out ComponentRegistrar>,
expectedExitCode: ExitCode = ExitCode.OK,
extras: List<String> = emptyList(),
messageRenderer: MessageRenderer? = null,
) {
val mainKt = tmpdir.resolve(src.name).apply {
writeText(src.content)
@@ -70,7 +72,7 @@ class AnalysisHandlerExtensionTest : TestCaseWithTmpdir() {
"-d", tmpdir.resolve("out").absolutePath
)
val (output, exitCode) = CompilerTestUtil.executeCompiler(compiler, args + outputPath + extras)
val (output, exitCode) = CompilerTestUtil.executeCompiler(compiler, args + outputPath + extras, messageRenderer)
assertEquals(expectedExitCode, exitCode, output)
}
@@ -5,6 +5,9 @@
package org.jetbrains.kotlin.cli
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSourceLocation
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
import org.jetbrains.kotlin.test.CompilerTestUtil
import org.jetbrains.kotlin.test.TestCaseWithTmpdir
@@ -104,15 +107,78 @@ class CustomCliTest : TestCaseWithTmpdir() {
compileAndCheckMainClass(listOf(mainKt), expectedMainClass = null)
}
private fun compileAndCheckMainClass(sourceFiles: List<File>, expectedMainClass: String?) {
private fun makeCompilerArgs(sourceFiles: List<File>, jarFile: File): List<String> {
// TODO: remove explicit version after implementing main fun detector (KT-44557)
return listOf("-language-version", "1.9", "-include-runtime", "-d", jarFile.absolutePath) + sourceFiles.map { it.absolutePath }
}
private fun compileAndCheckMainClass(sourceFiles: List<File>, expectedMainClass: String?, messageRenderer: MessageRenderer? = null) {
val jarFile = tmpdir.resolve("output.jar")
// TODO: remove explicit verion after implementing main fun detector (KT-44557)
val args = listOf("-language-version", "1.9", "-include-runtime", "-d", jarFile.absolutePath) + sourceFiles.map { it.absolutePath }
CompilerTestUtil.executeCompilerAssertSuccessful(K2JVMCompiler(), args)
val args = makeCompilerArgs(sourceFiles, jarFile)
CompilerTestUtil.executeCompilerAssertSuccessful(K2JVMCompiler(), args, messageRenderer)
JarFile(jarFile).use {
val mainClassAttr = it.manifest.mainAttributes.getValue("Main-Class")
Assert.assertEquals(expectedMainClass, mainClassAttr)
}
}
private fun compileAndGetDiagnostics(sourceFiles: List<File>): List<Diagnostic> {
val jarFile = tmpdir.resolve("output.jar")
val args = makeCompilerArgs(sourceFiles, jarFile)
val diagnostics = mutableListOf<Diagnostic>()
CompilerTestUtil.executeCompiler(K2JVMCompiler(), args, LoggingMessageRenderer(diagnostics))
return diagnostics
}
private data class Diagnostic(
val severity: CompilerMessageSeverity,
val message: String,
val location: CompilerMessageSourceLocation?
)
private class LoggingMessageRenderer(val diagnostics: MutableList<Diagnostic>) : MessageRenderer {
override fun renderPreamble(): String = ""
override fun render(
severity: CompilerMessageSeverity,
message: String,
location: CompilerMessageSourceLocation?
): String {
diagnostics.add(Diagnostic(severity, message, location))
return ""
}
override fun renderUsage(usage: String): String =
render(CompilerMessageSeverity.STRONG_WARNING, usage, null)
override fun renderConclusion(): String = ""
override fun getName(): String = "Redirector"
}
fun testDiagnosticRanges() {
val mainKt = tmpdir.resolve("main.kt").apply {
val quotes = "\"".repeat(3)
writeText(
"""
|fun main(args: Array<String>) {
| val x: Int = $quotes
| some
| multiline
| string
| $quotes
|}""".trimMargin()
)
}
val diagnostics = compileAndGetDiagnostics(listOf(mainKt))
require(diagnostics.size == 1) { "Expected 1 diagnostic, but found ${diagnostics.size}:\n${diagnostics.joinToString("\n")}" }
val diagnostic = diagnostics.single()
assertEquals(2, diagnostic.location?.line)
assertEquals(18, diagnostic.location?.column)
assertEquals(6, diagnostic.location?.lineEnd)
assertEquals(8, diagnostic.location?.columnEnd)
}
}
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.cli
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
import org.jetbrains.kotlin.test.CompilerTestUtil
import org.jetbrains.kotlin.test.TestCaseWithTmpdir
@@ -42,16 +43,17 @@ class FriendPathsTest : TestCaseWithTmpdir() {
doTestFriendPaths(File(tmpdir, "lib").relativeTo(File("").absoluteFile))
}
private fun doTestFriendPaths(libDest: File) {
private fun doTestFriendPaths(libDest: File, messageRenderer: MessageRenderer? = null) {
val libSrc = File(getTestDataDirectory(), "lib.kt")
CompilerTestUtil.executeCompilerAssertSuccessful(K2JVMCompiler(), listOf("-d", libDest.path, libSrc.path))
CompilerTestUtil.executeCompilerAssertSuccessful(K2JVMCompiler(), listOf("-d", libDest.path, libSrc.path), messageRenderer)
CompilerTestUtil.executeCompilerAssertSuccessful(
K2JVMCompiler(),
listOf(
"-d", tmpdir.path, "-cp", libDest.path, File(getTestDataDirectory(), "usage.kt").path,
"-Xfriend-paths=${libDest.path}"
)
),
messageRenderer,
)
}
}
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.codegen
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
import org.jetbrains.kotlin.config.ApiVersion
import org.jetbrains.kotlin.config.LanguageVersion
@@ -34,7 +35,8 @@ class JvmModuleProtoBufTest : KtUsefulTestCase() {
relativeDirectory: String,
compileWith: LanguageVersion = LanguageVersion.LATEST_STABLE,
loadWith: LanguageVersion = LanguageVersion.LATEST_STABLE,
extraOptions: List<String> = emptyList()
extraOptions: List<String> = emptyList(),
messageRenderer: MessageRenderer? = null,
) {
val directory = KtTestUtil.getTestDataPathBase() + relativeDirectory
val tmpdir = KtTestUtil.tmpDir(this::class.simpleName)
@@ -46,7 +48,8 @@ class JvmModuleProtoBufTest : KtUsefulTestCase() {
"-d", tmpdir.path,
"-module-name", moduleName,
"-language-version", compileWith.versionString
) + extraOptions
) + extraOptions,
messageRenderer
)
val mapping = ModuleMapping.loadModuleMapping(
@@ -11,6 +11,7 @@ import com.intellij.mock.MockProject
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.analyzer.AnalysisResult
import org.jetbrains.kotlin.cli.common.CLITool
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
import org.jetbrains.kotlin.cli.js.K2JSCompiler
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
import org.jetbrains.kotlin.config.CompilerConfiguration
@@ -60,7 +61,10 @@ class JsIrAnalysisHandlerExtensionTest : TestCaseWithTmpdir() {
private val outklib: String
get() = tmpdir.resolve("out.klib").absolutePath
private fun runTest(compiler: CLITool<*>, src: TestKtFile, libs: String, outFile: String, extras: List<String> = emptyList()) {
private fun runTest(
compiler: CLITool<*>, src: TestKtFile, libs: String, outFile: String, extras: List<String> = emptyList(),
messageRenderer: MessageRenderer? = null,
) {
val mainKt = tmpdir.resolve(src.name).apply {
writeText(src.content)
}
@@ -74,7 +78,7 @@ class JsIrAnalysisHandlerExtensionTest : TestCaseWithTmpdir() {
"-language-version", "1.9",
mainKt.absolutePath
)
CompilerTestUtil.executeCompilerAssertSuccessful(compiler, args + extras)
CompilerTestUtil.executeCompilerAssertSuccessful(compiler, args + extras, messageRenderer)
}
fun testShouldNotGenerateCodeJs() {