Skip to content

Commit 0bd5dd6

Browse files
committed
Improve incomplete unicode escape reporting
This improves diagnostic messages when \u escape is used incorrectly and { is missing. Instead of saying “unknown character escape: u”, it will now report that unicode escape sequence is incomplete and suggest what the correct syntax is.
1 parent fd8e175 commit 0bd5dd6

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/libsyntax/parse/lexer/mod.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ impl<'a> StringReader<'a> {
172172
self.span_diagnostic.span_err(sp, m)
173173
}
174174

175+
/// Suggest some help with a given span.
176+
pub fn help_span(&self, sp: Span, m: &str) {
177+
self.span_diagnostic.span_help(sp, m)
178+
}
179+
175180
/// Report a fatal error spanning [`from_pos`, `to_pos`).
176181
fn fatal_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) -> ! {
177182
self.fatal_span(codemap::mk_sp(from_pos, to_pos), m)
@@ -182,6 +187,11 @@ impl<'a> StringReader<'a> {
182187
self.err_span(codemap::mk_sp(from_pos, to_pos), m)
183188
}
184189

190+
/// Suggest some help spanning [`from_pos`, `to_pos`).
191+
fn help_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) {
192+
self.help_span(codemap::mk_sp(from_pos, to_pos), m)
193+
}
194+
185195
/// Report a lexical error spanning [`from_pos`, `to_pos`), appending an
186196
/// escaped character to the error message
187197
fn fatal_span_char(&self, from_pos: BytePos, to_pos: BytePos, m: &str, c: char) -> ! {
@@ -742,6 +752,13 @@ impl<'a> StringReader<'a> {
742752
valid
743753
}
744754
}
755+
'u' if !ascii_only => {
756+
self.err_span_(escaped_pos, self.last_pos,
757+
"incomplete unicode escape sequence");
758+
self.help_span_(escaped_pos, self.last_pos,
759+
"format of unicode escape sequences is `\\u{…}`");
760+
false
761+
}
745762
'\n' if delim == '"' => {
746763
self.consume_whitespace();
747764
true
@@ -757,16 +774,13 @@ impl<'a> StringReader<'a> {
757774
if ascii_only { "unknown byte escape" }
758775
else { "unknown character escape" },
759776
c);
760-
let sp = codemap::mk_sp(escaped_pos, last_pos);
761777
if e == '\r' {
762-
self.span_diagnostic.span_help(
763-
sp,
778+
self.help_span_(escaped_pos, last_pos,
764779
"this is an isolated carriage return; consider checking \
765780
your editor and version control settings")
766781
}
767782
if (e == '{' || e == '}') && !ascii_only {
768-
self.span_diagnostic.span_help(
769-
sp,
783+
self.help_span_(escaped_pos, last_pos,
770784
"if used in a formatting string, \
771785
curly braces are escaped with `{{` and `}}`")
772786
}

src/test/parse-fail/issue-23620-invalid-escapes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ fn main() {
4141
//~^ ERROR illegal unicode character escape
4242
//~^^ ERROR illegal character in numeric character escape:
4343
//~^^^ ERROR form of character escape may only be used with characters in the range [\x00-\x7f]
44-
//~^^^^ ERROR unknown character escape: u
44+
//~^^^^ ERROR incomplete unicode escape sequence
4545
}

0 commit comments

Comments
 (0)