Skip to content

Commit 4e28279

Browse files
committed
Auto merge of #87662 - FabianWolff:rb-string, r=estebank
Suggest `br` if the unknown string prefix `rb` is found Currently, for the following code: ```rust fn main() { rb"abc"; } ``` we issue the following suggestion: ``` help: consider inserting whitespace here | 2 | rb "abc"; | -- ``` With my changes (only in edition 2021, where unknown prefixes became an error), I get: ``` help: use `br` for a raw byte string | 2 | br"abc"; | ^^ ```
2 parents 29f8de0 + f2c9654 commit 4e28279

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

compiler/rustc_parse/src/lexer/mod.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -505,20 +505,28 @@ impl<'a> StringReader<'a> {
505505
// identifier tokens.
506506
fn report_unknown_prefix(&self, start: BytePos) {
507507
let prefix_span = self.mk_sp(start, self.pos);
508-
let msg = format!("prefix `{}` is unknown", self.str_from_to(start, self.pos));
508+
let prefix_str = self.str_from_to(start, self.pos);
509+
let msg = format!("prefix `{}` is unknown", prefix_str);
509510

510511
let expn_data = prefix_span.ctxt().outer_expn_data();
511512

512513
if expn_data.edition >= Edition::Edition2021 {
513514
// In Rust 2021, this is a hard error.
514515
let mut err = self.sess.span_diagnostic.struct_span_err(prefix_span, &msg);
515516
err.span_label(prefix_span, "unknown prefix");
516-
if expn_data.is_root() {
517+
if prefix_str == "rb" {
518+
err.span_suggestion_verbose(
519+
prefix_span,
520+
"use `br` for a raw byte string",
521+
"br".to_string(),
522+
Applicability::MaybeIncorrect,
523+
);
524+
} else if expn_data.is_root() {
517525
err.span_suggestion_verbose(
518526
prefix_span.shrink_to_hi(),
519527
"consider inserting whitespace here",
520528
" ".into(),
521-
Applicability::MachineApplicable,
529+
Applicability::MaybeIncorrect,
522530
);
523531
}
524532
err.note("prefixed identifiers and literals are reserved since Rust 2021");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// `br` and `rb` are easy to confuse; check that we issue a suggestion to help.
2+
3+
// edition:2021
4+
5+
fn main() {
6+
rb"abc";
7+
//~^ ERROR: prefix `rb` is unknown
8+
//~| HELP: use `br` for a raw byte string
9+
//~| ERROR: expected one of
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: prefix `rb` is unknown
2+
--> $DIR/raw-byte-string-prefix.rs:6:5
3+
|
4+
LL | rb"abc";
5+
| ^^ unknown prefix
6+
|
7+
= note: prefixed identifiers and literals are reserved since Rust 2021
8+
help: use `br` for a raw byte string
9+
|
10+
LL | br"abc";
11+
| ^^
12+
13+
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `"abc"`
14+
--> $DIR/raw-byte-string-prefix.rs:6:7
15+
|
16+
LL | rb"abc";
17+
| ^^^^^ expected one of 8 possible tokens
18+
19+
error: aborting due to 2 previous errors
20+

0 commit comments

Comments
 (0)