Skip to content

Commit

Permalink
Merge pull request #1799 from dtolnay/precedence
Browse files Browse the repository at this point in the history
Simplify distinction between leading and trailing precedence
  • Loading branch information
dtolnay authored Dec 26, 2024
2 parents 560dd9d + 331b4d0 commit cf75a99
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 36 deletions.
20 changes: 10 additions & 10 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -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,
);
Expand All @@ -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(),
);
Expand All @@ -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(),
);
Expand All @@ -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(),
);
Expand Down Expand Up @@ -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(),
);
Expand Down
47 changes: 21 additions & 26 deletions src/fixup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
//
Expand All @@ -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,
};

Expand Down Expand Up @@ -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
}
}
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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) {
Expand Down

0 comments on commit cf75a99

Please sign in to comment.