Introduce "-api-version" CLI option
The `@SinceKotlin("X.Y.Z")` annotation now hides a particular declaration from
resolution when the API version specified by the `-api-version` option is
_less_ than X.Y.Z. The comparison is performed as for versions in Maven:
MavenComparableVersion is in fact a copy of
org.apache.maven.artifact.versioning.ComparableVersion.
Also support "!API_VERSION" directive in diagnostic tests
#KT-14298 Fixed
This commit is contained in:
+5
@@ -30,6 +30,11 @@ public abstract class CommonCompilerArguments {
|
||||
@ValueDescription("<version>")
|
||||
public String languageVersion;
|
||||
|
||||
@GradleOption(DefaultValues.LanguageVersions.class)
|
||||
@Argument(value = "api-version", description = "Allow to use declarations only from the specified version of bundled libraries")
|
||||
@ValueDescription("<version>")
|
||||
public String apiVersion;
|
||||
|
||||
@GradleOption(DefaultValues.BooleanFalseDefault.class)
|
||||
@Argument(value = "nowarn", description = "Generate no warnings")
|
||||
public boolean suppressWarnings;
|
||||
|
||||
@@ -248,27 +248,58 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
|
||||
configuration.put(CLIConfigurationKeys.COMPILER_JAR_LOCATOR, locator);
|
||||
}
|
||||
|
||||
if (arguments.languageVersion != null) {
|
||||
LanguageVersion languageVersion = LanguageVersion.fromVersionString(arguments.languageVersion);
|
||||
if (languageVersion != null) {
|
||||
configuration.put(CommonConfigurationKeys.LANGUAGE_VERSION_SETTINGS, new LanguageVersionSettingsImpl(languageVersion));
|
||||
LanguageVersion languageVersion = parseVersion(configuration, arguments.languageVersion, "language");
|
||||
LanguageVersion apiVersion = parseVersion(configuration, arguments.apiVersion, "API");
|
||||
if (languageVersion != null || apiVersion != null) {
|
||||
if (languageVersion == null) {
|
||||
// If only "-api-version" is specified, language version is assumed to be the latest
|
||||
languageVersion = LanguageVersion.LATEST;
|
||||
}
|
||||
else {
|
||||
List<String> versionStrings = ArraysKt.map(LanguageVersion.values(), new Function1<LanguageVersion, String>() {
|
||||
@Override
|
||||
public String invoke(LanguageVersion version) {
|
||||
return version.getVersionString();
|
||||
}
|
||||
});
|
||||
String message = "Unknown language version: " + arguments.languageVersion + "\n" +
|
||||
"Supported language versions: " + StringsKt.join(versionStrings, ", ");
|
||||
if (apiVersion == null) {
|
||||
// If only "-language-version" is specified, API version is assumed to be equal to the language version
|
||||
// (API version cannot be greater than the language version)
|
||||
apiVersion = languageVersion;
|
||||
}
|
||||
|
||||
if (apiVersion.compareTo(languageVersion) > 0) {
|
||||
configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY).report(
|
||||
CompilerMessageSeverity.ERROR, message, CompilerMessageLocation.NO_LOCATION
|
||||
CompilerMessageSeverity.ERROR,
|
||||
"-api-version (" + apiVersion.getVersionString() + ") cannot be greater than " +
|
||||
"-language-version (" + languageVersion.getVersionString() + ")",
|
||||
CompilerMessageLocation.NO_LOCATION
|
||||
);
|
||||
}
|
||||
|
||||
configuration.put(CommonConfigurationKeys.LANGUAGE_VERSION_SETTINGS,
|
||||
new LanguageVersionSettingsImpl(languageVersion, ApiVersion.createByLanguageVersion(apiVersion)));
|
||||
}
|
||||
}
|
||||
|
||||
private static LanguageVersion parseVersion(
|
||||
@NotNull CompilerConfiguration configuration, @Nullable String value, @NotNull String versionOf
|
||||
) {
|
||||
if (value == null) return null;
|
||||
|
||||
LanguageVersion version = LanguageVersion.fromVersionString(value);
|
||||
if (version != null) {
|
||||
return version;
|
||||
}
|
||||
|
||||
List<String> versionStrings = ArraysKt.map(LanguageVersion.values(), new Function1<LanguageVersion, String>() {
|
||||
@Override
|
||||
public String invoke(LanguageVersion version) {
|
||||
return version.getVersionString();
|
||||
}
|
||||
});
|
||||
String message = "Unknown " + versionOf + " version: " + value + "\n" +
|
||||
"Supported " + versionOf + " versions: " + StringsKt.join(versionStrings, ", ");
|
||||
configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY).report(
|
||||
CompilerMessageSeverity.ERROR, message, CompilerMessageLocation.NO_LOCATION
|
||||
);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected abstract void setupPlatformSpecificArgumentsAndServices(
|
||||
@NotNull CompilerConfiguration configuration, @NotNull A arguments, @NotNull Services services
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user