From 47bf5e6e264708df7411033c386c71d4d3522798 Mon Sep 17 00:00:00 2001 From: Chris Kipp Date: Sat, 26 Mar 2022 21:32:01 +0100 Subject: [PATCH] fix: use correct ErrorMessageID for EmptyCatchOrFinallyBlock 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]`. --- compiler/src/dotty/tools/dotc/reporting/messages.scala | 2 +- compiler/test-resources/repl/i13208.default.scala | 2 +- tests/neg-custom-args/nowarn/nowarn-parser-error.check | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/reporting/messages.scala b/compiler/src/dotty/tools/dotc/reporting/messages.scala index 40a3e193546a..dc2d263678f2 100644 --- a/compiler/src/dotty/tools/dotc/reporting/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/messages.scala @@ -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) => "{}" diff --git a/compiler/test-resources/repl/i13208.default.scala b/compiler/test-resources/repl/i13208.default.scala index 9a18f9a67c66..048ca522421f 100644 --- a/compiler/test-resources/repl/i13208.default.scala +++ b/compiler/test-resources/repl/i13208.default.scala @@ -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 diff --git a/tests/neg-custom-args/nowarn/nowarn-parser-error.check b/tests/neg-custom-args/nowarn/nowarn-parser-error.check index ce0d9113cf7f..8be545a288a6 100644 --- a/tests/neg-custom-args/nowarn/nowarn-parser-error.check +++ b/tests/neg-custom-args/nowarn/nowarn-parser-error.check @@ -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