From f2c9654dcd53184eb5b585f117e856b87f9404c1 Mon Sep 17 00:00:00 2001 From: Fabian Wolff Date: Sat, 31 Jul 2021 15:37:36 +0200 Subject: [PATCH] Suggest `br` if the unknown string prefix `rb` is found --- compiler/rustc_parse/src/lexer/mod.rs | 14 ++++++++++--- .../ui/suggestions/raw-byte-string-prefix.rs | 10 ++++++++++ .../suggestions/raw-byte-string-prefix.stderr | 20 +++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/suggestions/raw-byte-string-prefix.rs create mode 100644 src/test/ui/suggestions/raw-byte-string-prefix.stderr diff --git a/compiler/rustc_parse/src/lexer/mod.rs b/compiler/rustc_parse/src/lexer/mod.rs index 98befe4066bc..1e65cc27154a 100644 --- a/compiler/rustc_parse/src/lexer/mod.rs +++ b/compiler/rustc_parse/src/lexer/mod.rs @@ -505,7 +505,8 @@ impl<'a> StringReader<'a> { // identifier tokens. fn report_unknown_prefix(&self, start: BytePos) { let prefix_span = self.mk_sp(start, self.pos); - let msg = format!("prefix `{}` is unknown", self.str_from_to(start, self.pos)); + let prefix_str = self.str_from_to(start, self.pos); + let msg = format!("prefix `{}` is unknown", prefix_str); let expn_data = prefix_span.ctxt().outer_expn_data(); @@ -513,12 +514,19 @@ impl<'a> StringReader<'a> { // In Rust 2021, this is a hard error. let mut err = self.sess.span_diagnostic.struct_span_err(prefix_span, &msg); err.span_label(prefix_span, "unknown prefix"); - if expn_data.is_root() { + if prefix_str == "rb" { + err.span_suggestion_verbose( + prefix_span, + "use `br` for a raw byte string", + "br".to_string(), + Applicability::MaybeIncorrect, + ); + } else if expn_data.is_root() { err.span_suggestion_verbose( prefix_span.shrink_to_hi(), "consider inserting whitespace here", " ".into(), - Applicability::MachineApplicable, + Applicability::MaybeIncorrect, ); } err.note("prefixed identifiers and literals are reserved since Rust 2021"); diff --git a/src/test/ui/suggestions/raw-byte-string-prefix.rs b/src/test/ui/suggestions/raw-byte-string-prefix.rs new file mode 100644 index 000000000000..576561c315ce --- /dev/null +++ b/src/test/ui/suggestions/raw-byte-string-prefix.rs @@ -0,0 +1,10 @@ +// `br` and `rb` are easy to confuse; check that we issue a suggestion to help. + +// edition:2021 + +fn main() { + rb"abc"; + //~^ ERROR: prefix `rb` is unknown + //~| HELP: use `br` for a raw byte string + //~| ERROR: expected one of +} diff --git a/src/test/ui/suggestions/raw-byte-string-prefix.stderr b/src/test/ui/suggestions/raw-byte-string-prefix.stderr new file mode 100644 index 000000000000..1498f5ac9351 --- /dev/null +++ b/src/test/ui/suggestions/raw-byte-string-prefix.stderr @@ -0,0 +1,20 @@ +error: prefix `rb` is unknown + --> $DIR/raw-byte-string-prefix.rs:6:5 + | +LL | rb"abc"; + | ^^ unknown prefix + | + = note: prefixed identifiers and literals are reserved since Rust 2021 +help: use `br` for a raw byte string + | +LL | br"abc"; + | ^^ + +error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `"abc"` + --> $DIR/raw-byte-string-prefix.rs:6:7 + | +LL | rb"abc"; + | ^^^^^ expected one of 8 possible tokens + +error: aborting due to 2 previous errors +