Fix error recovery in format macro parsing
This commit is contained in:
parent
497ee321af
commit
a8421cacfe
4 changed files with 94 additions and 18 deletions
|
@ -164,23 +164,22 @@ fn parse_args<'a>(
|
||||||
p.clear_expected_tokens();
|
p.clear_expected_tokens();
|
||||||
}
|
}
|
||||||
|
|
||||||
// `Parser::expect` tries to recover using the
|
match p.expect(&token::Comma) {
|
||||||
// `Parser::unexpected_try_recover` function. This function is able
|
Err(mut err) => {
|
||||||
// to recover if the expected token is a closing delimiter.
|
match token::TokenKind::Comma.similar_tokens() {
|
||||||
//
|
Some(tks) if tks.contains(&p.token.kind) => {
|
||||||
// As `,` is not a closing delimiter, it will always return an `Err`
|
// If a similar token is found, then it may be a typo. We
|
||||||
// variant.
|
// consider it as a comma, and continue parsing.
|
||||||
let mut err = p.expect(&token::Comma).unwrap_err();
|
err.emit();
|
||||||
|
p.bump();
|
||||||
match token::TokenKind::Comma.similar_tokens() {
|
}
|
||||||
Some(tks) if tks.contains(&p.token.kind) => {
|
// Otherwise stop the parsing and return the error.
|
||||||
// If a similar token is found, then it may be a typo. We
|
_ => return Err(err),
|
||||||
// consider it as a comma, and continue parsing.
|
}
|
||||||
err.emit();
|
}
|
||||||
p.bump();
|
Ok(recovered) => {
|
||||||
|
assert!(recovered);
|
||||||
}
|
}
|
||||||
// Otherwise stop the parsing and return the error.
|
|
||||||
_ => return Err(err),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
first = false;
|
first = false;
|
||||||
|
|
|
@ -277,7 +277,7 @@ impl<'a> Parser<'a> {
|
||||||
self.struct_span_err(sp, &msg)
|
self.struct_span_err(sp, &msg)
|
||||||
.span_suggestion_short(sp, "change this to `;`", ";".to_string(), appl)
|
.span_suggestion_short(sp, "change this to `;`", ";".to_string(), appl)
|
||||||
.emit();
|
.emit();
|
||||||
return Ok(false);
|
return Ok(true);
|
||||||
} else if self.look_ahead(0, |t| {
|
} else if self.look_ahead(0, |t| {
|
||||||
t == &token::CloseDelim(token::Brace)
|
t == &token::CloseDelim(token::Brace)
|
||||||
|| (
|
|| (
|
||||||
|
@ -295,7 +295,7 @@ impl<'a> Parser<'a> {
|
||||||
.span_label(self.token.span, "unexpected token")
|
.span_label(self.token.span, "unexpected token")
|
||||||
.span_suggestion_short(sp, "add `;` here", ";".to_string(), appl)
|
.span_suggestion_short(sp, "add `;` here", ";".to_string(), appl)
|
||||||
.emit();
|
.emit();
|
||||||
return Ok(false);
|
return Ok(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
src/test/ui/parser/issue-88770.rs
Normal file
11
src/test/ui/parser/issue-88770.rs
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
// Regression test for the ICE described in #88770.
|
||||||
|
|
||||||
|
// error-pattern:this file contains an unclosed delimiter
|
||||||
|
// error-pattern:expected one of
|
||||||
|
// error-pattern:missing `in` in `for` loop
|
||||||
|
// error-pattern:expected `;`, found `e`
|
||||||
|
|
||||||
|
fn m(){print!("",(c for&g
|
||||||
|
u
|
||||||
|
e
|
||||||
|
e
|
66
src/test/ui/parser/issue-88770.stderr
Normal file
66
src/test/ui/parser/issue-88770.stderr
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
error: this file contains an unclosed delimiter
|
||||||
|
--> $DIR/issue-88770.rs:11:3
|
||||||
|
|
|
||||||
|
LL | fn m(){print!("",(c for&g
|
||||||
|
| - - - unclosed delimiter
|
||||||
|
| | |
|
||||||
|
| | unclosed delimiter
|
||||||
|
| unclosed delimiter
|
||||||
|
...
|
||||||
|
LL | e
|
||||||
|
| ^
|
||||||
|
|
||||||
|
error: this file contains an unclosed delimiter
|
||||||
|
--> $DIR/issue-88770.rs:11:3
|
||||||
|
|
|
||||||
|
LL | fn m(){print!("",(c for&g
|
||||||
|
| - - - unclosed delimiter
|
||||||
|
| | |
|
||||||
|
| | unclosed delimiter
|
||||||
|
| unclosed delimiter
|
||||||
|
...
|
||||||
|
LL | e
|
||||||
|
| ^
|
||||||
|
|
||||||
|
error: this file contains an unclosed delimiter
|
||||||
|
--> $DIR/issue-88770.rs:11:3
|
||||||
|
|
|
||||||
|
LL | fn m(){print!("",(c for&g
|
||||||
|
| - - - unclosed delimiter
|
||||||
|
| | |
|
||||||
|
| | unclosed delimiter
|
||||||
|
| unclosed delimiter
|
||||||
|
...
|
||||||
|
LL | e
|
||||||
|
| ^
|
||||||
|
|
||||||
|
error: missing `in` in `for` loop
|
||||||
|
--> $DIR/issue-88770.rs:8:26
|
||||||
|
|
|
||||||
|
LL | fn m(){print!("",(c for&g
|
||||||
|
| __________________________^
|
||||||
|
LL | | u
|
||||||
|
| |_ help: try adding `in` here
|
||||||
|
|
||||||
|
error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found keyword `for`
|
||||||
|
--> $DIR/issue-88770.rs:8:21
|
||||||
|
|
|
||||||
|
LL | fn m(){print!("",(c for&g
|
||||||
|
| ^^^ expected one of 8 possible tokens
|
||||||
|
|
||||||
|
error: expected `;`, found `e`
|
||||||
|
--> $DIR/issue-88770.rs:10:2
|
||||||
|
|
|
||||||
|
LL | e
|
||||||
|
| ^ help: add `;` here
|
||||||
|
LL | e
|
||||||
|
| - unexpected token
|
||||||
|
|
||||||
|
error: expected one of `!`, `,`, `.`, `::`, `?`, `{`, or an operator, found `)`
|
||||||
|
--> $DIR/issue-88770.rs:11:3
|
||||||
|
|
|
||||||
|
LL | e
|
||||||
|
| ^ expected one of 7 possible tokens
|
||||||
|
|
||||||
|
error: aborting due to 7 previous errors
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue