From 2c3bd8e6cc80275d19498560f54de7c5e4908bd0 Mon Sep 17 00:00:00 2001 From: Andy Russell Date: Sat, 9 May 2026 17:41:28 -0400 Subject: [PATCH] suggest hex escapes for C-style escapes --- .../src/lexer/unescape_error_reporting.rs | 41 ++++- tests/ui/parser/byte-literals.stderr | 10 ++ tests/ui/parser/byte-string-literals.stderr | 10 ++ tests/ui/parser/foreign-escapes.rs | 25 +++ tests/ui/parser/foreign-escapes.stderr | 165 ++++++++++++++++++ 5 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 tests/ui/parser/foreign-escapes.rs create mode 100644 tests/ui/parser/foreign-escapes.stderr diff --git a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs index 895374ab2cc4b..bdbdb708a31b0 100644 --- a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs +++ b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs @@ -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; @@ -172,6 +172,8 @@ pub(crate) fn emit_unescape_error( "for more information, visit \ ", ); + + foreign_escape_suggestion(&mut diag, (&ec, span), err_span); } diag.emit() } @@ -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, + ); + 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, + }; + + diag.span_suggestion( + escape_span, + format!("if you meant to write {name}, use a hex escape"), + format!("x{hex}"), + Applicability::MaybeIncorrect, + ); +} + /// Pushes a character to a message string for error reporting pub(crate) fn escaped_char(c: char) -> String { match c { diff --git a/tests/ui/parser/byte-literals.stderr b/tests/ui/parser/byte-literals.stderr index 1c89e8e2864b6..4df5f764cfe85 100644 --- a/tests/ui/parser/byte-literals.stderr +++ b/tests/ui/parser/byte-literals.stderr @@ -5,6 +5,11 @@ LL | static FOO: u8 = b'\f'; | ^ unknown byte escape | = help: for more information, visit +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 @@ -13,6 +18,11 @@ LL | b'\f'; | ^ unknown byte escape | = help: for more information, visit +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 diff --git a/tests/ui/parser/byte-string-literals.stderr b/tests/ui/parser/byte-string-literals.stderr index 3e589258d4132..d036fae68ea1f 100644 --- a/tests/ui/parser/byte-string-literals.stderr +++ b/tests/ui/parser/byte-string-literals.stderr @@ -5,6 +5,11 @@ LL | static FOO: &'static [u8] = b"\f"; | ^ unknown byte escape | = help: for more information, visit +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 @@ -13,6 +18,11 @@ LL | b"\f"; | ^ unknown byte escape | = help: for more information, visit +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 diff --git a/tests/ui/parser/foreign-escapes.rs b/tests/ui/parser/foreign-escapes.rs new file mode 100644 index 0000000000000..d802f858f8e31 --- /dev/null +++ b/tests/ui/parser/foreign-escapes.rs @@ -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() {} diff --git a/tests/ui/parser/foreign-escapes.stderr b/tests/ui/parser/foreign-escapes.stderr new file mode 100644 index 0000000000000..619d7d1e6e8b7 --- /dev/null +++ b/tests/ui/parser/foreign-escapes.stderr @@ -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 +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 +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 +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 +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 +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 + = 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 + = 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 + = 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 +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 +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 +