-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Recover wrong-cased keywords that start items #99918
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/// Whatever to ignore case (`fn` vs `Fn` vs `FN`) or not. Used for recovering. | ||
#[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
pub enum Case { | ||
Sensitive, | ||
Insensitive, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -22,6 +22,7 @@ use rustc_ast::token::{self, Delimiter, Nonterminal, Token, TokenKind}; | |||||||||||
use rustc_ast::tokenstream::AttributesData; | ||||||||||||
use rustc_ast::tokenstream::{self, DelimSpan, Spacing}; | ||||||||||||
use rustc_ast::tokenstream::{TokenStream, TokenTree}; | ||||||||||||
use rustc_ast::util::case::Case; | ||||||||||||
use rustc_ast::AttrId; | ||||||||||||
use rustc_ast::DUMMY_NODE_ID; | ||||||||||||
use rustc_ast::{self as ast, AnonConst, AttrStyle, AttrVec, Const, Extern}; | ||||||||||||
|
@@ -604,6 +605,20 @@ impl<'a> Parser<'a> { | |||||||||||
self.token.is_keyword(kw) | ||||||||||||
} | ||||||||||||
|
||||||||||||
fn check_keyword_case(&mut self, kw: Symbol, case: Case) -> bool { | ||||||||||||
if self.check_keyword(kw) { | ||||||||||||
return true; | ||||||||||||
} | ||||||||||||
|
||||||||||||
if case == Case::Insensitive | ||||||||||||
&& let Some((ident, /* is_raw */ false)) = self.token.ident() | ||||||||||||
&& ident.as_str().to_lowercase() == kw.as_str().to_lowercase() { | ||||||||||||
true | ||||||||||||
} else { | ||||||||||||
false | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
/// If the next token is the given keyword, eats it and returns `true`. | ||||||||||||
/// Otherwise, returns `false`. An expectation is also added for diagnostics purposes. | ||||||||||||
// Public for rustfmt usage. | ||||||||||||
|
@@ -616,6 +631,33 @@ impl<'a> Parser<'a> { | |||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
/// Eats a keyword, optionally ignoring the case. | ||||||||||||
/// If the case differs (and is ignored) an error is issued. | ||||||||||||
/// This is useful for recovery. | ||||||||||||
fn eat_keyword_case(&mut self, kw: Symbol, case: Case) -> bool { | ||||||||||||
if self.eat_keyword(kw) { | ||||||||||||
return true; | ||||||||||||
} | ||||||||||||
|
||||||||||||
if case == Case::Insensitive | ||||||||||||
&& let Some((ident, /* is_raw */ false)) = self.token.ident() | ||||||||||||
&& ident.as_str().to_lowercase() == kw.as_str().to_lowercase() { | ||||||||||||
Comment on lines
+643
to
+644
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now I'm mildly amused at the thought of introducing a CamelCased keyword 😄 |
||||||||||||
self | ||||||||||||
.struct_span_err(ident.span, format!("keyword `{kw}` is written in a wrong case")) | ||||||||||||
.span_suggestion( | ||||||||||||
ident.span, | ||||||||||||
"write it in the correct case", | ||||||||||||
kw, | ||||||||||||
Applicability::MachineApplicable | ||||||||||||
).emit(); | ||||||||||||
|
||||||||||||
self.bump(); | ||||||||||||
return true; | ||||||||||||
} | ||||||||||||
|
||||||||||||
false | ||||||||||||
} | ||||||||||||
|
||||||||||||
fn eat_keyword_noexpect(&mut self, kw: Symbol) -> bool { | ||||||||||||
if self.token.is_keyword(kw) { | ||||||||||||
self.bump(); | ||||||||||||
|
@@ -1095,8 +1137,8 @@ impl<'a> Parser<'a> { | |||||||||||
} | ||||||||||||
|
||||||||||||
/// Parses asyncness: `async` or nothing. | ||||||||||||
fn parse_asyncness(&mut self) -> Async { | ||||||||||||
if self.eat_keyword(kw::Async) { | ||||||||||||
fn parse_asyncness(&mut self, case: Case) -> Async { | ||||||||||||
if self.eat_keyword_case(kw::Async, case) { | ||||||||||||
let span = self.prev_token.uninterpolated_span(); | ||||||||||||
Async::Yes { span, closure_id: DUMMY_NODE_ID, return_impl_trait_id: DUMMY_NODE_ID } | ||||||||||||
} else { | ||||||||||||
|
@@ -1105,19 +1147,19 @@ impl<'a> Parser<'a> { | |||||||||||
} | ||||||||||||
|
||||||||||||
/// Parses unsafety: `unsafe` or nothing. | ||||||||||||
fn parse_unsafety(&mut self) -> Unsafe { | ||||||||||||
if self.eat_keyword(kw::Unsafe) { | ||||||||||||
fn parse_unsafety(&mut self, case: Case) -> Unsafe { | ||||||||||||
if self.eat_keyword_case(kw::Unsafe, case) { | ||||||||||||
Unsafe::Yes(self.prev_token.uninterpolated_span()) | ||||||||||||
} else { | ||||||||||||
Unsafe::No | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
/// Parses constness: `const` or nothing. | ||||||||||||
fn parse_constness(&mut self) -> Const { | ||||||||||||
fn parse_constness(&mut self, case: Case) -> Const { | ||||||||||||
// Avoid const blocks to be parsed as const items | ||||||||||||
if self.look_ahead(1, |t| t != &token::OpenDelim(Delimiter::Brace)) | ||||||||||||
&& self.eat_keyword(kw::Const) | ||||||||||||
&& self.eat_keyword_case(kw::Const, case) | ||||||||||||
{ | ||||||||||||
Const::Yes(self.prev_token.uninterpolated_span()) | ||||||||||||
} else { | ||||||||||||
|
@@ -1372,8 +1414,8 @@ impl<'a> Parser<'a> { | |||||||||||
} | ||||||||||||
|
||||||||||||
/// Parses `extern string_literal?`. | ||||||||||||
fn parse_extern(&mut self) -> Extern { | ||||||||||||
if self.eat_keyword(kw::Extern) { | ||||||||||||
fn parse_extern(&mut self, case: Case) -> Extern { | ||||||||||||
if self.eat_keyword_case(kw::Extern, case) { | ||||||||||||
let mut extern_span = self.prev_token.span; | ||||||||||||
let abi = self.parse_abi(); | ||||||||||||
if let Some(abi) = abi { | ||||||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't see any obvious reason for the slow down, but can you try putting up PRs that revert some of these changes independently? Let's try to find if there's a single method that's introducing most of the perf regression (and it very well could be that it's just worse cache locality due to change in method inlining or arguments or something "silly" like that).