-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Rework raw ident suggestions #66592
Rework raw ident suggestions #66592
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -225,8 +225,21 @@ impl<'a> Parser<'a> { | |
self.token.span, | ||
&format!("expected identifier, found {}", self.this_token_descr()), | ||
); | ||
let valid_follow = &[ | ||
TokenKind::Eq, | ||
TokenKind::Colon, | ||
TokenKind::Comma, | ||
TokenKind::Semi, | ||
TokenKind::ModSep, | ||
TokenKind::OpenDelim(token::DelimToken::Brace), | ||
TokenKind::OpenDelim(token::DelimToken::Paren), | ||
TokenKind::CloseDelim(token::DelimToken::Brace), | ||
TokenKind::CloseDelim(token::DelimToken::Paren), | ||
]; | ||
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. Code like this is pure technical debt, and my preferred solution would be to remove the diagnostic (#66126 (comment)), especially given that raw identifiers are a compatibility feature that should never be recommended in general. 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. I think that with this new restriction we'll only be presenting this suggestion in the very rare cases where the intent was there. |
||
if let token::Ident(name, false) = self.token.kind { | ||
if Ident::new(name, self.token.span).is_raw_guess() { | ||
if Ident::new(name, self.token.span).is_raw_guess() && | ||
self.look_ahead(1, |t| valid_follow.contains(&t.kind)) | ||
{ | ||
err.span_suggestion( | ||
self.token.span, | ||
"you can escape reserved keywords to use them as identifiers", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
mod foo { | ||
pub fn r#let() {} | ||
pub fn break() {} //~ ERROR expected identifier, found keyword `break` | ||
} | ||
|
||
fn main() { | ||
foo::let(); //~ ERROR expected identifier, found keyword `let` | ||
r#break(); //~ ERROR cannot find function `break` in this scope | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
error: expected identifier, found keyword `break` | ||
--> $DIR/raw-name-use-suggestion.rs:3:12 | ||
| | ||
LL | pub fn break() {} | ||
| ^^^^^ expected identifier, found keyword | ||
| | ||
help: you can escape reserved keywords to use them as identifiers | ||
| | ||
LL | pub fn r#break() {} | ||
| ^^^^^^^ | ||
|
||
error: expected identifier, found keyword `let` | ||
--> $DIR/raw-name-use-suggestion.rs:7:10 | ||
| | ||
LL | foo::let(); | ||
| ^^^ expected identifier, found keyword | ||
| | ||
help: you can escape reserved keywords to use them as identifiers | ||
| | ||
LL | foo::r#let(); | ||
| ^^^^^ | ||
|
||
error[E0425]: cannot find function `break` in this scope | ||
--> $DIR/raw-name-use-suggestion.rs:8:5 | ||
| | ||
LL | r#break(); | ||
| ^^^^^^^ not found in this scope | ||
| | ||
help: possible candidate is found in another module, you can import it into scope | ||
| | ||
LL | use foo::r#break; | ||
| | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0425`. |
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 think this should also include
Bracket
-- e.g.: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.
(but it is a heuristic, and as such it may be better not to suggest changing
async { ... }
)