Skip to content

Commit 3fd7a06

Browse files
committed
REPL state now store a Context instead of a Run
1 parent 995ae34 commit 3fd7a06

File tree

3 files changed

+28
-40
lines changed

3 files changed

+28
-40
lines changed

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
7272
private def definitions(trees: List[untpd.Tree], state: State): Definitions = {
7373
import untpd._
7474

75-
implicit val ctx: Context = state.run.runContext
75+
implicit val ctx: Context = state.context
7676

7777
var valIdx = state.valIndex
7878

@@ -120,7 +120,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
120120

121121
assert(defs.stats.nonEmpty)
122122

123-
implicit val ctx: Context = defs.state.run.runContext
123+
implicit val ctx: Context = defs.state.context
124124

125125
val tmpl = Template(emptyConstructor, Nil, EmptyValDef, defs.stats)
126126
val module = ModuleDef(objectName(defs.state), tmpl)
@@ -137,12 +137,11 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
137137
}
138138

139139
private def runCompilationUnit(unit: CompilationUnit, state: State): Result[(CompilationUnit, State)] = {
140-
val run = state.run
141-
val reporter = state.run.runContext.reporter
142-
run.compileUnits(unit :: Nil)
140+
val ctx = state.context
141+
ctx.run.compileUnits(unit :: Nil)
143142

144-
if (!reporter.hasErrors) (unit, state).result
145-
else run.runContext.flushBufferedMessages().errors
143+
if (!ctx.reporter.hasErrors) (unit, state).result
144+
else ctx.reporter.removeBufferedMessages(ctx).errors
146145
}
147146

