From 2e33ba9dd22913b5e7aab472f70dc2f645079636 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 15 Jun 2015 17:08:49 +0200 Subject: [PATCH 1/6] Fix doc comment --- src/dotty/tools/dotc/reporting/ThrowingReporter.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/dotty/tools/dotc/reporting/ThrowingReporter.scala b/src/dotty/tools/dotc/reporting/ThrowingReporter.scala index eb854d5130fb..358b8d249c89 100644 --- a/src/dotty/tools/dotc/reporting/ThrowingReporter.scala +++ b/src/dotty/tools/dotc/reporting/ThrowingReporter.scala @@ -7,7 +7,8 @@ import collection.mutable import Reporter._ /** - * This class implements a Reporter that stores all messages + * This class implements a Reporter that throws all errors and sends warnings and other + * info to the underlying reporter. */ class ThrowingReporter(reportInfo: Reporter) extends Reporter { protected def doReport(d: Diagnostic)(implicit ctx: Context): Unit = d match { From de8368d5db436bb126e2f327d58fa1882e427e51 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 15 Jun 2015 17:09:12 +0200 Subject: [PATCH 2/6] Remove -resident setting --- src/dotty/tools/dotc/Bench.scala | 34 +++++-------------- src/dotty/tools/dotc/Main.scala | 17 ---------- .../tools/dotc/config/ScalaSettings.scala | 1 - 3 files changed, 9 insertions(+), 43 deletions(-) diff --git a/src/dotty/tools/dotc/Bench.scala b/src/dotty/tools/dotc/Bench.scala index 6abad7e8574b..417054dcea11 100644 --- a/src/dotty/tools/dotc/Bench.scala +++ b/src/dotty/tools/dotc/Bench.scala @@ -8,19 +8,7 @@ package dotc import core.Contexts.Context import reporting.Reporter -/* To do: - * - simplify hk types - * - have a second look at normalization (leave at method types if pt is method type?) - * - Don't open package objects from class files if they are present in source - * - Revise the way classes are inherited - when not followed by [...] or (...), - * assume the unparameterized type and forward type parameters as we do now for the synthetic head class. - */ object Bench extends Driver { - def resident(compiler: Compiler): Reporter = unsupported("resident") /*loop { line => - val command = new CompilerCommand(line split "\\s+" toList, new Settings(scalacError)) - compiler.reporter.reset() - new compiler.Run() compile command.files - }*/ private var numRuns = 1 @@ -29,17 +17,13 @@ object Bench extends Driver { private def ntimes(n: Int)(op: => Reporter): Reporter = (emptyReporter /: (0 until n)) ((_, _) => op) - override def doCompile(compiler: Compiler, fileNames: List[String], reporter: Option[Reporter] = None) - (implicit ctx: Context): Reporter = - if (new config.Settings.Setting.SettingDecorator[Boolean](ctx.base.settings.resident).value(ctx)) - resident(compiler) - else - ntimes(numRuns) { - val start = System.nanoTime() - val r = super.doCompile(compiler, fileNames, reporter) - ctx.println(s"time elapsed: ${(System.nanoTime - start) / 1000000}ms") - r - } + override def doCompile(compiler: Compiler, fileNames: List[String])(implicit ctx: Context): Reporter = + ntimes(numRuns) { + val start = System.nanoTime() + val r = super.doCompile(compiler, fileNames) + println(s"time elapsed: ${(System.nanoTime - start) / 1000000}ms") + r + } def extractNumArg(args: Array[String], name: String, default: Int = 1): (Int, Array[String]) = { val pos = args indexOf name @@ -47,11 +31,11 @@ object Bench extends Driver { else (args(pos + 1).toInt, (args take pos) ++ (args drop (pos + 2))) } - override def process(args: Array[String], rootCtx: Context, reporter: Option[Reporter] = None): Reporter = { + override def process(args: Array[String], rootCtx: Context): Reporter = { val (numCompilers, args1) = extractNumArg(args, "#compilers") val (numRuns, args2) = extractNumArg(args1, "#runs") this.numRuns = numRuns - ntimes(numCompilers)(super.process(args2, rootCtx, reporter)) + ntimes(numCompilers)(super.process(args2, rootCtx)) } } diff --git a/src/dotty/tools/dotc/Main.scala b/src/dotty/tools/dotc/Main.scala index 9aafc83ef55b..699a572349e2 100644 --- a/src/dotty/tools/dotc/Main.scala +++ b/src/dotty/tools/dotc/Main.scala @@ -1,7 +1,3 @@ -/* NSC -- new Scala compiler - * Copyright 2005-2013 LAMP/EPFL - * @author Martin Odersky - */ package dotty.tools package dotc @@ -11,18 +7,5 @@ import reporting.Reporter /* To do: */ object Main extends Driver { - def resident(compiler: Compiler): Reporter = unsupported("resident") /*loop { line => - val command = new CompilerCommand(line split "\\s+" toList, new Settings(scalacError)) - compiler.reporter.reset() - new compiler.Run() compile command.files - }*/ - override def newCompiler(): Compiler = new Compiler - - override def doCompile(compiler: Compiler, fileNames: List[String], reporter: Option[Reporter] = None)(implicit ctx: Context): Reporter = { - if (new config.Settings.Setting.SettingDecorator[Boolean](ctx.base.settings.resident).value(ctx)) - resident(compiler) - else - super.doCompile(compiler, fileNames, reporter) - } } diff --git a/src/dotty/tools/dotc/config/ScalaSettings.scala b/src/dotty/tools/dotc/config/ScalaSettings.scala index 2ba9070845c3..f8c155cad963 100644 --- a/src/dotty/tools/dotc/config/ScalaSettings.scala +++ b/src/dotty/tools/dotc/config/ScalaSettings.scala @@ -81,7 +81,6 @@ class ScalaSettings extends Settings.SettingGroup { val Xprintpos = BooleanSetting("-Xprint-pos", "Print tree positions, as offsets.") val printtypes = BooleanSetting("-Xprint-types", "Print tree types (debugging option).") val prompt = BooleanSetting("-Xprompt", "Display a prompt after each error (debugging option).") - val resident = BooleanSetting("-Xresident", "Compiler stays resident: read source filenames from standard input.") val script = StringSetting("-Xscript", "object", "Treat the source file as a script and wrap it in a main method.", "") val mainClass = StringSetting("-Xmain-class", "path", "Class for manifest's Main-Class entry (only useful with -d )", "") val Xshowcls = StringSetting("-Xshow-class", "class", "Show internal representation of class.", "") From 6beb1b402f84d4a3f3096a1fca2f71b69fb03c1a Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 15 Jun 2015 17:11:19 +0200 Subject: [PATCH 3/6] Refactor Driver - Make parts more reusable - Introduce hook "sourcesRequired" that controls whether no sources on the command line give a help message. --- src/dotty/tools/dotc/Driver.scala | 41 +++++++++++-------- .../tools/dotc/config/CompilerCommand.scala | 5 +-- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/dotty/tools/dotc/Driver.scala b/src/dotty/tools/dotc/Driver.scala index f5e41cbef771..7e9d4a5e4ae3 100644 --- a/src/dotty/tools/dotc/Driver.scala +++ b/src/dotty/tools/dotc/Driver.scala @@ -8,33 +8,40 @@ import scala.util.control.NonFatal abstract class Driver extends DotClass { - val prompt = "\ndotc>" + val prompt = "\ndotc> " protected def newCompiler(): Compiler protected def emptyReporter: Reporter = new StoreReporter - protected def doCompile(compiler: Compiler, fileNames: List[String], reporter: Option[Reporter] = None) - (implicit ctx: Context): Reporter = - if (fileNames.nonEmpty) { - val run = compiler.newRun(ctx, reporter) - run.compile(fileNames) - run.printSummary() - } else emptyReporter + protected def doCompile(compiler: Compiler, fileNames: List[String])(implicit ctx: Context): Reporter = + if (fileNames.nonEmpty) + try { + val run = compiler.newRun + run.compile(fileNames) + run.printSummary() + } + catch { + case ex: FatalError => + ctx.error(ex.getMessage) // signals that we should fail compilation. + ctx.typerState.reporter + } + else emptyReporter protected def initCtx = (new ContextBase).initialCtx - def process(args: Array[String], rootCtx: Context, reporter: Option[Reporter] = None): Reporter = { + protected def sourcesRequired = true + + def setup(args: Array[String], rootCtx: Context): (List[String], Context) = { val summary = CompilerCommand.distill(args)(rootCtx) implicit val ctx: Context = initCtx.fresh.setSettings(summary.sstate) - val fileNames = CompilerCommand.checkUsage(summary) - try { - doCompile(newCompiler(), fileNames, reporter) - } catch { - case ex: FatalError => - ctx.error(ex.getMessage) // signals that we should fail compilation. - ctx.typerState.reporter - } + val fileNames = CompilerCommand.checkUsage(summary, sourcesRequired) + (fileNames, ctx) + } + + def process(args: Array[String], rootCtx: Context): Reporter = { + val (fileNames, ctx) = setup(args, rootCtx) + doCompile(newCompiler(), fileNames)(ctx) } def main(args: Array[String]): Unit = diff --git a/src/dotty/tools/dotc/config/CompilerCommand.scala b/src/dotty/tools/dotc/config/CompilerCommand.scala index 3ba8db3bab7b..e34ca07f90f5 100644 --- a/src/dotty/tools/dotc/config/CompilerCommand.scala +++ b/src/dotty/tools/dotc/config/CompilerCommand.scala @@ -60,7 +60,7 @@ object CompilerCommand extends DotClass { * are already applied in context. * @return The list of files to compile. */ - def checkUsage(summary: ArgsSummary)(implicit ctx: Context): List[String] = { + def checkUsage(summary: ArgsSummary, sourcesRequired: Boolean)(implicit ctx: Context): List[String] = { val settings = ctx.settings /** Creates a help message for a subset of options based on cond */ @@ -121,8 +121,7 @@ object CompilerCommand extends DotClass { ctx.println(infoMessage) Nil } else { - if (summary.arguments.isEmpty && !settings.resident.value) - ctx.println(usageMessage) + if (sourcesRequired && summary.arguments.isEmpty) ctx.println(usageMessage) summary.arguments } } From 24825d6862a137f69cf2e6687742173b01cc79c7 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 15 Jun 2015 17:12:01 +0200 Subject: [PATCH 4/6] Make reporter ised in Compiler configurable Not needed right now, but will be useful later. --- src/dotty/tools/dotc/Compiler.scala | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/dotty/tools/dotc/Compiler.scala b/src/dotty/tools/dotc/Compiler.scala index e9b0a9676df0..6e2cab40d107 100644 --- a/src/dotty/tools/dotc/Compiler.scala +++ b/src/dotty/tools/dotc/Compiler.scala @@ -7,7 +7,7 @@ import Periods._ import Symbols._ import Scopes._ import typer.{FrontEnd, Typer, Mode, ImportInfo, RefChecks} -import reporting.{ConsoleReporter, Reporter} +import reporting.{Reporter, ConsoleReporter} import Phases.Phase import dotty.tools.dotc.transform._ import dotty.tools.dotc.transform.TreeTransforms.{TreeTransform, TreeTransformer} @@ -94,7 +94,7 @@ class Compiler { * for type checking. * imports For each element of RootImports, an import context */ - def rootContext(implicit ctx: Context, r: Option[Reporter] = None): Context = { + def rootContext(implicit ctx: Context): Context = { ctx.definitions.init(ctx) ctx.setPhasePlan(phases) val rootScope = new MutableScope @@ -106,20 +106,22 @@ class Compiler { .setOwner(defn.RootClass) .setTyper(new Typer) .setMode(Mode.ImplicitsEnabled) - .setTyperState(new MutableTyperState(ctx.typerState, r.getOrElse(new ConsoleReporter()(ctx)), isCommittable = true)) + .setTyperState(new MutableTyperState(ctx.typerState, rootReporter(ctx), isCommittable = true)) ctx.definitions.init(start) // set context of definitions to start def addImport(ctx: Context, symf: () => Symbol) = ctx.fresh.setImportInfo(ImportInfo.rootImport(symf)(ctx)) (start.setRunInfo(new RunInfo(start)) /: defn.RootImportFns)(addImport) } + protected def rootReporter(implicit ctx: Context): Reporter = new ConsoleReporter()(ctx) + def reset()(implicit ctx: Context): Unit = { ctx.base.reset() ctx.runInfo.clear() } - def newRun(implicit ctx: Context, r: Option[Reporter] = None): Run = { + def newRun(implicit ctx: Context): Run = { reset() - new Run(this)(rootContext(ctx, r)) + new Run(this)(rootContext) } } From 5fff6d8c3d67685c0deb7a375a96e479029137eb Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 15 Jun 2015 17:12:16 +0200 Subject: [PATCH 5/6] New main file for resident compiler mode. --- src/dotty/tools/dotc/Resident.scala | 55 +++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/dotty/tools/dotc/Resident.scala diff --git a/src/dotty/tools/dotc/Resident.scala b/src/dotty/tools/dotc/Resident.scala new file mode 100644 index 000000000000..d671ea25912b --- /dev/null +++ b/src/dotty/tools/dotc/Resident.scala @@ -0,0 +1,55 @@ +package dotty.tools +package dotc + +import core.Contexts.Context +import reporting.Reporter +import java.io.EOFException +import scala.annotation.tailrec + +/** A compiler which stays resident between runs. + * Usage: + * + * > scala dotty.tools.dotc.Resident + * + * dotc> "more options and files to compile" + * + * ... + * + * dotc> :reset // reset all options to the ones passed on the command line + * + * ... + * + * dotc> :q // quit + */ +object Resident extends Driver { + + object residentCompiler extends Compiler + + override def newCompiler(): Compiler = ??? + + override def sourcesRequired = false + + private val quit = ":q" + private val reset = ":reset" + + private def getLine() = { + Console.print(prompt) + try scala.io.StdIn.readLine() catch { case _: EOFException => quit } + } + + final override def process(args: Array[String], rootCtx: Context): Reporter = { + @tailrec def loop(args: Array[String], prevCtx: Context): Reporter = { + var (fileNames, ctx) = setup(args, prevCtx) + doCompile(residentCompiler, fileNames)(ctx) + var nextCtx = ctx + var line = getLine() + while (line == reset) { + nextCtx = rootCtx + line = getLine() + } + if (line.startsWith(quit)) ctx.typerState.reporter + else loop(line split "\\s+", nextCtx) + } + loop(args, rootCtx) + } +} From 840aa9354a9708548cf0e21be08a552403851c97 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 29 Jun 2015 09:23:34 +0200 Subject: [PATCH 6/6] Fix rebase breakage around reporters. Reporters are part of the context which is passed into a compiler run. Passing them as a separate option is an unnecessary complication. --- src/dotty/tools/dotc/FromTasty.scala | 2 +- test/dotty/partest/DPDirectCompiler.scala | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/dotty/tools/dotc/FromTasty.scala b/src/dotty/tools/dotc/FromTasty.scala index 1662d57e7866..705deedd3590 100644 --- a/src/dotty/tools/dotc/FromTasty.scala +++ b/src/dotty/tools/dotc/FromTasty.scala @@ -42,7 +42,7 @@ object FromTasty extends Driver { List(new ReadTastyTreesFromClasses) :: backendPhases } - override def newRun(implicit ctx: Context, reporter: Option[Reporter] = None): Run = { + override def newRun(implicit ctx: Context): Run = { reset() new TASTYRun(this)(rootContext) } diff --git a/test/dotty/partest/DPDirectCompiler.scala b/test/dotty/partest/DPDirectCompiler.scala index b04214893241..885de8c629de 100644 --- a/test/dotty/partest/DPDirectCompiler.scala +++ b/test/dotty/partest/DPDirectCompiler.scala @@ -23,9 +23,11 @@ class DPDirectCompiler(runner: DPTestRunner) extends nest.DirectCompiler(runner) } try { - val processor = if (opts0.exists(_.startsWith("#"))) dotty.tools.dotc.Bench else dotty.tools.dotc.Main + val processor = + if (opts0.exists(_.startsWith("#"))) dotty.tools.dotc.Bench else dotty.tools.dotc.Main val clogger = new ConsoleReporter(writer = clogWriter)(ctx) - val reporter = processor.process((sources.map(_.toString) ::: opts0).toArray, ctx, Some(clogger)) + val logCtx = ctx.fresh.setTyperState(ctx.typerState.withReporter(clogger)) + val reporter = processor.process((sources.map(_.toString) ::: opts0).toArray, logCtx) if (!reporter.hasErrors) runner.genPass() else { reporter.printSummary(ctx)