Skip to content

Commit f60d035

Browse files
committed
fix #10810: pass aliases to setting constructor
1 parent 4b9bf18 commit f60d035

File tree

3 files changed

+61
-55
lines changed

3 files changed

+61
-55
lines changed

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ trait CommonScalaSettings { self: Settings.SettingGroup =>
1212
protected def defaultClasspath: String = sys.env.getOrElse("CLASSPATH", ".")
1313

1414
/** Path related settings */
15-
val bootclasspath: Setting[String] = PathSetting("-bootclasspath", "Override location of bootstrap class files.", Defaults.scalaBootClassPath) withAbbreviation "--boot-class-path"
16-
val extdirs: Setting[String] = PathSetting("-extdirs", "Override location of installed extensions.", Defaults.scalaExtDirs) withAbbreviation "--extension-directories"
17-
val javabootclasspath: Setting[String] = PathSetting("-javabootclasspath", "Override java boot classpath.", Defaults.javaBootClassPath) withAbbreviation "--java-boot-class-path"
18-
val javaextdirs: Setting[String] = PathSetting("-javaextdirs", "Override java extdirs classpath.", Defaults.javaExtDirs) withAbbreviation "--java-extension-directories"
19-
val sourcepath: Setting[String] = PathSetting("-sourcepath", "Specify location(s) of source files.", Defaults.scalaSourcePath) withAbbreviation "--source-path"
15+
val bootclasspath: Setting[String] = PathSetting("-bootclasspath", "Override location of bootstrap class files.", Defaults.scalaBootClassPath, aliases = List("--boot-class-path"))
16+
val extdirs: Setting[String] = PathSetting("-extdirs", "Override location of installed extensions.", Defaults.scalaExtDirs, aliases = List("--extension-directories"))
17+
val javabootclasspath: Setting[String] = PathSetting("-javabootclasspath", "Override java boot classpath.", Defaults.javaBootClassPath, aliases = List("--java-boot-class-path"))
18+
val javaextdirs: Setting[String] = PathSetting("-javaextdirs", "Override java extdirs classpath.", Defaults.javaExtDirs, aliases = List("--java-extension-directories"))
19+
val sourcepath: Setting[String] = PathSetting("-sourcepath", "Specify location(s) of source files.", Defaults.scalaSourcePath, aliases = List("--source-path"))
2020
val sourceroot: Setting[String] = PathSetting("-sourceroot", "Specify workspace root directory.", ".")
2121

22-
val classpath: Setting[String] = PathSetting("-classpath", "Specify where to find user class files.", defaultClasspath) withAbbreviation "-cp" withAbbreviation "--class-path"
22+
val classpath: Setting[String] = PathSetting("-classpath", "Specify where to find user class files.", defaultClasspath, aliases = List("-cp", "--class-path"))
2323
val outputDir: Setting[AbstractFile] = OutputSetting("-d", "directory|jar", "Destination for generated classfiles.",
2424
new PlainDirectory(Directory(".")))
25-
val color: Setting[String] = ChoiceSetting("-color", "mode", "Colored output", List("always", "never"/*, "auto"*/), "always"/* "auto"*/) withAbbreviation "--color"
26-
val verbose: Setting[Boolean] = BooleanSetting("-verbose", "Output messages about what the compiler is doing.") withAbbreviation "--verbose"
27-
val version: Setting[Boolean] = BooleanSetting("-version", "Print product version and exit.") withAbbreviation "--version"
28-
val pageWidth: Setting[Int] = IntSetting("-pagewidth", "Set page width", 80) withAbbreviation "--page-width"
29-
val silentWarnings: Setting[Boolean] = BooleanSetting("-nowarn", "Silence all warnings.") withAbbreviation "--no-warnings"
25+
val color: Setting[String] = ChoiceSetting("-color", "mode", "Colored output", List("always", "never"/*, "auto"*/), "always"/* "auto"*/, aliases = List("--color"))
26+
val verbose: Setting[Boolean] = BooleanSetting("-verbose", "Output messages about what the compiler is doing.", aliases = List("--verbose"))
27+
val version: Setting[Boolean] = BooleanSetting("-version", "Print product version and exit.", aliases = List("--version"))
28+
val pageWidth: Setting[Int] = IntSetting("-pagewidth", "Set page width", 80, aliases = List("--page-width"))
29+
val silentWarnings: Setting[Boolean] = BooleanSetting("-nowarn", "Silence all warnings.", aliases = List("--no-warnings"))
3030

3131
/** Other settings */
32-
val encoding: Setting[String] = StringSetting("-encoding", "encoding", "Specify character encoding used by source files.", Properties.sourceEncoding) withAbbreviation "--encoding"
33-
val usejavacp: Setting[Boolean] = BooleanSetting("-usejavacp", "Utilize the java.class.path in classpath resolution.") withAbbreviation "--use-java-class-path"
32+
val encoding: Setting[String] = StringSetting("-encoding", "encoding", "Specify character encoding used by source files.", Properties.sourceEncoding, aliases = List("--encoding"))
33+
val usejavacp: Setting[Boolean] = BooleanSetting("-usejavacp", "Utilize the java.class.path in classpath resolution.", aliases = List("--use-java-class-path"))
3434

3535
/** Plugin-related setting */
3636
val plugin: Setting[List[String]] = MultiStringSetting ("-Xplugin", "paths", "Load a plugin from each classpath.")
@@ -88,19 +88,19 @@ class ScalaSettings extends Settings.SettingGroup with CommonScalaSettings {
8888
/** Path related settings */
8989
val semanticdbTarget: Setting[String] = PathSetting("-semanticdb-target", "Specify an alternative output directory for SemanticDB files.", "")
9090

91-
val deprecation: Setting[Boolean] = BooleanSetting("-deprecation", "Emit warning and location for usages of deprecated APIs.") withAbbreviation "--deprecation"
92-
val explainTypes: Setting[Boolean] = BooleanSetting("-explain-types", "Explain type errors in more detail.") withAbbreviation "--explain-types"
93-
val explain: Setting[Boolean] = BooleanSetting("-explain", "Explain errors in more detail.") withAbbreviation "--explain"
94-
val feature: Setting[Boolean] = BooleanSetting("-feature", "Emit warning and location for usages of features that should be imported explicitly.") withAbbreviation "--feature"
95-
val help: Setting[Boolean] = BooleanSetting("-help", "Print a synopsis of standard options.") withAbbreviation "--help"
96-
val release: Setting[String] = ChoiceSetting("-release", "release", "Compile code with classes specific to the given version of the Java platform available on the classpath and emit bytecode for this version.", supportedReleaseVersions, "").withAbbreviation("--release")
97-
val source: Setting[String] = ChoiceSetting("-source", "source version", "source version", List("3.0", "3.1", "3.0-migration", "3.1-migration"), "3.0").withAbbreviation("--source")
98-
val scalajs: Setting[Boolean] = BooleanSetting("-scalajs", "Compile in Scala.js mode (requires scalajs-library.jar on the classpath).") withAbbreviation "--scalajs"
99-
val unchecked: Setting[Boolean] = BooleanSetting("-unchecked", "Enable additional warnings where generated code depends on assumptions.") withAbbreviation "--unchecked"
100-
val uniqid: Setting[Boolean] = BooleanSetting("-uniqid", "Uniquely tag all identifiers in debugging output.") withAbbreviation "--unique-id"
101-
val language: Setting[List[String]] = MultiStringSetting("-language", "feature", "Enable one or more language features.") withAbbreviation "--language"
102-
val rewrite: Setting[Option[Rewrites]] = OptionSetting[Rewrites]("-rewrite", "When used in conjunction with a `...-migration` source version, rewrites sources to migrate to new version.") withAbbreviation "--rewrite"
103-
val fromTasty: Setting[Boolean] = BooleanSetting("-from-tasty", "Compile classes from tasty files. The arguments are .tasty or .jar files.") withAbbreviation "--from-tasty"
91+
val deprecation: Setting[Boolean] = BooleanSetting("-deprecation", "Emit warning and location for usages of deprecated APIs.", aliases = List("--deprecation"))
92+
val explainTypes: Setting[Boolean] = BooleanSetting("-explain-types", "Explain type errors in more detail.", aliases = List("--explain-types"))
93+
val explain: Setting[Boolean] = BooleanSetting("-explain", "Explain errors in more detail.", aliases = List("--explain"))
94+
val feature: Setting[Boolean] = BooleanSetting("-feature", "Emit warning and location for usages of features that should be imported explicitly.", aliases = List("--feature"))
95+
val help: Setting[Boolean] = BooleanSetting("-help", "Print a synopsis of standard options.", aliases = List("--help"))
96+
val release: Setting[String] = ChoiceSetting("-release", "release", "Compile code with classes specific to the given version of the Java platform available on the classpath and emit bytecode for this version.", supportedReleaseVersions, "", aliases = List("--release"))
97+
val source: Setting[String] = ChoiceSetting("-source", "source version", "source version", List("3.0", "3.1", "3.0-migration", "3.1-migration"), "3.0", aliases = List("--source"))
98+
val scalajs: Setting[Boolean] = BooleanSetting("-scalajs", "Compile in Scala.js mode (requires scalajs-library.jar on the classpath).", aliases = List("--scalajs"))
99+
val unchecked: Setting[Boolean] = BooleanSetting("-unchecked", "Enable additional warnings where generated code depends on assumptions.", aliases = List("--unchecked"))
100+
val uniqid: Setting[Boolean] = BooleanSetting("-uniqid", "Uniquely tag all identifiers in debugging output.", aliases = List("--unique-id"))
101+
val language: Setting[List[String]] = MultiStringSetting("-language", "feature", "Enable one or more language features.", aliases = List("--language"))
102+
val rewrite: Setting[Option[Rewrites]] = OptionSetting[Rewrites]("-rewrite", "When used in conjunction with a `...-migration` source version, rewrites sources to migrate to new version.", aliases = List("--rewrite"))
103+
val fromTasty: Setting[Boolean] = BooleanSetting("-from-tasty", "Compile classes from tasty files. The arguments are .tasty or .jar files.", aliases = List("--from-tasty"))
104104

105105
val newSyntax: Setting[Boolean] = BooleanSetting("-new-syntax", "Require `then` and `do` in control expressions.")
106106
val oldSyntax: Setting[Boolean] = BooleanSetting("-old-syntax", "Require `(...)` around conditions.")
@@ -109,8 +109,8 @@ class ScalaSettings extends Settings.SettingGroup with CommonScalaSettings {
109109
val YindentColons: Setting[Boolean] = BooleanSetting("-Yindent-colons", "Allow colons at ends-of-lines to start indentation blocks.")
110110

111111
/** Decompiler settings */
112-
val printTasty: Setting[Boolean] = BooleanSetting("-print-tasty", "Prints the raw tasty.") withAbbreviation "--print-tasty"
113-
val printLines: Setting[Boolean] = BooleanSetting("-print-lines", "Show source code line numbers.") withAbbreviation "--print-lines"
112+
val printTasty: Setting[Boolean] = BooleanSetting("-print-tasty", "Prints the raw tasty.", aliases = List("--print-tasty"))
113+
val printLines: Setting[Boolean] = BooleanSetting("-print-lines", "Show source code line numbers.", aliases = List("--print-lines"))
114114

115115
/** Scala.js-related settings */
116116
val scalajsGenStaticForwardersForNonTopLevelObjects: Setting[Boolean] = BooleanSetting("-scalajs-genStaticForwardersForNonTopLevelObjects", "Generate static forwarders even for non-top-level objects (Scala.js only)")
@@ -135,8 +135,8 @@ class ScalaSettings extends Settings.SettingGroup with CommonScalaSettings {
135135
val XverifySignatures: Setting[Boolean] = BooleanSetting("-Xverify-signatures", "Verify generic signatures in generated bytecode.")
136136
val XignoreScala2Macros: Setting[Boolean] = BooleanSetting("-Xignore-scala2-macros", "Ignore errors when compiling code that calls Scala2 macros, these will fail at runtime.")
137137
val XimportSuggestionTimeout: Setting[Int] = IntSetting("-Ximport-suggestion-timeout", "Timeout (in ms) for searching for import suggestions when errors are reported.", 8000)
138-
val Xsemanticdb: Setting[Boolean] = BooleanSetting("-Xsemanticdb", "Store information in SemanticDB.").withAbbreviation("-Ysemanticdb")
139-
val Xtarget: Setting[String] = ChoiceSetting("-Xtarget", "target", "Emit bytecode for the specified version of the Java platform. This might produce bytecode that will break at runtime. When on JDK 9+, consider -release as a safer alternative.", supportedTargetVersions, "") withAbbreviation "--Xtarget"
138+
val Xsemanticdb: Setting[Boolean] = BooleanSetting("-Xsemanticdb", "Store information in SemanticDB.", aliases = List("-Ysemanticdb"))
139+
val Xtarget: Setting[String] = ChoiceSetting("-Xtarget", "target", "Emit bytecode for the specified version of the Java platform. This might produce bytecode that will break at runtime. When on JDK 9+, consider -release as a safer alternative.", supportedTargetVersions, "", aliases = List("--Xtarget"))
140140

141141
val XmixinForceForwarders = ChoiceSetting(
142142
name = "-Xmixin-force-forwarders",
@@ -175,7 +175,7 @@ class ScalaSettings extends Settings.SettingGroup with CommonScalaSettings {
175175
val YnoPredef: Setting[Boolean] = BooleanSetting("-Yno-predef", "Compile without importing Predef.")
176176
val Yskip: Setting[List[String]] = PhasesSetting("-Yskip", "Skip")
177177
val Ydumpclasses: Setting[String] = StringSetting("-Ydump-classes", "dir", "Dump the generated bytecode to .class files (useful for reflective compilation that utilizes in-memory classloaders).", "")
178-
val YstopAfter: Setting[List[String]] = PhasesSetting("-Ystop-after", "Stop after") withAbbreviation ("-stop") // backward compat
178+
val YstopAfter: Setting[List[String]] = PhasesSetting("-Ystop-after", "Stop after", aliases = List("-stop")) // backward compat
179179
val YstopBefore: Setting[List[String]] = PhasesSetting("-Ystop-before", "Stop before") // stop before erasure as long as we have not debugged it fully
180180
val YshowSuppressedErrors: Setting[Boolean] = BooleanSetting("-Yshow-suppressed-errors", "Also show follow-on errors and warnings that are normally suppressed.")
181181
val YdetailedStats: Setting[Boolean] = BooleanSetting("-Ydetailed-stats", "Show detailed internal compiler stats (needs Stats.enabled to be set to true).")

compiler/src/dotty/tools/dotc/config/Settings.scala

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@ object Settings {
6767

6868
private var changed: Boolean = false
6969

70-
def withAbbreviation(abbrv: String): Setting[T] =
71-
copy(aliases = aliases :+ abbrv)(idx)
72-
73-
def dependsOn[U](setting: Setting[U], value: U): Setting[T] =
74-
copy(depends = depends :+ (setting, value))(idx)
75-
7670
def valueIn(state: SettingsState): T =
7771
state.value(idx).asInstanceOf[T]
7872

@@ -170,9 +164,11 @@ object Settings {
170164
missingArg
171165
}
172166

167+
def matches(argName: String) = (name :: aliases).exists(_ == argName)
168+
173169
if (prefix != "" && arg.startsWith(prefix))
174170
doSet(arg drop prefix.length)
175-
else if (prefix == "" && name == arg.takeWhile(_ != ':'))
171+
else if (prefix == "" && matches(arg.takeWhile(_ != ':')))
176172
doSet(arg.dropWhile(_ != ':').drop(1))
177173
else
178174
state
@@ -257,43 +253,40 @@ object Settings {
257253
setting
258254
}
259255

260-
def BooleanSetting(name: String, descr: String, initialValue: Boolean = false): Setting[Boolean] =
261-
publish(Setting(name, descr, initialValue))
256+
def BooleanSetting(name: String, descr: String, initialValue: Boolean = false, aliases: List[String] = Nil): Setting[Boolean] =
257+
publish(Setting(name, descr, initialValue, aliases = aliases))
262258

263259
def StringSetting(name: String, helpArg: String, descr: String, default: String, aliases: List[String] = Nil): Setting[String] =
264260
publish(Setting(name, descr, default, helpArg, aliases = aliases))
265261

266-
def ChoiceSetting(name: String, helpArg: String, descr: String, choices: List[String], default: String): Setting[String] =
267-
publish(Setting(name, descr, default, helpArg, Some(choices)))
262+
def ChoiceSetting(name: String, helpArg: String, descr: String, choices: List[String], default: String, aliases: List[String] = Nil): Setting[String] =
263+
publish(Setting(name, descr, default, helpArg, Some(choices), aliases = aliases))
268264

269-
def IntSetting(name: String, descr: String, default: Int): Setting[Int] =
270-
publish(Setting(name, descr, default))
265+
def IntSetting(name: String, descr: String, default: Int, aliases: List[String] = Nil): Setting[Int] =
266+
publish(Setting(name, descr, default, aliases = aliases))
271267

272268
def IntChoiceSetting(name: String, descr: String, choices: Seq[Int], default: Int): Setting[Int] =
273269
publish(Setting(name, descr, default, choices = Some(choices)))
274270

275-
def MultiStringSetting(name: String, helpArg: String, descr: String): Setting[List[String]] =
276-
publish(Setting(name, descr, Nil, helpArg))
271+
def MultiStringSetting(name: String, helpArg: String, descr: String, aliases: List[String] = Nil): Setting[List[String]] =
272+
publish(Setting(name, descr, Nil, helpArg, aliases = aliases))
277273

278274
def OutputSetting(name: String, helpArg: String, descr: String, default: AbstractFile): Setting[AbstractFile] =
279275
publish(Setting(name, descr, default, helpArg))
280276

281-
def PathSetting(name: String, descr: String, default: String): Setting[String] =
282-
publish(Setting(name, descr, default))
283-
284-
def PathSetting(name: String, helpArg: String, descr: String, default: String): Setting[String] =
285-
publish(Setting(name, descr, default, helpArg))
277+
def PathSetting(name: String, descr: String, default: String, aliases: List[String] = Nil): Setting[String] =
278+
publish(Setting(name, descr, default, aliases = aliases))
286279

287-
def PhasesSetting(name: String, descr: String, default: String = ""): Setting[List[String]] =
288-
publish(Setting(name, descr, if (default.isEmpty) Nil else List(default)))
280+
def PhasesSetting(name: String, descr: String, default: String = "", aliases: List[String] = Nil): Setting[List[String]] =
281+
publish(Setting(name, descr, if (default.isEmpty) Nil else List(default), aliases = aliases))
289282

290283
def PrefixSetting(name: String, pre: String, descr: String): Setting[List[String]] =
291284
publish(Setting(name, descr, Nil, prefix = pre))
292285

293286
def VersionSetting(name: String, descr: String, default: ScalaVersion = NoScalaVersion): Setting[ScalaVersion] =
294287
publish(Setting(name, descr, default))
295288

296-
def OptionSetting[T: ClassTag](name: String, descr: String): Setting[Option[T]] =
297-
publish(Setting(name, descr, None, propertyClass = Some(implicitly[ClassTag[T]].runtimeClass)))
289+
def OptionSetting[T: ClassTag](name: String, descr: String, aliases: List[String] = Nil): Setting[Option[T]] =
290+
publish(Setting(name, descr, None, propertyClass = Some(implicitly[ClassTag[T]].runtimeClass), aliases = aliases))
298291
}
299292
}

compiler/test/dotty/tools/dotc/config/ScalaSettingsTests.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ import org.junit.Assert._
99

1010
class ScalaSettingsTests:
1111

12+
@Test def `A setting with aliases is accepted`: Unit =
13+
class MySettings extends SettingGroup:
14+
val classpath: Setting[String] = PathSetting("-classpath", "Specify where to find user class files.", ".", aliases = List("--class-path", "-cp"))
15+
val settings = MySettings()
16+
val args = tokenize("-cp path/to/classes1:other/path/to/classes2")
17+
val summary = ArgsSummary(settings.defaultState, args, errors = Nil, warnings = Nil)
18+
val res = settings.processArguments(summary, processAll = true, skipped = Nil)
19+
val classpath = settings.classpath.valueIn(res.sstate)
20+
assertEquals(2, args.length)
21+
assertTrue(s"found warnings: ${res.warnings}", res.warnings.isEmpty)
22+
assertTrue(s"found errors: ${res.errors}", res.errors.isEmpty)
23+
assertTrue("wrong classpath", classpath == "path/to/classes1:other/path/to/classes2")
24+
1225
@Test def `A multistring setting is multivalued`: Unit =
1326
class SUT extends SettingGroup:
1427
val language: Setting[List[String]] = MultiStringSetting("-language", "feature", "Enable one or more language features.")

0 commit comments

Comments
 (0)