148147
final def compile(parsed: Parsed)(implicit state: State): Result[(CompilationUnit, State)] = {
@@ -154,7 +153,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
154153
final def typeOf(expr: String)(implicit state: State): Result[String] =
155154
typeCheck(expr).map { tree =>
156155
import dotc.ast.Trees._
157-
implicit val ctx = state.run.runContext
156+
implicit val ctx = state.context
158157
tree.rhs match {
159158
case Block(xs, _) => xs.last.tpe.widen.show
160159
case _ =>
@@ -218,23 +217,20 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
218217
}
219218

220219

221-
val run = state.run
222-
val reporter = newStoreReporter
223-
val src = new SourceFile(s"EvaluateExpr", expr)
224-
val runCtx =
225-
run.runContext.fresh
226-
.setReporter(reporter)
227-
.setSetting(run.runContext.settings.YstopAfter, List("frontend"))
220+
val src = new SourceFile("<typecheck>", expr)
221+
implicit val ctx = state.context.fresh
222+
.setReporter(newStoreReporter)
223+
.setSetting(state.context.settings.YstopAfter, List("frontend"))
228224

229-
wrapped(expr, src, state)(runCtx).flatMap { pkg =>
225+
wrapped(expr, src, state).flatMap { pkg =>
230226
val unit = new CompilationUnit(src)
231227
unit.untpdTree = pkg
232-
run.compileUnits(unit :: Nil, runCtx)
228+
ctx.run.compileUnits(unit :: Nil, ctx)
233229

234-
if (errorsAllowed || !reporter.hasErrors)
235-
unwrapped(unit.tpdTree, src)(runCtx)
230+
if (errorsAllowed || !ctx.reporter.hasErrors)
231+
unwrapped(unit.tpdTree, src)
236232
else {
237-
reporter.removeBufferedMessages(runCtx).errors
233+
ctx.reporter.removeBufferedMessages.errors
238234
}
239235
}
240236
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import scala.collection.JavaConverters._
5151
case class State(objectIndex: Int,
5252
valIndex: Int,
5353
imports: List[untpd.Import],
54-
run: Run)
54+
context: Context)
5555

5656
/** Main REPL instance, orchestrating input, compilation and presentation */
5757
class ReplDriver(settings: Array[String],
@@ -72,7 +72,7 @@ class ReplDriver(settings: Array[String],
7272
}
7373

7474
/** the initial, empty state of the REPL session */
75-
protected[this] def initState = State(0, 0, Nil, compiler.newRun(rootCtx, 0))
75+
protected[this] def initState = State(0, 0, Nil, rootCtx)
7676

7777
/** Reset state of repl to the initial state
7878
*
@@ -118,7 +118,7 @@ class ReplDriver(settings: Array[String],
118118
val comps = completions(line.cursor, line.line, state)
119119
candidates.addAll(comps.asJava)
120120
}
121-
implicit val ctx = state.run.runContext
121+
implicit val ctx = state.context
122122
try {
123123
val line = terminal.readLine(completer)
124124
ParseResult(line)
@@ -140,16 +140,16 @@ class ReplDriver(settings: Array[String],
140140
}
141141

142142
final def run(input: String)(implicit state: State): State = withRedirectedOutput {
143-
val parsed = ParseResult(input)(state.run.runContext)
143+
val parsed = ParseResult(input)(state.context)
144144
interpret(parsed)
145145
}
146146

147147
private def withRedirectedOutput(op: => State): State =
148148
Console.withOut(out) { Console.withErr(out) { op } }
149149

150150
private def newRun(state: State) = {
151-
val newRun = compiler.newRun(rootCtx.fresh.setReporter(newStoreReporter), state.objectIndex)
152-
state.copy(run = newRun)
151+
val run = compiler.newRun(rootCtx.fresh.setReporter(newStoreReporter), state.objectIndex)
152+
state.copy(context = run.runContext)
153153
}
154154

155155
/** Extract possible completions at the index of `cursor` in `expr` */
@@ -173,7 +173,7 @@ class ReplDriver(settings: Array[String],
173173
val file = new SourceFile("<completions>", expr)
174174
val unit = new CompilationUnit(file)
175175
unit.tpdTree = tree
176-
implicit val ctx = state.run.runContext.fresh.setCompilationUnit(unit)
176+
implicit val ctx = state.context.fresh.setCompilationUnit(unit)
177177
val srcPos = SourcePosition(file, Position(cursor))
178178
val (_, completions) = Interactive.completions(srcPos)
179179
completions.map(makeCandidate)
@@ -224,16 +224,16 @@ class ReplDriver(settings: Array[String],
224224
val newImports = newState.imports ++ extractImports(parsed.trees)
225225
val newStateWithImports = newState.copy(imports = newImports)
226226

227-
// display warnings
228-
displayErrors(newState.run.runContext.flushBufferedMessages())(newState)
227+
val warnings = newState.context.reporter.removeBufferedMessages(newState.context)
228+
displayErrors(warnings)(newState) // display warnings
229229
displayDefinitions(unit.tpdTree, newestWrapper)(newStateWithImports)
230230
}
231231
)
232232
}
233233

234234
/** Display definitions from `tree` */
235235
private def displayDefinitions(tree: tpd.Tree, newestWrapper: Name)(implicit state: State): State = {
236-
implicit val ctx = state.run.runContext
236+
implicit val ctx = state.context
237237

238238
def resAndUnit(denot: Denotation) = {
239239
import scala.util.{Success, Try}
@@ -319,7 +319,7 @@ class ReplDriver(settings: Array[String],
319319
initState
320320

321321
case Imports =>
322-
state.imports.foreach(i => out.println(SyntaxHighlighting(i.show(state.run.runContext))))
322+
state.imports.foreach(i => out.println(SyntaxHighlighting(i.show(state.context))))
323323
state
324324

325325
case Load(path) =>
@@ -356,7 +356,7 @@ class ReplDriver(settings: Array[String],
356356

357357
/** Output errors to `out` */
358358
private def displayErrors(errs: Seq[MessageContainer])(implicit state: State): State = {
359-
errs.map(renderMessage(_)(state.run.runContext)).foreach(out.println)
359+
errs.map(renderMessage(_)(state.context)).foreach(out.println)
360360
state
361361
}
362362
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,4 @@ package object repl {
1919
text.mkString(ctx.settings.pageWidth.value, ctx.settings.printLines.value)
2020
}
2121
}
22-
23-
private[repl] implicit class StoreReporterContext(val ctx: Context) extends AnyVal {
24-
def flushBufferedMessages(): List[MessageContainer] =
25-
ctx.reporter match {
26-
case rep: StoreReporter => rep.removeBufferedMessages(ctx)
27-
case _ => Nil
28-
}
29-
}
3022
}

0 commit comments

Comments
 (0)