From 4a938b5b3c475a5a12fa582eca2dd91d76cb0d3e Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Wed, 10 Apr 2019 10:35:48 -0700 Subject: [PATCH 1/5] Special error when using catch after try --- src/libsyntax/parse/parser.rs | 8 +++++++- src/test/ui/try-block/try-block-catch.rs | 9 +++++++++ src/test/ui/try-block/try-block-catch.stderr | 10 ++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/try-block/try-block-catch.rs create mode 100644 src/test/ui/try-block/try-block-catch.stderr diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 5b430d13516b4..98bad0a80aae2 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 diff --git a/src/test/ui/try-block/try-block-catch.rs b/src/test/ui/try-block/try-block-catch.rs new file mode 100644 index 0000000000000..8f3d6d87258af --- /dev/null +++ b/src/test/ui/try-block/try-block-catch.rs @@ -0,0 +1,9 @@ +// compile-flags: --edition 2018 + +#![feature(try_blocks)] + +fn main() { + let res: Option = try { + true + } catch { }; //~ ERROR `try {} catch` is not a valid syntax +} diff --git a/src/test/ui/try-block/try-block-catch.stderr b/src/test/ui/try-block/try-block-catch.stderr new file mode 100644 index 0000000000000..0689647d3b2a4 --- /dev/null +++ b/src/test/ui/try-block/try-block-catch.stderr @@ -0,0 +1,10 @@ +error: `try {} catch` is not a valid syntax + --> $DIR/try-block-catch.rs:8:4 + | +LL | } catch { }; //~ ERROR `try {} catch` is not a valid syntax + | ^^^^^ + | + = help: try using `match` on the result of the `try` block instead + +error: aborting due to previous error + From de02dd96fd955212d4555b2861f25a71c34db6b7 Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Wed, 10 Apr 2019 10:41:47 -0700 Subject: [PATCH 2/5] 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(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 98bad0a80aae2..4d2a9f2c13c81 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 8f3d6d87258af..d3a74c214226c 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 } From 2b6143126dacfd1ed0cb29b1da60c1e9dfa081b2 Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Wed, 10 Apr 2019 12:14:53 -0700 Subject: [PATCH 3/5] Fix error brought up by changing tabs to spaces --- src/test/ui/try-block/try-block-catch.stderr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/try-block/try-block-catch.stderr b/src/test/ui/try-block/try-block-catch.stderr index 0689647d3b2a4..3f5889f53bd23 100644 --- a/src/test/ui/try-block/try-block-catch.stderr +++ b/src/test/ui/try-block/try-block-catch.stderr @@ -1,5 +1,5 @@ error: `try {} catch` is not a valid syntax - --> $DIR/try-block-catch.rs:8:4 + --> $DIR/try-block-catch.rs:8:7 | LL | } catch { }; //~ ERROR `try {} catch` is not a valid syntax | ^^^^^ From 4af7cf37d46d614e72e791c13153ef9a706716e9 Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Wed, 10 Apr 2019 14:39:51 -0700 Subject: [PATCH 4/5] Fix tests, I think --- src/test/ui/try-block/try-block-catch.rs | 3 ++- src/test/ui/try-block/try-block-catch.stderr | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/ui/try-block/try-block-catch.rs b/src/test/ui/try-block/try-block-catch.rs index d3a74c214226c..a7dc41fc42377 100644 --- a/src/test/ui/try-block/try-block-catch.rs +++ b/src/test/ui/try-block/try-block-catch.rs @@ -5,5 +5,6 @@ fn main() { let res: Option = try { true - } catch { }; //~ ERROR `try {} catch` is not a valid syntax + } catch { }; + //~^ ERROR `try {} catch` is not a valid syntax } diff --git a/src/test/ui/try-block/try-block-catch.stderr b/src/test/ui/try-block/try-block-catch.stderr index 3f5889f53bd23..b54cc3ccbbbfa 100644 --- a/src/test/ui/try-block/try-block-catch.stderr +++ b/src/test/ui/try-block/try-block-catch.stderr @@ -1,7 +1,7 @@ error: `try {} catch` is not a valid syntax --> $DIR/try-block-catch.rs:8:7 | -LL | } catch { }; //~ ERROR `try {} catch` is not a valid syntax +LL | } catch { }; | ^^^^^ | = help: try using `match` on the result of the `try` block instead From 1156ce6f54dde7555bb00828ba4ec6c2a170fc83 Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Wed, 10 Apr 2019 19:22:43 -0700 Subject: [PATCH 5/5] 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(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 4d2a9f2c13c81..d95a5843eb549 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 a7dc41fc42377..d165015611d04 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 b54cc3ccbbbfa..39cf943f4d8ab 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 { };