From 3f3d0c57ac66b4fa42a3f10209dd1fde29c5ce57 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 27 Sep 2024 10:29:10 -0700 Subject: [PATCH 1/2] Add regression test for issue 1738 --- tests/test_expr.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_expr.rs b/tests/test_expr.rs index d20cce8d6a..f79e4fafda 100644 --- a/tests/test_expr.rs +++ b/tests/test_expr.rs @@ -642,6 +642,12 @@ fn test_assign_range_precedence() { syn::parse_str::("() .. () += ()").unwrap_err(); } +#[test] +fn test_chained_comparison() { + // https://github.com/dtolnay/syn/issues/1738 + let _ = syn::parse_str::("a = a < a <"); +} + #[test] fn test_fixup() { struct FlattenParens; From b3d2886fc9bbff5eb45995c72beec0463a8cec2a Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 27 Sep 2024 10:29:31 -0700 Subject: [PATCH 2/2] Fix infinite loop on chained comparison --- src/expr.rs | 4 ++-- tests/test_expr.rs | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index db8d5b9211..8216191c2f 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -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")); } } } @@ -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")); } } } diff --git a/tests/test_expr.rs b/tests/test_expr.rs index f79e4fafda..399972c424 100644 --- a/tests/test_expr.rs +++ b/tests/test_expr.rs @@ -646,6 +646,9 @@ fn test_assign_range_precedence() { fn test_chained_comparison() { // https://github.com/dtolnay/syn/issues/1738 let _ = syn::parse_str::("a = a < a <"); + + let err = syn::parse_str::("a < a < a").unwrap_err(); + assert_eq!("comparison operators cannot be chained", err.to_string()); } #[test]