Skip to content

Commit a93f176

Browse files
committed
Point to previous line for single expected token
1 parent f1dee43 commit a93f176

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,26 @@ impl<'a> Parser<'a> {
637637
let mut err = self.fatal(&format!("expected `{}`, found `{}`",
638638
token_str,
639639
this_token_str));
640-
err.span_label(self.span, format!("expected `{}`", token_str));
640+
641+
let sp = if self.token == token::Token::Eof {
642+
// EOF, don't want to point at the following char, but rather the last token
643+
self.prev_span
644+
} else {
645+
self.sess.codemap().next_point(self.prev_span)
646+
};
647+
let label_exp = format!("expected `{}`", token_str);
648+
let cm = self.sess.codemap();
649+
match (cm.lookup_line(self.span.lo()), cm.lookup_line(sp.lo())) {
650+
(Ok(ref a), Ok(ref b)) if a.line == b.line => {
651+
// When the spans are in the same line, it means that the only content between
652+
// them is whitespace, point only at the found token.
653+
err.span_label(self.span, label_exp);
654+
}
655+
_ => {
656+
err.span_label(sp, label_exp);
657+
err.span_label(self.span, "unexpected token");
658+
}
659+
}
641660
Err(err)
642661
}
643662
} else {

0 commit comments

Comments
 (0)