From 833f12ebd71259c21b03d8327107acab2309d064 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 29 Dec 2018 17:19:22 -0800 Subject: [PATCH] Suggest using raw identifiers in 2018 edition when using keywords --- src/libsyntax/parse/parser.rs | 13 ++++++++++++- .../edition-keywords-2015-2018-expansion.stderr | 6 +++++- .../edition-keywords-2018-2015-parsing.stderr | 12 ++++++++++-- .../edition-keywords-2018-2018-expansion.stderr | 6 +++++- .../edition-keywords-2018-2018-parsing.stderr | 12 ++++++++++-- .../ui/rust-2018/dyn-trait-compatibility.stderr | 6 +++++- 6 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 52da8a072c7..1a0366932db 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -798,7 +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 Some(token_descr) = self.token_descr() { + if let (true, token::Ident(ref s, false), true) = ( + self.span.rust_2018(), + &self.token, + self.token.is_used_keyword() || self.token.is_unused_keyword(), + ) { + err.span_suggestion_with_applicability( + self.span, + "you can escape reserved keywords to use them as identifiers", + format!("r#{}", s.to_string()), + Applicability::MaybeIncorrect, + ); + } else if let Some(token_descr) = self.token_descr() { err.span_label(self.span, format!("expected identifier, found {}", token_descr)); } else { err.span_label(self.span, "expected identifier"); diff --git a/src/test/ui/editions/edition-keywords-2015-2018-expansion.stderr b/src/test/ui/editions/edition-keywords-2015-2018-expansion.stderr index 5dd7030c77e..c598e2de4c6 100644 --- a/src/test/ui/editions/edition-keywords-2015-2018-expansion.stderr +++ b/src/test/ui/editions/edition-keywords-2015-2018-expansion.stderr @@ -2,9 +2,13 @@ error: expected identifier, found reserved keyword `async` --> $DIR/edition-keywords-2015-2018-expansion.rs:8:5 | LL | produces_async! {} //~ ERROR expected identifier, found reserved keyword - | ^^^^^^^^^^^^^^^^^^ 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 diff --git a/src/test/ui/editions/edition-keywords-2018-2015-parsing.stderr b/src/test/ui/editions/edition-keywords-2018-2015-parsing.stderr index bd49948eb95..5f89dc5cfbb 100644 --- a/src/test/ui/editions/edition-keywords-2018-2015-parsing.stderr +++ b/src/test/ui/editions/edition-keywords-2018-2015-parsing.stderr @@ -2,13 +2,21 @@ error: expected identifier, found reserved keyword `async` --> $DIR/edition-keywords-2018-2015-parsing.rs:8:13 | 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 diff --git a/src/test/ui/editions/edition-keywords-2018-2018-expansion.stderr b/src/test/ui/editions/edition-keywords-2018-2018-expansion.stderr index 44d0e78d06a..f0878f69b8f 100644 --- a/src/test/ui/editions/edition-keywords-2018-2018-expansion.stderr +++ b/src/test/ui/editions/edition-keywords-2018-2018-expansion.stderr @@ -2,9 +2,13 @@ error: expected identifier, found reserved keyword `async` --> $DIR/edition-keywords-2018-2018-expansion.rs:8:5 | LL | produces_async! {} //~ ERROR expected identifier, found reserved keyword `async` - | ^^^^^^^^^^^^^^^^^^ 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 diff --git a/src/test/ui/editions/edition-keywords-2018-2018-parsing.stderr b/src/test/ui/editions/edition-keywords-2018-2018-parsing.stderr index 8e596f743d7..994edb50f0d 100644 --- a/src/test/ui/editions/edition-keywords-2018-2018-parsing.stderr +++ b/src/test/ui/editions/edition-keywords-2018-2018-parsing.stderr @@ -2,13 +2,21 @@ error: expected identifier, found reserved keyword `async` --> $DIR/edition-keywords-2018-2018-parsing.rs:8:13 | 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 diff --git a/src/test/ui/rust-2018/dyn-trait-compatibility.stderr b/src/test/ui/rust-2018/dyn-trait-compatibility.stderr index bd72f9c6786..b7bcf987435 100644 --- a/src/test/ui/rust-2018/dyn-trait-compatibility.stderr +++ b/src/test/ui/rust-2018/dyn-trait-compatibility.stderr @@ -2,7 +2,11 @@ error: expected identifier, found keyword `dyn` --> $DIR/dyn-trait-compatibility.rs:4:16 | 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