-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Add a nicer error message for missing in for loop, fixes #40782. #45639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
6a62ea6
6d060bd
531b7f2
0d72853
175cfbf
d336f02
ed20f3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3154,13 +3154,48 @@ impl<'a> Parser<'a> { | |
// Parse: `for <src_pat> in <src_expr> <src_loop_block>` | ||
|
||
let pat = self.parse_pat()?; | ||
self.expect_keyword(keywords::In)?; | ||
let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?; | ||
let (iattrs, loop_block) = self.parse_inner_attrs_and_block()?; | ||
attrs.extend(iattrs); | ||
// Save the state of the parser before parsing 'in'. | ||
let parser_snapshot_before_in = self.clone(); | ||
match self.expect_keyword(keywords::In) { | ||
Ok(()) => { | ||
let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?; | ||
let (iattrs, loop_block) = self.parse_inner_attrs_and_block()?; | ||
attrs.extend(iattrs); | ||
|
||
let hi = self.prev_span; | ||
Ok(self.mk_expr(span_lo.to(hi), ExprKind::ForLoop(pat, expr, loop_block, opt_ident), attrs)) | ||
let hi = self.prev_span; | ||
Ok(self.mk_expr( | ||
span_lo.to(hi), | ||
ExprKind::ForLoop(pat, expr, loop_block, opt_ident), | ||
attrs)) | ||
} | ||
Err(mut in_err) => { | ||
let parser_snapshot_after_in = self.clone(); | ||
// Rewind to before attempting to parse the 'in'. | ||
mem::replace(self, parser_snapshot_before_in); | ||
|
||
match self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None) { | ||
Ok(_) => { | ||
// Successfully parsed the expr which means that the 'in' keyword is | ||
// missing, e.g. 'for i 0..2' | ||
in_err.cancel(); | ||
let in_span = parser_snapshot_after_in.prev_span | ||
.between(parser_snapshot_after_in.span); | ||
let mut err = self.sess.span_diagnostic | ||
.struct_span_err(in_span, "missing `in` in `for` loop"); | ||
err.span_label(in_span, "expected `in` here"); | ||
err.span_suggestion_short(in_span, "try adding `in` here", "in".into()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One more thing, make the suggestion There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, should be fixed now. |
||
Err(err) | ||
} | ||
Err(mut expr_err) => { | ||
// Couldn't parse as an expr, return original error and parser state. | ||
expr_err.cancel(); | ||
mem::replace(self, parser_snapshot_after_in); | ||
Err(in_err) | ||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
/// Parse a 'while' or 'while let' expression ('while' token already eaten) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
fn main() { | ||
for i 0..2 { | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error: missing `in` in `for` loop | ||
--> $DIR/issue-40782.rs:12:10 | ||
| | ||
12 | for i 0..2 { | ||
| ^ | ||
| | | ||
| expected `in` here | ||
| help: try adding `in` here | ||
|
||
error: aborting due to previous error | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be done in much simpler way without parser snapshots (please don't overuse the trick from #42578).