Skip to content

Commit 0031807

Browse files
committed
Improve unescape error reporting for multibyte characters in byte literals
1 parent f174fd7 commit 0031807

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

compiler/rustc_parse/src/lexer/unescape_error_reporting.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,7 @@ pub(crate) fn emit_unescape_error(
199199
err.span_label(span, format!("must be ASCII{postfix}"));
200200
// Note: the \\xHH suggestions are not given for raw byte string
201201
// literals, because they are araw and so cannot use any escapes.
202-
if (c as u32) <= 0xFF && mode != Mode::RawByteStr {
203-
err.span_suggestion(
204-
span,
205-
format!(
206-
"if you meant to use the unicode code point for {c:?}, use a \\xHH escape"
207-
),
208-
format!("\\x{:X}", c as u32),
209-
Applicability::MaybeIncorrect,
210-
);
211-
} else if mode == Mode::Byte {
202+
if mode == Mode::Byte {
212203
err.span_label(span, "this multibyte character does not fit into a single byte");
213204
} else if mode != Mode::RawByteStr {
214205
let mut utf8 = String::new();

tests/ui/suggestions/multibyte-escapes.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
fn main() {
44
b'µ';
55
//~^ ERROR: non-ASCII character in byte literal
6-
//~| HELP: if you meant to use the unicode code point for 'µ', use a \xHH escape
6+
//~| NOTE: this multibyte character does not fit into a single byte
77
//~| NOTE: must be ASCII
88

99
b'字';
@@ -15,4 +15,14 @@ fn main() {
1515
//~^ ERROR: non-ASCII character in byte string literal
1616
//~| HELP: if you meant to use the UTF-8 encoding of '字', use \xHH escapes
1717
//~| NOTE: must be ASCII
18+
19+
b"µ";
20+
//~^ ERROR: non-ASCII character in byte string literal
21+
//~| HELP: if you meant to use the UTF-8 encoding of 'µ', use \xHH escapes
22+
//~| NOTE: must be ASCII
23+
24+
b"ñ";
25+
//~^ ERROR: non-ASCII character in byte string literal
26+
//~| HELP: if you meant to use the UTF-8 encoding of 'ñ', use \xHH escapes
27+
//~| NOTE: must be ASCII
1828
}

tests/ui/suggestions/multibyte-escapes.stderr

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ error: non-ASCII character in byte literal
22
--> $DIR/multibyte-escapes.rs:4:7
33
|
44
LL | b'µ';
5-
| ^ must be ASCII
6-
|
7-
help: if you meant to use the unicode code point for 'µ', use a \xHH escape
8-
|
9-
LL - b'µ';
10-
LL + b'\xB5';
11-
|
5+
| ^
6+
| |
7+
| must be ASCII
8+
| this multibyte character does not fit into a single byte
129

1310
error: non-ASCII character in byte literal
1411
--> $DIR/multibyte-escapes.rs:9:7
@@ -31,5 +28,29 @@ LL - b"字";
3128
LL + b"\xE5\xAD\x97";
3229
|
3330

34-
error: aborting due to 3 previous errors
31+
error: non-ASCII character in byte string literal
32+
--> $DIR/multibyte-escapes.rs:19:7
33+
|
34+
LL | b"µ";
35+
| ^ must be ASCII
36+
|
37+
help: if you meant to use the UTF-8 encoding of 'µ', use \xHH escapes
38+
|
39+
LL - b"µ";
40+
LL + b"\xC2\xB5";
41+
|
42+
43+
error: non-ASCII character in byte string literal
44+
--> $DIR/multibyte-escapes.rs:24:7
45+
|
46+
LL | b"ñ";
47+
| ^ must be ASCII
48+
|
49+
help: if you meant to use the UTF-8 encoding of 'ñ', use \xHH escapes
50+
|
51+
LL - b"ñ";
52+
LL + b"\xC3\xB1";
53+
|
54+
55+
error: aborting due to 5 previous errors
3556

0 commit comments

Comments
 (0)