about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-12-02 20:06:42 +0000
committerbors <bors@rust-lang.org>2017-12-02 20:06:42 +0000
commitf9b0897c5d42a4dfa425db799073c6feb21b0178 (patch)
treeb92f2c4d8290b9998018f15f984e73e6bf8416c9 /src
parent377decc352e58aa2bb0e2deb2eb66c3e7241a622 (diff)
parentfe897409579177d1ffd964b6d5a8de0024e5df68 (diff)
downloadrust-f9b0897c5d42a4dfa425db799073c6feb21b0178.tar.gz
rust-f9b0897c5d42a4dfa425db799073c6feb21b0178.zip
Auto merge of #46381 - estebank:expected-span, r=nikomatsakis
Point to next token when it is in the expected line

r? @nikomatsakis
Diffstat (limited to 'src')
-rw-r--r--src/libsyntax/codemap.rs2
-rw-r--r--src/libsyntax/parse/parser.rs27
-rw-r--r--src/test/ui/did_you_mean/issue-40006.stderr4
-rw-r--r--src/test/ui/macro_backtrace/main.stderr8
4 files changed, 26 insertions, 15 deletions
diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs
index 3aac5334a38..3906ed431ce 100644
--- a/src/libsyntax/codemap.rs
+++ b/src/libsyntax/codemap.rs
@@ -358,7 +358,7 @@ impl CodeMap {
     }
 
     // If the relevant filemap is empty, we don't return a line number.
-    fn lookup_line(&self, pos: BytePos) -> Result<FileMapAndLine, Rc<FileMap>> {
+    pub fn lookup_line(&self, pos: BytePos) -> Result<FileMapAndLine, Rc<FileMap>> {
         let idx = self.lookup_filemap_idx(pos);
 
         let files = self.files.borrow();
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index a02c6b764b7..2461e65585f 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)
         }
diff --git a/src/test/ui/did_you_mean/issue-40006.stderr b/src/test/ui/did_you_mean/issue-40006.stderr
index 8fadb4ff6b6..88d63cdbb5d 100644
--- a/src/test/ui/did_you_mean/issue-40006.stderr
+++ b/src/test/ui/did_you_mean/issue-40006.stderr
@@ -48,9 +48,7 @@ error: expected one of `!` or `::`, found `(`
   --> $DIR/issue-40006.rs:24:9
    |
 24 |     ::Y (); //~ ERROR expected one of
-   |        -^ unexpected token
-   |        |
-   |        expected one of `!` or `::` here
+   |         ^ expected one of `!` or `::` here
 
 error: missing `fn`, `type`, or `const` for impl-item declaration
   --> $DIR/issue-40006.rs:28:8
diff --git a/src/test/ui/macro_backtrace/main.stderr b/src/test/ui/macro_backtrace/main.stderr
index e8434a79f44..4b10e5e19a3 100644
--- a/src/test/ui/macro_backtrace/main.stderr
+++ b/src/test/ui/macro_backtrace/main.stderr
@@ -2,18 +2,14 @@ error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found
   --> $DIR/main.rs:19:20
    |
 19 |     () => { syntax error }; //~ ERROR expected one of
-   |                   -^^^^^ unexpected token
-   |                   |
-   |                   expected one of 8 possible tokens here
+   |                    ^^^^^ expected one of 8 possible tokens here
 $DIR/main.rs:24:5: 24:13 note: in this expansion of pong! (defined in $DIR/main.rs)
 
 error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `error`
   --> $DIR/main.rs:19:20
    |
 19 |     () => { syntax error }; //~ ERROR expected one of
-   |                   -^^^^^ unexpected token
-   |                   |
-   |                   expected one of 8 possible tokens here
+   |                    ^^^^^ expected one of 8 possible tokens here
 $DIR/main.rs:25:5: 25:13 note: in this expansion of ping! (defined in <ping macros>)
 <ping macros>:1:11: 1:24 note: in this expansion of pong! (defined in $DIR/main.rs)