Support -Xexperimental and -Xuse-experimental, validate their values

#KT-22759 In Progress
This commit is contained in:
Alexander Udalov
2018-01-02 16:46:07 +01:00
parent 153c86c069
commit 77625831f7
29 changed files with 204 additions and 10 deletions
@@ -123,7 +123,7 @@ abstract class CommonCompilerArguments : CommonToolArguments() {
value = "-Xlegacy-smart-cast-after-try",
description = "Allow var smart casts despite assignment in try block"
)
var legacySmartCastAfterTry by FreezableVar(false)
var legacySmartCastAfterTry: Boolean by FreezableVar(false)
@Argument(
value = "-Xeffect-system",
@@ -137,11 +137,27 @@ abstract class CommonCompilerArguments : CommonToolArguments() {
)
var readDeserializedContracts: Boolean by FreezableVar(false)
@Argument(
value = "-Xexperimental",
valueDescription = "<fq.name>",
description = "Enable and propagate usages of experimental API for marker annotation with the given fully qualified name"
)
var experimental: Array<String>? by FreezableVar(null)
@Argument(
value = "-Xuse-experimental",
valueDescription = "<fq.name>",
description = "Enable usages of COMPILATION-affecting experimental API for marker annotation with the given fully qualified name"
)
var useExperimental: Array<String>? by FreezableVar(null)
open fun configureAnalysisFlags(collector: MessageCollector): MutableMap<AnalysisFlag<*>, Any> {
return HashMap<AnalysisFlag<*>, Any>().apply {
put(AnalysisFlag.skipMetadataVersionCheck, skipMetadataVersionCheck)
put(AnalysisFlag.multiPlatformDoNotCheckActual, noCheckActual)
put(AnalysisFlag.allowKotlinPackage, allowKotlinPackage)
put(AnalysisFlag.experimental, experimental?.toList().orEmpty())
put(AnalysisFlag.useExperimental, useExperimental?.toList().orEmpty())
}
}
@@ -22,6 +22,7 @@ import kotlin.reflect.KMutableProperty1
import kotlin.reflect.full.findAnnotation
import kotlin.reflect.full.memberProperties
@Target(AnnotationTarget.PROPERTY)
annotation class Argument(
val value: String,
val shortName: String = "",
@@ -23,6 +23,7 @@ import com.intellij.psi.util.PsiFormatUtil
import org.jetbrains.kotlin.analyzer.AnalysisResult
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.*
import org.jetbrains.kotlin.codegen.state.IncompatibleClassTrackerImpl
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.diagnostics.*
import org.jetbrains.kotlin.diagnostics.DiagnosticUtils.sortedDiagnostics
import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages
@@ -33,12 +34,16 @@ import org.jetbrains.kotlin.resolve.AnalyzingUtils
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.checkers.ExperimentalUsageChecker
import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics
import org.jetbrains.kotlin.resolve.jvm.JvmBindingContextSlices
import org.jetbrains.kotlin.resolve.jvm.JvmClassName
import org.jetbrains.kotlin.serialization.deserialization.IncompatibleVersionErrorData
class AnalyzerWithCompilerReport(private val messageCollector: MessageCollector) {
class AnalyzerWithCompilerReport(
private val messageCollector: MessageCollector,
private val languageVersionSettings: LanguageVersionSettings
) {
lateinit var analysisResult: AnalysisResult
private fun reportIncompleteHierarchies() {
@@ -94,6 +99,9 @@ class AnalyzerWithCompilerReport(private val messageCollector: MessageCollector)
fun analyzeAndReport(files: Collection<KtFile>, analyze: () -> AnalysisResult) {
analysisResult = analyze()
ExperimentalUsageChecker.checkCompilerArguments(analysisResult.moduleDescriptor, languageVersionSettings) { message ->
messageCollector.report(ERROR, message)
}
reportSyntaxErrors(files)
reportDiagnostics(analysisResult.bindingContext.diagnostics, messageCollector)
reportIncompleteHierarchies()
@@ -222,7 +222,9 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
return COMPILATION_ERROR;
}
AnalyzerWithCompilerReport analyzerWithCompilerReport = new AnalyzerWithCompilerReport(messageCollector);
AnalyzerWithCompilerReport analyzerWithCompilerReport = new AnalyzerWithCompilerReport(
messageCollector, CommonConfigurationKeysKt.getLanguageVersionSettings(configuration)
);
analyzerWithCompilerReport.analyzeAndReport(sourcesFiles, () -> TopDownAnalyzerFacadeForJS.analyzeFiles(sourcesFiles, config));
if (analyzerWithCompilerReport.hasErrors()) {
return COMPILATION_ERROR;
@@ -43,10 +43,7 @@ import org.jetbrains.kotlin.cli.jvm.config.*
import org.jetbrains.kotlin.codegen.*
import org.jetbrains.kotlin.codegen.state.GenerationState
import org.jetbrains.kotlin.codegen.state.GenerationStateEventCallback
import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.JVMConfigurationKeys
import org.jetbrains.kotlin.config.addKotlinSourceRoots
import org.jetbrains.kotlin.config.*
import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil
import org.jetbrains.kotlin.idea.MainFunctionDetector
import org.jetbrains.kotlin.javac.JavacWrapper
@@ -364,7 +361,7 @@ object KotlinToJVMBytecodeCompiler {
val collector = environment.messageCollector
val analysisStart = PerformanceCounter.currentTime()
val analyzerWithCompilerReport = AnalyzerWithCompilerReport(collector)
val analyzerWithCompilerReport = AnalyzerWithCompilerReport(collector, environment.configuration.languageVersionSettings)
analyzerWithCompilerReport.analyzeAndReport(sourceFiles) {
val project = environment.project
val moduleOutputs = environment.configuration.get(JVMConfigurationKeys.MODULES)?.mapNotNullTo(hashSetOf()) { module ->
@@ -62,7 +62,7 @@ open class MetadataSerializer(private val dependOnOldBuiltIns: Boolean) {
return
}
val analyzer = AnalyzerWithCompilerReport(messageCollector)
val analyzer = AnalyzerWithCompilerReport(messageCollector, configuration.languageVersionSettings)
analyzer.analyzeAndReport(files) {
CommonAnalyzerFacade.analyzeFiles(files, moduleName, dependOnOldBuiltIns, configuration.languageVersionSettings) { _, content ->
environment.createPackagePartProvider(content.moduleContentScope)
@@ -17,9 +17,12 @@
package org.jetbrains.kotlin.resolve.checkers
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.config.AnalysisFlag
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.*
@@ -55,7 +58,7 @@ object ExperimentalUsageChecker : CallChecker {
val markerDescriptor: ClassDescriptor,
val annotationFqName: FqName,
val severity: Severity,
private val impact: List<Impact>
val impact: List<Impact>
) {
val isCompilationOnly: Boolean get() = impact.all(Impact.COMPILATION::equals)
@@ -222,6 +225,37 @@ object ExperimentalUsageChecker : CallChecker {
}
}
fun checkCompilerArguments(module: ModuleDescriptor, languageVersionSettings: LanguageVersionSettings, reportError: (String) -> Unit) {
fun checkAnnotation(fqName: String, allowNonCompilationImpact: Boolean): Boolean {
val descriptor = module.resolveClassByFqName(FqName(fqName), NoLookupLocation.FOR_NON_TRACKED_SCOPE)
val experimentality = descriptor?.loadExperimentalityForMarkerAnnotation()
val message = when {
descriptor == null ->
"Experimental API marker $fqName is unresolved. " +
"Please make sure it's present in the module dependencies"
experimentality == null ->
"Class $fqName is not an experimental API marker annotation"
!allowNonCompilationImpact && !experimentality.impact.all(Experimentality.Impact.COMPILATION::equals) ->
"Experimental API marker $fqName has impact other than COMPILATION, " +
"therefore it can't be used with -Xuse-experimental"
else -> return true
}
reportError(message)
return false
}
val validExperimental =
languageVersionSettings.getFlag(AnalysisFlag.experimental)
.filter { checkAnnotation(it, allowNonCompilationImpact = true) }
val validUseExperimental =
languageVersionSettings.getFlag(AnalysisFlag.useExperimental)
.filter { checkAnnotation(it, allowNonCompilationImpact = false) }
for (fqName in validExperimental.intersect(validUseExperimental)) {
reportError("'-Xuse-experimental=$fqName' has no effect because '-Xexperimental=$fqName' is used")
}
}
object ClassifierUsage : ClassifierUsageChecker {
override fun check(targetDescriptor: ClassifierDescriptor, element: PsiElement, context: ClassifierUsageCheckerContext) {
checkExperimental(targetDescriptor, element, context)
+2
View File
@@ -7,6 +7,7 @@ where advanced options include:
-Xcoroutines={enable|warn|error}
Enable coroutines or report warnings or errors on declarations and use sites of 'suspend' modifier
-Xeffect-system Enable experimental language feature: effect system
-Xexperimental=<fq.name> Enable and propagate usages of experimental API for marker annotation with the given fully qualified name
-Xintellij-plugin-root=<path> Path to the kotlin-compiler.jar or directory where IntelliJ configuration files can be found
-Xlegacy-smart-cast-after-try Allow var smart casts despite assignment in try block
-Xmulti-platform Enable experimental language support for multi-platform projects
@@ -18,6 +19,7 @@ where advanced options include:
-Xrepeat=<count> Repeat compilation (for performance analysis)
-Xreport-output-files Report source to output files mapping
-Xskip-metadata-version-check Load classes with bad metadata version anyway (incl. pre-release classes)
-Xuse-experimental=<fq.name> Enable usages of COMPILATION-affecting experimental API for marker annotation with the given fully qualified name
Advanced options are non-standard and may be changed or removed without any notice.
OK
@@ -0,0 +1,8 @@
$TESTDATA_DIR$/experimentalAndUseExperimentalWithSameAnnotation.kt
-d
$TEMP_DIR$
-Xskip-runtime-version-check
-language-version
1.3
-Xexperimental=org.test.ExperimentalAPI
-Xuse-experimental=org.test.ExperimentalAPI
@@ -0,0 +1,4 @@
package org.test
@Experimental(Experimental.Level.ERROR, [Experimental.Impact.COMPILATION])
annotation class ExperimentalAPI
@@ -0,0 +1,3 @@
warning: language version 1.3 is experimental, there are no backwards compatibility guarantees for new language and library features
error: '-Xuse-experimental=org.test.ExperimentalAPI' has no effect because '-Xexperimental=org.test.ExperimentalAPI' is used
COMPILATION_ERROR
@@ -0,0 +1,5 @@
$TESTDATA_DIR$/experimentalIsNotAnnotation.kt
-d
$TEMP_DIR$
-Xexperimental=org.test.NotAnAnnotation1
-Xuse-experimental=org.test.NotAnAnnotation2
@@ -0,0 +1,4 @@
package org.test
enum class NotAnAnnotation1
interface NotAnAnnotation2
@@ -0,0 +1,3 @@
error: class org.test.NotAnAnnotation1 is not an experimental API marker annotation
error: class org.test.NotAnAnnotation2 is not an experimental API marker annotation
COMPILATION_ERROR
@@ -0,0 +1,5 @@
$TESTDATA_DIR$/experimentalIsNotMarker.kt
-d
$TEMP_DIR$
-Xexperimental=org.test.NotAMarker1
-Xuse-experimental=org.test.NotAMarker2
+4
View File
@@ -0,0 +1,4 @@
package org.test
annotation class NotAMarker1
annotation class NotAMarker2
+3
View File
@@ -0,0 +1,3 @@
error: class org.test.NotAMarker1 is not an experimental API marker annotation
error: class org.test.NotAMarker2 is not an experimental API marker annotation
COMPILATION_ERROR
+7
View File
@@ -0,0 +1,7 @@
$TESTDATA_DIR$/experimentalNested.kt
-d
$TEMP_DIR$
-Xskip-runtime-version-check
-language-version
1.3
-Xexperimental=org.test.Outer.Nested
+9
View File
@@ -0,0 +1,9 @@
package org.test
class Outer {
@Experimental(Experimental.Level.ERROR, [Experimental.Impact.LINKAGE])
annotation class Nested
}
@Outer.Nested
fun foo() {}
+2
View File
@@ -0,0 +1,2 @@
warning: language version 1.3 is experimental, there are no backwards compatibility guarantees for new language and library features
OK
@@ -0,0 +1,8 @@
$TESTDATA_DIR$/experimentalRuntimeScope.kt
-d
$TEMP_DIR$
-Xskip-runtime-version-check
-language-version
1.3
-Xexperimental=org.test.Experimental1
-Xuse-experimental=org.test.Experimental2
+7
View File
@@ -0,0 +1,7 @@
package org.test
@Experimental(Experimental.Level.ERROR, [Experimental.Impact.LINKAGE])
annotation class Experimental1
@Experimental(Experimental.Level.ERROR, [Experimental.Impact.RUNTIME])
annotation class Experimental2
@@ -0,0 +1,3 @@
warning: language version 1.3 is experimental, there are no backwards compatibility guarantees for new language and library features
error: experimental API marker org.test.Experimental2 has impact other than COMPILATION, therefore it can't be used with -Xuse-experimental
COMPILATION_ERROR
+6
View File
@@ -0,0 +1,6 @@
$TESTDATA_DIR$/experimentalUnresolved.kt
-d
$TEMP_DIR$
-Xuse-experimental=org.test.Unresolved1
-Xexperimental=org.test.Unresolved2
-Xuse-experimental=org.test.Unresolved3
+1
View File
@@ -0,0 +1 @@
fun test() {}
+4
View File
@@ -0,0 +1,4 @@
error: experimental API marker org.test.Unresolved2 is unresolved. Please make sure it's present in the module dependencies
error: experimental API marker org.test.Unresolved1 is unresolved. Please make sure it's present in the module dependencies
error: experimental API marker org.test.Unresolved3 is unresolved. Please make sure it's present in the module dependencies
COMPILATION_ERROR
+2
View File
@@ -44,6 +44,7 @@ where advanced options include:
-Xcoroutines={enable|warn|error}
Enable coroutines or report warnings or errors on declarations and use sites of 'suspend' modifier
-Xeffect-system Enable experimental language feature: effect system
-Xexperimental=<fq.name> Enable and propagate usages of experimental API for marker annotation with the given fully qualified name
-Xintellij-plugin-root=<path> Path to the kotlin-compiler.jar or directory where IntelliJ configuration files can be found
-Xlegacy-smart-cast-after-try Allow var smart casts despite assignment in try block
-Xmulti-platform Enable experimental language support for multi-platform projects
@@ -55,6 +56,7 @@ where advanced options include:
-Xrepeat=<count> Repeat compilation (for performance analysis)
-Xreport-output-files Report source to output files mapping
-Xskip-metadata-version-check Load classes with bad metadata version anyway (incl. pre-release classes)
-Xuse-experimental=<fq.name> Enable usages of COMPILATION-affecting experimental API for marker annotation with the given fully qualified name
Advanced options are non-standard and may be changed or removed without any notice.
OK
@@ -177,6 +177,42 @@ public class CliTestGenerated extends AbstractCliTest {
doJvmTest(fileName);
}
@TestMetadata("experimentalAndUseExperimentalWithSameAnnotation.args")
public void testExperimentalAndUseExperimentalWithSameAnnotation() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/experimentalAndUseExperimentalWithSameAnnotation.args");
doJvmTest(fileName);
}
@TestMetadata("experimentalIsNotAnnotation.args")
public void testExperimentalIsNotAnnotation() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/experimentalIsNotAnnotation.args");
doJvmTest(fileName);
}
@TestMetadata("experimentalIsNotMarker.args")
public void testExperimentalIsNotMarker() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/experimentalIsNotMarker.args");
doJvmTest(fileName);
}
@TestMetadata("experimentalNested.args")
public void testExperimentalNested() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/experimentalNested.args");
doJvmTest(fileName);
}
@TestMetadata("experimentalRuntimeScope.args")
public void testExperimentalRuntimeScope() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/experimentalRuntimeScope.args");
doJvmTest(fileName);
}
@TestMetadata("experimentalUnresolved.args")
public void testExperimentalUnresolved() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/experimentalUnresolved.args");
doJvmTest(fileName);
}
@TestMetadata("extraArgumentPassedInObsoleteForm.args")
public void testExtraArgumentPassedInObsoleteForm() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/extraArgumentPassedInObsoleteForm.args");
@@ -42,6 +42,10 @@ class AnalysisFlag<out T> internal constructor(
object Jsr305StateWarnByDefault {
operator fun provideDelegate(instance: Any?, property: KProperty<*>) = Flag(property.name, Jsr305State.DEFAULT)
}
object ListOfStrings {
operator fun provideDelegate(instance: Any?, property: KProperty<*>) = Flag(property.name, emptyList<String>())
}
}
companion object Flags {
@@ -56,5 +60,11 @@ class AnalysisFlag<out T> internal constructor(
@JvmStatic
val allowKotlinPackage by Flag.Boolean
@JvmStatic
val experimental by Flag.ListOfStrings
@JvmStatic
val useExperimental by Flag.ListOfStrings
}
}