Skip to content

Commit 833f12e

Browse files
committed
Suggest using raw identifiers in 2018 edition when using keywords
1 parent 433ef82 commit 833f12e

6 files changed

+47
-8
lines changed

src/libsyntax/parse/parser.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,18 @@ impl<'a> Parser<'a> {
798798
let mut err = self.struct_span_err(self.span,
799799
&format!("expected identifier, found {}",
800800
self.this_token_descr()));
801-
if let Some(token_descr) = self.token_descr() {
801+
if let (true, token::Ident(ref s, false), true) = (
802+
self.span.rust_2018(),
803+
&self.token,
804+
self.token.is_used_keyword() || self.token.is_unused_keyword(),
805+
) {
806+
err.span_suggestion_with_applicability(
807+
self.span,
808+
"you can escape reserved keywords to use them as identifiers",
809+
format!("r#{}", s.to_string()),
810+
Applicability::MaybeIncorrect,
811+
);
812+
} else if let Some(token_descr) = self.token_descr() {
802813
err.span_label(self.span, format!("expected identifier, found {}", token_descr));
803814
} else {
804815
err.span_label(self.span, "expected identifier");

src/test/ui/editions/edition-keywords-2015-2018-expansion.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ error: expected identifier, found reserved keyword `async`
22
--> $DIR/edition-keywords-2015-2018-expansion.rs:8:5
33
|
44
LL | produces_async! {} //~ ERROR expected identifier, found reserved keyword
5-
| ^^^^^^^^^^^^^^^^^^ expected identifier, found reserved keyword
5+
| ^^^^^^^^^^^^^^^^^^
66
|
77
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
8+
help: you can escape reserved keywords to use them as identifiers
9+
|
10+
LL | ( ) => ( pub fn r#async ( ) { } )
11+
| ^^^^^^^
812

913
error: aborting due to previous error
1014

src/test/ui/editions/edition-keywords-2018-2015-parsing.stderr

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@ error: expected identifier, found reserved keyword `async`
22
--> $DIR/edition-keywords-2018-2015-parsing.rs:8:13
33
|
44
LL | let mut async = 1; //~ ERROR expected identifier, found reserved keyword `async`
5-
| ^^^^^ expected identifier, found reserved keyword
5+
| ^^^^^
6+
help: you can escape reserved keywords to use them as identifiers
7+
|
8+
LL | let mut r#async = 1; //~ ERROR expected identifier, found reserved keyword `async`
9+
| ^^^^^^^
610

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

1321
error: no rules expected the token `r#async`
1422
--> $DIR/edition-keywords-2018-2015-parsing.rs:12:31

src/test/ui/editions/edition-keywords-2018-2018-expansion.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ error: expected identifier, found reserved keyword `async`
22
--> $DIR/edition-keywords-2018-2018-expansion.rs:8:5
33
|
44
LL | produces_async! {} //~ ERROR expected identifier, found reserved keyword `async`
5-
| ^^^^^^^^^^^^^^^^^^ expected identifier, found reserved keyword
5+
| ^^^^^^^^^^^^^^^^^^
66
|
77
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
8+
help: you can escape reserved keywords to use them as identifiers
9+
|
10+
LL | ( ) => ( pub fn r#async ( ) { } )
11+
| ^^^^^^^
812

913
error: aborting due to previous error
1014

src/test/ui/editions/edition-keywords-2018-2018-parsing.stderr

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@ error: expected identifier, found reserved keyword `async`
22
--> $DIR/edition-keywords-2018-2018-parsing.rs:8:13
33
|
44
LL | let mut async = 1; //~ ERROR expected identifier, found reserved keyword `async`
5-
| ^^^^^ expected identifier, found reserved keyword
5+
| ^^^^^
6+
help: you can escape reserved keywords to use them as identifiers
7+
|
8+
LL | let mut r#async = 1; //~ ERROR expected identifier, found reserved keyword `async`
9+
| ^^^^^^^
610

711
error: expected identifier, found reserved keyword `async`
812
--> $DIR/edition-keywords-2018-2018-parsing.rs:18:13
913
|
1014
LL | module::async(); //~ ERROR expected identifier, found reserved keyword `async`
11-
| ^^^^^ expected identifier, found reserved keyword
15+
| ^^^^^
16+
help: you can escape reserved keywords to use them as identifiers
17+
|
18+
LL | module::r#async(); //~ ERROR expected identifier, found reserved keyword `async`
19+
| ^^^^^^^
1220

1321
error: no rules expected the token `r#async`
1422
--> $DIR/edition-keywords-2018-2018-parsing.rs:12:31

src/test/ui/rust-2018/dyn-trait-compatibility.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ error: expected identifier, found keyword `dyn`
22
--> $DIR/dyn-trait-compatibility.rs:4:16
33
|
44
LL | type A1 = dyn::dyn; //~ERROR expected identifier, found keyword `dyn`
5-
| ^^^ expected identifier, found keyword
5+
| ^^^
6+
help: you can escape reserved keywords to use them as identifiers
7+
|
8+
LL | type A1 = dyn::r#dyn; //~ERROR expected identifier, found keyword `dyn`
9+
| ^^^^^
610

711
error: expected identifier, found `<`
812
--> $DIR/dyn-trait-compatibility.rs:5:14

0 commit comments

Comments
 (0)