Skip to content

Commit

Permalink
fix: use correct ErrorMessageID for EmptyCatchOrFinallyBlock
Browse files Browse the repository at this point in the history
Previously we were always assigning this to `EmptyCatchOrFinallyBlockID`
but both `EmptyCatchBlock` and `EmptyCatchOrFinallyBlock` both use this
class and pass in their corresponding `ErrorMessageID`. This caused the
following two code samples to give the same `ErrorMessageID` when they
should have been different:

```scala
try {}
```

```scala
try {} catch {} finally {}
```

The second one gave this as an error:

```
scala> try {} catch {} finally {}
-- [E000] Syntax Error: --------------------------------------------------------
1 |try {} catch {} finally {}
  |       ^^^^^^^^
  |       The catch block does not contain a valid expression, try
  |       adding a case like - case e: Exception => to the block
  |
  | longer explanation available when compiling with `-explain`

```

When the ID should `E001`, `EmptyCatchBlockId`. Now this correctly
returns:

```
scala> try {} catch{} finally {}
-- [E001] Syntax Error: --------------------------------------------------------
1 |try {} catch{} finally {}
  |       ^^^^^^^
  |       The catch block does not contain a valid expression, try
  |       adding a case like - case e: Exception => to the block
  |
  | longer explanation available when compiling with `-explain`
```
  • Loading branch information
ckipp01 committed Mar 26, 2022
1 parent 6f3fe05 commit 54a9ed7
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/reporting/messages.scala
Expand Up @@ -78,7 +78,7 @@ import transform.SymUtils._
def kind = "Reference"

abstract class EmptyCatchOrFinallyBlock(tryBody: untpd.Tree, errNo: ErrorMessageID)(using Context)
extends SyntaxMsg(EmptyCatchOrFinallyBlockID) {
extends SyntaxMsg(errNo) {
def explain = {
val tryString = tryBody match {
case Block(Nil, untpd.EmptyTree) => "{}"
Expand Down

0 comments on commit 54a9ed7

Please sign in to comment.