Skip to content

Commit 443ae75

Browse files
committed
Auto merge of #57209 - estebank:suggest-raw-ident, r=petrochenkov
Suggest using raw identifiers in 2018 edition when using keywords
2 parents b2b7a06 + 18e0bda commit 443ae75

19 files changed

+92
-0
lines changed

src/libsyntax/parse/parser.rs

+12
Original file line numberDiff line numberDiff line change
@@ -798,6 +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 token::Ident(ident, false) = &self.token {
802+
if ident.is_reserved() && !ident.is_path_segment_keyword() &&
803+
ident.name != keywords::Underscore.name()
804+
{
805+
err.span_suggestion_with_applicability(
806+
self.span,
807+
"you can escape reserved keywords to use them as identifiers",
808+
format!("r#{}", ident),
809+
Applicability::MaybeIncorrect,
810+
);
811+
}
812+
}
801813
if let Some(token_descr) = self.token_descr() {
802814
err.span_label(self.span, format!("expected identifier, found {}", token_descr));
803815
} else {

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

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ LL | produces_async! {} //~ ERROR expected identifier, found reserved keywor
55
| ^^^^^^^^^^^^^^^^^^ expected identifier, found reserved keyword
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

+8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ error: expected identifier, found reserved keyword `async`
33
|
44
LL | let mut async = 1; //~ ERROR expected identifier, found reserved keyword `async`
55
| ^^^^^ expected identifier, found reserved keyword
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`
1115
| ^^^^^ expected identifier, found reserved keyword
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

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ LL | produces_async! {} //~ ERROR expected identifier, found reserved keywor
55
| ^^^^^^^^^^^^^^^^^^ expected identifier, found reserved keyword
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

+8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ error: expected identifier, found reserved keyword `async`
33
|
44
LL | let mut async = 1; //~ ERROR expected identifier, found reserved keyword `async`
55
| ^^^^^ expected identifier, found reserved keyword
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`
1115
| ^^^^^ expected identifier, found reserved keyword
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/issues/issue-28433.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ error: expected identifier, found keyword `pub`
33
|
44
LL | pub duck,
55
| ^^^ expected identifier, found keyword
6+
help: you can escape reserved keywords to use them as identifiers
7+
|
8+
LL | r#pub duck,
9+
| ^^^^^
610

711
error: expected one of `(`, `,`, `=`, `{`, or `}`, found `duck`
812
--> $DIR/issue-28433.rs:4:9

src/test/ui/issues/issue-44406.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ error: expected identifier, found keyword `true`
33
|
44
LL | foo!(true); //~ ERROR expected type, found keyword
55
| ^^^^ expected identifier, found keyword
6+
help: you can escape reserved keywords to use them as identifiers
7+
|
8+
LL | foo!(r#true); //~ ERROR expected type, found keyword
9+
| ^^^^^^
610

711
error: expected type, found keyword `true`
812
--> $DIR/issue-44406.rs:8:10

src/test/ui/issues/issue-57198.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ error: expected identifier, found keyword `for`
33
|
44
LL | m::for();
55
| ^^^ expected identifier, found keyword
6+
help: you can escape reserved keywords to use them as identifiers
7+
|
8+
LL | m::r#for();
9+
| ^^^^^
610

711
error: aborting due to previous error
812

src/test/ui/lifetime_starts_expressions.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ error: expected identifier, found keyword `loop`
33
|
44
LL | loop { break 'label: loop { break 'label 42; }; }
55
| ^^^^ expected identifier, found keyword
6+
help: you can escape reserved keywords to use them as identifiers
7+
|
8+
LL | loop { break 'label: r#loop { break 'label 42; }; }
9+
| ^^^^^^
610

711
error: expected type, found keyword `loop`
812
--> $DIR/lifetime_starts_expressions.rs:6:26

