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

Fix ICE when misplaced visibility cannot be properly parsed #86932

Merged
merged 1 commit into from
Jul 8, 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
8 changes: 7 additions & 1 deletion compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1791,7 +1791,13 @@ impl<'a> Parser<'a> {
if self.check_keyword(kw::Pub) {
let sp = sp_start.to(self.prev_token.span);
if let Ok(snippet) = self.span_to_snippet(sp) {
let vis = self.parse_visibility(FollowedByType::No)?;
let vis = match self.parse_visibility(FollowedByType::No) {
Ok(v) => v,
Err(mut d) => {
d.cancel();
return Err(err);
}
};
Comment on lines +1794 to +1800
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of code is quite common, it'd be nice if we had some helper for it 🤔

let vs = pprust::vis_to_string(&vis);
let vs = vs.trim_end();
err.span_suggestion(
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/parser/issue-86895.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const pub () {}
//~^ ERROR expected one of `async`, `extern`, `fn`, or `unsafe`
pub fn main() {}
8 changes: 8 additions & 0 deletions src/test/ui/parser/issue-86895.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: expected one of `async`, `extern`, `fn`, or `unsafe`, found keyword `pub`
--> $DIR/issue-86895.rs:1:7
|
LL | const pub () {}
| ^^^ expected one of `async`, `extern`, `fn`, or `unsafe`

error: aborting due to previous error