Skip to content
Open
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
41 changes: 40 additions & 1 deletion compiler/rustc_parse/src/lexer/unescape_error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::iter::once;
use std::ops::Range;

use rustc_errors::{Applicability, DiagCtxtHandle, ErrorGuaranteed};
use rustc_errors::{Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed};
use rustc_literal_escaper::{EscapeError, Mode};
use rustc_span::{BytePos, Span};
use tracing::debug;
Expand Down Expand Up @@ -172,6 +172,8 @@ pub(crate) fn emit_unescape_error(
"for more information, visit \
<https://doc.rust-lang.org/reference/tokens.html#literals>",
);

foreign_escape_suggestion(&mut diag, (&ec, span), err_span);
}
diag.emit()
}
Expand Down Expand Up @@ -302,6 +304,43 @@ pub(crate) fn emit_unescape_error(
})
}

/// Add additional suggestions for escapes that are supported by C.
fn foreign_escape_suggestion(
diag: &mut Diag<'_>,
(escaped_char, escape_span): (&str, Span),
err_span: Span,
) {
if escaped_char == "?" {
diag.span_suggestion(
err_span,
"if you meant to write a literal question mark, don't escape the character",
"?",
Applicability::MaybeIncorrect,
);
Comment thread
euclio marked this conversation as resolved.
return;
}

let (name, hex) = match escaped_char {
"a" => ("an audible bell", String::from("07")),
"b" => ("a backspace", String::from("08")),
"f" => ("a form feed", String::from("0C")),
"v" => ("a vertical tab", String::from("0B")),
"e" => ("an ANSI escape sequence", String::from("1B")),
ec if u8::from_str_radix(ec, 8).is_ok() => {
diag.help(r"if you meant to write an ASCII control code, use a `\xNN` hex escape");
return;
}
_ => return,
};
Comment on lines +323 to +334
Copy link
Copy Markdown
Member

@mu001999 mu001999 May 15, 2026

Choose a reason for hiding this comment

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

Suggested change
let (name, hex) = match escaped_char {
"a" => ("an audible bell", String::from("07")),
"b" => ("a backspace", String::from("08")),
"f" => ("a form feed", String::from("0C")),
"v" => ("a vertical tab", String::from("0B")),
"e" => ("an ANSI escape sequence", String::from("1B")),
ec if u8::from_str_radix(ec, 8).is_ok() => {
diag.help(r"if you meant to write an ASCII control code, use a `\xNN` hex escape");
return;
}
_ => return,
};
if u8::from_str_radix(escaped_char, 8).is_ok() {
diag.help(r"if you meant to write an ASCII control code, use a `\xNN` hex escape");
return;
}
let (name, hex) = match escaped_char {
"a" => ("an audible bell", "07"),
"b" => ("a backspace", "08"),
"f" => ("a form feed", "0C"),
"v" => ("a vertical tab", "0B"),
"e" => ("an ANSI escape sequence", "1B"),
_ => return,
};

View changes since the review


diag.span_suggestion(
escape_span,
format!("if you meant to write {name}, use a hex escape"),
format!("x{hex}"),
Applicability::MaybeIncorrect,
);
Comment thread
euclio marked this conversation as resolved.
}

