Skip to content

Commit

Permalink
Fix stmt boundary after None-delimited group containing loop
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Dec 11, 2023
1 parent 2781584 commit a2112d4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
34 changes: 31 additions & 3 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1915,7 +1915,15 @@ pub(crate) mod parsing {
#[cfg(feature = "full")]
pub(crate) fn expr_early(input: ParseStream) -> Result<Expr> {
let mut attrs = input.call(expr_attrs)?;
let mut expr = if input.peek(Token![if]) {
let mut expr = if input.peek(token::Group) {
let allow_struct = AllowStruct(true);
let atom = expr_group(input, allow_struct)?;
if continue_parsing_arm(&atom) {
trailer_helper(input, atom)?
} else {
atom
}
} else if input.peek(Token![if]) {
Expr::If(input.parse()?)
} else if input.peek(Token![while]) {
Expr::While(input.parse()?)
Expand All @@ -1939,13 +1947,16 @@ pub(crate) mod parsing {
atom_labeled(input)?
} else {
let allow_struct = AllowStruct(true);
let mut expr = unary_expr(input, allow_struct)?;
unary_expr(input, allow_struct)?
};

if continue_parsing_arm(&expr) {
attrs.extend(expr.replace_attrs(Vec::new()));
expr.replace_attrs(attrs);

let allow_struct = AllowStruct(true);
return parse_expr(input, expr, allow_struct, Precedence::Any);
};
}

if input.peek(Token![.]) && !input.peek(Token![..]) || input.peek(Token![?]) {
expr = trailer_helper(input, expr)?;
Expand All @@ -1962,6 +1973,23 @@ pub(crate) mod parsing {
Ok(expr)
}

#[cfg(feature = "full")]
fn continue_parsing_arm(early: &Expr) -> bool {
match early {
Expr::If(_)
| Expr::While(_)
| Expr::ForLoop(_)
| Expr::Loop(_)
| Expr::Match(_)
| Expr::TryBlock(_)
| Expr::Unsafe(_)
| Expr::Const(_)
| Expr::Block(_) => false,
Expr::Group(group) => continue_parsing_arm(&group.expr),
_ => true,
}
}

#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
impl Parse for ExprLit {
fn parse(input: ParseStream) -> Result<Self> {
Expand Down
1 change: 0 additions & 1 deletion tests/test_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ fn test_macro_variable_match_arm() {

let expr = Group::new(Delimiter::None, quote!(loop {} + 1));
let tokens = quote!(match v { _ => #expr });
// FIXME
snapshot!(tokens as Expr, @r###"
Expr::Match {
expr: Expr::Path {
Expand Down

0 comments on commit a2112d4

Please sign in to comment.