Skip to content

Commit c4c63fc

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

File tree

17 files changed

+101
-85
lines changed

17 files changed

+101
-85
lines changed

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,16 @@ class Driver {
6666

6767
protected def command: CompilerCommand = ScalacCommand
6868

69-
def setup(args: Array[String], rootCtx: Context): (Option[List[AbstractFile]], Context) = {
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 If there is no setting like `-help` preventing us from continuing compilation,
73+
* this method returns a list of files to compile and an updated Context.
74+
* If compilation should be interrupted, this method returns None.
75+
*/
76+
def setup(args: Array[String], rootCtx: Context): Option[(List[AbstractFile], Context)] = {
7077
val ictx = rootCtx.fresh
71-
val settings = config.ScalaSettings()
72-
val summary = command.distill(args, settings, settings.defaultState)
78+
val summary = command.distill(args, ictx.settings)(ictx.settingsState)(using ictx)
7379
ictx.setSettings(summary.sstate)
7480
MacroClassLoader.init(ictx)
7581
Positioned.init(using ictx)
@@ -78,9 +84,9 @@ class Driver {
7884
if !ctx.settings.YdropComments.value || ctx.mode.is(Mode.ReadComments) then
7985
ictx.setProperty(ContextDoc, new ContextDocstrings)
8086
val fileNamesOrNone = command.checkUsage(summary, sourcesRequired)(using ctx.settings)(using ctx.settingsState)
81-
fileNamesOrNone.fold((None, ictx)) { fileNames =>
87+
fileNamesOrNone.map { fileNames =>
8288
val files = fileNames.map(ctx.getFile)
83-
(Some(files), fromTastySetup(files))
89+
(files, fromTastySetup(files))
8490
}
8591
}
8692
}
@@ -187,10 +193,11 @@ class Driver {
187193
* if compilation succeeded.
188194
*/
189195
def process(args: Array[String], rootCtx: Context): Reporter = {
190-
val (files, compileCtx) = setup(args, rootCtx)
191-
files.fold(compileCtx.reporter) {
192-
doCompile(newCompiler(using compileCtx), _)(using compileCtx)
193-
}
196+
setup(args, rootCtx) match
197+
case Some((files, compileCtx)) =>
198+
doCompile(newCompiler(using compileCtx), files)(using compileCtx)
199+
case None =>
200+
rootCtx.reporter
194201
}
195202

196203
def main(args: Array[String]): Unit = {

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,21 @@ class Resident extends Driver {
4040

4141
final override def process(args: Array[String], rootCtx: Context): Reporter = {
4242
@tailrec def loop(args: Array[String], prevCtx: Context): Reporter = {
43-
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
43+
setup(args, prevCtx) match
44+
case Some((files, ctx)) =>
45+
inContext(ctx) {
46+
doCompile(residentCompiler, files)
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+
case None =>
57+
prevCtx.reporter
5858
}
5959
loop(args, rootCtx)
6060
}

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

Lines changed: 8 additions & 8 deletions
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)(using Context): ArgsSummary =
4444
/**
4545
* Expands all arguments starting with @ to the contents of the
4646
* file named like each argument.
@@ -49,20 +49,20 @@ trait CliCommand:
4949
def stripComment(s: String) = s takeWhile (_ != '#')
5050
val path = Paths.get(arg stripPrefix "@")
5151
if (!Files.exists(path))
52-
throw new java.io.FileNotFoundException("argument file %s could not be found" format path.getFileName)
53-
54-
val lines = Files.readAllLines(path) // default to UTF-8 encoding
55-
56-
val params = lines.asScala map stripComment mkString " "
57-
CommandLineParser.tokenize(params)
52+
report.error(s"Argument file ${path.getFileName} could not be found")
53+
Nil
54+
else
55+
val lines = Files.readAllLines(path) // default to UTF-8 encoding
56+
val params = lines.asScala map stripComment mkString " "
57+
CommandLineParser.tokenize(params)
5858

5959
// expand out @filename to the contents of that filename
6060
def expandedArguments = args.toList flatMap {
6161
case x if x startsWith "@" => expandArg(x)
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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/src/dotty/tools/dotc/decompiler/IDEDecompilerDriver.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class IDEDecompilerDriver(val settings: List[String]) extends dotc.Driver {
1919
val rootCtx = initCtx.fresh.addMode(Mode.Interactive | Mode.ReadPositions | Mode.ReadComments)
2020
rootCtx.setSetting(rootCtx.settings.YretainTrees, true)
2121
rootCtx.setSetting(rootCtx.settings.fromTasty, true)
22-
val ctx = setup(settings.toArray :+ "dummy.scala", rootCtx)._2
22+
val ctx = setup(settings.toArray :+ "dummy.scala", rootCtx).get._2
2323
ctx.initialize()(using ctx)
2424
ctx
2525
}

compiler/src/dotty/tools/dotc/decompiler/Main.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ object Main extends dotc.Driver {
1818
new TASTYDecompiler
1919
}
2020

21-
override def setup(args0: Array[String], rootCtx: Context): (Option[List[AbstractFile]], Context) = {
21+
override def setup(args0: Array[String], rootCtx: Context): Option[(List[AbstractFile], Context)] = {
2222
var args = args0.filter(a => a != "-decompile")
2323
if (!args.contains("-from-tasty")) args = "-from-tasty" +: args
2424
if (args.contains("-d")) args = "-color:never" +: args

compiler/src/dotty/tools/dotc/interactive/InteractiveDriver.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ class InteractiveDriver(val settings: List[String]) extends Driver {
3232
val rootCtx = initCtx.fresh.addMode(Mode.ReadPositions).addMode(Mode.Interactive).addMode(Mode.ReadComments)
3333
rootCtx.setSetting(rootCtx.settings.YretainTrees, true)
3434
rootCtx.setSetting(rootCtx.settings.YcookComments, true)
35-
val ctx = setup(settings.toArray, rootCtx)._2
35+
val ctx = setup(settings.toArray, rootCtx) match
36+
case Some((_, ctx)) => ctx
37+
case None => rootCtx
3638
ctx.initialize()(using ctx)
3739
ctx
3840
}

compiler/src/dotty/tools/repl/ReplDriver.scala

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,14 @@ class ReplDriver(settings: Array[String],
6868
private def initialCtx = {
6969
val rootCtx = initCtx.fresh.addMode(Mode.ReadPositions | Mode.Interactive | Mode.ReadComments)
7070
rootCtx.setSetting(rootCtx.settings.YcookComments, true)
71-
val (files, ictx) = setup(settings, rootCtx)
72-
shouldStart = files.isDefined
73-
ictx.base.initialize()(using ictx)
74-
ictx
71+
setup(settings, rootCtx) match
72+
case Some((files, ictx)) =>
73+
shouldStart = true
74+
ictx.base.initialize()(using ictx)
75+
ictx
76+
case None =>
77+
shouldStart = false
78+
rootCtx
7579
}
7680

7781
/** the initial, empty state of the REPL session */
@@ -105,11 +109,9 @@ class ReplDriver(settings: Array[String],
105109

106110
/** Try to run REPL if there is nothing that prevents us doing so.
107111
*
108-
* Possible reason for unsuccessful run are raised flags in CLI like --help or --version
112+
* Possible reason for unsuccessful run are raised flags in CLI like --help or --version
109113
*/
110-
final def tryRunning = if shouldStart then
111-
println("Starting scala3 REPL...")
112-
runUntilQuit()
114+
final def tryRunning = if shouldStart then runUntilQuit()
113115

114116
/** Run REPL with `state` until `:quit` command found
115117
*

0 commit comments

Comments
 (0)