Introduce language version 2.0 and associate K2 compiler with it

This commit is contained in:
Mikhail Glukhikh
2022-11-28 18:20:10 +01:00
parent afe1150aec
commit 3dc05f4ec5
84 changed files with 233 additions and 151 deletions
@@ -643,7 +643,7 @@ abstract class CommonCompilerArguments : CommonToolArguments() {
}
}
private fun checkLanguageVersionIsStable(languageVersion: LanguageVersion, collector: MessageCollector) {
fun checkLanguageVersionIsStable(languageVersion: LanguageVersion, collector: MessageCollector) {
if (!languageVersion.isStable && !suppressVersionWarnings) {
collector.report(
CompilerMessageSeverity.STRONG_WARNING,
@@ -573,7 +573,7 @@ Also sets `-jvm-target` value equal to the selected JDK version"""
result[JvmAnalysisFlags.sanitizeParentheses] = sanitizeParentheses
result[JvmAnalysisFlags.suppressMissingBuiltinsError] = suppressMissingBuiltinsError
result[JvmAnalysisFlags.enableJvmPreview] = enableJvmPreview
result[AnalysisFlags.allowUnstableDependencies] = allowUnstableDependencies || useK2
result[AnalysisFlags.allowUnstableDependencies] = allowUnstableDependencies || useK2 || languageVersion.usesK2
result[JvmAnalysisFlags.disableUltraLightClasses] = disableUltraLightClasses
result[JvmAnalysisFlags.useIR] = !useOldBackend
return result
@@ -175,14 +175,15 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
ExitCode exitCode = OK;
if (K2JSCompilerArgumentsKt.isIrBackendEnabled(arguments)) {
LanguageVersionSettings languageVersionSettings = CommonConfigurationKeysKt.getLanguageVersionSettings(configuration);
LanguageVersion languageVersion = languageVersionSettings.getLanguageVersion();
if (K2JSCompilerArgumentsKt.isIrBackendEnabled(arguments) || languageVersion.getUsesK2()) {
exitCode = getIrCompiler().doExecute(arguments, configuration.copy(), rootDisposable, paths);
}
if (K2JSCompilerArgumentsKt.isPreIrBackendDisabled(arguments)) {
if (K2JSCompilerArgumentsKt.isPreIrBackendDisabled(arguments) || languageVersion.getUsesK2()) {
return exitCode;
}
LanguageVersionSettings languageVersionSettings = CommonConfigurationKeysKt.getLanguageVersionSettings(configuration);
if (CompilerSystemProperties.KOTLIN_JS_COMPILER_LEGACY_FORCE_ENABLED.getValue() != "true" && languageVersionSettings.getLanguageVersion().compareTo(LanguageVersion.KOTLIN_1_9) >= 0) {
messageCollector.report(ERROR, "Old Kotlin/JS compiler is no longer supported. Please migrate to the new JS IR backend", null);
@@ -287,11 +287,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
val outputKlibPath =
if (arguments.irProduceKlibFile) outputDir.resolve("$outputName.klib").normalize().absolutePath
else outputDirPath
if (arguments.useK2) {
messageCollector.report(
STRONG_WARNING,
"ATTENTION!\n This build uses experimental K2 compiler: \n -Xuse-k2"
)
if (configuration.get(CommonConfigurationKeys.USE_FIR) == true) {
sourceModule = processSourceModuleWithK2(environmentForJS, libraries, friendLibraries, arguments, outputKlibPath)
} else {
sourceModule = processSourceModule(environmentForJS, libraries, friendLibraries, arguments, outputKlibPath)
@@ -31,8 +31,7 @@ import org.jetbrains.kotlin.cli.jvm.plugins.PluginCliParser
import org.jetbrains.kotlin.cli.plugins.processCompilerPluginsOptions
import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.Services
import org.jetbrains.kotlin.config.*
import org.jetbrains.kotlin.ir.util.IrMessageLogger
import org.jetbrains.kotlin.metadata.deserialization.BinaryVersion
import org.jetbrains.kotlin.progress.CompilationCanceledException
@@ -77,7 +76,7 @@ abstract class CLICompiler<A : CommonCompilerArguments> : CLITool<A>() {
configuration.put(CLIConfigurationKeys.ORIGINAL_MESSAGE_COLLECTOR_KEY, messageCollector)
val collector = GroupingMessageCollector(messageCollector, arguments.allWarningsAsErrors).also {
configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, it)
configuration.put(MESSAGE_COLLECTOR_KEY, it)
}
configuration.put(IrMessageLogger.IR_MESSAGE_LOGGER, IrMessageCollector(collector))
@@ -88,7 +87,7 @@ abstract class CLICompiler<A : CommonCompilerArguments> : CLITool<A>() {
setupPlatformSpecificArgumentsAndServices(configuration, arguments, services)
val paths = computeKotlinPaths(collector, arguments)
if (collector.hasErrors()) {
return ExitCode.COMPILATION_ERROR
return COMPILATION_ERROR
}
val canceledStatus = services[CompilationCanceledStatus::class.java]
@@ -98,6 +97,36 @@ abstract class CLICompiler<A : CommonCompilerArguments> : CLITool<A>() {
try {
setIdeaIoUseFallback()
val useK2FromFlag = arguments.useK2
val languageVersion = configuration.languageVersionSettings.languageVersion
if (useK2FromFlag) {
when {
arguments.languageVersion == null -> {
messageCollector.report(
STRONG_WARNING,
"Compiler flag -Xuse-k2 is deprecated, please use -language-version 2.0 instead"
)
}
languageVersion.usesK2 -> {
messageCollector.report(
STRONG_WARNING,
"Compiler flag -Xuse-k2 is redundant"
)
}
else -> {
messageCollector.report(
STRONG_WARNING,
"With -Xuse-k2 compiler flag -language-version $languageVersion has no effect, please remove -Xuse-k2 flag or use -language-version 2.0 instead"
)
}
}
if (!languageVersion.usesK2) {
val languageVersionSettings = configuration.languageVersionSettings
configuration.languageVersionSettings = languageVersionSettings.copy(LanguageVersion.KOTLIN_2_0)
arguments.checkLanguageVersionIsStable(LanguageVersion.KOTLIN_2_0, messageCollector)
}
}
val code = doExecute(arguments, configuration, rootDisposable, paths)
performanceManager.notifyCompilationFinished()
@@ -115,12 +144,12 @@ abstract class CLICompiler<A : CommonCompilerArguments> : CLITool<A>() {
return if (collector.hasErrors()) COMPILATION_ERROR else code
} catch (e: CompilationCanceledException) {
collector.reportCompilationCancelled(e)
return ExitCode.OK
return OK
} catch (e: RuntimeException) {
val cause = e.cause
if (cause is CompilationCanceledException) {
collector.reportCompilationCancelled(cause)
return ExitCode.OK
return OK
} else {
throw e
}
@@ -22,8 +22,6 @@ fun <A : CommonCompilerArguments> CompilerConfiguration.setupCommonArguments(
createMetadataVersion: ((IntArray) -> BinaryVersion)? = null
) {
put(CommonConfigurationKeys.DISABLE_INLINE, arguments.noInline)
put(CommonConfigurationKeys.USE_FIR, arguments.useK2)
put(CommonConfigurationKeys.USE_LIGHT_TREE, arguments.useFirLT)
put(CommonConfigurationKeys.USE_FIR_EXTENDED_CHECKERS, arguments.useFirExtendedCheckers)
put(CommonConfigurationKeys.EXPECT_ACTUAL_LINKER, arguments.expectActualLinker)
putIfNotNull(CLIConfigurationKeys.INTELLIJ_PLUGIN_ROOT, arguments.intellijPluginRoot)
@@ -45,6 +43,10 @@ fun <A : CommonCompilerArguments> CompilerConfiguration.setupCommonArguments(
}
setupLanguageVersionSettings(arguments)
val usesK2 = arguments.useK2 || languageVersionSettings.languageVersion.usesK2
put(CommonConfigurationKeys.USE_FIR, usesK2)
put(CommonConfigurationKeys.USE_LIGHT_TREE, usesK2)
}
fun <A : CommonCompilerArguments> CompilerConfiguration.setupLanguageVersionSettings(arguments: A) {
@@ -75,11 +75,6 @@ object FirKotlinToJvmBytecodeCompiler {
): Boolean {
val performanceManager = projectConfiguration.get(CLIConfigurationKeys.PERF_MANAGER)
messageCollector.report(
STRONG_WARNING,
"ATTENTION!\n This build uses experimental K2 compiler: \n -Xuse-k2"
)
val notSupportedPlugins = mutableListOf<String?>().apply {
projectConfiguration.get(ComponentRegistrar.PLUGIN_COMPONENT_REGISTRARS).collectIncompatiblePluginNamesTo(this, ComponentRegistrar::supportsK2)
projectConfiguration.get(CompilerPluginRegistrar.COMPILER_PLUGIN_REGISTRARS).collectIncompatiblePluginNamesTo(this, CompilerPluginRegistrar::supportsK2)
@@ -89,9 +84,9 @@ object FirKotlinToJvmBytecodeCompiler {
messageCollector.report(
CompilerMessageSeverity.ERROR,
"""
|There are some plugins incompatible with K2 compiler:
|There are some plugins incompatible with language version 2.0:
|${notSupportedPlugins.joinToString(separator = "\n|") { " $it" }}
|Please remove -Xuse-k2
|Please use language version 1.* (e.g. 1.9)
""".trimMargin()
)
return false
@@ -99,11 +99,6 @@ fun compileModulesUsingFrontendIrAndLightTree(
performanceManager?.notifyCompilerInitialized(0, 0, targetDescription)
messageCollector.report(
CompilerMessageSeverity.STRONG_WARNING,
"ATTENTION!\n This build uses experimental K2 compiler: \n -Xuse-k2"
)
val outputs = mutableListOf<GenerationState>()
var mainClassFqName: FqName? = null
@@ -260,7 +260,7 @@ fun CompilerConfiguration.configureAdvancedJvmOptions(arguments: K2JVMCompilerAr
// TODO: ignore previous configuration value when we do not need old backend in scripting by default
val useOldBackend = arguments.useOldBackend || (!arguments.useIR && get(JVMConfigurationKeys.IR) == false)
val useIR = arguments.useK2 ||
val useIR = arguments.useK2 || languageVersionSettings.languageVersion.usesK2 ||
if (languageVersionSettings.supportsFeature(LanguageFeature.JvmIrEnabledByDefault)) {
!useOldBackend
} else {
@@ -59,7 +59,7 @@ class K2MetadataCompiler : CLICompiler<K2MetadataCompilerArguments>() {
): ExitCode {
val collector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY)
if (configuration.getBoolean(CommonConfigurationKeys.USE_FIR)) {
collector.report(ERROR, "K2 does not support compilation to metadata right now")
collector.report(ERROR, "Compilation to metadata is not supported in language version 2.0 right now")
return ExitCode.COMPILATION_ERROR
}
val performanceManager = configuration.getNotNull(CLIConfigurationKeys.PERF_MANAGER)