Make non-ASCII errors more consistent.
There are three kinds of "byte" literals: byte literals, byte string literals, and raw byte string literals. None are allowed to have non-ASCII chars in them. Two `EscapeError` variants exist for when that constraint is violated. - `NonAsciiCharInByte`: used for byte literals and byte string literals. - `NonAsciiCharInByteString`: used for raw byte string literals. As a result, the messages for raw byte string literals use different wording, without good reason. Also, byte string literals are incorrectly described as "byte constants" in some error messages. This commit eliminates `NonAsciiCharInByteString` so the three cases are handled similarly, and described correctly. The `mode` is enough to distinguish them. Note: Some existing error messages mention "byte constants" and some mention "byte literals". I went with the latter here, because it's a more correct name, as used by the Reference.
This commit is contained in:
parent
34b32b0dac
commit
7dbf2c0ed8
15 changed files with 62 additions and 74 deletions
|
|
@ -52,10 +52,8 @@ pub enum EscapeError {
|
|||
|
||||
/// Unicode escape code in byte literal.
|
||||
UnicodeEscapeInByte,
|
||||
/// Non-ascii character in byte literal.
|
||||
/// Non-ascii character in byte literal, byte string literal, or raw byte string literal.
|
||||
NonAsciiCharInByte,
|
||||
/// Non-ascii character in byte string literal.
|
||||
NonAsciiCharInByteString,
|
||||
|
||||
/// After a line ending with '\', the next line contains whitespace
|
||||
/// characters that are not skipped.
|
||||
|
|
@ -349,8 +347,7 @@ where
|
|||
let start = src.len() - chars.as_str().len() - c.len_utf8();
|
||||
let result = match c {
|
||||
'\r' => Err(EscapeError::BareCarriageReturnInRawString),
|
||||
c if is_byte && !c.is_ascii() => Err(EscapeError::NonAsciiCharInByteString),
|
||||
c => Ok(c),
|
||||
_ => ascii_check(c, is_byte),
|
||||
};
|
||||
let end = src.len() - chars.as_str().len();
|
||||
callback(start..end, result);
|
||||
|
|
|
|||
|
|
@ -289,9 +289,6 @@ fn test_unescape_raw_byte_str() {
|
|||
}
|
||||
|
||||
check("\r", &[(0..1, Err(EscapeError::BareCarriageReturnInRawString))]);
|
||||
check("🦀", &[(0..4, Err(EscapeError::NonAsciiCharInByteString))]);
|
||||
check(
|
||||
"🦀a",
|
||||
&[(0..4, Err(EscapeError::NonAsciiCharInByteString)), (4..5, Ok(byte_from_char('a')))],
|
||||
);
|
||||
check("🦀", &[(0..4, Err(EscapeError::NonAsciiCharInByte))]);
|
||||
check("🦀a", &[(0..4, Err(EscapeError::NonAsciiCharInByte)), (4..5, Ok(byte_from_char('a')))]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -231,16 +231,23 @@ pub(crate) fn emit_unescape_error(
|
|||
.emit();
|
||||
}
|
||||
EscapeError::NonAsciiCharInByte => {
|
||||
assert!(mode.is_byte());
|
||||
let (c, span) = last_char();
|
||||
let mut err = handler.struct_span_err(span, "non-ASCII character in byte constant");
|
||||
let desc = match mode {
|
||||
Mode::Byte => "byte literal",
|
||||
Mode::ByteStr => "byte string literal",
|
||||
Mode::RawByteStr => "raw byte string literal",
|
||||
_ => panic!("non-is_byte literal paired with NonAsciiCharInByte"),
|
||||
};
|
||||
let mut err = handler.struct_span_err(span, format!("non-ASCII character in {}", desc));
|
||||
let postfix = if unicode_width::UnicodeWidthChar::width(c).unwrap_or(1) == 0 {
|
||||
format!(" but is {:?}", c)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
err.span_label(span, &format!("byte constant must be ASCII{}", postfix));
|
||||
if (c as u32) <= 0xFF {
|
||||
err.span_label(span, &format!("must be ASCII{}", postfix));
|
||||
// Note: the \\xHH suggestions are not given for raw byte string
|
||||
// literals, because they are araw and so cannot use any escapes.
|
||||
if (c as u32) <= 0xFF && mode != Mode::RawByteStr {
|
||||
err.span_suggestion(
|
||||
span,
|
||||
&format!(
|
||||
|
|
@ -250,9 +257,9 @@ pub(crate) fn emit_unescape_error(
|
|||
format!("\\x{:X}", c as u32),
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
} else if matches!(mode, Mode::Byte) {
|
||||
} else if mode == Mode::Byte {
|
||||
err.span_label(span, "this multibyte character does not fit into a single byte");
|
||||
} else if matches!(mode, Mode::ByteStr) {
|
||||
} else if mode != Mode::RawByteStr {
|
||||
let mut utf8 = String::new();
|
||||
utf8.push(c);
|
||||
err.span_suggestion(
|
||||
|
|
@ -270,19 +277,6 @@ pub(crate) fn emit_unescape_error(
|
|||
}
|
||||
err.emit();
|
||||
}
|
||||
EscapeError::NonAsciiCharInByteString => {
|
||||
assert!(mode.is_byte());
|
||||
let (c, span) = last_char();
|
||||
let postfix = if unicode_width::UnicodeWidthChar::width(c).unwrap_or(1) == 0 {
|
||||
format!(" but is {:?}", c)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
handler
|
||||
.struct_span_err(span, "raw byte string must be ASCII")
|
||||
.span_label(span, &format!("must be ASCII{}", postfix))
|
||||
.emit();
|
||||
}
|
||||
EscapeError::OutOfRangeHexEscape => {
|
||||
handler
|
||||
.struct_span_err(span, "out of range hex escape")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue