Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b416f13

Browse files
committedDec 31, 2018
Use structured suggestion for braceless unicode escape squence
1 parent 7edc434 commit b416f13

File tree

5 files changed

+47
-16
lines changed

5 files changed

+47
-16
lines changed
 

‎src/libsyntax/parse/lexer/mod.rs

+30-6
Original file line numberDiff line numberDiff line change
@@ -945,12 +945,36 @@ impl<'a> StringReader<'a> {
945945
self.scan_unicode_escape(delim) && !ascii_only
946946
} else {
947947
let span = self.mk_sp(start, self.pos);
948-
self.sess.span_diagnostic
949-
.struct_span_err(span, "incorrect unicode escape sequence")
950-
.span_help(span,
951-
"format of unicode escape sequences is \
952-
`\\u{…}`")
953-
.emit();
948+
let mut suggestion = "\\u{".to_owned();
949+
let mut err = self.sess.span_diagnostic.struct_span_err(
950+
span,
951+
"incorrect unicode escape sequence",
952+
);
953+
let mut i = 0;
954+
while let (Some(ch), true) = (self.ch, i < 6) {
955+
if ch.is_digit(16) {
956+
suggestion.push(ch);
957+
self.bump();
958+
i += 1;
959+
} else {
960+
break;
961+
}
962+
}
963+
if i != 0 {
964+
suggestion.push('}');
965+
err.span_suggestion_with_applicability(
966+
self.mk_sp(start, self.pos),
967+
"format of unicode escape sequences uses braces",
968+
suggestion,
969+
Applicability::MaybeIncorrect,
970+
);
971+
} else {
972+
err.span_help(
973+
span,
974+
"format of unicode escape sequences is `\\u{…}`",
975+
);
976+
}
977+
err.emit();
954978
false
955979
};
956980
if ascii_only {

‎src/libsyntax_ext/format.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,8 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
845845
}
846846
} else if next_c.is_digit(16) {
847847
skips.push(next_pos);
848-
// We suggest adding `{` and `}` when appropriate, accept it here as if it
849-
// were correct
848+
// We suggest adding `{` and `}` when appropriate, accept it here as if
849+
// it were correct
850850
let mut i = 0; // consume up to 6 hexanumeric chars
851851
while let (Some((next_pos, c)), _) = (s.next(), i < 6) {
852852
if c.is_digit(16) {

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ 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 is `/u{…}`
8-
--> $DIR/format-string-error-2.rs:77:20
9-
|
10-
LL | println!("/x7B}/u8 {", 1);
11-
| ^^
5+
| ^^-
6+
| |
7+
| help: format of unicode escape sequences uses braces: `/u{8}`
128

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

‎src/test/ui/parser/issue-23620-invalid-escapes.rs

+3
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@ fn main() {
3535
//~^ ERROR invalid character in numeric character escape:
3636
//~^^ ERROR form of character escape may only be used with characters in the range [\x00-\x7f]
3737
//~^^^ ERROR incorrect unicode escape sequence
38+
39+
let _ = "\u8f";
40+
//~^ ERROR incorrect unicode escape sequence
3841
}

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,13 @@ help: format of unicode escape sequences is `/u{…}`
118118
LL | let _ = "/xf /u";
119119
| ^^
120120

121-
error: aborting due to 17 previous errors
121+
error: incorrect unicode escape sequence
122+
--> $DIR/issue-23620-invalid-escapes.rs:39:14
123+
|
124+
LL | let _ = "/u8f";
125+
| ^^--
126+
| |
127+
| help: format of unicode escape sequences uses braces: `/u{8f}`
128+
129+
error: aborting due to 18 previous errors
122130

0 commit comments

Comments
 (0)
Please sign in to comment.