Skip to content

Commit e7ddf3f

Browse files
committed
Refactor settings & improve dx
1 parent 6e8b355 commit e7ddf3f

File tree

9 files changed

+393
-345
lines changed

9 files changed

+393
-345
lines changed

compiler/src/dotty/tools/backend/jvm/BackendUtils.scala

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,7 @@ class BackendUtils(val postProcessor: PostProcessor) {
2020
import bTypes.*
2121
import coreBTypes.jliLambdaMetaFactoryAltMetafactoryHandle
2222

23-
// Keep synchronized with `minTargetVersion` and `maxTargetVersion` in ScalaSettings
24-
lazy val classfileVersion: Int = compilerSettings.target match {
25-
case "8" => asm.Opcodes.V1_8
26-
case "9" => asm.Opcodes.V9
27-
case "10" => asm.Opcodes.V10
28-
case "11" => asm.Opcodes.V11
29-
case "12" => asm.Opcodes.V12
30-
case "13" => asm.Opcodes.V13
31-
case "14" => asm.Opcodes.V14
32-
case "15" => asm.Opcodes.V15
33-
case "16" => asm.Opcodes.V16
34-
case "17" => asm.Opcodes.V17
35-
case "18" => asm.Opcodes.V18
36-
case "19" => asm.Opcodes.V19
37-
case "20" => asm.Opcodes.V20
38-
case "21" => asm.Opcodes.V21
39-
case "22" => asm.Opcodes.V22
40-
}
23+
lazy val classfileVersion: Int = BackendUtils.classfileVersionMap(compilerSettings.target)
4124

4225
lazy val extraProc: Int = {
4326
import GenBCodeOps.addFlagIf
@@ -184,3 +167,23 @@ class BackendUtils(val postProcessor: PostProcessor) {
184167
}
185168
}
186169
}
170+
171+
object BackendUtils {
172+
lazy val classfileVersionMap = Map(
173+
"8" -> asm.Opcodes.V1_8,
174+
"9" -> asm.Opcodes.V9,
175+
"10" -> asm.Opcodes.V10,
176+
"11" -> asm.Opcodes.V11,
177+
"12" -> asm.Opcodes.V12,
178+
"13" -> asm.Opcodes.V13,
179+
"14" -> asm.Opcodes.V14,
180+
"15" -> asm.Opcodes.V15,
181+
"16" -> asm.Opcodes.V16,
182+
"17" -> asm.Opcodes.V17,
183+
"18" -> asm.Opcodes.V18,
184+
"19" -> asm.Opcodes.V19,
185+
"20" -> asm.Opcodes.V20,
186+
"21" -> asm.Opcodes.V21,
187+
"22" -> asm.Opcodes.V22,
188+
)
189+
}

compiler/src/dotty/tools/dotc/Driver.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import dotty.tools.io.AbstractFile
1010
import reporting.*
1111
import core.Decorators.*
1212
import config.Feature
13+
import dotty.tools.dotc.config.ScalaSettings
1314

1415
import scala.util.control.NonFatal
1516
import fromtasty.{TASTYCompiler, TastyFileUtil}
@@ -199,6 +200,7 @@ class Driver {
199200
}
200201