/// Pushes a character to a message string for error reporting
pub(crate) fn escaped_char(c: char) -> String {
match c {
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/parser/byte-literals.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ LL | static FOO: u8 = b'\f';
| ^ unknown byte escape
|
= help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
help: if you meant to write a form feed, use a hex escape
|
LL - static FOO: u8 = b'\f';
LL + static FOO: u8 = b'\x0C';
|

error: unknown byte escape: `f`
--> $DIR/byte-literals.rs:6:8
Expand All @@ -13,6 +18,11 @@ LL | b'\f';
| ^ unknown byte escape
|
= help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
help: if you meant to write a form feed, use a hex escape
|
LL - b'\f';
LL + b'\x0C';
|

error: invalid character in numeric character escape: `Z`
--> $DIR/byte-literals.rs:7:10
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/parser/byte-string-literals.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ LL | static FOO: &'static [u8] = b"\f";
| ^ unknown byte escape
|
= help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
help: if you meant to write a form feed, use a hex escape
|
LL - static FOO: &'static [u8] = b"\f";
LL + static FOO: &'static [u8] = b"\x0C";
|

error: unknown byte escape: `f`
--> $DIR/byte-string-literals.rs:4:8
Expand All @@ -13,6 +18,11 @@ LL | b"\f";
| ^ unknown byte escape
|
= help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
help: if you meant to write a form feed, use a hex escape
|
LL - b"\f";
LL + b"\x0C";
|

error: invalid character in numeric character escape: `Z`
--> $DIR/byte-string-literals.rs:5:10
Expand Down
25 changes: 25 additions & 0 deletions tests/ui/parser/foreign-escapes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Specified by both C and Rust
pub const SINGLE_QUOTE: char = '\'';
pub const DOUBLE_QUOTE: char = '\"';
pub const BACKSLASH: char = '\\';
pub const NEWLINE: char = '\n';
pub const CARRIAGE_RETURN: char = '\r';
pub const HORIZONTAL_TAB: char = '\t';
pub const NULL: char = '\0';

// Specified by C, but not Rust
pub const QUESTION_MARK: char = '\?'; //~ ERROR unknown character escape
pub const AUDIBLE_BELL: char = '\a'; //~ ERROR unknown character escape
pub const BACKSPACE: char = '\b'; //~ ERROR unknown character escape
pub const FORM_FEED: char = '\f'; //~ ERROR unknown character escape
pub const VERTICAL_TAB: char = '\v'; //~ ERROR unknown character escape
pub const OCTAL: char = '\1'; //~ ERROR unknown character escape
pub const OCTAL_TWO_DIGIT: char = '\12'; //~ ERROR unknown character escape
pub const OCTAL_THREE_DIGIT: char = '\12'; //~ ERROR unknown character escape
pub const OCTAL_OUT_OF_RANGE: char = '\9'; //~ ERROR unknown character escape

// Not specified by C, but recognized by GCC as an extension.
// Used for ANSI escape sequences in terminal emulators.
pub const ESCAPE: char = '\e'; //~ ERROR unknown character escape

fn main() {}
165 changes: 165 additions & 0 deletions tests/ui/parser/foreign-escapes.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
error: unknown character escape: `?`
--> $DIR/foreign-escapes.rs:11:35
|
LL | pub const QUESTION_MARK: char = '\?';
| ^ unknown character escape
|
= help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal
|
LL - pub const QUESTION_MARK: char = '\?';
LL + pub const QUESTION_MARK: char = r"\?";
|
help: if you meant to write a literal question mark, don't escape the character
|
LL - pub const QUESTION_MARK: char = '\?';
LL + pub const QUESTION_MARK: char = '?';
|

error: unknown character escape: `a`
--> $DIR/foreign-escapes.rs:12:34
|
LL | pub const AUDIBLE_BELL: char = '\a';
| ^ unknown character escape
|
= help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal
|
LL - pub const AUDIBLE_BELL: char = '\a';
LL + pub const AUDIBLE_BELL: char = r"\a";
|
help: if you meant to write an audible bell, use a hex escape
|
LL - pub const AUDIBLE_BELL: char = '\a';
LL + pub const AUDIBLE_BELL: char = '\x07';
|

error: unknown character escape: `b`
--> $DIR/foreign-escapes.rs:13:31
|
LL | pub const BACKSPACE: char = '\b';
| ^ unknown character escape
|
= help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal
|
LL - pub const BACKSPACE: char = '\b';
LL + pub const BACKSPACE: char = r"\b";
|
help: if you meant to write a backspace, use a hex escape
|
LL - pub const BACKSPACE: char = '\b';
LL + pub const BACKSPACE: char = '\x08';
|

error: unknown character escape: `f`
--> $DIR/foreign-escapes.rs:14:31
|
LL | pub const FORM_FEED: char = '\f';
| ^ unknown character escape
|
= help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal
|
LL - pub const FORM_FEED: char = '\f';
LL + pub const FORM_FEED: char = r"\f";
|
help: if you meant to write a form feed, use a hex escape
|
LL - pub const FORM_FEED: char = '\f';
LL + pub const FORM_FEED: char = '\x0C';
|

error: unknown character escape: `v`
--> $DIR/foreign-escapes.rs:15:34
|
LL | pub const VERTICAL_TAB: char = '\v';
| ^ unknown character escape
|
= help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal
|
LL - pub const VERTICAL_TAB: char = '\v';
LL + pub const VERTICAL_TAB: char = r"\v";
|
help: if you meant to write a vertical tab, use a hex escape
|
LL - pub const VERTICAL_TAB: char = '\v';
LL + pub const VERTICAL_TAB: char = '\x0B';
|

error: unknown character escape: `1`
--> $DIR/foreign-escapes.rs:16:27
|
LL | pub const OCTAL: char = '\1';
| ^ unknown character escape
|
= help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
= help: if you meant to write an ASCII control code, use a `\xNN` hex escape
help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal
|
LL - pub const OCTAL: char = '\1';
LL + pub const OCTAL: char = r"\1";
|

error: unknown character escape: `1`
--> $DIR/foreign-escapes.rs:17:37
|
LL | pub const OCTAL_TWO_DIGIT: char = '\12';
| ^ unknown character escape
|
= help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
= help: if you meant to write an ASCII control code, use a `\xNN` hex escape
help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal
|
LL - pub const OCTAL_TWO_DIGIT: char = '\12';
LL + pub const OCTAL_TWO_DIGIT: char = r"\12";
|

error: unknown character escape: `1`
--> $DIR/foreign-escapes.rs:18:39
|
LL | pub const OCTAL_THREE_DIGIT: char = '\12';
| ^ unknown character escape
|
= help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
= help: if you meant to write an ASCII control code, use a `\xNN` hex escape
help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal
|
LL - pub const OCTAL_THREE_DIGIT: char = '\12';
LL + pub const OCTAL_THREE_DIGIT: char = r"\12";
|

error: unknown character escape: `9`
--> $DIR/foreign-escapes.rs:19:40
|
LL | pub const OCTAL_OUT_OF_RANGE: char = '\9';
| ^ unknown character escape
|
= help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal
|
LL - pub const OCTAL_OUT_OF_RANGE: char = '\9';
LL + pub const OCTAL_OUT_OF_RANGE: char = r"\9";
|

error: unknown character escape: `e`
--> $DIR/foreign-escapes.rs:23:28
|
LL | pub const ESCAPE: char = '\e';
| ^ unknown character escape
|
= help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal
|
LL - pub const ESCAPE: char = '\e';
LL + pub const ESCAPE: char = r"\e";
|
help: if you meant to write an ANSI escape sequence, use a hex escape
|
LL - pub const ESCAPE: char = '\e';
LL + pub const ESCAPE: char = '\x1B';
|

error: aborting due to 10 previous errors

Loading