Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: Fix panic in 'const impl' recovery #81876

Merged
merged 1 commit into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,9 +1010,18 @@ impl<'a> Parser<'a> {
) -> PResult<'a, ItemInfo> {
let impl_span = self.token.span;
let mut err = self.expected_ident_found();
let mut impl_info = self.parse_item_impl(attrs, defaultness)?;

// Only try to recover if this is implementing a trait for a type
let mut impl_info = match self.parse_item_impl(attrs, defaultness) {
Ok(impl_info) => impl_info,
Err(mut recovery_error) => {
// Recovery failed, raise the "expected identifier" error
recovery_error.cancel();
return Err(err);
}
};

match impl_info.1 {
// only try to recover if this is implementing a trait for a type
ItemKind::Impl(box ImplKind {
of_trait: Some(ref trai), ref mut constness, ..
}) => {
Expand All @@ -1030,6 +1039,7 @@ impl<'a> Parser<'a> {
ItemKind::Impl { .. } => return Err(err),
_ => unreachable!(),
}

Ok(impl_info)
}

Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/parser/issue-81806.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trait T { const
impl //~ ERROR: expected identifier, found keyword `impl`
}

fn main() {}
17 changes: 17 additions & 0 deletions src/test/ui/parser/issue-81806.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: expected identifier, found keyword `impl`
--> $DIR/issue-81806.rs:2:1
|
LL | trait T { const
| - while parsing this item list starting here
LL | impl
| ^^^^ expected identifier, found keyword
LL | }
| - the item list ends here
|
help: you can escape reserved keywords to use them as identifiers
|
LL | r#impl
| ^^^^^^

error: aborting due to previous error