Skip to content

Commit ecb9775

Browse files
authored
Rollup merge of #140175 - Kivooeo:new-fix-one, r=compiler-errors
`rc""` more clear error message here is small fix that provides better error message when user is trying to use `rc""` the same way it was made for `rb""` example of it's work ```rust | 2 | rc"\n"; | ^^ unknown prefix | = note: prefixed identifiers and literals are reserved since Rust 2021 help: use `cr` for a raw C-string | 2 - rc"\n"; 2 + cr"\n"; | ``` **related issue** fixes #140170 cc `@cyrgani` (issue author)
2 parents 45b5d8b + 44b19e5 commit ecb9775

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

compiler/rustc_parse/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,7 @@ parse_unknown_prefix = prefix `{$prefix}` is unknown
893893
.label = unknown prefix
894894
.note = prefixed identifiers and literals are reserved since Rust 2021
895895
.suggestion_br = use `br` for a raw byte string
896+
.suggestion_cr = use `cr` for a raw C-string
896897
.suggestion_str = if you meant to write a string literal, use double quotes
897898
.suggestion_whitespace = consider inserting whitespace here
898899

compiler/rustc_parse/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -2140,6 +2140,13 @@ pub(crate) enum UnknownPrefixSugg {
21402140
style = "verbose"
21412141
)]
21422142
UseBr(#[primary_span] Span),
2143+
#[suggestion(
2144+
parse_suggestion_cr,
2145+
code = "cr",
2146+
applicability = "maybe-incorrect",
2147+
style = "verbose"
2148+
)]
2149+
UseCr(#[primary_span] Span),
21432150
#[suggestion(
21442151
parse_suggestion_whitespace,
21452152
code = " ",

compiler/rustc_parse/src/lexer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
257257
let lit_start = start + BytePos(prefix_len);
258258
self.pos = lit_start;
259259
self.cursor = Cursor::new(&str_before[prefix_len as usize..]);
260-
261260
self.report_unknown_prefix(start);
262261
let prefix_span = self.mk_sp(start, lit_start);
263262
return (Token::new(self.ident(start), prefix_span), preceded_by_whitespace);
@@ -790,13 +789,14 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
790789
fn report_unknown_prefix(&self, start: BytePos) {
791790
let prefix_span = self.mk_sp(start, self.pos);
792791
let prefix = self.str_from_to(start, self.pos);
793-
794792
let expn_data = prefix_span.ctxt().outer_expn_data();
795793

796794
if expn_data.edition.at_least_rust_2021() {
797795
// In Rust 2021, this is a hard error.
798796
let sugg = if prefix == "rb" {
799797
Some(errors::UnknownPrefixSugg::UseBr(prefix_span))
798+
} else if prefix == "rc" {
799+
Some(errors::UnknownPrefixSugg::UseCr(prefix_span))
800800
} else if expn_data.is_root() {
801801
if self.cursor.first() == '\''
802802
&& let Some(start) = self.last_lifetime
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// `rc` and `cr` are easy to confuse; check that we issue a suggestion to help.
2+
3+
//@ edition:2021
4+
5+
fn main() {
6+
rc"abc";
7+
//~^ ERROR: prefix `rc` is unknown
8+
//~| HELP: use `cr` for a raw C-string
9+
//~| ERROR: expected one of
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error: prefix `rc` is unknown
2+
--> $DIR/raw-c-string-prefix.rs:6:5
3+
|
4+
LL | rc"abc";
5+
| ^^ unknown prefix
6+
|
7+
= note: prefixed identifiers and literals are reserved since Rust 2021
8+
help: use `cr` for a raw C-string
9+
|
10+
LL - rc"abc";
11+
LL + cr"abc";
12+
|
13+
14+
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `"abc"`
15+
--> $DIR/raw-c-string-prefix.rs:6:7
16+
|
17+
LL | rc"abc";
18+
| ^^^^^ expected one of 8 possible tokens
19+
20+
error: aborting due to 2 previous errors
21+

0 commit comments

Comments
 (0)