From 43d7536a288e7781fe3d9356e420dfff770aa70d Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Thu, 7 Jan 2021 18:58:23 +0100 Subject: [PATCH] Protect scripts compilation from passing -Xuse-ir via configuration as well as other options that require changes in the compilation setup before compiler options from the configuration could be processed --- .../plugin/impl/compilationContext.kt | 15 ++---- .../compiler/plugin/impl/errorReporting.kt | 46 +++++++++++++------ 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/compilationContext.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/compilationContext.kt index 7277b7eb983..64afb984dd5 100644 --- a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/compilationContext.kt +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/compilationContext.kt @@ -170,19 +170,12 @@ private fun CompilerConfiguration.updateWithCompilerOptions( false } ?: run { messageCollector.reportArgumentParseProblems(it) - reportArgumentsIgnoredGenerally( - it, - messageCollector, - ignoredOptionsReportingState - ) + val error = reportArgumentsNotAllowed(it, messageCollector, ignoredOptionsReportingState) + reportArgumentsIgnoredGenerally(it, messageCollector, ignoredOptionsReportingState) if (isRefinement) { - reportArgumentsIgnoredFromRefinement( - it, - messageCollector, - ignoredOptionsReportingState - ) + reportArgumentsIgnoredFromRefinement(it, messageCollector, ignoredOptionsReportingState) } - true + !error } } } diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/errorReporting.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/errorReporting.kt index 99f7030b227..3231113d89f 100644 --- a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/errorReporting.kt +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/errorReporting.kt @@ -92,15 +92,32 @@ class IgnoredOptionsReportingState { var currentArguments = K2JVMCompilerArguments() } +internal fun reportArgumentsNotAllowed( + arguments: K2JVMCompilerArguments, + messageCollector: MessageCollector, + reportingState: IgnoredOptionsReportingState +) = + reportInvalidArguments( + arguments, + "The following compiler arguments are not allowed in the script compilation configuration: ", + CompilerMessageSeverity.ERROR, + messageCollector, + reportingState, + K2JVMCompilerArguments::useJavac, + K2JVMCompilerArguments::useIR, + K2JVMCompilerArguments::useOldBackend, + K2JVMCompilerArguments::useFir + ) + internal fun reportArgumentsIgnoredGenerally( arguments: K2JVMCompilerArguments, messageCollector: MessageCollector, reportingState: IgnoredOptionsReportingState -) { - - reportIgnoredArguments( +) = + reportInvalidArguments( arguments, "The following compiler arguments are ignored on script compilation: ", + CompilerMessageSeverity.STRONG_WARNING, messageCollector, reportingState, K2JVMCompilerArguments::version, @@ -121,14 +138,14 @@ internal fun reportArgumentsIgnoredGenerally( K2JVMCompilerArguments::reportPerf, K2JVMCompilerArguments::dumpPerf ) -} internal fun reportArgumentsIgnoredFromRefinement( arguments: K2JVMCompilerArguments, messageCollector: MessageCollector, reportingState: IgnoredOptionsReportingState -) { - reportIgnoredArguments( +) = + reportInvalidArguments( arguments, "The following compiler arguments are ignored when configured from refinement callbacks: ", + CompilerMessageSeverity.STRONG_WARNING, messageCollector, reportingState, K2JVMCompilerArguments::noJdk, @@ -138,23 +155,26 @@ internal fun reportArgumentsIgnoredFromRefinement( K2JVMCompilerArguments::noStdlib, K2JVMCompilerArguments::noReflect ) -} -private fun reportIgnoredArguments( - arguments: K2JVMCompilerArguments, message: String, + +private fun reportInvalidArguments( + arguments: K2JVMCompilerArguments, + message: String, severity: CompilerMessageSeverity, messageCollector: MessageCollector, reportingState: IgnoredOptionsReportingState, vararg toIgnore: KMutableProperty1 -) { - val ignoredArgKeys = toIgnore.mapNotNull { argProperty -> +): Boolean { + val invalidArgKeys = toIgnore.mapNotNull { argProperty -> if (argProperty.get(arguments) != argProperty.get(reportingState.currentArguments)) { argProperty.annotations.firstIsInstanceOrNull()?.value ?: throw IllegalStateException("unknown compiler argument property: $argProperty: no Argument annotation found") } else null } - if (ignoredArgKeys.isNotEmpty()) { - messageCollector.report(CompilerMessageSeverity.STRONG_WARNING, "$message${ignoredArgKeys.joinToString(", ")}") + if (invalidArgKeys.isNotEmpty()) { + messageCollector.report(severity, "$message${invalidArgKeys.joinToString(", ")}") + return true } + return false } val MessageCollector.reporter: MessageReporter