Rollup merge of #59847 - Kampfkarren:try-block-catch, r=estebank

Error when using `catch` after `try`

Part of https://github.com/rust-lang/rust/issues/31436
This commit is contained in:
Mazdak Farrokhzad 2019-04-12 20:36:11 +02:00 committed by GitHub
commit 8f111951a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 1 deletions

View file

@ -4091,7 +4091,15 @@ 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,
"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))
}
}
// `match` token already eaten

View file

@ -0,0 +1,10 @@
// compile-flags: --edition 2018
#![feature(try_blocks)]
fn main() {
let res: Option<bool> = try {
true
} catch { };
//~^ ERROR keyword `catch` cannot follow a `try` block
}

View file

@ -0,0 +1,10 @@
error: keyword `catch` cannot follow a `try` block
--> $DIR/try-block-catch.rs:8:7
|
LL | } catch { };
| ^^^^^
|
= help: try using `match` on the result of the `try` block instead
error: aborting due to previous error