diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index a3a265450ab0e..6de2ca5b9b874 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3154,7 +3154,13 @@ impl<'a> Parser<'a> { // Parse: `for in ` 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); diff --git a/src/test/ui/issue-40782.rs b/src/test/ui/issue-40782.rs new file mode 100644 index 0000000000000..56ee225105f89 --- /dev/null +++ b/src/test/ui/issue-40782.rs @@ -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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + for i 0..2 { + } +} + diff --git a/src/test/ui/issue-40782.stderr b/src/test/ui/issue-40782.stderr new file mode 100644 index 0000000000000..0d49eebbdbfc9 --- /dev/null +++ b/src/test/ui/issue-40782.stderr @@ -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 +