Skip to content

Commit cd143da

Browse files
committed
fix(parser): make empty catch an incomplete.
When parsing a `TRY` if there is an empty catch block instead of just returning a syntax error return an incomplete if you're at the EOF. This ensures that in the REPL if you are in a position like: ```scala scala> try { | ??? | } catch ``` And you hit enter you'll still be able to continue. Fixes #4393
1 parent 29f9d33 commit cd143da

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,12 @@ object Parsers {
280280
syntaxError(msg, offset)
281281
skip(stopAtComma = true)
282282

283+
def syntaxErrorOrIncomplete(msg: Message, span: Span): Unit =
284+
if (in.token == EOF) incompleteInputError(msg)
285+
else
286+
syntaxError(msg, span)
287+
skip(stopAtComma = true)
288+
283289
/** Consume one token of the specified type, or
284290
* signal an error if it is not there.
285291
*
@@ -2003,7 +2009,7 @@ object Parsers {
20032009
handler match {
20042010
case Block(Nil, EmptyTree) =>
20052011
assert(handlerStart != -1)
2006-
syntaxError(
2012+
syntaxErrorOrIncomplete(
20072013
EmptyCatchBlock(body),
20082014
Span(handlerStart, endOffset(handler))
20092015
)

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,14 @@ class ReplVerboseTests extends ReplTest(ReplTest.defaultOptions :+ "-verbose"):
300300
run("val a = 42")
301301
assert(storedOutput().trim().endsWith("val a: Int = 42"))
302302
}
303+
304+
@Test def `i4393-incomplete-catch`: Unit = contextually {
305+
assert(ParseResult.isIncomplete("""|try {
306+
| ???
307+
|} catch""".stripMargin))
308+
assert(ParseResult.isIncomplete("""|try {
309+
| ???
310+
|} catch {""".stripMargin))
311+
}
312+
303313
end ReplVerboseTests

0 commit comments

Comments
 (0)