Fix invalid suggestions for non-ASCII characters in byte constants
This commit is contained in:
parent
fc24bcead1
commit
c1abb6f4d6
6 changed files with 98 additions and 20 deletions
|
@ -153,16 +153,37 @@ pub(crate) fn emit_unescape_error(
|
||||||
EscapeError::NonAsciiCharInByte => {
|
EscapeError::NonAsciiCharInByte => {
|
||||||
assert!(mode.is_bytes());
|
assert!(mode.is_bytes());
|
||||||
let (c, span) = last_char();
|
let (c, span) = last_char();
|
||||||
handler
|
let mut err = handler.struct_span_err(span, "non-ASCII character in byte constant");
|
||||||
.struct_span_err(span, "non-ASCII character in byte constant")
|
err.span_label(span, "byte constant must be ASCII");
|
||||||
.span_label(span, "byte constant must be ASCII")
|
if (c as u32) <= 0xFF {
|
||||||
.span_suggestion(
|
err.span_suggestion(
|
||||||
span,
|
span,
|
||||||
"use a \\xHH escape for a non-ASCII byte",
|
&format!(
|
||||||
|
"if you meant to use the unicode code point for '{}', use a \\xHH escape",
|
||||||
|
c
|
||||||
|
),
|
||||||
format!("\\x{:X}", c as u32),
|
format!("\\x{:X}", c as u32),
|
||||||
Applicability::MachineApplicable,
|
Applicability::MaybeIncorrect,
|
||||||
)
|
);
|
||||||
.emit();
|
} else if matches!(mode, Mode::Byte) {
|
||||||
|
err.span_label(span, "this multibyte character does not fit into a single byte");
|
||||||
|
} else if matches!(mode, Mode::ByteStr) {
|
||||||
|
let mut utf8 = String::new();
|
||||||
|
utf8.push(c);
|
||||||
|
err.span_suggestion(
|
||||||
|
span,
|
||||||
|
&format!(
|
||||||
|
"if you meant to use the UTF-8 encoding of '{}', use \\xHH escapes",
|
||||||
|
c
|
||||||
|
),
|
||||||
|
utf8.as_bytes()
|
||||||
|
.iter()
|
||||||
|
.map(|b: &u8| format!("\\x{:X}", *b))
|
||||||
|
.fold("".to_string(), |a, c| a + &c),
|
||||||
|
Applicability::MaybeIncorrect,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
err.emit();
|
||||||
}
|
}
|
||||||
EscapeError::NonAsciiCharInByteString => {
|
EscapeError::NonAsciiCharInByteString => {
|
||||||
assert!(mode.is_bytes());
|
assert!(mode.is_bytes());
|
||||||
|
|
|
@ -2,10 +2,12 @@ error: non-ASCII character in byte constant
|
||||||
--> $DIR/key-value-non-ascii.rs:3:19
|
--> $DIR/key-value-non-ascii.rs:3:19
|
||||||
|
|
|
|
||||||
LL | #[rustc_dummy = b"ffi.rs"]
|
LL | #[rustc_dummy = b"ffi.rs"]
|
||||||
| ^
|
| ^ byte constant must be ASCII
|
||||||
| |
|
|
|
||||||
| byte constant must be ASCII
|
help: if you meant to use the UTF-8 encoding of 'ffi', use \xHH escapes
|
||||||
| help: use a \xHH escape for a non-ASCII byte: `\xFB03`
|
|
|
||||||
|
LL | #[rustc_dummy = b"/xEF/xAC/x83.rs"]
|
||||||
|
| ^^^^^^^^^^^^
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
|
|
@ -36,10 +36,12 @@ error: non-ASCII character in byte constant
|
||||||
--> $DIR/byte-literals.rs:10:7
|
--> $DIR/byte-literals.rs:10:7
|
||||||
|
|
|
|
||||||
LL | b'é';
|
LL | b'é';
|
||||||
| ^
|
| ^ byte constant must be ASCII
|
||||||
| |
|
|
|
||||||
| byte constant must be ASCII
|
help: if you meant to use the unicode code point for 'é', use a \xHH escape
|
||||||
| help: use a \xHH escape for a non-ASCII byte: `\xE9`
|
|
|
||||||
|
LL | b'\xE9';
|
||||||
|
| ^^^^
|
||||||
|
|
||||||
error[E0763]: unterminated byte constant
|
error[E0763]: unterminated byte constant
|
||||||
--> $DIR/byte-literals.rs:11:6
|
--> $DIR/byte-literals.rs:11:6
|
||||||
|
|
|
@ -24,10 +24,12 @@ error: non-ASCII character in byte constant
|
||||||
--> $DIR/byte-string-literals.rs:6:7
|
--> $DIR/byte-string-literals.rs:6:7
|
||||||
|
|
|
|
||||||
LL | b"é";
|
LL | b"é";
|
||||||
| ^
|
| ^ byte constant must be ASCII
|
||||||
| |
|
|
|
||||||
| byte constant must be ASCII
|
help: if you meant to use the unicode code point for 'é', use a \xHH escape
|
||||||
| help: use a \xHH escape for a non-ASCII byte: `\xE9`
|
|
|
||||||
|
LL | b"\xE9";
|
||||||
|
| ^^^^
|
||||||
|
|
||||||
error: raw byte string must be ASCII
|
error: raw byte string must be ASCII
|
||||||
--> $DIR/byte-string-literals.rs:7:10
|
--> $DIR/byte-string-literals.rs:7:10
|
||||||
|
|
18
src/test/ui/suggestions/multibyte-escapes.rs
Normal file
18
src/test/ui/suggestions/multibyte-escapes.rs
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
// Regression test for #87397.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
b'µ';
|
||||||
|
//~^ ERROR: non-ASCII character in byte constant
|
||||||
|
//~| HELP: if you meant to use the unicode code point for 'µ', use a \xHH escape
|
||||||
|
//~| NOTE: byte constant must be ASCII
|
||||||
|
|
||||||
|
b'字';
|
||||||
|
//~^ ERROR: non-ASCII character in byte constant
|
||||||
|
//~| NOTE: this multibyte character does not fit into a single byte
|
||||||
|
//~| NOTE: byte constant must be ASCII
|
||||||
|
|
||||||
|
b"字";
|
||||||
|
//~^ ERROR: non-ASCII character in byte constant
|
||||||
|
//~| HELP: if you meant to use the UTF-8 encoding of '字', use \xHH escapes
|
||||||
|
//~| NOTE: byte constant must be ASCII
|
||||||
|
}
|
33
src/test/ui/suggestions/multibyte-escapes.stderr
Normal file
33
src/test/ui/suggestions/multibyte-escapes.stderr
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
error: non-ASCII character in byte constant
|
||||||
|
--> $DIR/multibyte-escapes.rs:4:7
|
||||||
|
|
|
||||||
|
LL | b'µ';
|
||||||
|
| ^ byte constant must be ASCII
|
||||||
|
|
|
||||||
|
help: if you meant to use the unicode code point for 'µ', use a \xHH escape
|
||||||
|
|
|
||||||
|
LL | b'\xB5';
|
||||||
|
| ^^^^
|
||||||
|
|
||||||
|
error: non-ASCII character in byte constant
|
||||||
|
--> $DIR/multibyte-escapes.rs:9:7
|
||||||
|
|
|
||||||
|
LL | b'字';
|
||||||
|
| ^^
|
||||||
|
| |
|
||||||
|
| byte constant must be ASCII
|
||||||
|
| this multibyte character does not fit into a single byte
|
||||||
|
|
||||||
|
error: non-ASCII character in byte constant
|
||||||
|
--> $DIR/multibyte-escapes.rs:14:7
|
||||||
|
|
|
||||||
|
LL | b"字";
|
||||||
|
| ^^ byte constant must be ASCII
|
||||||
|
|
|
||||||
|
help: if you meant to use the UTF-8 encoding of '字', use \xHH escapes
|
||||||
|
|
|
||||||
|
LL | b"\xE5\xAD\x97";
|
||||||
|
| ^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: aborting due to 3 previous errors
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue