Auto merge of #46381 - estebank:expected-span, r=nikomatsakis

Point to next token when it is in the expected line

r? @nikomatsakis
This commit is contained in:
bors
2017-12-02 20:06:42 +00:00
4 changed files with 26 additions and 15 deletions

View File

@@ -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)
}