about summary refs log tree commit diff
path: root/src/libsyntax/parse/parser.rs
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2017-11-29 13:28:47 -0800
committerEsteban Küber <esteban@kuber.com.ar>2017-11-29 13:28:47 -0800
commitfe897409579177d1ffd964b6d5a8de0024e5df68 (patch)
tree1422bb80ad3e65a3542d5386c5a0bc979d7df874 /src/libsyntax/parse/parser.rs
parentea51b19dc7587bac0011282506ad4678cca9c1eb (diff)
downloadrust-fe897409579177d1ffd964b6d5a8de0024e5df68.tar.gz
rust-fe897409579177d1ffd964b6d5a8de0024e5df68.zip
Point to next token when it is in the expected line
Diffstat (limited to 'src/libsyntax/parse/parser.rs')
-rw-r--r--src/libsyntax/parse/parser.rs27
1 files changed, 22 insertions, 5 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 0b03429ea2e..4e79407caea 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -660,11 +660,28 @@ impl<'a> Parser<'a> {
             } else {
                 label_sp
             };
-            if self.span.contains(sp) {
-                err.span_label(self.span, label_exp);
-            } else {
-                err.span_label(sp, label_exp);
-                err.span_label(self.span, "unexpected token");
+
+            let cm = self.sess.codemap();
+            match (cm.lookup_line(self.span.lo()), cm.lookup_line(sp.lo())) {
+                (Ok(ref a), Ok(ref b)) if a.line == b.line => {
+                    // When the spans are in the same line, it means that the only content between
+                    // them is whitespace, point at the found token in that case:
+                    //
+                    // X |     () => { syntax error };
+                    //   |                    ^^^^^ expected one of 8 possible tokens here
+                    //
+                    // instead of having:
+                    //
+                    // X |     () => { syntax error };
+                    //   |                   -^^^^^ unexpected token
+                    //   |                   |
+                    //   |                   expected one of 8 possible tokens here
+                    err.span_label(self.span, label_exp);
+                }
+                _ => {
+                    err.span_label(sp, label_exp);
+                    err.span_label(self.span, "unexpected token");
+                }
             }
             Err(err)
         }