@@ -11,32 +11,34 @@ use crate::errors::{MoreThanOneCharNote, MoreThanOneCharSugg, NoBraceUnicodeSub,
11
11
12
12
pub ( crate ) fn emit_unescape_error (
13
13
handler : & Handler ,
14
- // interior part of the literal, without quotes
14
+ // interior part of the literal, between quotes
15
15
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 ,
20
20
mode : Mode ,
21
21
// range of the error inside `lit`
22
22
range : Range < usize > ,
23
23
error : EscapeError ,
24
24
) {
25
25
debug ! (
26
26
"emit_unescape_error: {:?}, {:?}, {:?}, {:?}, {:?}" ,
27
- lit, span_with_quotes , mode, range, error
27
+ lit, full_lit_span , mode, range, error
28
28
) ;
29
29
let last_char = || {
30
30
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 ) ) ;
32
32
( c, span)
33
33
} ;
34
34
match error {
35
35
EscapeError :: LoneSurrogateUnicodeEscape => {
36
- handler. emit_err ( UnescapeError :: InvalidUnicodeEscape { span, surrogate : true } ) ;
36
+ handler
37
+ . emit_err ( UnescapeError :: InvalidUnicodeEscape { span : err_span, surrogate : true } ) ;
37
38
}
38
39
EscapeError :: OutOfRangeUnicodeEscape => {
39
- handler. emit_err ( UnescapeError :: InvalidUnicodeEscape { span, surrogate : false } ) ;
40
+ handler
41
+ . emit_err ( UnescapeError :: InvalidUnicodeEscape { span : err_span, surrogate : false } ) ;
40
42
}
41
43
EscapeError :: MoreThanOneChar => {
42
44
use unicode_normalization:: { char:: is_combining_mark, UnicodeNormalization } ;
@@ -49,12 +51,16 @@ pub(crate) fn emit_unescape_error(
49
51
let normalized = lit. nfc ( ) . to_string ( ) ;
50
52
if normalized. chars ( ) . count ( ) == 1 {
51
53
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
+ } ) ;
53
59
}
54
60
let escaped_marks =
55
61
rest. iter ( ) . map ( |c| c. escape_default ( ) . to_string ( ) ) . collect :: < Vec < _ > > ( ) ;
56
62
note = Some ( MoreThanOneCharNote :: AllCombining {
57
- span,
63
+ span : err_span ,
58
64
chr : format ! ( "{first}" ) ,
59
65
len : escaped_marks. len ( ) ,
60
66
escaped_marks : escaped_marks. join ( "" ) ,
@@ -69,10 +75,12 @@ pub(crate) fn emit_unescape_error(
69
75
. collect ( ) ;
70
76
71
77
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
+ } ) ;
74
82
note = Some ( MoreThanOneCharNote :: NonPrinting {
75
- span,
83
+ span : err_span ,
76
84
escaped : lit. escape_default ( ) . to_string ( ) ,
77
85
} ) ;
78
86
}
@@ -91,21 +99,21 @@ pub(crate) fn emit_unescape_error(
91
99
}
92
100
let sugg = format ! ( "{prefix}\" {escaped}\" " ) ;
93
101
MoreThanOneCharSugg :: Quotes {
94
- span : span_with_quotes ,
102
+ span : full_lit_span ,
95
103
is_byte : mode == Mode :: Byte ,
96
104
sugg,
97
105
}
98
106
} ) ;
99
107
handler. emit_err ( UnescapeError :: MoreThanOneChar {
100
- span : span_with_quotes ,
108
+ span : full_lit_span ,
101
109
note,
102
110
suggestion : sugg,
103
111
} ) ;
104
112
}
105
113
EscapeError :: EscapeOnlyChar => {
106
114
let ( c, char_span) = last_char ( ) ;
107
115
handler. emit_err ( UnescapeError :: EscapeOnlyChar {
108
- span,
116
+ span : err_span ,
109
117
char_span,
110
118
escaped_sugg : c. escape_default ( ) . to_string ( ) ,
111
119
escaped_msg : escaped_char ( c) ,
@@ -114,11 +122,11 @@ pub(crate) fn emit_unescape_error(
114
122
}
115
123
EscapeError :: BareCarriageReturn => {
116
124
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 } ) ;
118
126
}
119
127
EscapeError :: BareCarriageReturnInRawString => {
120
128
assert ! ( mode. in_double_quotes( ) ) ;
121
- handler. emit_err ( UnescapeError :: BareCrRawString ( span ) ) ;
129
+ handler. emit_err ( UnescapeError :: BareCrRawString ( err_span ) ) ;
122
130
}
123
131
EscapeError :: InvalidEscape => {
124
132
let ( c, span) = last_char ( ) ;
@@ -143,7 +151,7 @@ pub(crate) fn emit_unescape_error(
143
151
} else {
144
152
if mode == Mode :: Str || mode == Mode :: Char {
145
153
diag. span_suggestion (
146
- span_with_quotes ,
154
+ full_lit_span ,
147
155
"if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal" ,
148
156
format ! ( "r\" {lit}\" " ) ,
149
157
Applicability :: MaybeIncorrect ,
@@ -158,7 +166,7 @@ pub(crate) fn emit_unescape_error(
158
166
diag. emit ( ) ;
159
167
}
160
168
EscapeError :: TooShortHexEscape => {
161
- handler. emit_err ( UnescapeError :: TooShortHexEscape ( span ) ) ;
169
+ handler. emit_err ( UnescapeError :: TooShortHexEscape ( err_span ) ) ;
162
170
}
163
171
EscapeError :: InvalidCharInHexEscape | EscapeError :: InvalidCharInUnicodeEscape => {
164
172
let ( c, span) = last_char ( ) ;
@@ -210,7 +218,7 @@ pub(crate) fn emit_unescape_error(
210
218
err. emit ( ) ;
211
219
}
212
220
EscapeError :: OutOfRangeHexEscape => {
213
- handler. emit_err ( UnescapeError :: OutOfRangeHexEscape ( span ) ) ;
221
+ handler. emit_err ( UnescapeError :: OutOfRangeHexEscape ( err_span ) ) ;
214
222
}
215
223
EscapeError :: LeadingUnderscoreUnicodeEscape => {
216
224
let ( c, span) = last_char ( ) ;
@@ -220,10 +228,11 @@ pub(crate) fn emit_unescape_error(
220
228
} ) ;
221
229
}
222
230
EscapeError :: OverlongUnicodeEscape => {
223
- handler. emit_err ( UnescapeError :: OverlongUnicodeEscape ( span ) ) ;
231
+ handler. emit_err ( UnescapeError :: OverlongUnicodeEscape ( err_span ) ) ;
224
232
}
225
233
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 ( ) ) ) ;
227
236
}
228
237
EscapeError :: NoBraceInUnicodeEscape => {
229
238
let mut suggestion = "\\ u{" . to_owned ( ) ;
@@ -238,34 +247,34 @@ pub(crate) fn emit_unescape_error(
238
247
let ( label, sub) = if suggestion_len > 0 {
239
248
suggestion. push ( '}' ) ;
240
249
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 } )
242
251
} else {
243
- ( Some ( span ) , NoBraceUnicodeSub :: Help )
252
+ ( Some ( err_span ) , NoBraceUnicodeSub :: Help )
244
253
} ;
245
- handler. emit_err ( UnescapeError :: NoBraceInUnicodeEscape { span, label, sub } ) ;
254
+ handler. emit_err ( UnescapeError :: NoBraceInUnicodeEscape { span : err_span , label, sub } ) ;
246
255
}
247
256
EscapeError :: UnicodeEscapeInByte => {
248
- handler. emit_err ( UnescapeError :: UnicodeEscapeInByte ( span ) ) ;
257
+ handler. emit_err ( UnescapeError :: UnicodeEscapeInByte ( err_span ) ) ;
249
258
}
250
259
EscapeError :: EmptyUnicodeEscape => {
251
- handler. emit_err ( UnescapeError :: EmptyUnicodeEscape ( span ) ) ;
260
+ handler. emit_err ( UnescapeError :: EmptyUnicodeEscape ( err_span ) ) ;
252
261
}
253
262
EscapeError :: ZeroChars => {
254
- handler. emit_err ( UnescapeError :: ZeroChars ( span ) ) ;
263
+ handler. emit_err ( UnescapeError :: ZeroChars ( err_span ) ) ;
255
264
}
256
265
EscapeError :: LoneSlash => {
257
- handler. emit_err ( UnescapeError :: LoneSlash ( span ) ) ;
266
+ handler. emit_err ( UnescapeError :: LoneSlash ( err_span ) ) ;
258
267
}
259
268
EscapeError :: UnskippedWhitespaceWarning => {
260
269
let ( c, char_span) = last_char ( ) ;
261
270
handler. emit_warning ( UnescapeError :: UnskippedWhitespace {
262
- span,
271
+ span : err_span ,
263
272
ch : escaped_char ( c) ,
264
273
char_span,
265
274
} ) ;
266
275
}
267
276
EscapeError :: MultipleSkippedLinesWarning => {
268
- handler. emit_warning ( UnescapeError :: MultipleSkippedLinesWarning ( span ) ) ;
277
+ handler. emit_warning ( UnescapeError :: MultipleSkippedLinesWarning ( err_span ) ) ;
269
278
}
270
279
}
271
280
}
0 commit comments