add recovery to parse_labeled_expr

This commit is contained in:
Mazdak Farrokhzad 2019-12-03 13:11:34 +01:00
parent 3ed5ba7fa8
commit 4e01b70964
3 changed files with 27 additions and 3 deletions

View File

@ -1096,9 +1096,11 @@ impl<'a> Parser<'a> {
}
let msg = "expected `while`, `for`, `loop` or `{` after a label";
let mut err = self.fatal(msg);
err.span_label(self.token.span, msg);
return Err(err);
self.struct_span_err(self.token.span, msg)
.span_label(self.token.span, msg)
.emit();
// Continue as an expression in an effort to recover on `'label: non_block_expr`.
self.parse_expr()
}
/// Returns a string literal if the next token is a string literal.

View File

@ -0,0 +1,5 @@
fn main() {
'label: 1 + 1; //~ ERROR expected `while`, `for`, `loop` or `{` after a label
let _recovery_witness: () = 0; //~ ERROR mismatched types
}

View File

@ -0,0 +1,17 @@
error: expected `while`, `for`, `loop` or `{` after a label
--> $DIR/recover-labeled-non-block-expr.rs:2:13
|
LL | 'label: 1 + 1;
| ^ expected `while`, `for`, `loop` or `{` after a label
error[E0308]: mismatched types
--> $DIR/recover-labeled-non-block-expr.rs:4:33
|
LL | let _recovery_witness: () = 0;
| -- ^ expected `()`, found integer
| |
| expected due to this
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0308`.