201202
def main(args: Array[String]): Unit = {
203+
println("settings: " + ScalaSettings.allSettings)
202204
// Preload scala.util.control.NonFatal. Otherwise, when trying to catch a StackOverflowError,
203205
// we may try to load it but fail with another StackOverflowError and lose the original exception,
204206
// see <https://groups.google.com/forum/#!topic/scala-user/kte6nak-zPM>.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import Settings.*
55
import core.Contexts.*
66

77
abstract class CompilerCommand extends CliCommand:
8-
type ConcreteSettings = ScalaSettings
8+
type ConcreteSettings = ScalaSettings.type
99

10-
final def helpMsg(using settings: ScalaSettings)(using SettingsState, Context): String =
10+
final def helpMsg(using settings: ConcreteSettings)(using SettingsState, Context): String =
1111
settings.allSettings.find(isHelping) match
1212
case Some(s) => s.description
1313
case _ =>
@@ -20,7 +20,7 @@ abstract class CompilerCommand extends CliCommand:
2020
else if (settings.XshowPhases.value) phasesMessage
2121
else ""
2222

23-
final def isHelpFlag(using settings: ScalaSettings)(using SettingsState): Boolean =
23+
final def isHelpFlag(using settings: ConcreteSettings)(using SettingsState): Boolean =
2424
import settings.*
2525
val flags = Set(help, Vhelp, Whelp, Xhelp, Yhelp, showPlugins, XshowPhases)
2626
flags.exists(_.value) || allSettings.exists(isHelping)

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

Lines changed: 231 additions & 209 deletions
Large diffs are not rendered by default.

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

Lines changed: 111 additions & 89 deletions
Large diffs are not rendered by default.

compiler/src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ object Contexts {
513513
s"""Context(
514514
|${outersIterator.map(ctx => cinfo(using ctx)).mkString("\n\n")})""".stripMargin
515515

516-
def settings: ScalaSettings = base.settings
516+
def settings: ScalaSettings.type = base.settings
517517
def definitions: Definitions = base.definitions
518518
def platform: Platform = base.platform
519519
def pendingUnderlying: util.HashSet[Type] = base.pendingUnderlying
@@ -861,8 +861,7 @@ object Contexts {
861861
with Phases.PhasesBase
862862
with Plugins {
863863

864-
/** The applicable settings */
865-
val settings: ScalaSettings = new ScalaSettings
864+
val settings: ScalaSettings.type = ScalaSettings
866865

867866
/** The initial context */
868867
val initialCtx: Context = FreshContext.initial(this: @unchecked, settings)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ScalaCommandTest:
1717
def temporaryFolder = _temporaryFolder
1818

1919
@Test def `Simple one parameter`: Unit = inContext {
20-
val settings = config.ScalaSettings()
20+
val settings = config.ScalaSettings
2121
val args = "-cp path/to/classes1:other/path/to/classes2 files".split(" ")
2222
val summary = ScalacCommand.distill(args, settings)()
2323
given SettingsState = summary.sstate
@@ -26,7 +26,7 @@ class ScalaCommandTest:
2626
}
2727

2828
@Test def `Unfold @file`: Unit = inContext {
29-
val settings = config.ScalaSettings()
29+
val settings = config.ScalaSettings
3030
val file = temporaryFolder.newFile("config")
3131
val writer = java.io.FileWriter(file);
3232
writer.write("-sourceroot myNewRoot someMoreFiles");

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class SettingsTests {
4343

4444
@Test def acceptUnconstrained: Unit =
4545
object Settings extends SettingGroup:
46-
val foo = StringSetting("-foo", "foo", "Foo", "a")
47-
val bar = IntSetting("-bar", "Bar", 0)
46+
val foo = StringSetting("", "foo", "foo", "Foo", "a")
47+
val bar = IntSetting("", "bar", "Bar", 0)
4848

4949
val args = List("-foo", "b", "-bar", "1")
5050
val summary = Settings.processArguments(args, true)
@@ -72,7 +72,7 @@ class SettingsTests {
7272

7373
@Test def `dont crash on many options`: Unit =
7474
object Settings extends SettingGroup:
75-
val option = BooleanSetting("-option", "Some option")
75+
val option = BooleanSetting("", "-option", "Some option")
7676

7777
val limit = 6000
7878
val args = List.fill(limit)("-option")
@@ -87,7 +87,7 @@ class SettingsTests {
8787

8888
@Test def `bad option warning consumes an arg`: Unit =
8989
object Settings extends SettingGroup:
90-
val option = BooleanSetting("-option", "Some option")
90+
val option = BooleanSetting("", "-option", "Some option")
9191

9292
val args = List("-adoption", "dogs", "cats")
9393
val summary = Settings.processArguments(args, processAll = true)
@@ -97,7 +97,7 @@ class SettingsTests {
9797

9898
@Test def `bad option settings throws`: Unit =
9999
object Settings extends SettingGroup:
100-
val option = BooleanSetting("-option", "Some option")
100+
val option = BooleanSetting("", "-option", "Some option")
101101

102102
def checkMessage(s: String): (Throwable => Boolean) = t =>
103103
if t.getMessage == s then true
@@ -112,12 +112,12 @@ class SettingsTests {
112112

113113
@Test def validateChoices: Unit =
114114
object Settings extends SettingGroup:
115-
val foo = ChoiceSetting("-foo", "foo", "Foo", List("a", "b"), "a")
116-
val bar = IntChoiceSetting("-bar", "Bar", List(0, 1, 2), 0)
117-
val baz = IntChoiceSetting("-baz", "Baz", 0 to 10, 10)
115+
val foo = ChoiceSetting("", "-foo", "foo", "Foo", List("a", "b"), "a")
116+
val bar = IntChoiceSetting("", "-bar", "Bar", List(0, 1, 2), 0)
117+
val baz = IntChoiceSetting("", "-baz", "Baz", 0 to 10, 10)
118118

119-
val quux = ChoiceSetting("-quux", "quux", "Quux", List(), "")
120-
val quuz = IntChoiceSetting("-quuz", "Quuz", List(), 0)
119+
val quux = ChoiceSetting("", "-quux", "quux", "Quux", List(), "")
120+
val quuz = IntChoiceSetting("", "-quuz", "Quuz", List(), 0)
121121

122122
locally {
123123
val args = List("-foo", "b", "-bar", "1", "-baz", "5")
@@ -169,7 +169,7 @@ class SettingsTests {
169169

170170
@Test def `Allow IntSetting's to be set with a colon`: Unit =
171171
object Settings extends SettingGroup:
172-
val foo = IntSetting("-foo", "foo", 80)
172+
val foo = IntSetting("", "-foo", "foo", 80)
173173
import Settings._
174174

175175
val args = List("-foo:100")
@@ -181,10 +181,10 @@ class SettingsTests {
181181

182182
@Test def `Set BooleanSettings correctly`: Unit =
183183
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)
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)
188188
import Settings._
189189

190190
val args = List("-foo:true", "-bar:false", "-baz", "-qux:true", "-qux:false")

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ScalaSettingsTests:
1212

1313
@Test def `A setting with aliases is accepted`: Unit =
1414
class MySettings extends SettingGroup:
15-
val classpath: Setting[String] = PathSetting("-classpath", "Specify where to find user class files.", ".", aliases = List("--class-path", "-cp"))
15+
val classpath: Setting[String] = PathSetting("", "classpath", "Specify where to find user class files.", ".", aliases = List("--class-path", "-cp"))
1616
val settings = MySettings()
1717
val args = tokenize("-cp path/to/classes1:other/path/to/classes2")
1818
val summary = ArgsSummary(settings.defaultState, args, errors = Nil, warnings = Nil)
@@ -25,7 +25,7 @@ class ScalaSettingsTests:
2525

2626
@Test def `A multistring setting is multivalued`: Unit =
2727
class SUT extends SettingGroup:
28-
val language: Setting[List[String]] = MultiStringSetting("-language", "feature", "Enable one or more language features.")
28+
val language: Setting[List[String]] = MultiStringSetting("", "language", "feature", "Enable one or more language features.")
2929
val sut = SUT()
3030
val args = tokenize("-language:implicitConversions,dynamics")
3131
val sumy = ArgsSummary(sut.defaultState, args, errors = Nil, warnings = Nil)
@@ -39,7 +39,7 @@ class ScalaSettingsTests:
3939

4040
@Test def `t9719 Apply -language more than once`: Unit =
4141
class SUT extends SettingGroup:
42-
val language: Setting[List[String]] = MultiStringSetting("-language", "feature", "Enable one or more language features.")
42+
val language: Setting[List[String]] = MultiStringSetting("", "language", "feature", "Enable one or more language features.")
4343
val sut = SUT()
4444
val args = tokenize("-language:implicitConversions -language:dynamics")
4545
val sumy = ArgsSummary(sut.defaultState, args, errors = Nil, warnings = Nil)
@@ -53,7 +53,7 @@ class ScalaSettingsTests:
5353

5454
@Test def `Warn if multistring element is supplied multiply`: Unit =
5555
class SUT extends SettingGroup:
56-
val language: Setting[List[String]] = MultiStringSetting("-language", "feature", "Enable one or more language features.")
56+
val language: Setting[List[String]] = MultiStringSetting("", "language", "feature", "Enable one or more language features.")
5757
val sut = SUT()
5858
val args = tokenize("-language:dynamics -language:implicitConversions -language:dynamics")
5959
val sumy = ArgsSummary(sut.defaultState, args, errors = Nil, warnings = Nil)
@@ -67,7 +67,7 @@ class ScalaSettingsTests:
6767

6868
@Test def `WConf setting is parsed`: Unit =
6969
import reporting.{Action, Diagnostic, NoExplanation}
70-
val sets = new ScalaSettings
70+
val sets = ScalaSettings
7171
val args = List("-Wconf:cat=deprecation:s,cat=feature:e", "-Wconf:msg=a problem\\.:s")
7272
val sumy = ArgsSummary(sets.defaultState, args, errors = Nil, warnings = Nil)
7373
val proc = sets.processArguments(sumy, processAll = true, skipped = Nil)
@@ -85,7 +85,7 @@ class ScalaSettingsTests:
8585

8686
@Test def `i18367 rightmost WConf flags take precedence over flags to the left`: Unit =
8787
import reporting.{Action, Diagnostic}
88-
val sets = new ScalaSettings
88+
val sets = ScalaSettings
8989
val args = List("-Wconf:cat=deprecation:e", "-Wconf:cat=deprecation:s")
9090
val sumy = ArgsSummary(sets.defaultState, args, errors = Nil, warnings = Nil)
9191
val proc = sets.processArguments(sumy, processAll = true, skipped = Nil)

0 commit comments

Comments
 (0)