1
Fork 0

Rename the span args to emit_unescape_error.

The `span` arg is described in a comment as "interior span of the
literal, without quotes", which is incorrect. It's actually the span of
the error part of the literal, corresponding to `range`.

This commit renames `span` and `span_without_quotes` to make things
clearer, and fixes the erroneous comment.
This commit is contained in:
Nicholas Nethercote 2023-12-11 16:56:52 +11:00
parent 4acc5e6480
commit 423bf4233d

View file

@ -11,12 +11,12 @@ use crate::errors::{MoreThanOneCharNote, MoreThanOneCharSugg, NoBraceUnicodeSub,
pub(crate) fn emit_unescape_error( pub(crate) fn emit_unescape_error(
handler: &Handler, handler: &Handler,
// interior part of the literal, without quotes // interior part of the literal, between quotes
lit: &str, lit: &str,
// full span of the literal, including quotes // full span of the literal, including quotes and any prefix
span_with_quotes: Span, full_lit_span: Span,
// interior span of the literal, without quotes // span of the error part of the literal
span: Span, err_span: Span,
mode: Mode, mode: Mode,
// range of the error inside `lit` // range of the error inside `lit`
range: Range<usize>, range: Range<usize>,
@ -24,19 +24,21 @@ pub(crate) fn emit_unescape_error(
) { ) {
debug!( debug!(
"emit_unescape_error: {:?}, {:?}, {:?}, {:?}, {:?}", "emit_unescape_error: {:?}, {:?}, {:?}, {:?}, {:?}",
lit, span_with_quotes, mode, range, error lit, full_lit_span, mode, range, error
); );
let last_char = || { let last_char = || {
let c = lit[range.clone()].chars().next_back().unwrap(); let c = lit[range.clone()].chars().next_back().unwrap();
let span = span.with_lo(span.hi() - BytePos(c.len_utf8() as u32)); let span = err_span.with_lo(err_span.hi() - BytePos(c.len_utf8() as u32));
(c, span) (c, span)
}; };
match error { match error {
EscapeError::LoneSurrogateUnicodeEscape => { EscapeError::LoneSurrogateUnicodeEscape => {
handler.emit_err(UnescapeError::InvalidUnicodeEscape { span, surrogate: true }); handler
.emit_err(UnescapeError::InvalidUnicodeEscape { span: err_span, surrogate: true });
} }
EscapeError::OutOfRangeUnicodeEscape => { EscapeError::OutOfRangeUnicodeEscape => {
handler.emit_err(UnescapeError::InvalidUnicodeEscape { span, surrogate: false }); handler
.emit_err(UnescapeError::InvalidUnicodeEscape { span: err_span, surrogate: false });
} }
EscapeError::MoreThanOneChar => { EscapeError::MoreThanOneChar => {
use unicode_normalization::{char::is_combining_mark, UnicodeNormalization}; use unicode_normalization::{char::is_combining_mark, UnicodeNormalization};
@ -49,12 +51,16 @@ pub(crate) fn emit_unescape_error(
let normalized = lit.nfc().to_string(); let normalized = lit.nfc().to_string();
if normalized.chars().count() == 1 { if normalized.chars().count() == 1 {
let ch = normalized.chars().next().unwrap().escape_default().to_string(); let ch = normalized.chars().next().unwrap().escape_default().to_string();
sugg = Some(MoreThanOneCharSugg::NormalizedForm { span, ch, normalized }); sugg = Some(MoreThanOneCharSugg::NormalizedForm {
span: err_span,
ch,
normalized,
});
} }
let escaped_marks = let escaped_marks =
rest.iter().map(|c| c.escape_default().to_string()).collect::<Vec<_>>(); rest.iter().map(|c| c.escape_default().to_string()).collect::<Vec<_>>();
note = Some(MoreThanOneCharNote::AllCombining { note = Some(MoreThanOneCharNote::AllCombining {
span, span: err_span,
chr: format!("{first}"), chr: format!("{first}"),
len: escaped_marks.len(), len: escaped_marks.len(),
escaped_marks: escaped_marks.join(""), escaped_marks: escaped_marks.join(""),
@ -69,10 +75,12 @@ pub(crate) fn emit_unescape_error(
.collect(); .collect();
if let &[ch] = printable.as_slice() { if let &[ch] = printable.as_slice() {
sugg = sugg = Some(MoreThanOneCharSugg::RemoveNonPrinting {
Some(MoreThanOneCharSugg::RemoveNonPrinting { span, ch: ch.to_string() }); span: err_span,
ch: ch.to_string(),
});
note = Some(MoreThanOneCharNote::NonPrinting { note = Some(MoreThanOneCharNote::NonPrinting {
span, span: err_span,
escaped: lit.escape_default().to_string(), escaped: lit.escape_default().to_string(),
}); });
} }
@ -91,13 +99,13 @@ pub(crate) fn emit_unescape_error(
} }
let sugg = format!("{prefix}\"{escaped}\""); let sugg = format!("{prefix}\"{escaped}\"");
MoreThanOneCharSugg::Quotes { MoreThanOneCharSugg::Quotes {
span: span_with_quotes, span: full_lit_span,
is_byte: mode == Mode::Byte, is_byte: mode == Mode::Byte,
sugg, sugg,
} }
}); });
handler.emit_err(UnescapeError::MoreThanOneChar { handler.emit_err(UnescapeError::MoreThanOneChar {
span: span_with_quotes, span: full_lit_span,
note, note,
suggestion: sugg, suggestion: sugg,
}); });
@ -105,7 +113,7 @@ pub(crate) fn emit_unescape_error(
EscapeError::EscapeOnlyChar => { EscapeError::EscapeOnlyChar => {
let (c, char_span) = last_char(); let (c, char_span) = last_char();
handler.emit_err(UnescapeError::EscapeOnlyChar { handler.emit_err(UnescapeError::EscapeOnlyChar {
span, span: err_span,
char_span, char_span,
escaped_sugg: c.escape_default().to_string(), escaped_sugg: c.escape_default().to_string(),
escaped_msg: escaped_char(c), escaped_msg: escaped_char(c),
@ -114,11 +122,11 @@ pub(crate) fn emit_unescape_error(
} }
EscapeError::BareCarriageReturn => { EscapeError::BareCarriageReturn => {
let double_quotes = mode.in_double_quotes(); let double_quotes = mode.in_double_quotes();
handler.emit_err(UnescapeError::BareCr { span, double_quotes }); handler.emit_err(UnescapeError::BareCr { span: err_span, double_quotes });
} }
EscapeError::BareCarriageReturnInRawString => { EscapeError::BareCarriageReturnInRawString => {
assert!(mode.in_double_quotes()); assert!(mode.in_double_quotes());
handler.emit_err(UnescapeError::BareCrRawString(span)); handler.emit_err(UnescapeError::BareCrRawString(err_span));
} }
EscapeError::InvalidEscape => { EscapeError::InvalidEscape => {
let (c, span) = last_char(); let (c, span) = last_char();
@ -143,7 +151,7 @@ pub(crate) fn emit_unescape_error(
} else { } else {
if mode == Mode::Str || mode == Mode::Char { if mode == Mode::Str || mode == Mode::Char {
diag.span_suggestion( diag.span_suggestion(
span_with_quotes, full_lit_span,
"if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal", "if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal",
format!("r\"{lit}\""), format!("r\"{lit}\""),
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
@ -158,7 +166,7 @@ pub(crate) fn emit_unescape_error(
diag.emit(); diag.emit();
} }
EscapeError::TooShortHexEscape => { EscapeError::TooShortHexEscape => {
handler.emit_err(UnescapeError::TooShortHexEscape(span)); handler.emit_err(UnescapeError::TooShortHexEscape(err_span));
} }
EscapeError::InvalidCharInHexEscape | EscapeError::InvalidCharInUnicodeEscape => { EscapeError::InvalidCharInHexEscape | EscapeError::InvalidCharInUnicodeEscape => {
let (c, span) = last_char(); let (c, span) = last_char();
@ -210,7 +218,7 @@ pub(crate) fn emit_unescape_error(
err.emit(); err.emit();
} }
EscapeError::OutOfRangeHexEscape => { EscapeError::OutOfRangeHexEscape => {
handler.emit_err(UnescapeError::OutOfRangeHexEscape(span)); handler.emit_err(UnescapeError::OutOfRangeHexEscape(err_span));
} }
EscapeError::LeadingUnderscoreUnicodeEscape => { EscapeError::LeadingUnderscoreUnicodeEscape => {
let (c, span) = last_char(); let (c, span) = last_char();
@ -220,10 +228,11 @@ pub(crate) fn emit_unescape_error(
}); });
} }
EscapeError::OverlongUnicodeEscape => { EscapeError::OverlongUnicodeEscape => {
handler.emit_err(UnescapeError::OverlongUnicodeEscape(span)); handler.emit_err(UnescapeError::OverlongUnicodeEscape(err_span));
} }
EscapeError::UnclosedUnicodeEscape => { EscapeError::UnclosedUnicodeEscape => {
handler.emit_err(UnescapeError::UnclosedUnicodeEscape(span, span.shrink_to_hi())); handler
.emit_err(UnescapeError::UnclosedUnicodeEscape(err_span, err_span.shrink_to_hi()));
} }
EscapeError::NoBraceInUnicodeEscape => { EscapeError::NoBraceInUnicodeEscape => {
let mut suggestion = "\\u{".to_owned(); let mut suggestion = "\\u{".to_owned();
@ -238,34 +247,34 @@ pub(crate) fn emit_unescape_error(
let (label, sub) = if suggestion_len > 0 { let (label, sub) = if suggestion_len > 0 {
suggestion.push('}'); suggestion.push('}');
let hi = char_span.lo() + BytePos(suggestion_len as u32); let hi = char_span.lo() + BytePos(suggestion_len as u32);
(None, NoBraceUnicodeSub::Suggestion { span: span.with_hi(hi), suggestion }) (None, NoBraceUnicodeSub::Suggestion { span: err_span.with_hi(hi), suggestion })
} else { } else {
(Some(span), NoBraceUnicodeSub::Help) (Some(err_span), NoBraceUnicodeSub::Help)
}; };
handler.emit_err(UnescapeError::NoBraceInUnicodeEscape { span, label, sub }); handler.emit_err(UnescapeError::NoBraceInUnicodeEscape { span: err_span, label, sub });
} }
EscapeError::UnicodeEscapeInByte => { EscapeError::UnicodeEscapeInByte => {
handler.emit_err(UnescapeError::UnicodeEscapeInByte(span)); handler.emit_err(UnescapeError::UnicodeEscapeInByte(err_span));
} }
EscapeError::EmptyUnicodeEscape => { EscapeError::EmptyUnicodeEscape => {
handler.emit_err(UnescapeError::EmptyUnicodeEscape(span)); handler.emit_err(UnescapeError::EmptyUnicodeEscape(err_span));
} }
EscapeError::ZeroChars => { EscapeError::ZeroChars => {
handler.emit_err(UnescapeError::ZeroChars(span)); handler.emit_err(UnescapeError::ZeroChars(err_span));
} }
EscapeError::LoneSlash => { EscapeError::LoneSlash => {
handler.emit_err(UnescapeError::LoneSlash(span)); handler.emit_err(UnescapeError::LoneSlash(err_span));
} }
EscapeError::UnskippedWhitespaceWarning => { EscapeError::UnskippedWhitespaceWarning => {
let (c, char_span) = last_char(); let (c, char_span) = last_char();
handler.emit_warning(UnescapeError::UnskippedWhitespace { handler.emit_warning(UnescapeError::UnskippedWhitespace {
span, span: err_span,
ch: escaped_char(c), ch: escaped_char(c),
char_span, char_span,
}); });
} }
EscapeError::MultipleSkippedLinesWarning => { EscapeError::MultipleSkippedLinesWarning => {
handler.emit_warning(UnescapeError::MultipleSkippedLinesWarning(span)); handler.emit_warning(UnescapeError::MultipleSkippedLinesWarning(err_span));
} }
} }
} }