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

Lines changed: 13 additions & 12 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

0 commit comments

Comments
 (0)