From 4a938b5b3c475a5a12fa582eca2dd91d76cb0d3e Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Wed, 10 Apr 2019 10:35:48 -0700 Subject: [PATCH] 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 5b430d13516..98bad0a80aa 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3618,7 +3618,13 @@ fn parse_try_block(&mut self, span_lo: Span, mut attrs: ThinVec) { 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 00000000000..8f3d6d87258 --- /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 00000000000..0689647d3b2 --- /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 +