Skip to content

Commit 4380056

Browse files
authored
Rollup merge of #87659 - FabianWolff:issue-87397, r=davidtwco
Fix invalid suggestions for non-ASCII characters in byte constants Fixes #87397.
2 parents b1166e1 + c1abb6f commit 4380056

File tree

6 files changed

+98
-20
lines changed

6 files changed

+98
-20
lines changed

compiler/rustc_parse/src/lexer/unescape_error_reporting.rs

+29-8
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,37 @@ pub(crate) fn emit_unescape_error(
153153
EscapeError::NonAsciiCharInByte => {
154154
assert!(mode.is_bytes());
155155
let (c, span) = last_char();
156-
handler
157-
.struct_span_err(span, "non-ASCII character in byte constant")
158-
.span_label(span, "byte constant must be ASCII")
159-
.span_suggestion(
156+
let mut err = handler.struct_span_err(span, "non-ASCII character in byte constant");
157+
err.span_label(span, "byte constant must be ASCII");
158+
if (c as u32) <= 0xFF {
159+
err.span_suggestion(
160160
span,
161-
"use a \\xHH escape for a non-ASCII byte",
161+
&format!(
162+
"if you meant to use the unicode code point for '{}', use a \\xHH escape",
163+
c
164+
),
162165
format!("\\x{:X}", c as u32),
163-
Applicability::MachineApplicable,
164-
)
165-
.emit();
166+
Applicability::MaybeIncorrect,
167+
);
168+
} else if matches!(mode, Mode::Byte) {
169+
err.span_label(span, "this multibyte character does not fit into a single byte");
170+
} else if matches!(mode, Mode::ByteStr) {
171+
let mut utf8 = String::new();
172+
utf8.push(c);
173+
err.span_suggestion(
174+
span,
175+
&format!(
176+
"if you meant to use the UTF-8 encoding of '{}', use \\xHH escapes",
177+
c
178+
),
179+
utf8.as_bytes()
180+
.iter()
181+
.map(|b: &u8| format!("\\x{:X}", *b))
182+
.fold("".to_string(), |a, c| a + &c),
183+
Applicability::MaybeIncorrect,
184+
);
185+
}
186+
err.emit();
166187
}
167188
EscapeError::NonAsciiCharInByteString => {
168189
assert!(mode.is_bytes());

src/test/ui/attributes/key-value-non-ascii.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ error: non-ASCII character in byte constant
22
--> $DIR/key-value-non-ascii.rs:3:19
33
|
44
LL | #[rustc_dummy = b"ffi.rs"]
5-
| ^
6-
| |
7-
| byte constant must be ASCII
8-
| help: use a \xHH escape for a non-ASCII byte: `\xFB03`
5+
| ^ byte constant must be ASCII
6+
|
7+
help: if you meant to use the UTF-8 encoding of 'ffi', use \xHH escapes
8+
|
9+
LL | #[rustc_dummy = b"/xEF/xAC/x83.rs"]
10+
| ^^^^^^^^^^^^
911

1012
error: aborting due to previous error
1113

src/test/ui/parser/byte-literals.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ error: non-ASCII character in byte constant
3636
--> $DIR/byte-literals.rs:10:7
3737
|
3838
LL | b'é';
39-
| ^
40-
| |
41-
| byte constant must be ASCII
42-
| help: use a \xHH escape for a non-ASCII byte: `\xE9`
39+
| ^ byte constant must be ASCII
40+
|
41+
help: if you meant to use the unicode code point for 'é', use a \xHH escape
42+
|
43+
LL | b'\xE9';
44+
| ^^^^
4345

4446
error[E0763]: unterminated byte constant
4547
--> $DIR/byte-literals.rs:11:6

src/test/ui/parser/byte-string-literals.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ error: non-ASCII character in byte constant
2424
--> $DIR/byte-string-literals.rs:6:7
2525
|
2626
LL | b"é";
27-
| ^
28-
| |
29-
| byte constant must be ASCII
30-
| help: use a \xHH escape for a non-ASCII byte: `\xE9`
27+
| ^ byte constant must be ASCII
28+
|
29+
help: if you meant to use the unicode code point for 'é', use a \xHH escape
30+
|
31+
LL | b"\xE9";
32+
| ^^^^
3133

3234
error: raw byte string must be ASCII
3335
--> $DIR/byte-string-literals.rs:7:10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Regression test for #87397.
2+
3+
fn main() {
4+
b'µ';
5+
//~^ ERROR: non-ASCII character in byte constant
6+
//~| HELP: if you meant to use the unicode code point for 'µ', use a \xHH escape
7+
//~| NOTE: byte constant must be ASCII
8+
9+
b'字';
10+
//~^ ERROR: non-ASCII character in byte constant
11+
//~| NOTE: this multibyte character does not fit into a single byte
12+
//~| NOTE: byte constant must be ASCII
13+
14+
b"字";
15+
//~^ ERROR: non-ASCII character in byte constant
16+
//~| HELP: if you meant to use the UTF-8 encoding of '字', use \xHH escapes
17+
//~| NOTE: byte constant must be ASCII
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error: non-ASCII character in byte constant
2+
--> $DIR/multibyte-escapes.rs:4:7
3+
|
4+
LL | b'µ';
5+
| ^ byte constant must be ASCII
6+
|
7+
help: if you meant to use the unicode code point for 'µ', use a \xHH escape
8+
|
9+
LL | b'\xB5';
10+
| ^^^^
11+
12+
error: non-ASCII character in byte constant
13+
--> $DIR/multibyte-escapes.rs:9:7
14+
|
15+
LL | b'字';
16+
| ^^
17+
| |
18+
| byte constant must be ASCII
19+
| this multibyte character does not fit into a single byte
20+
21+
error: non-ASCII character in byte constant
22+
--> $DIR/multibyte-escapes.rs:14:7
23+
|
24+
LL | b"字";
25+
| ^^ byte constant must be ASCII
26+
|
27+
help: if you meant to use the UTF-8 encoding of '字', use \xHH escapes
28+
|
29+
LL | b"\xE5\xAD\x97";
30+
| ^^^^^^^^^^^^
31+
32+
error: aborting due to 3 previous errors
33+

0 commit comments

Comments
 (0)