Skip to content

Commit e16ff26

Browse files
Allow BooleanSettings to be set with a colon (#16425)
e.g. "-explain:true" and "-explain:false" should be allowed
2 parents e842810 + 54b1f61 commit e16ff26

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ object Settings:
6969

7070
def updateIn(state: SettingsState, x: Any): SettingsState = x match
7171
case _: T => state.update(idx, x)
72-
case _ => throw IllegalArgumentException(s"found: $x of type ${x.getClass.getName}, required: ${implicitly[ClassTag[T]]}")
72+
case _ => throw IllegalArgumentException(s"found: $x of type ${x.getClass.getName}, required: ${summon[ClassTag[T]]}")
7373

7474
def isDefaultIn(state: SettingsState): Boolean = valueIn(state) == default
7575

76-
def isMultivalue: Boolean = implicitly[ClassTag[T]] == ListTag
76+
def isMultivalue: Boolean = summon[ClassTag[T]] == ListTag
7777

7878
def legalChoices: String =
7979
choices match {
@@ -106,6 +106,11 @@ object Settings:
106106
def missingArg =
107107
fail(s"missing argument for option $name", args)
108108

109+
def setBoolean(argValue: String, args: List[String]) =
110+
if argValue.equalsIgnoreCase("true") || argValue.isEmpty then update(true, args)
111+
else if argValue.equalsIgnoreCase("false") then update(false, args)
112+
else fail(s"$argValue is not a valid choice for boolean setting $name", args)
113+
109114
def setString(argValue: String, args: List[String]) =
110115
choices match
111116
case Some(xs) if !xs.contains(argValue) =>
@@ -126,9 +131,9 @@ object Settings:
126131
catch case _: NumberFormatException =>
127132
fail(s"$argValue is not an integer argument for $name", args)
128133

129-
def doSet(argRest: String) = ((implicitly[ClassTag[T]], args): @unchecked) match {
134+
def doSet(argRest: String) = ((summon[ClassTag[T]], args): @unchecked) match {
130135
case (BooleanTag, _) =>
131-
update(true, args)
136+
setBoolean(argRest, args)
132137
case (OptionTag, _) =>
133138
update(Some(propertyClass.get.getConstructor().newInstance()), args)
134139
case (ListTag, _) =>
@@ -290,6 +295,6 @@ object Settings:
290295
publish(Setting(name, descr, default))
291296

292297
def OptionSetting[T: ClassTag](name: String, descr: String, aliases: List[String] = Nil): Setting[Option[T]] =
293-
publish(Setting(name, descr, None, propertyClass = Some(implicitly[ClassTag[T]].runtimeClass), aliases = aliases))
298+
publish(Setting(name, descr, None, propertyClass = Some(summon[ClassTag[T]].runtimeClass), aliases = aliases))
294299
}
295300
end Settings

compiler/test/dotty/tools/dotc/SettingsTests.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,25 @@ class SettingsTests {
179179
assertEquals(100, foo.value)
180180
}
181181

182+
@Test def `Set BooleanSettings correctly`: Unit =
183+
object Settings extends SettingGroup:
184+
val foo = BooleanSetting("-foo", "foo", false)
185+
val bar = BooleanSetting("-bar", "bar", true)
186+
val baz = BooleanSetting("-baz", "baz", false)
187+
val qux = BooleanSetting("-qux", "qux", false)
188+
import Settings._
189+
190+
val args = List("-foo:true", "-bar:false", "-baz", "-qux:true", "-qux:false")
191+
val summary = processArguments(args, processAll = true)
192+
assertTrue(s"Setting args errors:\n ${summary.errors.take(5).mkString("\n ")}", summary.errors.isEmpty)
193+
withProcessedArgs(summary) {
194+
assertEquals(true, foo.value)
195+
assertEquals(false, bar.value)
196+
assertEquals(true, baz.value)
197+
assertEquals(false, qux.value)
198+
assertEquals(List("Flag -qux set repeatedly"), summary.warnings)
199+
}
200+
182201
private def withProcessedArgs(summary: ArgsSummary)(f: SettingsState ?=> Unit) = f(using summary.sstate)
183202

184203
extension [T](setting: Setting[T])

0 commit comments

Comments
 (0)