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

Optimize the diagnostic generation for extern unsafe #97172

Merged
merged 1 commit into from
May 31, 2022

Conversation

SparrowLii
Copy link
Member

This PR does the following about diagnostic generation when parsing foreign mod:

  1. Fixes the FIXME about avoiding depending on the error message text.
  2. Continue parsing when unsafe is followed by { (just like unsafe extern {...}).
  3. Add test case.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label May 19, 2022
@rust-highfive
Copy link
Collaborator

r? @compiler-errors

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 19, 2022
Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

Left some comments, sorry for the delay in reviewing.

compiler/rustc_parse/src/parser/item.rs Outdated Show resolved Hide resolved
let current_qual_sp = current_qual_sp.to(sp_start);
if let Ok(current_qual) = self.span_to_snippet(current_qual_sp) {
let invalid_qual_sp = self.token.uninterpolated_span();
let invalid_qual = self.span_to_snippet(invalid_qual_sp).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

Can you get rid of this unwrap? Maybe if let Ok((current_qual, invalid_qual)) = ..?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I think we can use a let...chain here

// If the next token is `{`, we can continue parsing items.
if self.look_ahead(1, |t| t.kind == token::OpenDelim(Delimiter::Brace)) {
err.emit();
self.eat_keyword(kw::Unsafe);
Copy link
Member

Choose a reason for hiding this comment

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

do we want to modify the unsafety variable since we parsed this keyword?

Copy link
Member Author

Choose a reason for hiding this comment

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

yeh, I think so

&format!("`{}` must come before `{}`", invalid_qual, current_qual),
format!("{} {}", invalid_qual, current_qual),
Applicability::MachineApplicable,
).note("keyword order for functions declaration is `pub`, `default`, `const`, `async`, `unsafe`, `extern`");
Copy link
Member

Choose a reason for hiding this comment

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

"keyword order for functions declaration"

This isn't a function declaration, though, right?

Copy link
Member Author

@SparrowLii SparrowLii May 28, 2022

Choose a reason for hiding this comment

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

That makes sense. And I think we can omit this note, since we just want to parse a foreign mod here.

@SparrowLii
Copy link
Member Author

SparrowLii commented May 28, 2022

Thanks for reviewing! I made some correcting here

Ok(items) => {
let module = ast::ForeignMod { unsafety, abi, items };
Ok((Ident::empty(), ItemKind::ForeignMod(module)))
if self.token.is_keyword(kw::Unsafe) {
Copy link
Member

Choose a reason for hiding this comment

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

Actually, now that I think of it, I'm not satisfied with the fact that this if block is larger than the rest of the function.. Do we expect extern "C" unsafe to show up much in practice?

@compiler-errors
Copy link
Member

Thanks @SparrowLii, this is much cleaner!

@bors r+ rollup

@bors
Copy link
Contributor

bors commented May 30, 2022

📌 Commit 0be2ca9 has been approved by compiler-errors

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 30, 2022
compiler-errors added a commit to compiler-errors/rust that referenced this pull request May 30, 2022
…r-errors

Optimize the diagnostic generation for `extern unsafe`

This PR does the following about diagnostic generation when parsing foreign mod:
1. Fixes the FIXME about avoiding depending on the error message text.
2. Continue parsing when `unsafe` is followed by `{` (just like `unsafe extern {...}`).
3. Add test case.
bors added a commit to rust-lang-ci/rust that referenced this pull request May 31, 2022
…piler-errors

Rollup of 6 pull requests

Successful merges:

 - rust-lang#89685 (refactor: VecDeques Iter fields to private)
 - rust-lang#97172 (Optimize the diagnostic generation for `extern unsafe`)
 - rust-lang#97395 (Miri call ABI check: ensure type size+align stay the same)
 - rust-lang#97431 (don't do `Sized` and other return type checks on RPIT's real type)
 - rust-lang#97555 (Source code page: line number click adds `NaN`)
 - rust-lang#97558 (Fix typos in comment)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 22da719 into rust-lang:master May 31, 2022
@rustbot rustbot added this to the 1.63.0 milestone May 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants