Skip to content

Commit 8f11195

Browse files
authored
Rollup merge of rust-lang#59847 - Kampfkarren:try-block-catch, r=estebank
Error when using `catch` after `try` Part of rust-lang#31436
2 parents 29f9dd2 + 1156ce6 commit 8f11195

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4091,7 +4091,15 @@ impl<'a> Parser<'a> {
40914091
{
40924092
let (iattrs, body) = self.parse_inner_attrs_and_block()?;
40934093
attrs.extend(iattrs);
4094-
Ok(self.mk_expr(span_lo.to(body.span), ExprKind::TryBlock(body), attrs))
4094+
if self.eat_keyword(keywords::Catch) {
4095+
let mut error = self.struct_span_err(self.prev_span,
4096+
"keyword `catch` cannot follow a `try` block");
4097+
error.help("try using `match` on the result of the `try` block instead");
4098+
error.emit();
4099+
Err(error)
4100+
} else {
4101+
Ok(self.mk_expr(span_lo.to(body.span), ExprKind::TryBlock(body), attrs))
4102+
}
40954103
}
40964104

40974105
// `match` token already eaten
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// compile-flags: --edition 2018
2+
3+
#![feature(try_blocks)]
4+
5+
fn main() {
6+
let res: Option<bool> = try {
7+
true
8+
} catch { };
9+
//~^ ERROR keyword `catch` cannot follow a `try` block
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: keyword `catch` cannot follow a `try` block
2+
--> $DIR/try-block-catch.rs:8:7
3+
|
4+
LL | } catch { };
5+
| ^^^^^
6+
|
7+
= help: try using `match` on the result of the `try` block instead
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)