Skip to content

Commit 6a84737

Browse files
authored
Merge pull request #15811 from som-snytt/issue/15808-repl-load-truncated
2 parents d032d1f + 39786b8 commit 6a84737

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ object ParseResult {
139139
Settings.command -> (arg => Settings(arg)),
140140
)
141141

142-
def apply(source: SourceFile)(implicit state: State): ParseResult = {
142+
def apply(source: SourceFile)(using state: State): ParseResult = {
143143
val sourceCode = source.content().mkString
144144
sourceCode match {
145145
case "" => Newline
@@ -167,8 +167,14 @@ object ParseResult {
167167
}
168168
}
169169

170-
def apply(sourceCode: String)(implicit state: State): ParseResult =
171-
apply(SourceFile.virtual(str.REPL_SESSION_LINE + (state.objectIndex + 1), sourceCode, maybeIncomplete = true))
170+
def apply(sourceCode: String)(using state: State): ParseResult =
171+
maybeIncomplete(sourceCode, maybeIncomplete = true)
172+
173+
def complete(sourceCode: String)(using state: State): ParseResult =
174+
maybeIncomplete(sourceCode, maybeIncomplete = false)
175+
176+
private def maybeIncomplete(sourceCode: String, maybeIncomplete: Boolean)(using state: State): ParseResult =
177+
apply(SourceFile.virtual(str.REPL_SESSION_LINE + (state.objectIndex + 1), sourceCode, maybeIncomplete = maybeIncomplete))
172178

173179
/** Check if the input is incomplete.
174180
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class ReplCompiler extends Compiler:
166166
PackageDef(Ident(nme.EMPTY_PACKAGE), List(wrapper))
167167
}
168168

169-
ParseResult(sourceFile)(state) match {
169+
ParseResult(sourceFile) match {
170170
case Parsed(_, trees, _) =>
171171
wrap(trees).result
172172
case SyntaxErrors(_, reported, trees) =>

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,15 @@ class ReplDriver(settings: Array[String],
146146
|Type in expressions for evaluation. Or try :help.""".stripMargin)
147147

148148
/** Blockingly read a line, getting back a parse result */
149-
def readLine(state: State): ParseResult = {
149+
def readLine()(using state: State): ParseResult = {
150150
val completer: Completer = { (_, line, candidates) =>
151151
val comps = completions(line.cursor, line.line, state)
152152
candidates.addAll(comps.asJava)
153153
}
154154
given Context = state.context
155155
try {
156156
val line = terminal.readLine(completer)
157-
ParseResult(line)(state)
157+
ParseResult(line)
158158
} catch {
159159
case _: EndOfFileException |
160160
_: UserInterruptException => // Ctrl+D or Ctrl+C
@@ -163,7 +163,7 @@ class ReplDriver(settings: Array[String],
163163
}
164164

165165
@tailrec def loop(using state: State)(): State = {
166-
val res = readLine(state)
166+
val res = readLine()
167167
if (res == Quit) state
168168
else loop(using interpret(res))()
169169
}
@@ -173,8 +173,7 @@ class ReplDriver(settings: Array[String],
173173
}
174174

175175
final def run(input: String)(using state: State): State = runBody {
176-
val parsed = ParseResult(input)(state)
177-
interpret(parsed)
176+
interpret(ParseResult.complete(input))
178177
}
179178

180179
private def runBody(body: => State): State = rendering.classLoader()(using rootCtx).asContext(withRedirectedOutput(body))

compiler/test/dotty/tools/repl/LoadTests.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ class LoadTests extends ReplTest {
4747
|""".stripMargin
4848
)
4949

50+
@Test def truncated = loadTest(
51+
file = """|def f: Unit =
52+
| for i <- 1 to 2
53+
| do
54+
| println(i)""".stripMargin, // was: unindent expected, but eof found
55+
defs = """|def f: Unit
56+
|""".stripMargin,
57+
runCode = """f""",
58+
output = """|1
59+
|2
60+
|""".stripMargin
61+
)
62+
5063
def loadTest(file: String, defs: String, runCode: String, output: String) =
5164
eval(s":load ${writeFile(file)}") andThen {
5265
assertMultiLineEquals(defs, storedOutput())

0 commit comments

Comments
 (0)