Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve FixupContext when printing closure body without brace #1825

Merged
merged 1 commit into from
Jan 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3218,7 +3218,7 @@ pub(crate) mod printing {
Expr::Call(e) => print_expr_call(e, tokens, fixup),
Expr::Cast(e) => print_expr_cast(e, tokens, fixup),
#[cfg(feature = "full")]
Expr::Closure(e) => e.to_tokens(tokens),
Expr::Closure(e) => print_expr_closure(e, tokens, fixup),
#[cfg(feature = "full")]
Expr::Const(e) => e.to_tokens(tokens),
#[cfg(feature = "full")]
Expand Down Expand Up @@ -3485,25 +3485,30 @@ pub(crate) mod printing {
#[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
impl ToTokens for ExprClosure {
fn to_tokens(&self, tokens: &mut TokenStream) {
outer_attrs_to_tokens(&self.attrs, tokens);
self.lifetimes.to_tokens(tokens);
self.constness.to_tokens(tokens);
self.movability.to_tokens(tokens);
self.asyncness.to_tokens(tokens);
self.capture.to_tokens(tokens);
self.or1_token.to_tokens(tokens);
self.inputs.to_tokens(tokens);
self.or2_token.to_tokens(tokens);
self.output.to_tokens(tokens);
if matches!(self.output, ReturnType::Default)
|| matches!(&*self.body, Expr::Block(body) if body.attrs.is_empty() && body.label.is_none())
{
self.body.to_tokens(tokens);
} else {
token::Brace::default().surround(tokens, |tokens| {
print_expr(&self.body, tokens, FixupContext::new_stmt());
});
}
print_expr_closure(self, tokens, FixupContext::NONE);
}
}

#[cfg(feature = "full")]
fn print_expr_closure(e: &ExprClosure, tokens: &mut TokenStream, fixup: FixupContext) {
outer_attrs_to_tokens(&e.attrs, tokens);
e.lifetimes.to_tokens(tokens);
e.constness.to_tokens(tokens);
e.movability.to_tokens(tokens);
e.asyncness.to_tokens(tokens);
e.capture.to_tokens(tokens);
e.or1_token.to_tokens(tokens);
e.inputs.to_tokens(tokens);
e.or2_token.to_tokens(tokens);
e.output.to_tokens(tokens);
if matches!(e.output, ReturnType::Default)
|| matches!(&*e.body, Expr::Block(body) if body.attrs.is_empty() && body.label.is_none())
{
print_expr(&e.body, tokens, fixup.rightmost_subexpression());
} else {
token::Brace::default().surround(tokens, |tokens| {
print_expr(&e.body, tokens, FixupContext::new_stmt());
});
}
}

Expand Down
Loading