From a308ac800a236cc6d1447cbc56776b304c95e316 Mon Sep 17 00:00:00 2001 From: noti0na1 Date: Wed, 22 May 2024 18:15:17 +0200 Subject: [PATCH] Accept legacy language features --- .../src/dotty/tools/dotc/config/Feature.scala | 7 ++++++ .../tools/dotc/config/ScalaSettings.scala | 2 +- .../dotc/config/ScalaSettingsProperties.scala | 3 +++ .../dotty/tools/dotc/config/Settings.scala | 23 ++++++++++++------- .../test/dotty/tools/dotc/SettingsTests.scala | 8 +++---- 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/config/Feature.scala b/compiler/src/dotty/tools/dotc/config/Feature.scala index 91100627981b..c04c58b419c9 100644 --- a/compiler/src/dotty/tools/dotc/config/Feature.scala +++ b/compiler/src/dotty/tools/dotc/config/Feature.scala @@ -69,6 +69,13 @@ object Feature: (betterMatchTypeExtractors, "Enable better match type extractors") ) + // legacy language features from Scala 2 that are no longer supported. + val legacyFeatures = List( + "higherKinds", + "existentials", + "reflectiveCalls" + ) + private def enabledLanguageFeaturesBySetting(using Context): List[String] = ctx.settings.language.value.asInstanceOf diff --git a/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala b/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala index bb28e06150fe..c64521ec74e1 100644 --- a/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala +++ b/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala @@ -114,7 +114,7 @@ trait CommonScalaSettings: val explainTypes: Setting[Boolean] = BooleanSetting(RootSetting, "explain-types", "Explain type errors in more detail (deprecated, use -explain instead).", aliases = List("--explain-types", "-explaintypes")) val explainCyclic: Setting[Boolean] = BooleanSetting(RootSetting, "explain-cyclic", "Explain cyclic reference errors in more detail.", aliases = List("--explain-cyclic")) val unchecked: Setting[Boolean] = BooleanSetting(RootSetting, "unchecked", "Enable additional warnings where generated code depends on assumptions.", initialValue = true, aliases = List("--unchecked")) - val language: Setting[List[ChoiceWithHelp[String]]] = MultiChoiceHelpSetting(RootSetting, "language", "feature", "Enable one or more language features.", choices = ScalaSettingsProperties.supportedLanguageFeatures, default = Nil, aliases = List("--language")) + val language: Setting[List[ChoiceWithHelp[String]]] = MultiChoiceHelpSetting(RootSetting, "language", "feature", "Enable one or more language features.", choices = ScalaSettingsProperties.supportedLanguageFeatures, legacyChoices = ScalaSettingsProperties.legacyLanguageFeatures, default = Nil, aliases = List("--language")) val experimental: Setting[Boolean] = BooleanSetting(RootSetting, "experimental", "Annotate all top-level definitions with @experimental. This enables the use of experimental features anywhere in the project.") /* Coverage settings */ diff --git a/compiler/src/dotty/tools/dotc/config/ScalaSettingsProperties.scala b/compiler/src/dotty/tools/dotc/config/ScalaSettingsProperties.scala index a839d3e3be19..022916cc9f53 100644 --- a/compiler/src/dotty/tools/dotc/config/ScalaSettingsProperties.scala +++ b/compiler/src/dotty/tools/dotc/config/ScalaSettingsProperties.scala @@ -30,6 +30,9 @@ object ScalaSettingsProperties: def supportedLanguageFeatures: List[ChoiceWithHelp[String]] = Feature.values.map((n, d) => ChoiceWithHelp(n.toString, d)) + val legacyLanguageFeatures: List[String] = + Feature.legacyFeatures + def defaultClasspath: String = sys.env.getOrElse("CLASSPATH", ".") def defaultPageWidth: Int = { diff --git a/compiler/src/dotty/tools/dotc/config/Settings.scala b/compiler/src/dotty/tools/dotc/config/Settings.scala index 5042737c30cb..9250303e8cc8 100644 --- a/compiler/src/dotty/tools/dotc/config/Settings.scala +++ b/compiler/src/dotty/tools/dotc/config/Settings.scala @@ -85,7 +85,9 @@ object Settings: propertyClass: Option[Class[?]] = None, deprecation: Option[Deprecation] = None, // kept only for -Xkind-projector option compatibility - legacyArgs: Boolean = false)(private[Settings] val idx: Int): + legacyArgs: Boolean = false, + // accept legacy choices (for example, valid in Scala 2 but no longer supported) + legacyChoices: Option[Seq[?]] = None)(private[Settings] val idx: Int): validateSettingString(prefix.getOrElse(name)) aliases.foreach(validateSettingString) @@ -206,9 +208,14 @@ object Settings: def appendList(strings: List[String], argValue: String, args: List[String]) = choices match - case Some(valid) => strings.filterNot(valid.contains) match - case Nil => update(strings, argValue, args) - case invalid => invalidChoices(invalid) + case Some(valid) => strings.partition(valid.contains) match + case (_, Nil) => update(strings, argValue, args) + case (validStrs, invalidStrs) => legacyChoices match + case Some(validBefore) => + invalidStrs.filterNot(validBefore.contains) match + case Nil => update(validStrs, argValue, args) + case realInvalidStrs => invalidChoices(realInvalidStrs) + case _ => invalidChoices(invalidStrs) case _ => update(strings, argValue, args) def doSet(argRest: String) = @@ -380,11 +387,11 @@ object Settings: def ChoiceSetting(category: SettingCategory, name: String, helpArg: String, descr: String, choices: List[String], default: String, aliases: List[String] = Nil, legacyArgs: Boolean = false, deprecation: Option[Deprecation] = None): Setting[String] = publish(Setting(category, prependName(name), descr, default, helpArg, Some(choices), aliases = aliases, legacyArgs = legacyArgs, deprecation = deprecation)) - def MultiChoiceSetting(category: SettingCategory, name: String, helpArg: String, descr: String, choices: List[String], default: List[String] = Nil, aliases: List[String] = Nil, deprecation: Option[Deprecation] = None): Setting[List[String]] = - publish(Setting(category, prependName(name), descr, default, helpArg, Some(choices), aliases = aliases, deprecation = deprecation)) + def MultiChoiceSetting(category: SettingCategory, name: String, helpArg: String, descr: String, choices: List[String], default: List[String] = Nil, legacyChoices: List[String] = Nil, aliases: List[String] = Nil, deprecation: Option[Deprecation] = None): Setting[List[String]] = + publish(Setting(category, prependName(name), descr, default, helpArg, Some(choices), legacyChoices = Some(legacyChoices), aliases = aliases, deprecation = deprecation)) - def MultiChoiceHelpSetting(category: SettingCategory, name: String, helpArg: String, descr: String, choices: List[ChoiceWithHelp[String]], default: List[ChoiceWithHelp[String]], aliases: List[String] = Nil, deprecation: Option[Deprecation] = None): Setting[List[ChoiceWithHelp[String]]] = - publish(Setting(category, prependName(name), descr, default, helpArg, Some(choices), aliases = aliases, deprecation = deprecation)) + def MultiChoiceHelpSetting(category: SettingCategory, name: String, helpArg: String, descr: String, choices: List[ChoiceWithHelp[String]], default: List[ChoiceWithHelp[String]], legacyChoices: List[String] = Nil, aliases: List[String] = Nil, deprecation: Option[Deprecation] = None): Setting[List[ChoiceWithHelp[String]]] = + publish(Setting(category, prependName(name), descr, default, helpArg, Some(choices), legacyChoices = Some(legacyChoices), aliases = aliases, deprecation = deprecation)) def IntSetting(category: SettingCategory, name: String, descr: String, default: Int, aliases: List[String] = Nil, deprecation: Option[Deprecation] = None): Setting[Int] = publish(Setting(category, prependName(name), descr, default, aliases = aliases, deprecation = deprecation)) diff --git a/compiler/test/dotty/tools/dotc/SettingsTests.scala b/compiler/test/dotty/tools/dotc/SettingsTests.scala index 301dc10ab54e..996ab22f67b1 100644 --- a/compiler/test/dotty/tools/dotc/SettingsTests.scala +++ b/compiler/test/dotty/tools/dotc/SettingsTests.scala @@ -272,8 +272,8 @@ class SettingsTests { val booleanSetting = BooleanSetting(RootSetting, "booleanSetting", "booleanSetting", false) val stringSetting = StringSetting(RootSetting, "stringSetting", "stringSetting", "", "test") val choiceSetting = ChoiceSetting(RootSetting, "choiceSetting", "choiceSetting", "", List("a", "b"), "a") - val multiChoiceSetting= MultiChoiceSetting(RootSetting, "multiChoiceSetting", "multiChoiceSetting", "", List("a", "b"), List()) - val multiChoiceHelpSetting= MultiChoiceHelpSetting(RootSetting, "multiChoiceHelpSetting", "multiChoiceHelpSetting", "", List(ChoiceWithHelp("a", "a"), ChoiceWithHelp("b", "b")), List()) + val multiChoiceSetting= MultiChoiceSetting(RootSetting, "multiChoiceSetting", "multiChoiceSetting", "", List("a", "b"), List(), legacyChoices = List("c")) + val multiChoiceHelpSetting= MultiChoiceHelpSetting(RootSetting, "multiChoiceHelpSetting", "multiChoiceHelpSetting", "", List(ChoiceWithHelp("a", "a"), ChoiceWithHelp("b", "b")), List(), legacyChoices = List("c")) val intSetting = IntSetting(RootSetting, "intSetting", "intSetting", 0) val intChoiceSetting = IntChoiceSetting(RootSetting, "intChoiceSetting", "intChoiceSetting", List(1,2,3), 1) val multiStringSetting = MultiStringSetting(RootSetting, "multiStringSetting", "multiStringSetting", "", List("a", "b"), List()) @@ -289,8 +289,8 @@ class SettingsTests { List("-booleanSetting", "true"), List("-stringSetting", "newTest"), List("-choiceSetting", "b"), - List("-multiChoiceSetting", "a,b"), - List("-multiChoiceHelpSetting", "a,b"), + List("-multiChoiceSetting", "a,b,c"), + List("-multiChoiceHelpSetting", "a,b,c"), List("-intSetting", "42"), List("-intChoiceSetting", "2"), List("-multiStringSetting", "a,b"),