Auto merge of #107526 - obeis:for-missing-iterator, r=estebank,compiler-errors

Recover form missing expression in `for` loop

Close #78537
r? `@estebank`
This commit is contained in:
bors 2023-02-05 20:33:05 +00:00
commit 75a0be98f2
5 changed files with 48 additions and 0 deletions

View File

@ -128,6 +128,9 @@ parse_missing_in_in_for_loop = missing `in` in `for` loop
.use_in_not_of = try using `in` here instead
.add_in = try adding `in` here
parse_missing_expression_in_for_loop = missing expression to iterate on in `for` loop
.suggestion = try adding an expression to the `for` loop
parse_missing_comma_after_match_arm = expected `,` following `match` arm
.suggestion = missing a comma here to end this `match` arm

View File

@ -433,6 +433,18 @@ pub(crate) enum MissingInInForLoopSub {
AddIn(#[primary_span] Span),
}
#[derive(Diagnostic)]
#[diag(parse_missing_expression_in_for_loop)]
pub(crate) struct MissingExpressionInForLoop {
#[primary_span]
#[suggestion(
code = "/* expression */ ",
applicability = "has-placeholders",
style = "verbose"
)]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(parse_missing_comma_after_match_arm)]
pub(crate) struct MissingCommaAfterMatchArm {

View File

@ -2471,6 +2471,21 @@ fn parse_for_expr(&mut self, opt_label: Option<Label>, lo: Span) -> PResult<'a,
let pat = self.recover_parens_around_for_head(pat, begin_paren);
// Recover from missing expression in `for` loop
if matches!(expr.kind, ExprKind::Block(..))
&& !matches!(self.token.kind, token::OpenDelim(token::Delimiter::Brace))
&& self.may_recover()
{
self.sess
.emit_err(errors::MissingExpressionInForLoop { span: expr.span.shrink_to_lo() });
let err_expr = self.mk_expr(expr.span, ExprKind::Err);
let block = self.mk_block(vec![], BlockCheckMode::Default, self.prev_token.span);
return Ok(self.mk_expr(
lo.to(self.prev_token.span),
ExprKind::ForLoop(pat, err_expr, block, opt_label),
));
}
let (attrs, loop_block) = self.parse_inner_attrs_and_block()?;
let kind = ExprKind::ForLoop(pat, expr, loop_block, opt_label);

View File

@ -0,0 +1,5 @@
fn main() {
for i in {
//~^ ERROR missing expression to iterate on in `for` loop
}
}

View File

@ -0,0 +1,13 @@
error: missing expression to iterate on in `for` loop
--> $DIR/missing-expression-in-for-loop.rs:2:14
|
LL | for i in {
| ^
|
help: try adding an expression to the `for` loop
|
LL | for i in /* expression */ {
| ++++++++++++++++
error: aborting due to previous error