From 6c47b4a39ce30b20a72d66328b2f548e44f6e14f Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Tue, 24 Oct 2017 13:17:28 +0300 Subject: [PATCH] Language feature for nullability assertions on enhanced nullability --- .../cli/common/arguments/K2JVMCompilerArguments.kt | 3 +++ .../jetbrains/kotlin/cli/common/CLICompiler.java | 9 +++++++++ .../org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt | 13 +++++++++++-- compiler/testData/cli/jvm/extraHelp.out | 2 ++ .../kotlin/config/LanguageVersionSettings.kt | 2 ++ 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt index a222d3dc1dd..acbc57204c4 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt @@ -106,6 +106,9 @@ class K2JVMCompilerArguments : CommonCompilerArguments() { @Argument(value = "-Xno-param-assertions", description = "Don't generate not-null assertions on parameters of methods accessible from Java") var noParamAssertions: Boolean by FreezableVar(false) + @Argument(value = "-Xstrict-java-nullability-assertions", description = "Generate nullability assertions for non-null Java expressions") + var strictJavaNullabilityAssertions: Boolean by FreezableVar(false) + @Argument(value = "-Xno-optimize", description = "Disable optimizations") var noOptimize: Boolean by FreezableVar(false) diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java b/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java index 7f36cc2a2d8..ad6240a5de4 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java @@ -204,6 +204,8 @@ public abstract class CLICompiler extends CLI extraLanguageFeatures.put(LanguageFeature.Coroutines, coroutinesState); } + setupPlatformSpecificLanguageFeatureSettings(extraLanguageFeatures, arguments); + CommonConfigurationKeysKt.setLanguageVersionSettings(configuration, new LanguageVersionSettingsImpl( languageVersion, ApiVersion.createByLanguageVersion(apiVersion), @@ -212,6 +214,13 @@ public abstract class CLICompiler extends CLI )); } + protected void setupPlatformSpecificLanguageFeatureSettings( + @NotNull Map extraLanguageFeatures, + @NotNull A commandLineArguments + ) { + // do nothing + } + @Nullable private static KotlinPaths computeKotlinPaths(@NotNull MessageCollector messageCollector, @NotNull CommonCompilerArguments arguments) { KotlinPaths paths; diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt index d63353c33da..fa09cb3ac2b 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt @@ -228,6 +228,17 @@ class K2JVMCompiler : CLICompiler() { } } + override fun setupPlatformSpecificLanguageFeatureSettings( + extraLanguageFeatures: MutableMap, + commandLineArguments: K2JVMCompilerArguments + ) { + if (commandLineArguments.strictJavaNullabilityAssertions) { + extraLanguageFeatures[LanguageFeature.StrictJavaNullabilityAssertions] = LanguageFeature.State.ENABLED + } + + super.setupPlatformSpecificLanguageFeatureSettings(extraLanguageFeatures, commandLineArguments) + } + private fun registerJavacIfNeeded(environment: KotlinCoreEnvironment, arguments: K2JVMCompilerArguments): Boolean { if (arguments.useJavac) { @@ -374,8 +385,6 @@ class K2JVMCompiler : CLICompiler() { configuration.put(JVMConfigurationKeys.ADD_BUILT_INS_FROM_COMPILER_TO_DEPENDENCIES, arguments.addCompilerBuiltIns) configuration.put(JVMConfigurationKeys.CREATE_BUILT_INS_FROM_MODULE_DEPENDENCIES, arguments.loadBuiltInsFromDependencies) - - arguments.declarationsOutputPath?.let { configuration.put(JVMConfigurationKeys.DECLARATIONS_JSON_PATH, it) } } diff --git a/compiler/testData/cli/jvm/extraHelp.out b/compiler/testData/cli/jvm/extraHelp.out index 63ecaa010a3..3792f66e16f 100644 --- a/compiler/testData/cli/jvm/extraHelp.out +++ b/compiler/testData/cli/jvm/extraHelp.out @@ -26,6 +26,8 @@ where advanced options include: Script resolver environment in key-value pairs (the value could be quoted and escaped) -Xsingle-module Combine modules for source files and binary dependencies into a single module -Xskip-runtime-version-check Allow Kotlin runtime libraries of incompatible versions in the classpath + -Xstrict-java-nullability-assertions + Generate nullability assertions for non-null Java expressions -Xuse-javac Use javac for Java source and class files analysis -Xuse-old-class-files-reading Use old class files reading implementation (may slow down the build and should be used in case of problems with the new implementation) -Xallow-kotlin-package Allow compiling code in package 'kotlin' and allow not requiring kotlin.stdlib in module-info diff --git a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt index 292b472f3d6..f01005467d6 100644 --- a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt +++ b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt @@ -74,6 +74,8 @@ enum class LanguageFeature( RestrictionOfWrongAnnotationsWithUseSiteTargetsOnTypes(KOTLIN_1_3), ProhibitInnerClassesOfGenericClassExtendingThrowable(KOTLIN_1_3), + StrictJavaNullabilityAssertions(sinceVersion = null, defaultState = State.DISABLED), + // Experimental features Coroutines(KOTLIN_1_1, ApiVersion.KOTLIN_1_1, "https://kotlinlang.org/docs/diagnostics/experimental-coroutines", State.ENABLED_WITH_WARNING),