Skip to content

Commit 2cd0d14

Browse files
committed
Address review comments
- Suggest raw ident escaping in all editions - Keep primary label in all cases
1 parent 833f12e commit 2cd0d14

18 files changed

+68
-19
lines changed

src/libsyntax/parse/parser.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -798,18 +798,19 @@ 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 (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() {
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+
}
813+
if let Some(token_descr) = self.token_descr() {
813814
err.span_label(self.span, format!("expected identifier, found {}", token_descr));
814815
} else {
815816
err.span_label(self.span, "expected identifier");

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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-
| ^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^ 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)
88
help: you can escape reserved keywords to use them as identifiers

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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-
| ^^^^^
5+
| ^^^^^ expected identifier, found reserved keyword
66
help: you can escape reserved keywords to use them as identifiers
77
|
88
LL | let mut r#async = 1; //~ ERROR expected identifier, found reserved keyword `async`
@@ -12,7 +12,7 @@ error: expected identifier, found reserved keyword `async`
1212
--> $DIR/edition-keywords-2018-2015-parsing.rs:18:13
1313
|
1414
LL | module::async(); //~ ERROR expected identifier, found reserved keyword `async`
15-
| ^^^^^
15+
| ^^^^^ expected identifier, found reserved keyword
1616
help: you can escape reserved keywords to use them as identifiers
1717
|
1818
LL | module::r#async(); //~ ERROR expected identifier, found reserved keyword `async`

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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-
| ^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^ 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)
88
help: you can escape reserved keywords to use them as identifiers

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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-
| ^^^^^
5+
| ^^^^^ expected identifier, found reserved keyword
66
help: you can escape reserved keywords to use them as identifiers
77
|
88
LL | let mut r#async = 1; //~ ERROR expected identifier, found reserved keyword `async`
@@ -12,7 +12,7 @@ error: expected identifier, found reserved keyword `async`
1212
--> $DIR/edition-keywords-2018-2018-parsing.rs:18:13
1313
|
1414
LL | module::async(); //~ ERROR expected identifier, found reserved keyword `async`
15-
| ^^^^^
15+
| ^^^^^ expected identifier, found reserved keyword
1616
help: you can escape reserved keywords to use them as identifiers
1717
|
1818
LL | module::r#async(); //~ ERROR expected identifier, found reserved keyword `async`

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/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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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-
| ^^^
5+
| ^^^ expected identifier, found keyword
66
help: you can escape reserved keywords to use them as identifiers
77
|
88
LL | type A1 = dyn::r#dyn; //~ERROR expected identifier, found keyword `dyn`

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)