Skip to content

Commit 7eaebfb

Browse files
authored
Merge pull request #1739 from dtolnay/chainedcomparison
Fix infinite loop on chained comparison
2 parents 346efae + b3d2886 commit 7eaebfb

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,15 @@ fn test_assign_range_precedence() {
642642
syn::parse_str::<Expr>("() .. () += ()").unwrap_err();
643643
}
644644

645+
#[test]
646+
fn test_chained_comparison() {
647+
// https://github.com/dtolnay/syn/issues/1738
648+
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());
652+
}
653+
645654
#[test]
646655
fn test_fixup() {
647656
struct FlattenParens;

0 commit comments

Comments
 (0)