Skip to content

Commit

Permalink
Improve error on break followed by labeled loop
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 6, 2023
1 parent 32ab979 commit a876185
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2463,18 +2463,36 @@ pub(crate) mod parsing {

#[cfg(feature = "full")]
fn expr_break(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprBreak> {
let break_token: Token![break] = input.parse()?;

let ahead = input.fork();
let label: Option<Lifetime> = ahead.parse()?;
if label.is_some() && ahead.peek(Token![:]) {
// Not allowed: `break 'label: loop {...}`
// Parentheses are required. `break ('label: loop {...})`
let _ = ambiguous_expr(input, allow_struct)?;
let start_span = label.unwrap().apostrophe;
let end_span = input.cursor().prev_span();
return Err(crate::error::new2(
start_span,
end_span,
"parentheses required",
));
}

input.advance_to(&ahead);
let expr = if can_begin_expr(input) && (allow_struct.0 || !input.peek(token::Brace)) {
let expr = ambiguous_expr(input, allow_struct)?;
Some(Box::new(expr))
} else {
None
};

Ok(ExprBreak {
attrs: Vec::new(),
break_token: input.parse()?,
label: input.parse()?,
expr: {
if can_begin_expr(input) && (allow_struct.0 || !input.peek(token::Brace)) {
let expr = ambiguous_expr(input, allow_struct)?;
Some(Box::new(expr))
} else {
None
}
},
break_token,
label,
expr,
})
}

Expand Down

0 comments on commit a876185

Please sign in to comment.