-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
There was a problem hiding this 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.
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(); |
There was a problem hiding this comment.
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)) = ..
?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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`"); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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) { |
There was a problem hiding this comment.
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?
Thanks @SparrowLii, this is much cleaner! @bors r+ rollup |
📌 Commit 0be2ca9 has been approved by |
…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.
…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
This PR does the following about diagnostic generation when parsing foreign mod:
unsafe
is followed by{
(just likeunsafe extern {...}
).