Skip to content

Commit

Permalink
Merge leftmost_subexpression_with_operator and leftmost_subexpression…
Browse files Browse the repository at this point in the history
… into one method
  • Loading branch information
dtolnay committed Jan 1, 2025
1 parent 15220b9 commit 19d93ba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
8 changes: 6 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3306,7 +3306,7 @@ pub(crate) mod printing {
&e.left,
Precedence::of(&e.left) <= Precedence::Range,
tokens,
fixup.leftmost_subexpression(),
fixup.leftmost_subexpression_with_operator(false, false),
);
e.eq_token.to_tokens(tokens);
print_subexpression(
Expand Down Expand Up @@ -3475,7 +3475,11 @@ pub(crate) mod printing {
&e.expr,
Precedence::of(&e.expr) < Precedence::Cast,
tokens,
fixup.leftmost_subexpression(),
fixup.leftmost_subexpression_with_operator(
#[cfg(feature = "full")]
false,
false,
),
);
e.as_token.to_tokens(tokens);
e.ty.to_tokens(tokens);
Expand Down
34 changes: 13 additions & 21 deletions src/fixup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ impl FixupContext {
///
/// Not every expression has a leftmost subexpression. For example neither
/// `-$a` nor `[$a]` have one.
pub fn leftmost_subexpression(self) -> Self {
pub fn leftmost_subexpression_with_operator(
self,
#[cfg(feature = "full")] next_operator_can_begin_expr: bool,
next_operator_can_begin_generics: bool,
) -> Self {
FixupContext {
#[cfg(feature = "full")]
stmt: false,
Expand All @@ -209,10 +213,10 @@ impl FixupContext {
#[cfg(feature = "full")]
rightmost_subexpression: false,
#[cfg(feature = "full")]
next_operator_can_begin_expr: false,
next_operator_can_begin_expr,
#[cfg(feature = "full")]
next_operator_can_continue_expr: true,
next_operator_can_begin_generics: false,
next_operator_can_begin_generics,
..self
}
}
Expand Down Expand Up @@ -242,22 +246,6 @@ impl FixupContext {
}
}

/// Transform this fixup into the one that should apply when printing a
/// leftmost subexpression followed by punctuation that is legal as the
/// first token of an expression.
pub fn leftmost_subexpression_with_operator(
self,
#[cfg(feature = "full")] next_operator_can_begin_expr: bool,
next_operator_can_begin_generics: bool,
) -> Self {
FixupContext {
#[cfg(feature = "full")]
next_operator_can_begin_expr,
next_operator_can_begin_generics,
..self.leftmost_subexpression()
}
}

/// Transform this fixup into the one that should apply when printing the
/// rightmost subexpression of the current expression.
///
Expand Down Expand Up @@ -393,10 +381,14 @@ fn test_leftmost_rightmost_invariant() {
};
assert_eq!(i, BITS);
assert_eq!(
fixup.leftmost_subexpression().rightmost_subexpression(),
fixup
.leftmost_subexpression_with_operator(false, false)
.rightmost_subexpression(),
FixupContext {
rightmost_subexpression: true,
..fixup.rightmost_subexpression().leftmost_subexpression()
..fixup
.rightmost_subexpression()
.leftmost_subexpression_with_operator(false, false)
},
);
}
Expand Down

0 comments on commit 19d93ba

Please sign in to comment.