src/test/ui/parser/associated-types-project-from-hrtb-explicit.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ error: expected identifier, found keyword `for`
33
|
44
LL | fn foo2<I>(x: <I as for<'x> Foo<&'x isize>>::A)
55
| ^^^ expected identifier, found keyword
6+
help: you can escape reserved keywords to use them as identifiers
7+
|
8+
LL | fn foo2<I>(x: <I as r#for<'x> Foo<&'x isize>>::A)
9+
| ^^^^^
610

711
error: expected one of `::` or `>`, found `Foo`
812
--> $DIR/associated-types-project-from-hrtb-explicit.rs:12:29

src/test/ui/parser/bad-value-ident-false.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ error: expected identifier, found keyword `false`
33
|
44
LL | fn false() { } //~ ERROR expected identifier, found keyword `false`
55
| ^^^^^ expected identifier, found keyword
6+
help: you can escape reserved keywords to use them as identifiers
7+
|
8+
LL | fn r#false() { } //~ ERROR expected identifier, found keyword `false`
9+
| ^^^^^^^
610

711
error: aborting due to previous error
812

src/test/ui/parser/bad-value-ident-true.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ error: expected identifier, found keyword `true`
33
|
44
LL | fn true() { } //~ ERROR expected identifier, found keyword `true`
55
| ^^^^ expected identifier, found keyword
6+
help: you can escape reserved keywords to use them as identifiers
7+
|
8+
LL | fn r#true() { } //~ ERROR expected identifier, found keyword `true`
9+
| ^^^^^^
610

711
error: aborting due to previous error
812

src/test/ui/parser/issue-15980.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ LL | Err(ref e) if e.kind == io::EndOfFile {
66
LL | //~^ NOTE while parsing this struct
77
LL | return
88
| ^^^^^^ expected identifier, found keyword
9+
help: you can escape reserved keywords to use them as identifiers
10+
|
11+
LL | r#return
12+
|
913

1014
error: expected one of `.`, `=>`, `?`, or an operator, found `_`
1115
--> $DIR/issue-15980.rs:15:9

src/test/ui/parser/keyword.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ error: expected identifier, found keyword `break`
33
|
44
LL | pub mod break {
55
| ^^^^^ expected identifier, found keyword
6+
help: you can escape reserved keywords to use them as identifiers
7+
|
8+
LL | pub mod r#break {
9+
| ^^^^^^^
610

711
error: aborting due to previous error
812

src/test/ui/parser/macro-keyword.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ error: expected identifier, found reserved keyword `macro`
33
|
44
LL | fn macro() { //~ ERROR expected identifier, found reserved keyword `macro`
55
| ^^^^^ expected identifier, found reserved keyword
6+
help: you can escape reserved keywords to use them as identifiers
7+
|
8+
LL | fn r#macro() { //~ ERROR expected identifier, found reserved keyword `macro`
9+
| ^^^^^^^
610

711
error: aborting due to previous error
812

src/test/ui/parser/removed-syntax-field-let.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ error: expected identifier, found keyword `let`
33
|
44
LL | let foo: (),
55
| ^^^ expected identifier, found keyword
6+
help: you can escape reserved keywords to use them as identifiers
7+
|
8+
LL | r#let foo: (),
9+
| ^^^^^
610

711
error: expected `:`, found `foo`
812
--> $DIR/removed-syntax-field-let.rs:4:9

src/test/ui/parser/use-as-where-use-ends-with-mod-sep.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ error: expected identifier, found keyword `as`
33
|
44
LL | use std::any:: as foo; //~ ERROR expected identifier, found keyword `as`
55
| ^^ expected identifier, found keyword
6+
help: you can escape reserved keywords to use them as identifiers
7+
|
8+
LL | use std::any:: r#as foo; //~ ERROR expected identifier, found keyword `as`
9+
| ^^^^
610

711
error: expected one of `::`, `;`, or `as`, found `foo`
812
--> $DIR/use-as-where-use-ends-with-mod-sep.rs:3:19

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

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ error: expected identifier, found keyword `dyn`
33
|
44
LL | type A1 = dyn::dyn; //~ERROR expected identifier, found keyword `dyn`
55
| ^^^ expected identifier, found keyword
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

src/test/ui/try-block/try-block-in-edition2015.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ LL | let try_result: Option<_> = try {
66
LL | //~^ ERROR expected struct, variant or union type, found macro `try`
77
LL | let x = 5; //~ ERROR expected identifier, found keyword
88
| ^^^ expected identifier, found keyword
9+
help: you can escape reserved keywords to use them as identifiers
10+
|
11+
LL | r#let x = 5; //~ ERROR expected identifier, found keyword
12+
| ^^^^^
913

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

0 commit comments

Comments
 (0)