Skip to content

Commit 4e01b70

Browse files
committed
add recovery to parse_labeled_expr
1 parent 3ed5ba7 commit 4e01b70

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/librustc_parse/parser/expr.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,9 +1096,11 @@ impl<'a> Parser<'a> {
10961096
}
10971097

10981098
let msg = "expected `while`, `for`, `loop` or `{` after a label";
1099-
let mut err = self.fatal(msg);
1100-
err.span_label(self.token.span, msg);
1101-
return Err(err);
1099+
self.struct_span_err(self.token.span, msg)
1100+
.span_label(self.token.span, msg)
1101+
.emit();
1102+
// Continue as an expression in an effort to recover on `'label: non_block_expr`.
1103+
self.parse_expr()
11021104
}
11031105

11041106
/// Returns a string literal if the next token is a string literal.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
'label: 1 + 1; //~ ERROR expected `while`, `for`, `loop` or `{` after a label
3+
4+
let _recovery_witness: () = 0; //~ ERROR mismatched types
5+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: expected `while`, `for`, `loop` or `{` after a label
2+
--> $DIR/recover-labeled-non-block-expr.rs:2:13
3+
|
4+
LL | 'label: 1 + 1;
5+
| ^ expected `while`, `for`, `loop` or `{` after a label
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/recover-labeled-non-block-expr.rs:4:33
9+
|
10+
LL | let _recovery_witness: () = 0;
11+
| -- ^ expected `()`, found integer
12+
| |
13+
| expected due to this
14+
15+
error: aborting due to 2 previous errors
16+
17+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)