Skip to content

Commit 4a938b5

Browse files
committed
Special error when using catch after try
1 parent 53b622a commit 4a938b5

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3618,7 +3618,13 @@ impl<'a> Parser<'a> {
36183618
{
36193619
let (iattrs, body) = self.parse_inner_attrs_and_block()?;
36203620
attrs.extend(iattrs);
3621-
Ok(self.mk_expr(span_lo.to(body.span), ExprKind::TryBlock(body), attrs))
3621+
if self.eat_keyword(keywords::Catch) {
3622+
let mut error = self.struct_span_err(self.prev_span, "`try {} catch` is not a valid syntax");
3623+
error.help("try using `match` on the result of the `try` block instead");
3624+
Err(error)
3625+
} else {
3626+
Ok(self.mk_expr(span_lo.to(body.span), ExprKind::TryBlock(body), attrs))
3627+
}
36223628
}
36233629

36243630
// `match` token already eaten
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// compile-flags: --edition 2018
2+
3+
#![feature(try_blocks)]
4+
5+
fn main() {
6+
let res: Option<bool> = try {
7+
true
8+
} catch { }; //~ ERROR `try {} catch` is not a valid syntax
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: `try {} catch` is not a valid syntax
2+
--> $DIR/try-block-catch.rs:8:4
3+
|
4+
LL | } catch { }; //~ ERROR `try {} catch` is not a valid syntax
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)