Skip to content

Commit 873c4b7

Browse files
committed
Make sure compilation yields a state with a new Run
1 parent a291bd9 commit 873c4b7

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
155155
PackageDef(Ident(nme.EMPTY_PACKAGE), module)
156156
}
157157

158-
def newRun(state: State, initCtx: Context) = new Run(this, initCtx) {
158+
def newRun(initCtx: Context, objectIndex: Int) = new Run(this, initCtx) {
159159
override protected[this] def rootContext(implicit ctx: Context) =
160-
addMagicImports(super.rootContext.fresh.setReporter(storeReporter), state)
160+
addMagicImports(super.rootContext.fresh.setReporter(storeReporter), objectIndex)
161161
}
162162

163163
def createUnit(defs: Definitions, sourceCode: String): Result[CompilationUnit] = {
@@ -183,7 +183,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
183183
} yield (unit, state)
184184
}
185185

186-
private[this] def addMagicImports(initCtx: Context, state: State): Context = {
186+
private[this] def addMagicImports(initCtx: Context, objectIndex: Int): Context = {
187187
def addImport(path: TermName)(implicit ctx: Context) = {
188188
val ref = tpd.ref(ctx.requiredModuleRef(path.toTermName))
189189
val symbol = ctx.newImportSymbol(ctx.owner, ref)
@@ -193,7 +193,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
193193
}
194194

195195
List
196-
.range(1, state.objectIndex + 1)
196+
.range(1, objectIndex + 1)
197197
.foldLeft(addImport("dotty.Show".toTermName)(initCtx)) { (ictx, i) =>
198198
addImport(nme.EMPTY_PACKAGE ++ "." ++ objectNames(i))(ictx)
199199
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ case class State(objectIndex: Int,
6464
def withHistory(h: History) = copy(history = h)
6565

6666
def newRun(comp: ReplCompiler, rootCtx: Context): State =
67-
copy(run = comp.newRun(this, rootCtx))
67+
copy(run = comp.newRun(rootCtx, objectIndex))
6868
}
6969

7070
/** A list of possible completions at the index of `cursor`
@@ -93,7 +93,7 @@ class ReplDriver(settings: Array[String], protected val out: PrintStream = Syste
9393
}
9494

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

9898
/** Reset state of repl to the initial state
9999
*
@@ -139,10 +139,8 @@ class ReplDriver(settings: Array[String], protected val out: PrintStream = Syste
139139
}
140140
}
141141

142-
final def run(input: String)(implicit state: State): State = {
143-
val freshState = state.newRun(compiler, rootCtx)
144-
run(ParseResult(input)(freshState.run.runContext))(freshState)
145-
}
142+
final def run(input: String)(implicit state: State): State =
143+
run(ParseResult(input)(state.run.runContext))(state.newRun(compiler, rootCtx))
146144

147145
final def run(res: ParseResult)(implicit state: State): State =
148146
interpret(res)
@@ -184,7 +182,9 @@ class ReplDriver(settings: Array[String], protected val out: PrintStream = Syste
184182
private def interpret(res: ParseResult)(implicit state: State): State =
185183
res match {
186184
case parsed: Parsed =>
187-
compile(parsed).withHistory(parsed.sourceCode :: state.history)
185+
compile(parsed)
186+
.withHistory(parsed.sourceCode :: state.history)
187+
.newRun(compiler, rootCtx)
188188

189189
case SyntaxErrors(src, errs, _) =>
190190
displayErrors(errs)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
scala> val xs = scala.collection.mutable.ListBuffer[Int]
2+
1 | val xs = scala.collection.mutable.ListBuffer[Int]
3+
| ^
4+
| missing parameter type for parameter elems, expected = ?
5+
scala> val xs = scala.collection.mutable.ListBuffer[Int]()
6+
val xs: scala.collection.mutable.ListBuffer[Int] = ListBuffer()

0 commit comments

Comments
 (0)