diff options
| author | Fabian Wolff <fabian.wolff@alumni.ethz.ch> | 2021-07-31 14:37:01 +0200 |
|---|---|---|
| committer | Fabian Wolff <fabian.wolff@alumni.ethz.ch> | 2021-07-31 15:21:11 +0200 |
| commit | c1abb6f4d6756086d74008cacf51e42ae2cda7bb (patch) | |
| tree | eeacf856c8aff267961e10762e82865e7779f192 /compiler/rustc_parse | |
| parent | fc24bcead1d401ae061538d011e4a319c4195b56 (diff) | |
| download | rust-c1abb6f4d6756086d74008cacf51e42ae2cda7bb.tar.gz rust-c1abb6f4d6756086d74008cacf51e42ae2cda7bb.zip | |
Fix invalid suggestions for non-ASCII characters in byte constants
Diffstat (limited to 'compiler/rustc_parse')
| -rw-r--r-- | compiler/rustc_parse/src/lexer/unescape_error_reporting.rs | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs index a580f0c55d0..0f76898a912 100644 --- a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs +++ b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs @@ -153,16 +153,37 @@ pub(crate) fn emit_unescape_error( EscapeError::NonAsciiCharInByte => { assert!(mode.is_bytes()); let (c, span) = last_char(); - handler - .struct_span_err(span, "non-ASCII character in byte constant") - .span_label(span, "byte constant must be ASCII") - .span_suggestion( + let mut err = handler.struct_span_err(span, "non-ASCII character in byte constant"); + err.span_label(span, "byte constant must be ASCII"); + if (c as u32) <= 0xFF { + err.span_suggestion( 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), - Applicability::MachineApplicable, - ) - .emit(); + Applicability::MaybeIncorrect, + ); + } 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 => { assert!(mode.is_bytes()); |
