Skip to content

Suggest br if the unknown string prefix rb is found #87662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions compiler/rustc_parse/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,20 +505,28 @@ 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();

if expn_data.edition >= Edition::Edition2021 {
// 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");
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/suggestions/raw-byte-string-prefix.rs
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 20 additions & 0 deletions src/test/ui/suggestions/raw-byte-string-prefix.stderr
Original file line number Diff line number Diff line change
@@ -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