about summary refs log tree commit diff
path: root/src/libsyntax/parse/parser.rs
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2017-11-04 13:49:28 +0800
committerGitHub <noreply@github.com>2017-11-04 13:49:28 +0800
commitea572657daea279bca23cad44f3489a9d4d2985e (patch)
treee147ed0726ced6d25c7230544465fefc294c7983 /src/libsyntax/parse/parser.rs
parentae512c4144a8cb2de048ff91895357d9fb5199c3 (diff)
parented20f3b5c0ae32802450c77da5c94c239c3a3500 (diff)
downloadrust-ea572657daea279bca23cad44f3489a9d4d2985e.tar.gz
rust-ea572657daea279bca23cad44f3489a9d4d2985e.zip
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.
Diffstat (limited to 'src/libsyntax/parse/parser.rs')
-rw-r--r--src/libsyntax/parse/parser.rs8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 5176379ee2f..424f46840ad 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -3154,7 +3154,13 @@ impl<'a> Parser<'a> {
         // Parse: `for <src_pat> in <src_expr> <src_loop_block>`
 
         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 (iattrs, loop_block) = self.parse_inner_attrs_and_block()?;
         attrs.extend(iattrs);