Skip to content

Commit

Permalink
Disallow range with upper bound in lhs of binop
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed May 11, 2024
1 parent 46bc5aa commit a794288
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
6 changes: 5 additions & 1 deletion src/expr.rs
Expand Up @@ -1391,7 +1391,11 @@ pub(crate) mod parsing {
) -> Result<Expr> {
loop {
let ahead = input.fork();
if let Ok(op) = ahead.parse::<BinOp>() {
if let Expr::Range(ExprRange { end: Some(_), .. }) = lhs {
// A range with an upper bound cannot be the left-hand side of
// another binary operator.
break;
} else if let Ok(op) = ahead.parse::<BinOp>() {
let precedence = Precedence::of(&op);
if precedence < base {
break;
Expand Down
29 changes: 5 additions & 24 deletions tests/test_expr.rs
Expand Up @@ -379,30 +379,11 @@ fn test_range_precedence() {
}
"###);

// FIXME: should fail to parse. A range with a lower bound cannot be the
// upper bound of another range.
snapshot!(".. () .." as Expr, @r###"
Expr::Range {
limits: RangeLimits::HalfOpen,
end: Some(Expr::Range {
start: Some(Expr::Tuple),
limits: RangeLimits::HalfOpen,
}),
}
"###);

// FIXME: should fail to parse. A range with an upper bound cannot be the
// lower bound of another range.
snapshot!("() .. () .." as Expr, @r###"
Expr::Range {
start: Some(Expr::Range {
start: Some(Expr::Tuple),
limits: RangeLimits::HalfOpen,
end: Some(Expr::Tuple),
}),
limits: RangeLimits::HalfOpen,
}
"###);
// A range with a lower bound cannot be the upper bound of another range,
// and a range with an upper bound cannot be the lower bound of another
// range.
syn::parse_str::<Expr>(".. x ..").unwrap_err();
syn::parse_str::<Expr>("x .. x ..").unwrap_err();
}

#[test]
Expand Down

0 comments on commit a794288

Please sign in to comment.