diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2018-06-09 18:39:14 -0700 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2018-06-19 15:19:16 -0700 |
| commit | a93f176b7440a38e6eda7e1b5e338cd4d05f92a8 (patch) | |
| tree | a856cc7f43892bb3a61f2a937ff95738f16a139c /src/libsyntax/parse/parser.rs | |
| parent | f1dee43887821e34988f4427db009d19fae69496 (diff) | |
| download | rust-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.rs | 21 |
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 { |
