Rollup merge of #45639 - LaurentMazare:master, r=petrochenkov
Add a nicer error message for missing in for loop, fixes #40782. As suggested by @estebank in issue #40782, this works in the same way as #42578: if the in keyword is missing, we continue parsing the expression and if this works correctly an adapted error message is produced. Otherwise we return the old error. A specific test case has also been added. This is my first PR on rust-lang/rust so any feedback is very welcome.
This commit is contained in:
commit
ea572657da
@ -3154,7 +3154,13 @@ impl<'a> Parser<'a> {
|
|||||||
// Parse: `for <src_pat> in <src_expr> <src_loop_block>`
|
// Parse: `for <src_pat> in <src_expr> <src_loop_block>`
|
||||||
|
|
||||||
let pat = self.parse_pat()?;
|
let pat = self.parse_pat()?;
|
||||||
self.expect_keyword(keywords::In)?;
|
if !self.eat_keyword(keywords::In) {
|
||||||
|
let in_span = self.prev_span.between(self.span);
|
||||||
|
let mut err = self.sess.span_diagnostic
|
||||||
|
.struct_span_err(in_span, "missing `in` in `for` loop");
|
||||||
|
err.span_suggestion_short(in_span, "try adding `in` here", " in ".into());
|
||||||
|
err.emit();
|
||||||
|
}
|
||||||
let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
|
let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
|
||||||
let (iattrs, loop_block) = self.parse_inner_attrs_and_block()?;
|
let (iattrs, loop_block) = self.parse_inner_attrs_and_block()?;
|
||||||
attrs.extend(iattrs);
|
attrs.extend(iattrs);
|
||||||
|
15
src/test/ui/issue-40782.rs
Normal file
15
src/test/ui/issue-40782.rs
Normal file
@ -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 {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
8
src/test/ui/issue-40782.stderr
Normal file
8
src/test/ui/issue-40782.stderr
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
error: missing `in` in `for` loop
|
||||||
|
--> $DIR/issue-40782.rs:12:10
|
||||||
|
|
|
||||||
|
12 | for i 0..2 {
|
||||||
|
| ^ help: try adding `in` here
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user