Skip to content

Commit 9ab05b4

Browse files
authored
Rollup merge of #81876 - osa1:issue81806, r=matthewjasper
parser: Fix panic in 'const impl' recovery The panic happens when in recovery parsing a full `impl` (`parse_item_impl`) fails and we drop the `DiagnosticBuilder` for the recovery suggestion and return the `parse_item_impl` error. We now raise the original error "expected identifier found `impl`" when parsing the `impl` fails. Note that the regression test is slightly simplified version of the original repro in #81806, to make the error output smaller and more resilient to unrelated changes in parser error messages. Fixes #81806
2 parents a63085d + 6eb1bd4 commit 9ab05b4

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

compiler/rustc_parse/src/parser/item.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1010,9 +1010,18 @@ impl<'a> Parser<'a> {
10101010
) -> PResult<'a, ItemInfo> {
10111011
let impl_span = self.token.span;
10121012
let mut err = self.expected_ident_found();
1013-
let mut impl_info = self.parse_item_impl(attrs, defaultness)?;
1013+
1014+
// Only try to recover if this is implementing a trait for a type
1015+
let mut impl_info = match self.parse_item_impl(attrs, defaultness) {
1016+
Ok(impl_info) => impl_info,
1017+
Err(mut recovery_error) => {
1018+
// Recovery failed, raise the "expected identifier" error
1019+
recovery_error.cancel();
1020+
return Err(err);
1021+
}
1022+
};
1023+
10141024
match impl_info.1 {
1015-
// only try to recover if this is implementing a trait for a type
10161025
ItemKind::Impl(box ImplKind {
10171026
of_trait: Some(ref trai), ref mut constness, ..
10181027
}) => {
@@ -1030,6 +1039,7 @@ impl<'a> Parser<'a> {
10301039
ItemKind::Impl { .. } => return Err(err),
10311040
_ => unreachable!(),
10321041
}
1042+
10331043
Ok(impl_info)
10341044
}
10351045

src/test/ui/parser/issue-81806.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
trait T { const
2+
impl //~ ERROR: expected identifier, found keyword `impl`
3+
}
4+
5+
fn main() {}

src/test/ui/parser/issue-81806.stderr

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: expected identifier, found keyword `impl`
2+
--> $DIR/issue-81806.rs:2:1
3+
|
4+
LL | trait T { const
5+
| - while parsing this item list starting here
6+
LL | impl
7+
| ^^^^ expected identifier, found keyword
8+
LL | }
9+
| - the item list ends here
10+
|
11+
help: you can escape reserved keywords to use them as identifiers
12+
|
13+
LL | r#impl
14+
| ^^^^^^
15+
16+
error: aborting due to previous error
17+

0 commit comments

Comments
 (0)