Skip to content

Commit 6a62ea6

Browse files
committed
Add a nicer error message for missing in for loop, fixes #40782.
1 parent f0fe716 commit 6a62ea6

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

src/libsyntax/parse/parser.rs

+48-6
Original file line numberDiff line numberDiff line change
@@ -3154,13 +3154,55 @@ impl<'a> Parser<'a> {
31543154
// Parse: `for <src_pat> in <src_expr> <src_loop_block>`
31553155

31563156
let pat = self.parse_pat()?;
3157-
self.expect_keyword(keywords::In)?;
3158-
let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
3159-
let (iattrs, loop_block) = self.parse_inner_attrs_and_block()?;
3160-
attrs.extend(iattrs);
3157+
// Save the state of the parser before parsing 'in'.
3158+
let parser_snapshot_before_in = self.clone();
3159+
match self.expect_keyword(keywords::In) {
3160+
Ok(()) => {
3161+
let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
3162+
let (iattrs, loop_block) = self.parse_inner_attrs_and_block()?;
3163+
attrs.extend(iattrs);
31613164

3162-
let hi = self.prev_span;
3163-
Ok(self.mk_expr(span_lo.to(hi), ExprKind::ForLoop(pat, expr, loop_block, opt_ident), attrs))
3165+
let hi = self.prev_span;
3166+
Ok(self.mk_expr(
3167+
span_lo.to(hi),
3168+
ExprKind::ForLoop(pat, expr, loop_block, opt_ident),
3169+
attrs))
3170+
}
3171+
Err(mut in_err) => {
3172+
let parser_snapshot_after_in = self.clone();
3173+
// Rewind to before attempting to parse the 'in'.
3174+
mem::replace(self, parser_snapshot_before_in);
3175+
3176+
match self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None) {
3177+
Ok(expr) => {
3178+
// Successfully parsed the expr, print a nice error message.
3179+
in_err.cancel();
3180+
let in_span = parser_snapshot_after_in.span;
3181+
let mut err = self.sess.span_diagnostic
3182+
.struct_span_err(in_span, "missing `in` in `for` loop");
3183+
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);
3194+
Err(err)
3195+
}
3196+
Err(mut expr_err) => {
3197+
// Couldn't parse as an expr, return original error and parser state.
3198+
expr_err.cancel();
3199+
mem::replace(self, parser_snapshot_after_in);
3200+
Err(in_err)
3201+
}
3202+
}
3203+
}
3204+
3205+
}
31643206
}
31653207

31663208
/// Parse a 'while' or 'while let' expression ('while' token already eaten)

src/test/ui/issue-40782.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
for i 0..2 {
13+
}
14+
}
15+

src/test/ui/issue-40782.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: missing `in` in `for` loop
2+
--> $DIR/issue-40782.rs:12:11
3+
|
4+
12 | for i 0..2 {
5+
| ^
6+
| |
7+
| expected `in` here
8+
| help: try adding `in`: `for i in 0..2`
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)