Special error when using catch after try
This commit is contained in:
parent
53b622a48a
commit
4a938b5b3c
@ -3618,7 +3618,13 @@ fn parse_try_block(&mut self, span_lo: Span, mut attrs: ThinVec<Attribute>)
|
||||
{
|
||||
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
|
||||
|
9
src/test/ui/try-block/try-block-catch.rs
Normal file
9
src/test/ui/try-block/try-block-catch.rs
Normal file
@ -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
|
||||
}
|
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: `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
|
||||
|
Loading…
Reference in New Issue
Block a user