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 `EmptyCatchAndFinallyBlock` 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 than they currently are:

```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`
```

The first example with the missing catch and finally block should
actually be `[E002]`.
  • Loading branch information
ckipp01 committed Mar 26, 2022
1 parent 6f3fe05 commit 47bf5e6
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion compiler/test-resources/repl/i13208.default.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
scala> try 1
1 warning found
-- [E000] Syntax Warning: ------------------------------------------------------
-- [E002] Syntax Warning: ------------------------------------------------------
1 | try 1
| ^^^^^
| A try without catch or finally is equivalent to putting
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-custom-args/nowarn/nowarn-parser-error.check
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
| an identifier expected, but 'def' found
|
| longer explanation available when compiling with `-explain`
-- [E000] Syntax Warning: tests/neg-custom-args/nowarn/nowarn-parser-error.scala:2:10 ----------------------------------
-- [E002] Syntax Warning: tests/neg-custom-args/nowarn/nowarn-parser-error.scala:2:10 ----------------------------------
2 | def a = try 1 // warn
| ^^^^^
| A try without catch or finally is equivalent to putting
Expand Down

0 comments on commit 47bf5e6

Please sign in to comment.