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:
commit
8f111951a1
@ -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
|
||||
|
10
src/test/ui/try-block/try-block-catch.rs
Normal file
10
src/test/ui/try-block/try-block-catch.rs
Normal 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
|
||||
}
|
10
src/test/ui/try-block/try-block-catch.stderr
Normal file
10
src/test/ui/try-block/try-block-catch.stderr
Normal 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user