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

Rework raw ident suggestions #66592

Merged
merged 2 commits into from
Nov 24, 2019
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
15 changes: 14 additions & 1 deletion src/librustc_parse/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Copy link
Member

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.:

macro_rules! async { () => {} }

Copy link
Member

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 { ... })

TokenKind::OpenDelim(token::DelimToken::Paren),
TokenKind::CloseDelim(token::DelimToken::Brace),
TokenKind::CloseDelim(token::DelimToken::Paren),
];
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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",
Expand Down
7 changes: 4 additions & 3 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2893,15 +2893,16 @@ fn names_to_string(names: &[Name]) -> String {
if i > 0 {
result.push_str("::");
}
if Ident::with_dummy_span(*name).is_raw_guess() {
result.push_str("r#");
}
result.push_str(&name.as_str());
}
result
}

fn path_names_to_string(path: &Path) -> String {
names_to_string(&path.segments.iter()
.map(|seg| seg.ident.name)
.collect::<Vec<_>>())
names_to_string(&path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>())
}

/// A somewhat inefficient routine to obtain the name of a module.
Expand Down
5 changes: 0 additions & 5 deletions src/test/ui/async-await/no-const-async.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ error: expected identifier, found keyword `async`
|
LL | pub const async fn x() {}
| ^^^^^ expected identifier, found keyword
|
help: you can escape reserved keywords to use them as identifiers
|
LL | pub const r#async fn x() {}
| ^^^^^^^

error: expected `:`, found keyword `fn`
--> $DIR/no-const-async.rs:5:17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ error: expected identifier, found keyword `for`
|
LL | fn foo2<I>(x: <I as for<'x> Foo<&'x isize>>::A)
| ^^^ expected identifier, found keyword
|
help: you can escape reserved keywords to use them as identifiers
|
LL | fn foo2<I>(x: <I as r#for<'x> Foo<&'x isize>>::A)
| ^^^^^

error: expected one of `::` or `>`, found `Foo`
--> $DIR/associated-types-project-from-hrtb-explicit.rs:10:29
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ error: expected identifier, found keyword `trait`
|
LL | trait T {
| ^^^^^ expected identifier, found keyword
|
help: you can escape reserved keywords to use them as identifiers
|
LL | r#trait T {
| ^^^^^^^

error: expected `:`, found `T`
--> $DIR/missing-close-brace-in-struct.rs:4:7
Expand Down
5 changes: 0 additions & 5 deletions src/test/ui/parser/removed-syntax-field-let.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ error: expected identifier, found keyword `let`
|
LL | let foo: (),
| ^^^ expected identifier, found keyword
|
help: you can escape reserved keywords to use them as identifiers
|
LL | r#let foo: (),
| ^^^^^

error: expected `:`, found `foo`
--> $DIR/removed-syntax-field-let.rs:2:9
Expand Down
5 changes: 0 additions & 5 deletions src/test/ui/parser/use-as-where-use-ends-with-mod-sep.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ error: expected identifier, found keyword `as`
|
LL | use std::any:: as foo;
| ^^ expected identifier, found keyword
|
help: you can escape reserved keywords to use them as identifiers
|
LL | use std::any:: r#as foo;
| ^^^^

error: expected one of `::`, `;`, or `as`, found `foo`
--> $DIR/use-as-where-use-ends-with-mod-sep.rs:1:19
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/suggestions/raw-name-use-suggestion.rs
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
}
36 changes: 36 additions & 0 deletions src/test/ui/suggestions/raw-name-use-suggestion.stderr
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`.
5 changes: 0 additions & 5 deletions src/test/ui/try-block/try-block-in-edition2015.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ LL | let try_result: Option<_> = try {
LL |
LL | let x = 5;
| ^^^ expected identifier, found keyword
|
help: you can escape reserved keywords to use them as identifiers
|
LL | r#let x = 5;
| ^^^^^

error[E0574]: expected struct, variant or union type, found macro `try`
--> $DIR/try-block-in-edition2015.rs:4:33
Expand Down