@@ -11,32 +11,34 @@ use crate::errors::{MoreThanOneCharNote, MoreThanOneCharSugg, NoBraceUnicodeSub,
1111
1212pub ( crate ) fn emit_unescape_error (
1313 handler : & Handler ,
14- // interior part of the literal, without quotes
14+ // interior part of the literal, between quotes
1515 lit : & str ,
16- // full span of the literal, including quotes
17- span_with_quotes : Span ,
18- // interior span of the literal, without quotes
19- span : Span ,
16+ // full span of the literal, including quotes and any prefix
17+ full_lit_span : Span ,
18+ // span of the error part of the literal
19+ err_span : Span ,
2020 mode : Mode ,
2121 // range of the error inside `lit`
2222 range : Range < usize > ,
2323 error : EscapeError ,
2424) {
2525 debug ! (
2626 "emit_unescape_error: {:?}, {:?}, {:?}, {:?}, {:?}" ,
27- lit, span_with_quotes , mode, range, error
27+ lit, full_lit_span , mode, range, error
2828 ) ;
2929 let last_char = || {
3030 let c = lit[ range. clone ( ) ] . chars ( ) . next_back ( ) . unwrap ( ) ;
31- let span = span . with_lo ( span . hi ( ) - BytePos ( c. len_utf8 ( ) as u32 ) ) ;
31+ let span = err_span . with_lo ( err_span . hi ( ) - BytePos ( c. len_utf8 ( ) as u32 ) ) ;
3232 ( c, span)
3333 } ;
3434 match error {
3535 EscapeError :: LoneSurrogateUnicodeEscape => {
36- handler. emit_err ( UnescapeError :: InvalidUnicodeEscape { span, surrogate : true } ) ;
36+ handler
37+ . emit_err ( UnescapeError :: InvalidUnicodeEscape { span : err_span, surrogate : true } ) ;
3738 }
3839 EscapeError :: OutOfRangeUnicodeEscape => {
39- handler. emit_err ( UnescapeError :: InvalidUnicodeEscape { span, surrogate : false } ) ;
40+ handler
41+ . emit_err ( UnescapeError :: InvalidUnicodeEscape { span : err_span, surrogate : false } ) ;
4042 }
4143 EscapeError :: MoreThanOneChar => {
4244 use unicode_normalization:: { char:: is_combining_mark, UnicodeNormalization } ;
@@ -49,12 +51,16 @@ pub(crate) fn emit_unescape_error(
4951 let normalized = lit. nfc ( ) . to_string ( ) ;
5052 if normalized. chars ( ) . count ( ) == 1 {
5153 let ch = normalized. chars ( ) . next ( ) . unwrap ( ) . escape_default ( ) . to_string ( ) ;
52- sugg = Some ( MoreThanOneCharSugg :: NormalizedForm { span, ch, normalized } ) ;
54+ sugg = Some ( MoreThanOneCharSugg :: NormalizedForm {
55+ span : err_span,
56+ ch,
57+ normalized,
58+ } ) ;
5359 }
5460 let escaped_marks =
5561 rest. iter ( ) . map ( |c| c. escape_default ( ) . to_string ( ) ) . collect :: < Vec < _ > > ( ) ;
5662 note = Some ( MoreThanOneCharNote :: AllCombining {
57- span,
63+ span : err_span ,
5864 chr : format ! ( "{first}" ) ,
5965 len : escaped_marks. len ( ) ,
6066 escaped_marks : escaped_marks. join ( "" ) ,
@@ -69,10 +75,12 @@ pub(crate) fn emit_unescape_error(
6975 . collect ( ) ;
7076
7177 if let & [ ch] = printable. as_slice ( ) {
72- sugg =
73- Some ( MoreThanOneCharSugg :: RemoveNonPrinting { span, ch : ch. to_string ( ) } ) ;
78+ sugg = Some ( MoreThanOneCharSugg :: RemoveNonPrinting {
79+ span : err_span,
80+ ch : ch. to_string ( ) ,
81+ } ) ;
7482 note = Some ( MoreThanOneCharNote :: NonPrinting {
75- span,
83+ span : err_span ,
7684 escaped : lit. escape_default ( ) . to_string ( ) ,
7785 } ) ;
7886 }
@@ -91,21 +99,21 @@ pub(crate) fn emit_unescape_error(
9199 }
92100 let sugg = format ! ( "{prefix}\" {escaped}\" " ) ;
93101 MoreThanOneCharSugg :: Quotes {
94- span : span_with_quotes ,
102+ span : full_lit_span ,
95103 is_byte : mode == Mode :: Byte ,
96104 sugg,
97105 }
98106 } ) ;
99107 handler. emit_err ( UnescapeError :: MoreThanOneChar {
100- span : span_with_quotes ,
108+ span : full_lit_span ,
101109 note,
102110 suggestion : sugg,
103111 } ) ;
104112 }
105113 EscapeError :: EscapeOnlyChar => {
106114 let ( c, char_span) = last_char ( ) ;
107115 handler. emit_err ( UnescapeError :: EscapeOnlyChar {
108- span,
116+ span : err_span ,
109117 char_span,
110118 escaped_sugg : c. escape_default ( ) . to_string ( ) ,
111119 escaped_msg : escaped_char ( c) ,
@@ -114,11 +122,11 @@ pub(crate) fn emit_unescape_error(
114122 }
115123 EscapeError :: BareCarriageReturn => {
116124 let double_quotes = mode. in_double_quotes ( ) ;
117- handler. emit_err ( UnescapeError :: BareCr { span, double_quotes } ) ;
125+ handler. emit_err ( UnescapeError :: BareCr { span : err_span , double_quotes } ) ;
118126 }
119127 EscapeError :: BareCarriageReturnInRawString => {
120128 assert ! ( mode. in_double_quotes( ) ) ;
121- handler. emit_err ( UnescapeError :: BareCrRawString ( span ) ) ;
129+ handler. emit_err ( UnescapeError :: BareCrRawString ( err_span ) ) ;
122130 }
123131 EscapeError :: InvalidEscape => {
124132 let ( c, span) = last_char ( ) ;
@@ -143,7 +151,7 @@ pub(crate) fn emit_unescape_error(
143151 } else {
144152 if mode == Mode :: Str || mode == Mode :: Char {
145153 diag. span_suggestion (
146- span_with_quotes ,
154+ full_lit_span ,
147155 "if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal" ,
148156 format ! ( "r\" {lit}\" " ) ,
149157 Applicability :: MaybeIncorrect ,
@@ -158,7 +166,7 @@ pub(crate) fn emit_unescape_error(
158166 diag. emit ( ) ;
159167 }
160168 EscapeError :: TooShortHexEscape => {
161- handler. emit_err ( UnescapeError :: TooShortHexEscape ( span ) ) ;
169+ handler. emit_err ( UnescapeError :: TooShortHexEscape ( err_span ) ) ;
162170 }
163171 EscapeError :: InvalidCharInHexEscape | EscapeError :: InvalidCharInUnicodeEscape => {
164172 let ( c, span) = last_char ( ) ;
@@ -210,7 +218,7 @@ pub(crate) fn emit_unescape_error(
210218 err. emit ( ) ;
211219 }
212220 EscapeError :: OutOfRangeHexEscape => {
213- handler. emit_err ( UnescapeError :: OutOfRangeHexEscape ( span ) ) ;
221+ handler. emit_err ( UnescapeError :: OutOfRangeHexEscape ( err_span ) ) ;
214222 }
215223 EscapeError :: LeadingUnderscoreUnicodeEscape => {
216224 let ( c, span) = last_char ( ) ;
@@ -220,10 +228,11 @@ pub(crate) fn emit_unescape_error(
220228 } ) ;
221229 }
222230 EscapeError :: OverlongUnicodeEscape => {
223- handler. emit_err ( UnescapeError :: OverlongUnicodeEscape ( span ) ) ;
231+ handler. emit_err ( UnescapeError :: OverlongUnicodeEscape ( err_span ) ) ;
224232 }
225233 EscapeError :: UnclosedUnicodeEscape => {
226- handler. emit_err ( UnescapeError :: UnclosedUnicodeEscape ( span, span. shrink_to_hi ( ) ) ) ;
234+ handler
235+ . emit_err ( UnescapeError :: UnclosedUnicodeEscape ( err_span, err_span. shrink_to_hi ( ) ) ) ;
227236 }
228237 EscapeError :: NoBraceInUnicodeEscape => {
229238 let mut suggestion = "\\ u{" . to_owned ( ) ;
@@ -238,34 +247,34 @@ pub(crate) fn emit_unescape_error(
238247 let ( label, sub) = if suggestion_len > 0 {
239248 suggestion. push ( '}' ) ;
240249 let hi = char_span. lo ( ) + BytePos ( suggestion_len as u32 ) ;
241- ( None , NoBraceUnicodeSub :: Suggestion { span : span . with_hi ( hi) , suggestion } )
250+ ( None , NoBraceUnicodeSub :: Suggestion { span : err_span . with_hi ( hi) , suggestion } )
242251 } else {
243- ( Some ( span ) , NoBraceUnicodeSub :: Help )
252+ ( Some ( err_span ) , NoBraceUnicodeSub :: Help )
244253 } ;
245- handler. emit_err ( UnescapeError :: NoBraceInUnicodeEscape { span, label, sub } ) ;
254+ handler. emit_err ( UnescapeError :: NoBraceInUnicodeEscape { span : err_span , label, sub } ) ;
246255 }
247256 EscapeError :: UnicodeEscapeInByte => {
248- handler. emit_err ( UnescapeError :: UnicodeEscapeInByte ( span ) ) ;
257+ handler. emit_err ( UnescapeError :: UnicodeEscapeInByte ( err_span ) ) ;
249258 }
250259 EscapeError :: EmptyUnicodeEscape => {
251- handler. emit_err ( UnescapeError :: EmptyUnicodeEscape ( span ) ) ;
260+ handler. emit_err ( UnescapeError :: EmptyUnicodeEscape ( err_span ) ) ;
252261 }
253262 EscapeError :: ZeroChars => {
254- handler. emit_err ( UnescapeError :: ZeroChars ( span ) ) ;
263+ handler. emit_err ( UnescapeError :: ZeroChars ( err_span ) ) ;
255264 }
256265 EscapeError :: LoneSlash => {
257- handler. emit_err ( UnescapeError :: LoneSlash ( span ) ) ;
266+ handler. emit_err ( UnescapeError :: LoneSlash ( err_span ) ) ;
258267 }
259268 EscapeError :: UnskippedWhitespaceWarning => {
260269 let ( c, char_span) = last_char ( ) ;
261270 handler. emit_warning ( UnescapeError :: UnskippedWhitespace {
262- span,
271+ span : err_span ,
263272 ch : escaped_char ( c) ,
264273 char_span,
265274 } ) ;
266275 }
267276 EscapeError :: MultipleSkippedLinesWarning => {
268- handler. emit_warning ( UnescapeError :: MultipleSkippedLinesWarning ( span ) ) ;
277+ handler. emit_warning ( UnescapeError :: MultipleSkippedLinesWarning ( err_span ) ) ;
269278 }
270279 }
271280}
0 commit comments