Skip to content

Commit f65ee87

Browse files
committed
Fix a REPL issue while parsing empty trees (e.g. ;)
1 parent f25fa1f commit f65ee87

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
146146
* }
147147
* ```
148148
*/
149-
def wrapped(defs: Definitions): untpd.PackageDef = {
149+
def wrapped(defs: Definitions, sourceCode: String): untpd.PackageDef = {
150150
import untpd._
151151

152152
implicit val ctx: Context = defs.state.run.runContext
@@ -156,7 +156,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
156156
List(
157157
ModuleDef(objectName(defs.state), tmpl)
158158
.withMods(new Modifiers(Module | Final))
159-
.withPos(Position(defs.stats.head.pos.start, defs.stats.last.pos.end))
159+
.withPos(Position(0, sourceCode.length))
160160
)
161161
}
162162

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

171171
def createUnit(defs: Definitions, sourceCode: String): Result[CompilationUnit] = {
172172
val unit = new CompilationUnit(new SourceFile(objectName(defs.state).toString, sourceCode))
173-
unit.untpdTree = wrapped(defs)
173+
unit.untpdTree = wrapped(defs, sourceCode)
174174
unit.result
175175
}
176176

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class ReplDriver(settings: Array[String],
186186

187187
private def interpret(res: ParseResult)(implicit state: State): State =
188188
res match {
189-
case parsed: Parsed =>
189+
case parsed: Parsed if parsed.trees.nonEmpty =>
190190
compile(parsed)
191191
.withHistory(parsed.sourceCode :: state.history)
192192
.newRun(compiler, rootCtx)
@@ -195,9 +195,13 @@ class ReplDriver(settings: Array[String],
195195
displayErrors(errs)
196196
state.withHistory(src :: state.history)
197197

198-
case Newline | SigKill => state
199-
200198
case cmd: Command => interpretCommand(cmd)
199+
200+
case SigKill => // TODO
201+
state
202+
203+
case _ => // new line, empty tree
204+
state
201205
}
202206

203207
/** Compile `parsed` trees and evolve `state` in accordance */

compiler/test-resources/repl/parsing

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
scala> ;
2+
scala> ;;
3+
scala> 1; 2
4+
val res0: Int = 1
5+
val res1: Int = 2
6+
scala> 1;
7+
val res2: Int = 1
8+
scala> 1;; 2
9+
val res3: Int = 1
10+
val res4: Int = 2
11+
scala> }
12+
1 | }
13+
| ^
14+
| eof expected, but '}' found

0 commit comments

Comments
 (0)