Skip to content

rc"" more clear error message #140175

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
Apr 23, 2025
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
1 change: 1 addition & 0 deletions compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,7 @@ parse_unknown_prefix = prefix `{$prefix}` is unknown
.label = unknown prefix
.note = prefixed identifiers and literals are reserved since Rust 2021
.suggestion_br = use `br` for a raw byte string
.suggestion_cr = use `cr` for a raw C-string
.suggestion_str = if you meant to write a string literal, use double quotes
.suggestion_whitespace = consider inserting whitespace here

Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2140,6 +2140,13 @@ pub(crate) enum UnknownPrefixSugg {
style = "verbose"
)]
UseBr(#[primary_span] Span),
#[suggestion(
parse_suggestion_cr,
code = "cr",
applicability = "maybe-incorrect",
style = "verbose"
)]
UseCr(#[primary_span] Span),
#[suggestion(
parse_suggestion_whitespace,
code = " ",
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
let lit_start = start + BytePos(prefix_len);
self.pos = lit_start;
self.cursor = Cursor::new(&str_before[prefix_len as usize..]);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please don't remove unrelated lines

self.report_unknown_prefix(start);
let prefix_span = self.mk_sp(start, lit_start);
return (Token::new(self.ident(start), prefix_span), preceded_by_whitespace);
Expand Down Expand Up @@ -789,13 +788,14 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
fn report_unknown_prefix(&self, start: BytePos) {
let prefix_span = self.mk_sp(start, self.pos);
let prefix = self.str_from_to(start, self.pos);

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

if expn_data.edition.at_least_rust_2021() {
// In Rust 2021, this is a hard error.
let sugg = if prefix == "rb" {
Some(errors::UnknownPrefixSugg::UseBr(prefix_span))
} else if prefix == "rc" {
Some(errors::UnknownPrefixSugg::UseCr(prefix_span))
} else if expn_data.is_root() {
if self.cursor.first() == '\''
&& let Some(start) = self.last_lifetime
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/suggestions/raw-c-string-prefix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// `rc` and `cr` are easy to confuse; check that we issue a suggestion to help.

//@ edition:2021

fn main() {
rc"abc";
//~^ ERROR: prefix `rc` is unknown
//~| HELP: use `cr` for a raw C-string
//~| ERROR: expected one of
}
21 changes: 21 additions & 0 deletions tests/ui/suggestions/raw-c-string-prefix.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error: prefix `rc` is unknown
--> $DIR/raw-c-string-prefix.rs:6:5
|
LL | rc"abc";
| ^^ unknown prefix
|
= note: prefixed identifiers and literals are reserved since Rust 2021
help: use `cr` for a raw C-string
|
LL - rc"abc";
LL + cr"abc";
|

error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `"abc"`
--> $DIR/raw-c-string-prefix.rs:6:7
|
LL | rc"abc";
| ^^^^^ expected one of 8 possible tokens

error: aborting due to 2 previous errors

Loading