Skip to content

Commit

Permalink
Fix ranges bailout edge cases with assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Dec 31, 2024
1 parent 9ea6782 commit a08dedf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
26 changes: 24 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,11 @@ pub(crate) mod parsing {
if precedence < base {
break;
}
if precedence == Precedence::Assign {
if let Expr::Range(_) = lhs {
break;
}
}
if precedence == Precedence::Compare {
if let Expr::Binary(lhs) = &lhs {
if Precedence::of_binop(&lhs.op) == Precedence::Compare {
Expand All @@ -1339,7 +1344,13 @@ pub(crate) mod parsing {
op,
right,
});
} else if Precedence::Assign >= base && input.peek(Token![=]) && !input.peek(Token![=>])
} else if Precedence::Assign >= base
&& input.peek(Token![=])
&& !input.peek(Token![=>])
&& match lhs {
Expr::Range(_) => false,
_ => true,
}
{
let eq_token: Token![=] = input.parse()?;
let right = parse_binop_rhs(input, allow_struct, Precedence::Assign)?;
Expand Down Expand Up @@ -2874,7 +2885,18 @@ pub(crate) mod parsing {
|| input.peek(Token![.]) && !input.peek(Token![..])
|| input.peek(Token![?])
|| input.peek(Token![=>])
|| !allow_struct.0 && input.peek(token::Brace))
|| !allow_struct.0 && input.peek(token::Brace)
|| input.peek(Token![=]) && !input.peek(Token![==])
|| input.peek(Token![+=])
|| input.peek(Token![-=])
|| input.peek(Token![*=])
|| input.peek(Token![/=])
|| input.peek(Token![%=])
|| input.peek(Token![^=])
|| input.peek(Token![&=])
|| input.peek(Token![|=])
|| input.peek(Token![<<=])
|| input.peek(Token![>>=]))
{
Ok(None)
} else {
Expand Down
25 changes: 24 additions & 1 deletion tests/test_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ fn test_range_precedence() {
}

#[test]
fn test_ranges_dots_bailout() {
fn test_ranges_bailout() {
syn::parse_str::<Expr>(".. ?").unwrap_err();
syn::parse_str::<Expr>(".. .field").unwrap_err();

Expand Down Expand Up @@ -476,6 +476,29 @@ fn test_ranges_dots_bailout() {
member: Member::Named("field"),
}
"#);

snapshot!("return .. = ()" as Expr, @r"
Expr::Assign {
left: Expr::Return {
expr: Some(Expr::Range {
limits: RangeLimits::HalfOpen,
}),
},
right: Expr::Tuple,
}
");

snapshot!("return .. += ()" as Expr, @r"
Expr::Binary {
left: Expr::Return {
expr: Some(Expr::Range {
limits: RangeLimits::HalfOpen,
}),
},
op: BinOp::AddAssign,
right: Expr::Tuple,
}
");
}

#[test]
Expand Down

0 comments on commit a08dedf

Please sign in to comment.