Skip to content

Commit b79bc84

Browse files
committed
Fix problem with badly initialized context
1 parent 25aa624 commit b79bc84

File tree

9 files changed

+44
-34
lines changed

9 files changed

+44
-34
lines changed

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

+15-6
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,17 @@ class Driver {
6666

6767
protected def command: CompilerCommand = ScalacCommand
6868

69+
/** Setup context with initialized settings from CLI arguments, then check if there are any settings that
70+
* would change the default behaviour of the compiler.
71+
*
72+
* @return A tuple of Option of List and Context.
73+
* If there is no setting preventing us from continuing compilation, function returns Some(List)
74+
* and Context updated respectively with compilation files. In particular, it can be Some(Nil).
75+
* If compilation should be interruped, function returns None and an old Context.
76+
*/
6977
def setup(args: Array[String], rootCtx: Context): (Option[List[AbstractFile]], Context) = {
7078
val ictx = rootCtx.fresh
71-
val settings = config.ScalaSettings()
72-
val summary = command.distill(args, settings, settings.defaultState)
79+
val summary = command.distill(args, ictx.settings)(ictx.settingsState)
7380
ictx.setSettings(summary.sstate)
7481
MacroClassLoader.init(ictx)
7582
Positioned.init(using ictx)
@@ -78,10 +85,12 @@ class Driver {
7885
if !ctx.settings.YdropComments.value || ctx.mode.is(Mode.ReadComments) then
7986
ictx.setProperty(ContextDoc, new ContextDocstrings)
8087
val fileNamesOrNone = command.checkUsage(summary, sourcesRequired)(using ctx.settings)(using ctx.settingsState)
81-
fileNamesOrNone.fold((None, ictx)) { fileNames =>
82-
val files = fileNames.map(ctx.getFile)
83-
(Some(files), fromTastySetup(files))
84-
}
88+
fileNamesOrNone match
89+
case Some(fileNames) =>
90+
val files = fileNames.map(ctx.getFile)
91+
(Some(files), fromTastySetup(files))
92+
case None =>
93+
(None, ictx)
8594
}
8695
}
8796

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

+15-14
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,21 @@ class Resident extends Driver {
4141
final override def process(args: Array[String], rootCtx: Context): Reporter = {
4242
@tailrec def loop(args: Array[String], prevCtx: Context): Reporter = {
4343
var (possibleFiles, ctx) = setup(args, prevCtx)
44-
if possibleFiles.isDefined then
45-
inContext(ctx) {
46-
doCompile(residentCompiler, possibleFiles.get) // using more complex constructs like fold or map instead of get will make @tailrec complain
47-
}
48-
var nextCtx = ctx
49-
var line = getLine()
50-
while (line == reset) {
51-
nextCtx = rootCtx
52-
line = getLine()
53-
}
54-
if (line.startsWith(quit)) ctx.reporter
55-
else loop(line split "\\s+", nextCtx)
56-
else
57-
ctx.reporter
44+
possibleFiles match
45+
case Some(files) =>
46+
inContext(ctx) {
47+
doCompile(residentCompiler, files)
48+
}
49+
var nextCtx = ctx
50+
var line = getLine()
51+
while (line == reset) {
52+
nextCtx = rootCtx
53+
line = getLine()
54+
}
55+
if (line.startsWith(quit)) ctx.reporter
56+
else loop(line split "\\s+", nextCtx)
57+
case None =>
58+
ctx.reporter
5859
}
5960
loop(args, rootCtx)
6061
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ trait CliCommand:
4040
"""
4141

4242
/** Distill arguments into summary detailing settings, errors and files to main */
43-
def distill(args: Array[String], sg: Settings.SettingGroup, ss: SettingsState): ArgsSummary =
43+
def distill(args: Array[String], sg: Settings.SettingGroup)(ss: SettingsState = sg.defaultState): ArgsSummary =
4444
/**
4545
* Expands all arguments starting with @ to the contents of the
4646
* file named like each argument.
@@ -62,7 +62,7 @@ trait CliCommand:
6262
case x => List(x)
6363
}
6464

65-
sg.processArguments(expandedArguments, ss, processAll = true)
65+
sg.processArguments(expandedArguments, processAll = true, settingsState = ss)
6666

6767
/** Creates a help message for a subset of options based on cond */
6868
protected def availableOptionsMsg(cond: Setting[?] => Boolean)(using settings: ConcreteSettings)(using SettingsState): String =

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Properties._
1010
import scala.collection.JavaConverters._
1111

1212
abstract class CompilerCommand extends CliCommand:
13-
final type ConcreteSettings = ScalaSettings
13+
type ConcreteSettings = ScalaSettings
1414

1515
final def helpMsg(using settings: ScalaSettings)(using SettingsState, Context): String =
1616
if (settings.help.value) usageMessage

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ object PathResolver {
144144
}
145145
else inContext(ContextBase().initialCtx) {
146146
val ArgsSummary(sstate, rest, errors, warnings) =
147-
ctx.settings.processArguments(args.toList, ctx.settingsState, true)
147+
ctx.settings.processArguments(args.toList, true, ctx.settingsState)
148148
errors.foreach(println)
149149
val pr = new PathResolver()(using ctx.fresh.setSettings(sstate))
150150
println(" COMMAND: 'scala %s'".format(args.mkString(" ")))

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ object Settings {
244244
}
245245
}
246246

247-
def processArguments(arguments: List[String], settingsState: SettingsState, processAll: Boolean): ArgsSummary =
247+
def processArguments(arguments: List[String], processAll: Boolean, settingsState: SettingsState = defaultState): ArgsSummary =
248248
processArguments(ArgsSummary(settingsState, arguments, Nil, Nil), processAll, Nil)
249249

250250
def publish[T](settingf: Int => Setting[T]): Setting[T] = {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ScalaCommandTest:
1616
@Test def `Simple one parameter`: Unit =
1717
val settings = config.ScalaSettings()
1818
val args = "-cp path/to/classes1:other/path/to/classes2 files".split(" ")
19-
val summary = ScalacCommand.distill(args, settings, settings.defaultState)
19+
val summary = ScalacCommand.distill(args, settings)()
2020
given SettingsState = summary.sstate
2121
assertEquals("path/to/classes1:other/path/to/classes2", settings.classpath.value)
2222
assertEquals("files" :: Nil, summary.arguments)
@@ -28,7 +28,7 @@ class ScalaCommandTest:
2828
writer.write("-sourceroot myNewRoot someMoreFiles");
2929
writer.close();
3030
val args = s"-cp path/to/classes1:other/path/to/classes2 @$file someFiles".split(" ")
31-
val summary = ScalacCommand.distill(args, settings, settings.defaultState)
31+
val summary = ScalacCommand.distill(args, settings)()
3232

3333
given SettingsState = summary.sstate
3434
assertEquals("path/to/classes1:other/path/to/classes2", settings.classpath.value)

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class SettingsTests {
4747

4848
inContext {
4949
val args = List("-foo", "b", "-bar", "1")
50-
val summary = Settings.processArguments(args, Settings.defaultState, true)
50+
val summary = Settings.processArguments(args, true)
5151
assertTrue(summary.errors.isEmpty)
5252
given SettingsState = summary.sstate
5353
assertEquals("b", Settings.foo.value)
@@ -65,7 +65,7 @@ class SettingsTests {
6565

6666
inContext {
6767
val args = List("-foo", "b", "-bar", "1", "-baz", "5")
68-
val summary = Settings.processArguments(args, Settings.defaultState, true)
68+
val summary = Settings.processArguments(args, true)
6969
assertTrue(summary.errors.isEmpty)
7070
given SettingsState = summary.sstate
7171
assertEquals("b", Settings.foo.value)
@@ -75,15 +75,15 @@ class SettingsTests {
7575

7676
inContext {
7777
val args = List("-foo:b")
78-
val summary = Settings.processArguments(args, Settings.defaultState, true)
78+
val summary = Settings.processArguments(args, true)
7979
assertTrue(summary.errors.isEmpty)
8080
given SettingsState = summary.sstate
8181
assertEquals("b", Settings.foo.value)
8282
}
8383

8484
inContext {
8585
val args = List("-foo", "c", "-bar", "3", "-baz", "-1")
86-
val summary = Settings.processArguments(args, Settings.defaultState, true)
86+
val summary = Settings.processArguments(args, true)
8787
val expectedErrors = List(
8888
"c is not a valid choice for -foo",
8989
"3 is not a valid choice for -bar",
@@ -94,14 +94,14 @@ class SettingsTests {
9494

9595
inContext {
9696
val args = List("-foo:c")
97-
val summary = Settings.processArguments(args, Settings.defaultState, true)
97+
val summary = Settings.processArguments(args, true)
9898
val expectedErrors = List("c is not a valid choice for -foo")
9999
assertEquals(expectedErrors, summary.errors)
100100
}
101101

102102
inContext {
103103
val args = List("-quux", "a", "-quuz", "0")
104-
val summary = Settings.processArguments(args, Settings.defaultState, true)
104+
val summary = Settings.processArguments(args, true)
105105
val expectedErrors = List(
106106
"a is not a valid choice for -quux",
107107
"0 is not a valid choice for -quuz",

scaladoc/src/dotty/tools/scaladoc/Scaladoc.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ object Scaladoc:
7676
given CompilerContext = newContext
7777
val ss = ScaladocSettings()
7878
import ss._
79-
val summary = ScaladocCommand.distill(args, ss, ss.defaultState)
79+
val summary = ScaladocCommand.distill(args, ss)()
8080
val argumentFilesOrNone = ScaladocCommand.checkUsage(summary, true)(using ss)(using summary.sstate)
8181

8282
extension[T](arg: Setting[T])

0 commit comments

Comments
 (0)