Skip to content

Commit

Permalink
Merge pull request #1739 from dtolnay/chainedcomparison
Browse files Browse the repository at this point in the history
Fix infinite loop on chained comparison
  • Loading branch information
dtolnay authored Sep 27, 2024
2 parents 346efae + b3d2886 commit 7eaebfb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@ pub(crate) mod parsing {
if precedence == Precedence::Compare {
if let Expr::Binary(lhs) = &lhs {
if Precedence::of_binop(&lhs.op) == Precedence::Compare {
break;
return Err(input.error("comparison operators cannot be chained"));
}
}
}
Expand Down Expand Up @@ -1346,7 +1346,7 @@ pub(crate) mod parsing {
if precedence == Precedence::Compare {
if let Expr::Binary(lhs) = &lhs {
if Precedence::of_binop(&lhs.op) == Precedence::Compare {
break;
return Err(input.error("comparison operators cannot be chained"));
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/test_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,15 @@ fn test_assign_range_precedence() {
syn::parse_str::<Expr>("() .. () += ()").unwrap_err();
}

#[test]
fn test_chained_comparison() {
// https://github.com/dtolnay/syn/issues/1738
let _ = syn::parse_str::<Expr>("a = a < a <");

let err = syn::parse_str::<Expr>("a < a < a").unwrap_err();
assert_eq!("comparison operators cannot be chained", err.to_string());
}

#[test]
fn test_fixup() {
struct FlattenParens;
Expand Down

0 comments on commit 7eaebfb

Please sign in to comment.