Skip to content

Commit ff022f5

Browse files
authored
Merge pull request #1702 from dtolnay/preclet
Create a new precedence level for Expr::Let
2 parents 2959338 + 1f9e082 commit ff022f5

File tree

3 files changed

+13
-23
lines changed

3 files changed

+13
-23
lines changed

src/expr.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3253,30 +3253,12 @@ pub(crate) mod printing {
32533253
};
32543254

32553255
// These cases require parenthesization independently of precedence.
3256-
match (&*e.left, &e.op) {
3256+
if let BinOp::Lt(_) | BinOp::Shl(_) = &e.op {
32573257
// `x as i32 < y` has the parser thinking that `i32 < y` is the
32583258
// beginning of a path type. It starts trying to parse `x as (i32 <
32593259
// y ...` instead of `(x as i32) < ...`. We need to convince it
32603260
// _not_ to do that.
3261-
(_, BinOp::Lt(_) | BinOp::Shl(_)) if classify::confusable_with_adjacent_lt(&e.left) => {
3262-
left_needs_group = true;
3263-
}
3264-
3265-
// We are given `(let _ = a) OP b`.
3266-
//
3267-
// - When `OP <= LAnd` we should print `let _ = a OP b` to avoid
3268-
// redundant parens as the parser will interpret this as `(let _ =
3269-
// a) OP b`.
3270-
//
3271-
// - Otherwise, e.g. when we have `(let a = b) < c` in AST, parens
3272-
// are required since the parser would interpret `let a = b < c`
3273-
// as `let a = (b < c)`. To achieve this, we force parens.
3274-
#[cfg(feature = "full")]
3275-
(Expr::Let(_), _) if binop_prec > Precedence::And => {
3276-
left_needs_group = true;
3277-
}
3278-
3279-
_ => {}
3261+
left_needs_group |= classify::confusable_with_adjacent_lt(&e.left);
32803262
}
32813263

32823264
print_subexpression(

src/fixup.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl FixupContext {
239239
/// "let chain".
240240
pub fn needs_group_as_let_scrutinee(self, expr: &Expr) -> bool {
241241
self.parenthesize_exterior_struct_lit && classify::confusable_with_adjacent_block(expr)
242-
|| self.trailing_precedence(expr) <= Precedence::And
242+
|| self.trailing_precedence(expr) < Precedence::Let
243243
}
244244

245245
/// Determines the effective precedence of a left subexpression. Some
@@ -265,7 +265,11 @@ impl FixupContext {
265265
match expr {
266266
// Increase precedence of expressions that extend to the end of
267267
// current statement or group.
268-
Expr::Break(_) | Expr::Closure(_) | Expr::Return(_) | Expr::Yield(_) => {
268+
Expr::Break(_)
269+
| Expr::Closure(_)
270+
| Expr::Let(_)
271+
| Expr::Return(_)
272+
| Expr::Yield(_) => {
269273
return Precedence::Prefix;
270274
}
271275
Expr::Range(e) if e.start.is_none() => return Precedence::Prefix,

src/precedence.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ pub(crate) enum Precedence {
1919
Or,
2020
// &&
2121
And,
22+
// let
23+
#[cfg(feature = "printing")]
24+
Let,
2225
// == != < > <= >=
2326
Compare,
2427
// |
@@ -97,8 +100,9 @@ impl Precedence {
97100
Expr::Assign(_) => Precedence::Assign,
98101
Expr::Range(_) => Precedence::Range,
99102
Expr::Binary(e) => Precedence::of_binop(&e.op),
103+
Expr::Let(_) => Precedence::Let,
100104
Expr::Cast(_) => Precedence::Cast,
101-
Expr::Let(_) | Expr::Reference(_) | Expr::Unary(_) => Precedence::Prefix,
105+
Expr::Reference(_) | Expr::Unary(_) => Precedence::Prefix,
102106

103107
Expr::Array(_)
104108
| Expr::Async(_)

0 commit comments

Comments
 (0)