about summary refs log tree commit diff
path: root/src/libsyntax/parse/lexer
diff options
context:
space:
mode:
authorSimonas Kazlauskas <git@kazlauskas.me>2015-07-10 21:37:21 +0300
committerSimonas Kazlauskas <git@kazlauskas.me>2015-07-10 22:26:19 +0300
commit0bd5dd6449c9db734bd2d1700ea4b50e22b220be (patch)
tree9d4df601110df7da605e21db7ccbabcaf4fc6135 /src/libsyntax/parse/lexer
parentfd8e175c4e39537b16beb40c704a17fcf9796852 (diff)
downloadrust-0bd5dd6449c9db734bd2d1700ea4b50e22b220be.tar.gz
rust-0bd5dd6449c9db734bd2d1700ea4b50e22b220be.zip
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.
Diffstat (limited to 'src/libsyntax/parse/lexer')
-rw-r--r--src/libsyntax/parse/lexer/mod.rs24
1 files changed, 19 insertions, 5 deletions
diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs
index 507bd9de2a1..b5085b5c44c 100644
--- a/src/libsyntax/parse/lexer/mod.rs
+++ b/src/libsyntax/parse/lexer/mod.rs
@@ -172,6 +172,11 @@ impl<'a> StringReader<'a> {
         self.span_diagnostic.span_err(sp, m)
     }
 
+    /// Suggest some help with a given span.
+    pub fn help_span(&self, sp: Span, m: &str) {
+        self.span_diagnostic.span_help(sp, m)
+    }
+
     /// Report a fatal error spanning [`from_pos`, `to_pos`).
     fn fatal_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) -> ! {
         self.fatal_span(codemap::mk_sp(from_pos, to_pos), m)
@@ -182,6 +187,11 @@ impl<'a> StringReader<'a> {
         self.err_span(codemap::mk_sp(from_pos, to_pos), m)
     }
 
+    /// Suggest some help spanning [`from_pos`, `to_pos`).
+    fn help_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) {
+        self.help_span(codemap::mk_sp(from_pos, to_pos), m)
+    }
+
     /// Report a lexical error spanning [`from_pos`, `to_pos`), appending an
     /// escaped character to the error message
     fn fatal_span_char(&self, from_pos: BytePos, to_pos: BytePos, m: &str, c: char) -> ! {
@@ -742,6 +752,13 @@ impl<'a> StringReader<'a> {
                                    valid
                                 }
                             }
+                            'u' if !ascii_only => {
+                                self.err_span_(escaped_pos, self.last_pos,
+                                    "incomplete unicode escape sequence");
+                                self.help_span_(escaped_pos, self.last_pos,
+                                    "format of unicode escape sequences is `\\u{…}`");
+                                false
+                            }
                             '\n' if delim == '"' => {
                                 self.consume_whitespace();
                                 true
@@ -757,16 +774,13 @@ impl<'a> StringReader<'a> {
                                     if ascii_only { "unknown byte escape" }
                                     else { "unknown character escape" },
                                     c);
-                                let sp = codemap::mk_sp(escaped_pos, last_pos);
                                 if e == '\r' {
-                                    self.span_diagnostic.span_help(
-                                        sp,
+                                    self.help_span_(escaped_pos, last_pos,
                                         "this is an isolated carriage return; consider checking \
                                          your editor and version control settings")
                                 }
                                 if (e == '{' || e == '}') && !ascii_only {
-                                    self.span_diagnostic.span_help(
-                                        sp,
+                                    self.help_span_(escaped_pos, last_pos,
                                         "if used in a formatting string, \
                                         curly braces are escaped with `{{` and `}}`")
                                 }