Skip to content

Commit f9b0897

Browse files
committed
Auto merge of #46381 - estebank:expected-span, r=nikomatsakis
Point to next token when it is in the expected line r? @nikomatsakis
2 parents 377decc + fe89740 commit f9b0897

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

src/libsyntax/codemap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ impl CodeMap {
358358
}
359359

360360
// If the relevant filemap is empty, we don't return a line number.
361-
fn lookup_line(&self, pos: BytePos) -> Result<FileMapAndLine, Rc<FileMap>> {
361+
pub fn lookup_line(&self, pos: BytePos) -> Result<FileMapAndLine, Rc<FileMap>> {
362362
let idx = self.lookup_filemap_idx(pos);
363363

364364
let files = self.files.borrow();

src/libsyntax/parse/parser.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -660,11 +660,28 @@ impl<'a> Parser<'a> {
660660
} else {
661661
label_sp
662662
};
663-
if self.span.contains(sp) {
664-
err.span_label(self.span, label_exp);
665-
} else {
666-
err.span_label(sp, label_exp);
667-
err.span_label(self.span, "unexpected token");
663+
664+
let cm = self.sess.codemap();
665+
match (cm.lookup_line(self.span.lo()), cm.lookup_line(sp.lo())) {
666+
(Ok(ref a), Ok(ref b)) if a.line == b.line => {
667+
// When the spans are in the same line, it means that the only content between
668+
// them is whitespace, point at the found token in that case:
669+
//
670+
// X | () => { syntax error };
671+
// | ^^^^^ expected one of 8 possible tokens here
672+
//
673+
// instead of having:
674+
//
675+
// X | () => { syntax error };
676+
// | -^^^^^ unexpected token
677+
// | |
678+
// | expected one of 8 possible tokens here
679+
err.span_label(self.span, label_exp);
680+
}
681+
_ => {
682+
err.span_label(sp, label_exp);
683+
err.span_label(self.span, "unexpected token");
684+
}
668685
}
669686
Err(err)
670687
}

src/test/ui/did_you_mean/issue-40006.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ error: expected one of `!` or `::`, found `(`
4848
--> $DIR/issue-40006.rs:24:9
4949
|
5050
24 | ::Y (); //~ ERROR expected one of
51-
| -^ unexpected token
52-
| |
53-
| expected one of `!` or `::` here
51+
| ^ expected one of `!` or `::` here
5452

5553
error: missing `fn`, `type`, or `const` for impl-item declaration
5654
--> $DIR/issue-40006.rs:28:8

src/test/ui/macro_backtrace/main.stderr

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@ error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found
22
--> $DIR/main.rs:19:20
33
|
44
19 | () => { syntax error }; //~ ERROR expected one of
5-
| -^^^^^ unexpected token
6-
| |
7-
| expected one of 8 possible tokens here
5+
| ^^^^^ expected one of 8 possible tokens here
86
$DIR/main.rs:24:5: 24:13 note: in this expansion of pong! (defined in $DIR/main.rs)
97

108
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `error`
119
--> $DIR/main.rs:19:20
1210
|
1311
19 | () => { syntax error }; //~ ERROR expected one of
14-
| -^^^^^ unexpected token
15-
| |
16-
| expected one of 8 possible tokens here
12+
| ^^^^^ expected one of 8 possible tokens here
1713
$DIR/main.rs:25:5: 25:13 note: in this expansion of ping! (defined in <ping macros>)
1814
<ping macros>:1:11: 1:24 note: in this expansion of pong! (defined in $DIR/main.rs)
1915

0 commit comments

Comments
 (0)