Skip to content

Suggest using raw identifiers in 2018 edition when using keywords #57209

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

Merged
merged 3 commits into from
Jan 2, 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
12 changes: 12 additions & 0 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,18 @@ impl<'a> Parser<'a> {
let mut err = self.struct_span_err(self.span,
&format!("expected identifier, found {}",
self.this_token_descr()));
if let token::Ident(ident, false) = &self.token {
if ident.is_reserved() && !ident.is_path_segment_keyword() &&
ident.name != keywords::Underscore.name()
{
err.span_suggestion_with_applicability(
self.span,
"you can escape reserved keywords to use them as identifiers",
format!("r#{}", ident),
Applicability::MaybeIncorrect,
);
}
}
if let Some(token_descr) = self.token_descr() {
err.span_label(self.span, format!("expected identifier, found {}", token_descr));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ LL | produces_async! {} //~ ERROR expected identifier, found reserved keywor
| ^^^^^^^^^^^^^^^^^^ expected identifier, found reserved keyword
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
help: you can escape reserved keywords to use them as identifiers
|
LL | ( ) => ( pub fn r#async ( ) { } )
| ^^^^^^^

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ error: expected identifier, found reserved keyword `async`
|
LL | let mut async = 1; //~ ERROR expected identifier, found reserved keyword `async`
| ^^^^^ expected identifier, found reserved keyword
help: you can escape reserved keywords to use them as identifiers
|
LL | let mut r#async = 1; //~ ERROR expected identifier, found reserved keyword `async`
| ^^^^^^^

error: expected identifier, found reserved keyword `async`
--> $DIR/edition-keywords-2018-2015-parsing.rs:18:13
|
LL | module::async(); //~ ERROR expected identifier, found reserved keyword `async`
| ^^^^^ expected identifier, found reserved keyword
help: you can escape reserved keywords to use them as identifiers
|
LL | module::r#async(); //~ ERROR expected identifier, found reserved keyword `async`
| ^^^^^^^

error: no rules expected the token `r#async`
--> $DIR/edition-keywords-2018-2015-parsing.rs:12:31
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ LL | produces_async! {} //~ ERROR expected identifier, found reserved keywor
| ^^^^^^^^^^^^^^^^^^ expected identifier, found reserved keyword
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
help: you can escape reserved keywords to use them as identifiers
|
LL | ( ) => ( pub fn r#async ( ) { } )
| ^^^^^^^

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ error: expected identifier, found reserved keyword `async`
|
LL | let mut async = 1; //~ ERROR expected identifier, found reserved keyword `async`
| ^^^^^ expected identifier, found reserved keyword
help: you can escape reserved keywords to use them as identifiers
|
LL | let mut r#async = 1; //~ ERROR expected identifier, found reserved keyword `async`
| ^^^^^^^

error: expected identifier, found reserved keyword `async`
--> $DIR/edition-keywords-2018-2018-parsing.rs:18:13
|
LL | module::async(); //~ ERROR expected identifier, found reserved keyword `async`
| ^^^^^ expected identifier, found reserved keyword
help: you can escape reserved keywords to use them as identifiers
|
LL | module::r#async(); //~ ERROR expected identifier, found reserved keyword `async`
| ^^^^^^^

error: no rules expected the token `r#async`
--> $DIR/edition-keywords-2018-2018-parsing.rs:12:31
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/issues/issue-28433.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ error: expected identifier, found keyword `pub`
|
LL | pub duck,
| ^^^ expected identifier, found keyword
help: you can escape reserved keywords to use them as identifiers
|
LL | r#pub duck,
| ^^^^^

error: expected one of `(`, `,`, `=`, `{`, or `}`, found `duck`
--> $DIR/issue-28433.rs:4:9
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/issues/issue-44406.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ error: expected identifier, found keyword `true`
|
LL | foo!(true); //~ ERROR expected type, found keyword
| ^^^^ expected identifier, found keyword
help: you can escape reserved keywords to use them as identifiers
|
LL | foo!(r#true); //~ ERROR expected type, found keyword
| ^^^^^^

error: expected type, found keyword `true`
--> $DIR/issue-44406.rs:8:10
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/issues/issue-57198.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ error: expected identifier, found keyword `for`
|
LL | m::for();
| ^^^ expected identifier, found keyword
help: you can escape reserved keywords to use them as identifiers
|
LL | m::r#for();
| ^^^^^

error: aborting due to previous error

4 changes: 4 additions & 0 deletions src/test/ui/lifetime_starts_expressions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ error: expected identifier, found keyword `loop`
|
LL | loop { break 'label: loop { break 'label 42; }; }
| ^^^^ expected identifier, found keyword
help: you can escape reserved keywords to use them as identifiers
|
LL | loop { break 'label: r#loop { break 'label 42; }; }
| ^^^^^^

error: expected type, found keyword `loop`
--> $DIR/lifetime_starts_expressions.rs:6:26
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ 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:12:29
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/parser/bad-value-ident-false.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ error: expected identifier, found keyword `false`
|
LL | fn false() { } //~ ERROR expected identifier, found keyword `false`
| ^^^^^ expected identifier, found keyword
help: you can escape reserved keywords to use them as identifiers
|
LL | fn r#false() { } //~ ERROR expected identifier, found keyword `false`
| ^^^^^^^

error: aborting due to previous error

4 changes: 4 additions & 0 deletions src/test/ui/parser/bad-value-ident-true.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ error: expected identifier, found keyword `true`
|
LL | fn true() { } //~ ERROR expected identifier, found keyword `true`
| ^^^^ expected identifier, found keyword
help: you can escape reserved keywords to use them as identifiers
|
LL | fn r#true() { } //~ ERROR expected identifier, found keyword `true`
| ^^^^^^

error: aborting due to previous error

4 changes: 4 additions & 0 deletions src/test/ui/parser/issue-15980.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ LL | Err(ref e) if e.kind == io::EndOfFile {
LL | //~^ NOTE while parsing this struct
LL | return
| ^^^^^^ expected identifier, found keyword
help: you can escape reserved keywords to use them as identifiers
|
LL | r#return
|

error: expected one of `.`, `=>`, `?`, or an operator, found `_`
--> $DIR/issue-15980.rs:15:9
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/parser/keyword.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ error: expected identifier, found keyword `break`
|
LL | pub mod break {
| ^^^^^ expected identifier, found keyword
help: you can escape reserved keywords to use them as identifiers
|
LL | pub mod r#break {
| ^^^^^^^

error: aborting due to previous error

4 changes: 4 additions & 0 deletions src/test/ui/parser/macro-keyword.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ error: expected identifier, found reserved keyword `macro`
|
LL | fn macro() { //~ ERROR expected identifier, found reserved keyword `macro`
| ^^^^^ expected identifier, found reserved keyword
help: you can escape reserved keywords to use them as identifiers
|
LL | fn r#macro() { //~ ERROR expected identifier, found reserved keyword `macro`
| ^^^^^^^

error: aborting due to previous error

4 changes: 4 additions & 0 deletions src/test/ui/parser/removed-syntax-field-let.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ 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:4:9
Expand Down
4 changes: 4 additions & 0 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,6 +3,10 @@ error: expected identifier, found keyword `as`
|
LL | use std::any:: as foo; //~ ERROR expected identifier, found keyword `as`
| ^^ expected identifier, found keyword
help: you can escape reserved keywords to use them as identifiers
|
LL | use std::any:: r#as foo; //~ ERROR expected identifier, found keyword `as`
| ^^^^

error: expected one of `::`, `;`, or `as`, found `foo`
--> $DIR/use-as-where-use-ends-with-mod-sep.rs:3:19
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/rust-2018/dyn-trait-compatibility.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ error: expected identifier, found keyword `dyn`
|
LL | type A1 = dyn::dyn; //~ERROR expected identifier, found keyword `dyn`
| ^^^ expected identifier, found keyword
help: you can escape reserved keywords to use them as identifiers
|
LL | type A1 = dyn::r#dyn; //~ERROR expected identifier, found keyword `dyn`
| ^^^^^

error: expected identifier, found `<`
--> $DIR/dyn-trait-compatibility.rs:5:14
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/try-block/try-block-in-edition2015.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ LL | let try_result: Option<_> = try {
LL | //~^ ERROR expected struct, variant or union type, found macro `try`
LL | let x = 5; //~ ERROR expected identifier, found keyword
| ^^^ expected identifier, found keyword
help: you can escape reserved keywords to use them as identifiers
|
LL | r#let x = 5; //~ ERROR expected identifier, found keyword
| ^^^^^

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