diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt index 3eca4a481ab..3ed7b91eefb 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt @@ -112,6 +112,12 @@ abstract class CommonCompilerArguments : CommonToolArguments() { ) var newInference: Boolean by FreezableVar(false) + @Argument( + value = "-Xlegacy-smart-cast-after-try", + description = "Allow var smart casts despite assignment in try block" + ) + var legacySmartCastAfterTry by FreezableVar(false) + open fun configureAnalysisFlags(collector: MessageCollector): MutableMap, Any> { return HashMap, Any>().apply { put(AnalysisFlag.skipMetadataVersionCheck, skipMetadataVersionCheck) 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 d19703e49e3..852cc901011 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java @@ -208,6 +208,10 @@ public abstract class CLICompiler extends CLI extraLanguageFeatures.put(LanguageFeature.NewInference, LanguageFeature.State.ENABLED); } + if (arguments.getLegacySmartCastAfterTry()) { + extraLanguageFeatures.put(LanguageFeature.SoundSmartCastsAfterTry, LanguageFeature.State.DISABLED); + } + setupPlatformSpecificLanguageFeatureSettings(extraLanguageFeatures, arguments); CommonConfigurationKeysKt.setLanguageVersionSettings(configuration, new LanguageVersionSettingsImpl( diff --git a/compiler/testData/cli/js/jsExtraHelp.out b/compiler/testData/cli/js/jsExtraHelp.out index b2537f72e14..8a4a9dd08c5 100644 --- a/compiler/testData/cli/js/jsExtraHelp.out +++ b/compiler/testData/cli/js/jsExtraHelp.out @@ -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 -Xintellij-plugin-root= 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 -Xnew-inference Enable new experimental generic type inference algorithm -Xno-check-actual Do not check presence of 'actual' modifier in multi-platform projects diff --git a/compiler/testData/cli/jvm/extraHelp.out b/compiler/testData/cli/jvm/extraHelp.out index 9dfb64ff290..897fc17b10a 100644 --- a/compiler/testData/cli/jvm/extraHelp.out +++ b/compiler/testData/cli/jvm/extraHelp.out @@ -34,6 +34,7 @@ where advanced options include: -Xcoroutines={enable|warn|error} Enable coroutines or report warnings or errors on declarations and use sites of 'suspend' modifier -Xintellij-plugin-root= 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 -Xnew-inference Enable new experimental generic type inference algorithm -Xno-check-actual Do not check presence of 'actual' modifier in multi-platform projects diff --git a/compiler/testData/cli/jvm/legacySmartCastsAfterTry.args b/compiler/testData/cli/jvm/legacySmartCastsAfterTry.args new file mode 100644 index 00000000000..2b146c76c33 --- /dev/null +++ b/compiler/testData/cli/jvm/legacySmartCastsAfterTry.args @@ -0,0 +1,4 @@ +$TESTDATA_DIR$/legacySmartCastsAfterTry.kt +-d +$TEMP_DIR$ +-Xlegacy-smart-cast-after-try diff --git a/compiler/testData/cli/jvm/legacySmartCastsAfterTry.kt b/compiler/testData/cli/jvm/legacySmartCastsAfterTry.kt new file mode 100644 index 00000000000..02cd99b5913 --- /dev/null +++ b/compiler/testData/cli/jvm/legacySmartCastsAfterTry.kt @@ -0,0 +1,9 @@ +fun foo() { + var some: String? = null + some = "alpha" + try { + some = "omega" + } + catch (e: Exception) {} + some.length +} \ No newline at end of file diff --git a/compiler/testData/cli/jvm/legacySmartCastsAfterTry.out b/compiler/testData/cli/jvm/legacySmartCastsAfterTry.out new file mode 100644 index 00000000000..909579400f6 --- /dev/null +++ b/compiler/testData/cli/jvm/legacySmartCastsAfterTry.out @@ -0,0 +1,4 @@ +compiler/testData/cli/jvm/legacySmartCastsAfterTry.kt:2:25: warning: variable 'some' initializer is redundant + var some: String? = null + ^ +OK diff --git a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java index 6f4b94d8e28..3d6f05ed9ad 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java @@ -338,6 +338,12 @@ public class CliTestGenerated extends AbstractCliTest { doJvmTest(fileName); } + @TestMetadata("legacySmartCastsAfterTry.args") + public void testLegacySmartCastsAfterTry() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/legacySmartCastsAfterTry.args"); + doJvmTest(fileName); + } + @TestMetadata("multipleTextRangesInDiagnosticsOrder.args") public void testMultipleTextRangesInDiagnosticsOrder() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/multipleTextRangesInDiagnosticsOrder.args");