From 4a938b5b3c475a5a12fa582eca2dd91d76cb0d3e Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Wed, 10 Apr 2019 10:35:48 -0700 Subject: Special error when using catch after try --- src/libsyntax/parse/parser.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 5b430d13516..98bad0a80aa 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3618,7 +3618,13 @@ impl<'a> Parser<'a> { { let (iattrs, body) = self.parse_inner_attrs_and_block()?; attrs.extend(iattrs); - Ok(self.mk_expr(span_lo.to(body.span), ExprKind::TryBlock(body), attrs)) + if self.eat_keyword(keywords::Catch) { + let mut error = self.struct_span_err(self.prev_span, "`try {} catch` is not a valid syntax"); + error.help("try using `match` on the result of the `try` block instead"); + Err(error) + } else { + Ok(self.mk_expr(span_lo.to(body.span), ExprKind::TryBlock(body), attrs)) + } } // `match` token already eaten -- cgit 1.4.1-3-g733a5 From de02dd96fd955212d4555b2861f25a71c34db6b7 Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Wed, 10 Apr 2019 10:41:47 -0700 Subject: Adhere to tidy script --- src/libsyntax/parse/parser.rs | 3 ++- src/test/ui/try-block/try-block-catch.rs | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 98bad0a80aa..4d2a9f2c13c 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3619,7 +3619,8 @@ impl<'a> Parser<'a> { let (iattrs, body) = self.parse_inner_attrs_and_block()?; attrs.extend(iattrs); if self.eat_keyword(keywords::Catch) { - let mut error = self.struct_span_err(self.prev_span, "`try {} catch` is not a valid syntax"); + let mut error = self.struct_span_err(self.prev_span, + "`try {} catch` is not a valid syntax"); error.help("try using `match` on the result of the `try` block instead"); Err(error) } else { diff --git a/src/test/ui/try-block/try-block-catch.rs b/src/test/ui/try-block/try-block-catch.rs index 8f3d6d87258..d3a74c21422 100644 --- a/src/test/ui/try-block/try-block-catch.rs +++ b/src/test/ui/try-block/try-block-catch.rs @@ -3,7 +3,7 @@ #![feature(try_blocks)] fn main() { - let res: Option = try { - true - } catch { }; //~ ERROR `try {} catch` is not a valid syntax + let res: Option = try { + true + } catch { }; //~ ERROR `try {} catch` is not a valid syntax } -- cgit 1.4.1-3-g733a5 From 1156ce6f54dde7555bb00828ba4ec6c2a170fc83 Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Wed, 10 Apr 2019 19:22:43 -0700 Subject: Feedback --- src/libsyntax/parse/parser.rs | 3 ++- src/test/ui/try-block/try-block-catch.rs | 2 +- src/test/ui/try-block/try-block-catch.stderr | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 4d2a9f2c13c..d95a5843eb5 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3620,8 +3620,9 @@ impl<'a> Parser<'a> { attrs.extend(iattrs); if self.eat_keyword(keywords::Catch) { let mut error = self.struct_span_err(self.prev_span, - "`try {} catch` is not a valid syntax"); + "keyword `catch` cannot follow a `try` block"); error.help("try using `match` on the result of the `try` block instead"); + error.emit(); Err(error) } else { Ok(self.mk_expr(span_lo.to(body.span), ExprKind::TryBlock(body), attrs)) diff --git a/src/test/ui/try-block/try-block-catch.rs b/src/test/ui/try-block/try-block-catch.rs index a7dc41fc423..d165015611d 100644 --- a/src/test/ui/try-block/try-block-catch.rs +++ b/src/test/ui/try-block/try-block-catch.rs @@ -6,5 +6,5 @@ fn main() { let res: Option = try { true } catch { }; - //~^ ERROR `try {} catch` is not a valid syntax + //~^ ERROR keyword `catch` cannot follow a `try` block } diff --git a/src/test/ui/try-block/try-block-catch.stderr b/src/test/ui/try-block/try-block-catch.stderr index b54cc3ccbbb..39cf943f4d8 100644 --- a/src/test/ui/try-block/try-block-catch.stderr +++ b/src/test/ui/try-block/try-block-catch.stderr @@ -1,4 +1,4 @@ -error: `try {} catch` is not a valid syntax +error: keyword `catch` cannot follow a `try` block --> $DIR/try-block-catch.rs:8:7 | LL | } catch { }; -- cgit 1.4.1-3-g733a5