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>2018-06-09 18:39:14 -0700
committerEsteban Küber <esteban@kuber.com.ar>2018-06-19 15:19:16 -0700
commita93f176b7440a38e6eda7e1b5e338cd4d05f92a8 (patch)
treea856cc7f43892bb3a61f2a937ff95738f16a139c /src/libsyntax/parse/parser.rs
parentf1dee43887821e34988f4427db009d19fae69496 (diff)
downloadrust-a93f176b7440a38e6eda7e1b5e338cd4d05f92a8.tar.gz
rust-a93f176b7440a38e6eda7e1b5e338cd4d05f92a8.zip
Point to previous line for single expected token
Diffstat (limited to 'src/libsyntax/parse/parser.rs')
-rw-r--r--src/libsyntax/parse/parser.rs21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 5d04aa711c1..4045053283e 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -637,7 +637,26 @@ impl<'a> Parser<'a> {
                 let mut err = self.fatal(&format!("expected `{}`, found `{}`",
                                                   token_str,
                                                   this_token_str));
-                err.span_label(self.span, format!("expected `{}`", token_str));
+
+                let sp = if self.token == token::Token::Eof {
+                    // EOF, don't want to point at the following char, but rather the last token
+                    self.prev_span
+                } else {
+                    self.sess.codemap().next_point(self.prev_span)
+                };
+                let label_exp = format!("expected `{}`", token_str);
+                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 only at the found token.
+                        err.span_label(self.span, label_exp);
+                    }
+                    _ => {
+                        err.span_label(sp, label_exp);
+                        err.span_label(self.span, "unexpected token");
+                    }
+                }
                 Err(err)
             }
         } else {