Skip to content

Commit

Permalink
fix(parser): make empty catch an incomplete.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ckipp01 committed Feb 19, 2022
1 parent 29f9d33 commit cd143da
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Expand Up @@ -280,6 +280,12 @@ object Parsers {
syntaxError(msg, offset)
skip(stopAtComma = true)

def syntaxErrorOrIncomplete(msg: Message, span: Span): Unit =
if (in.token == EOF) incompleteInputError(msg)
else
syntaxError(msg, span)
skip(stopAtComma = true)

/** Consume one token of the specified type, or
* signal an error if it is not there.
*
Expand Down Expand Up @@ -2003,7 +2009,7 @@ object Parsers {
handler match {
case Block(Nil, EmptyTree) =>
assert(handlerStart != -1)
syntaxError(
syntaxErrorOrIncomplete(
EmptyCatchBlock(body),
Span(handlerStart, endOffset(handler))
)
Expand Down
10 changes: 10 additions & 0 deletions compiler/test/dotty/tools/repl/ReplCompilerTests.scala
Expand Up @@ -300,4 +300,14 @@ class ReplVerboseTests extends ReplTest(ReplTest.defaultOptions :+ "-verbose"):
run("val a = 42")
assert(storedOutput().trim().endsWith("val a: Int = 42"))
}

@Test def `i4393-incomplete-catch`: Unit = contextually {
assert(ParseResult.isIncomplete("""|try {
| ???
|} catch""".stripMargin))
assert(ParseResult.isIncomplete("""|try {
| ???
|} catch {""".stripMargin))
}

end ReplVerboseTests

0 comments on commit cd143da

Please sign in to comment.