diff --git a/src/expr.rs b/src/expr.rs index 961f308d9c..e90816d485 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -3280,7 +3280,7 @@ pub(crate) mod printing { e.eq_token.to_tokens(tokens); print_subexpression( &e.right, - fixup.trailing_precedence(&e.right) < Precedence::Assign, + fixup.precedence(&e.right) < Precedence::Assign, tokens, fixup.subsequent_subexpression(), ); @@ -3348,8 +3348,8 @@ pub(crate) mod printing { ); let binop_prec = Precedence::of_binop(&e.op); - let left_prec = left_fixup.leading_precedence(&e.left); - let right_prec = fixup.trailing_precedence(&e.right); + let left_prec = left_fixup.precedence(&e.left); + let right_prec = fixup.precedence(&e.right); let (left_needs_group, right_needs_group) = match binop_prec { Precedence::Assign => (left_prec <= Precedence::Range, right_prec < binop_prec), Precedence::Compare => (left_prec <= binop_prec, right_prec <= binop_prec), @@ -3422,7 +3422,7 @@ pub(crate) mod printing { let needs_group = if let Expr::Field(func) = &*e.func { func.member.is_named() } else { - func_fixup.leading_precedence(&e.func) < Precedence::Unambiguous + func_fixup.precedence(&e.func) < Precedence::Unambiguous }; print_subexpression(&e.func, needs_group, tokens, func_fixup); @@ -3598,7 +3598,7 @@ pub(crate) mod printing { ); print_subexpression( &e.expr, - obj_fixup.leading_precedence(&e.expr) < Precedence::Unambiguous, + obj_fixup.precedence(&e.expr) < Precedence::Unambiguous, tokens, obj_fixup, ); @@ -3755,7 +3755,7 @@ pub(crate) mod printing { let start_fixup = fixup.leftmost_subexpression_with_begin_operator(true, false); print_subexpression( start, - start_fixup.leading_precedence(start) <= Precedence::Range, + start_fixup.precedence(start) <= Precedence::Range, tokens, start_fixup, ); @@ -3764,7 +3764,7 @@ pub(crate) mod printing { if let Some(end) = &e.end { print_subexpression( end, - fixup.trailing_precedence(end) <= Precedence::Range, + fixup.precedence(end) <= Precedence::Range, tokens, fixup.subsequent_subexpression(), ); @@ -3787,7 +3787,7 @@ pub(crate) mod printing { e.mutability.to_tokens(tokens); print_subexpression( &e.expr, - fixup.trailing_precedence(&e.expr) < Precedence::Prefix, + fixup.precedence(&e.expr) < Precedence::Prefix, tokens, fixup.subsequent_subexpression(), ); @@ -3806,7 +3806,7 @@ pub(crate) mod printing { e.mutability.to_tokens(tokens); print_subexpression( &e.expr, - fixup.trailing_precedence(&e.expr) < Precedence::Prefix, + fixup.precedence(&e.expr) < Precedence::Prefix, tokens, fixup.subsequent_subexpression(), ); @@ -3916,7 +3916,7 @@ pub(crate) mod printing { e.op.to_tokens(tokens); print_subexpression( &e.expr, - fixup.trailing_precedence(&e.expr) < Precedence::Prefix, + fixup.precedence(&e.expr) < Precedence::Prefix, tokens, fixup.subsequent_subexpression(), ); diff --git a/src/fixup.rs b/src/fixup.rs index fb1c42182e..fa19a7e10f 100644 --- a/src/fixup.rs +++ b/src/fixup.rs @@ -93,21 +93,21 @@ pub(crate) struct FixupContext { // This is the difference between: // - // let _ = 1 + return 1; // no parens if rightmost subexpression + // let _ = (return) - 1; // without paren, this would return -1 // - // let _ = 1 + (return 1) + 1; // needs parens + // let _ = return + 1; // no paren because '+' cannot begin expr // #[cfg(feature = "full")] - parenthesize_exterior_jump: bool, + next_operator_can_begin_expr: bool, // This is the difference between: // - // let _ = (return) - 1; // without paren, this would return -1 + // let _ = 1 + return 1; // no parens if rightmost subexpression // - // let _ = return + 1; // no paren because '+' cannot begin expr + // let _ = 1 + (return 1) + 1; // needs parens // #[cfg(feature = "full")] - next_operator_can_begin_expr: bool, + next_operator_can_continue_expr: bool, // This is the difference between: // @@ -134,9 +134,9 @@ impl FixupContext { #[cfg(feature = "full")] parenthesize_exterior_struct_lit: false, #[cfg(feature = "full")] - parenthesize_exterior_jump: false, - #[cfg(feature = "full")] next_operator_can_begin_expr: false, + #[cfg(feature = "full")] + next_operator_can_continue_expr: false, next_operator_can_begin_generics: false, }; @@ -195,7 +195,10 @@ impl FixupContext { leftmost_subexpression_in_match_arm: self.match_arm || self.leftmost_subexpression_in_match_arm, #[cfg(feature = "full")] - parenthesize_exterior_jump: true, + next_operator_can_begin_expr: false, + #[cfg(feature = "full")] + next_operator_can_continue_expr: true, + next_operator_can_begin_generics: false, ..self } } @@ -215,7 +218,10 @@ impl FixupContext { #[cfg(feature = "full")] leftmost_subexpression_in_match_arm: false, #[cfg(feature = "full")] - parenthesize_exterior_jump: true, + next_operator_can_begin_expr: false, + #[cfg(feature = "full")] + next_operator_can_continue_expr: true, + next_operator_can_begin_generics: false, ..self } } @@ -285,12 +291,12 @@ impl FixupContext { #[cfg(feature = "full")] pub fn needs_group_as_let_scrutinee(self, expr: &Expr) -> bool { self.parenthesize_exterior_struct_lit && classify::confusable_with_adjacent_block(expr) - || self.trailing_precedence(expr) < Precedence::Let + || self.precedence(expr) < Precedence::Let } - /// Determines the effective precedence of a left subexpression. Some - /// expressions have lower precedence when adjacent to particular operators. - pub fn leading_precedence(self, expr: &Expr) -> Precedence { + /// Determines the effective precedence of a subexpression. Some expressions + /// have higher or lower precedence when adjacent to particular operators. + pub fn precedence(self, expr: &Expr) -> Precedence { #[cfg(feature = "full")] if self.next_operator_can_begin_expr { // Decrease precedence of value-less jumps when followed by an @@ -300,15 +306,8 @@ impl FixupContext { return Precedence::Jump; } } - self.precedence(expr) - } - - /// Determines the effective precedence of a right subexpression. Some - /// expressions have higher precedence on the right side of a binary - /// operator than on the left. - pub fn trailing_precedence(self, expr: &Expr) -> Precedence { #[cfg(feature = "full")] - if !self.parenthesize_exterior_jump { + if !self.next_operator_can_continue_expr { match expr { // Increase precedence of expressions that extend to the end of // current statement or group. @@ -323,10 +322,6 @@ impl FixupContext { _ => {} } } - self.leading_precedence(expr) - } - - fn precedence(self, expr: &Expr) -> Precedence { if self.next_operator_can_begin_generics { if let Expr::Cast(cast) = expr { if classify::trailing_unparameterized_path(&cast.ty) {