Skip to content

Commit 6d060bd

Browse files
committed
Fix spans and error messages.
1 parent 6a62ea6 commit 6d060bd

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3174,23 +3174,16 @@ impl<'a> Parser<'a> {
31743174
mem::replace(self, parser_snapshot_before_in);
31753175

31763176
match self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None) {
3177-
Ok(expr) => {
3178-
// Successfully parsed the expr, print a nice error message.
3177+
Ok(_) => {
3178+
// Successfully parsed the expr which means that the 'in' keyword is
3179+
// missing, e.g. 'for i 0..2'
31793180
in_err.cancel();
3180-
let in_span = parser_snapshot_after_in.span;
3181+
let in_span = parser_snapshot_after_in.prev_span
3182+
.between(parser_snapshot_after_in.span);
31813183
let mut err = self.sess.span_diagnostic
31823184
.struct_span_err(in_span, "missing `in` in `for` loop");
31833185
err.span_label(in_span, "expected `in` here");
3184-
let sugg = pprust::to_string(|s| {
3185-
s.s.word("for ")?;
3186-
s.print_pat(&pat)?;
3187-
s.s.word(" in ")?;
3188-
s.print_expr(&expr)
3189-
});
3190-
err.span_suggestion(
3191-
in_span,
3192-
"try adding `in`",
3193-
sugg);
3186+
err.span_suggestion_short(in_span, "try adding `in` here", "in".into());
31943187
Err(err)
31953188
}
31963189
Err(mut expr_err) => {

src/test/ui/issue-40782.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: missing `in` in `for` loop
2-
--> $DIR/issue-40782.rs:12:11
2+
--> $DIR/issue-40782.rs:12:10
33
|
44
12 | for i 0..2 {
5-
| ^
6-
| |
7-
| expected `in` here
8-
| help: try adding `in`: `for i in 0..2`
5+
| ^
6+
| |
7+
| expected `in` here
8+
| help: try adding `in` here
99

1010
error: aborting due to previous error
1111

0 commit comments

Comments
 (0)