From fe897409579177d1ffd964b6d5a8de0024e5df68 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Wed, 29 Nov 2017 13:28:47 -0800 Subject: Point to next token when it is in the expected line --- src/libsyntax/parse/parser.rs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'src/libsyntax/parse') 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) } -- cgit 1.4.1-3-g733a5