Skip to content

Commit b3d2886

Browse files
committed
Fix infinite loop on chained comparison
1 parent 3f3d0c5 commit b3d2886

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,7 @@ pub(crate) mod parsing {
12841284
if precedence == Precedence::Compare {
12851285
if let Expr::Binary(lhs) = &lhs {
12861286
if Precedence::of_binop(&lhs.op) == Precedence::Compare {
1287-
break;
1287+
return Err(input.error("comparison operators cannot be chained"));
12881288
}
12891289
}
12901290
}
@@ -1346,7 +1346,7 @@ pub(crate) mod parsing {
13461346
if precedence == Precedence::Compare {
13471347
if let Expr::Binary(lhs) = &lhs {
13481348
if Precedence::of_binop(&lhs.op) == Precedence::Compare {
1349-
break;
1349+
return Err(input.error("comparison operators cannot be chained"));
13501350
}
13511351
}
13521352
}

tests/test_expr.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,9 @@ fn test_assign_range_precedence() {
646646
fn test_chained_comparison() {
647647
// https://github.com/dtolnay/syn/issues/1738
648648
let _ = syn::parse_str::<Expr>("a = a < a <");
649+
650+
let err = syn::parse_str::<Expr>("a < a < a").unwrap_err();
651+
assert_eq!("comparison operators cannot be chained", err.to_string());
649652
}
650653

651654
#[test]

0 commit comments

Comments
 (0)