diff options
| author | Kampfkarren <boynedmaster@gmail.com> | 2019-04-10 10:35:48 -0700 |
|---|---|---|
| committer | Kampfkarren <boynedmaster@gmail.com> | 2019-04-10 10:35:48 -0700 |
| commit | 4a938b5b3c475a5a12fa582eca2dd91d76cb0d3e (patch) | |
| tree | ed8e4bdc1fa1a9033961b4eaa2f9c6b85674ddb5 | |
| parent | 53b622a48a7945e223dbc5cfb30f7cbd38acc7c5 (diff) | |
| download | rust-4a938b5b3c475a5a12fa582eca2dd91d76cb0d3e.tar.gz rust-4a938b5b3c475a5a12fa582eca2dd91d76cb0d3e.zip | |
Special error when using catch after try
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 8 | ||||
| -rw-r--r-- | src/test/ui/try-block/try-block-catch.rs | 9 | ||||
| -rw-r--r-- | src/test/ui/try-block/try-block-catch.stderr | 10 |
3 files changed, 26 insertions, 1 deletions
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 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<bool> = 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 + |
