diff --git a/compiler/src/dotty/tools/dotc/config/Settings.scala b/compiler/src/dotty/tools/dotc/config/Settings.scala index 277833afbd5d..084d79ed4556 100644 --- a/compiler/src/dotty/tools/dotc/config/Settings.scala +++ b/compiler/src/dotty/tools/dotc/config/Settings.scala @@ -69,11 +69,11 @@ object Settings: def updateIn(state: SettingsState, x: Any): SettingsState = x match case _: T => state.update(idx, x) - case _ => throw IllegalArgumentException(s"found: $x of type ${x.getClass.getName}, required: ${implicitly[ClassTag[T]]}") + case _ => throw IllegalArgumentException(s"found: $x of type ${x.getClass.getName}, required: ${summon[ClassTag[T]]}") def isDefaultIn(state: SettingsState): Boolean = valueIn(state) == default - def isMultivalue: Boolean = implicitly[ClassTag[T]] == ListTag + def isMultivalue: Boolean = summon[ClassTag[T]] == ListTag def legalChoices: String = choices match { @@ -106,6 +106,11 @@ object Settings: def missingArg = fail(s"missing argument for option $name", args) + def setBoolean(argValue: String, args: List[String]) = + if argValue.equalsIgnoreCase("true") || argValue.isEmpty then update(true, args) + else if argValue.equalsIgnoreCase("false") then update(false, args) + else fail(s"$argValue is not a valid choice for boolean setting $name", args) + def setString(argValue: String, args: List[String]) = choices match case Some(xs) if !xs.contains(argValue) => @@ -126,9 +131,9 @@ object Settings: catch case _: NumberFormatException => fail(s"$argValue is not an integer argument for $name", args) - def doSet(argRest: String) = ((implicitly[ClassTag[T]], args): @unchecked) match { + def doSet(argRest: String) = ((summon[ClassTag[T]], args): @unchecked) match { case (BooleanTag, _) => - update(true, args) + setBoolean(argRest, args) case (OptionTag, _) => update(Some(propertyClass.get.getConstructor().newInstance()), args) case (ListTag, _) => @@ -290,6 +295,6 @@ object Settings: publish(Setting(name, descr, default)) def OptionSetting[T: ClassTag](name: String, descr: String, aliases: List[String] = Nil): Setting[Option[T]] = - publish(Setting(name, descr, None, propertyClass = Some(implicitly[ClassTag[T]].runtimeClass), aliases = aliases)) + publish(Setting(name, descr, None, propertyClass = Some(summon[ClassTag[T]].runtimeClass), aliases = aliases)) } end Settings diff --git a/compiler/test/dotty/tools/dotc/SettingsTests.scala b/compiler/test/dotty/tools/dotc/SettingsTests.scala index e3076f055d51..a42ee38a649a 100644 --- a/compiler/test/dotty/tools/dotc/SettingsTests.scala +++ b/compiler/test/dotty/tools/dotc/SettingsTests.scala @@ -179,6 +179,22 @@ class SettingsTests { assertEquals(100, foo.value) } + @Test def `Set BooleanSettings correctly`: Unit = + object Settings extends SettingGroup: + val foo = BooleanSetting("-foo", "foo", false) + val bar = BooleanSetting("-bar", "bar", true) + val baz = BooleanSetting("-baz", "baz", false) + import Settings._ + + val args = List("-foo:true", "-bar:false", "-baz") + val summary = processArguments(args, processAll = true) + assertTrue(s"Setting args errors:\n ${summary.errors.take(5).mkString("\n ")}", summary.errors.isEmpty) + withProcessedArgs(summary) { + assertEquals(true, foo.value) + assertEquals(false, bar.value) + assertEquals(true, baz.value) + } + private def withProcessedArgs(summary: ArgsSummary)(f: SettingsState ?=> Unit) = f(using summary.sstate) extension [T](setting: Setting[T])