Skip to content

Commit 7af4959

Browse files
Rollup merge of #77587 - ehuss:unicode-escape-span, r=ecstatic-morse
Fix span for unicode escape suggestion. If a unicode escape is missing the curly braces, the suggested fix is to add the curly braces, but the span for the fix was incorrect. It was not covering the `\u`, but the suggested text includes the `\u`, causing the resulting fix to be `"\u\u{1234}"`. This changes it so that the span includes the `\u`. An alternate fix would be to remove `\u` from the suggested fix, but I think the error message reads better if the entire escape is included.
2 parents 6fbda95 + 35192ff commit 7af4959

File tree

3 files changed

+6
-9
lines changed

3 files changed

+6
-9
lines changed

compiler/rustc_parse/src/lexer/unescape_error_reporting.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,9 @@ pub(crate) fn emit_unescape_error(
181181

182182
if suggestion_len > 0 {
183183
suggestion.push('}');
184-
let lo = char_span.lo();
185-
let hi = lo + BytePos(suggestion_len as u32);
184+
let hi = char_span.lo() + BytePos(suggestion_len as u32);
186185
diag.span_suggestion(
187-
span.with_lo(lo).with_hi(hi),
186+
span.with_hi(hi),
188187
"format of unicode escape sequences uses braces",
189188
suggestion,
190189
Applicability::MaybeIncorrect,

src/test/ui/fmt/format-string-error-2.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ error: incorrect unicode escape sequence
22
--> $DIR/format-string-error-2.rs:77:20
33
|
44
LL | println!("\x7B}\u8 {", 1);
5-
| ^^-
6-
| |
7-
| help: format of unicode escape sequences uses braces: `\u{8}`
5+
| ^^^ help: format of unicode escape sequences uses braces: `\u{8}`
86

97
error: invalid format string: expected `'}'`, found `'a'`
108
--> $DIR/format-string-error-2.rs:5:5

src/test/ui/parser/issue-23620-invalid-escapes.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ error: incorrect unicode escape sequence
8080
--> $DIR/issue-23620-invalid-escapes.rs:32:14
8181
|
8282
LL | let _ = "\u8f";
83-
| ^^--
84-
| |
85-
| help: format of unicode escape sequences uses braces: `\u{8f}`
83+
| ^^^-
84+
| |
85+
| help: format of unicode escape sequences uses braces: `\u{8f}`
8686

8787
error: aborting due to 13 previous errors
8888

0 commit comments

Comments
 (0)