Fix spans and error messages.
This commit is contained in:
@@ -3174,23 +3174,16 @@ impl<'a> Parser<'a> {
|
|||||||
mem::replace(self, parser_snapshot_before_in);
|
mem::replace(self, parser_snapshot_before_in);
|
||||||
|
|
||||||
match self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None) {
|
match self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None) {
|
||||||
Ok(expr) => {
|
Ok(_) => {
|
||||||
// Successfully parsed the expr, print a nice error message.
|
// Successfully parsed the expr which means that the 'in' keyword is
|
||||||
|
// missing, e.g. 'for i 0..2'
|
||||||
in_err.cancel();
|
in_err.cancel();
|
||||||
let in_span = parser_snapshot_after_in.span;
|
let in_span = parser_snapshot_after_in.prev_span
|
||||||
|
.between(parser_snapshot_after_in.span);
|
||||||
let mut err = self.sess.span_diagnostic
|
let mut err = self.sess.span_diagnostic
|
||||||
.struct_span_err(in_span, "missing `in` in `for` loop");
|
.struct_span_err(in_span, "missing `in` in `for` loop");
|
||||||
err.span_label(in_span, "expected `in` here");
|
err.span_label(in_span, "expected `in` here");
|
||||||
let sugg = pprust::to_string(|s| {
|
err.span_suggestion_short(in_span, "try adding `in` here", "in".into());
|
||||||
s.s.word("for ")?;
|
|
||||||
s.print_pat(&pat)?;
|
|
||||||
s.s.word(" in ")?;
|
|
||||||
s.print_expr(&expr)
|
|
||||||
});
|
|
||||||
err.span_suggestion(
|
|
||||||
in_span,
|
|
||||||
"try adding `in`",
|
|
||||||
sugg);
|
|
||||||
Err(err)
|
Err(err)
|
||||||
}
|
}
|
||||||
Err(mut expr_err) => {
|
Err(mut expr_err) => {
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
error: missing `in` in `for` loop
|
error: missing `in` in `for` loop
|
||||||
--> $DIR/issue-40782.rs:12:11
|
--> $DIR/issue-40782.rs:12:10
|
||||||
|
|
|
|
||||||
12 | for i 0..2 {
|
12 | for i 0..2 {
|
||||||
| ^
|
| ^
|
||||||
| |
|
| |
|
||||||
| expected `in` here
|
| expected `in` here
|
||||||
| help: try adding `in`: `for i in 0..2`
|
| help: try adding `in` here
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user