From beb37b0dcbd7ca886c6533f5e2a7d637d951780a Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Mon, 10 Feb 2025 20:48:24 +0900 Subject: [PATCH 1/8] chore: add rewrite_last_closure log similar to rewrite_closure --- src/closures.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/closures.rs b/src/closures.rs index 1f59fbc2960..182eb6e3865 100644 --- a/src/closures.rs +++ b/src/closures.rs @@ -353,6 +353,8 @@ pub(crate) fn rewrite_last_closure( expr: &ast::Expr, shape: Shape, ) -> RewriteResult { + debug!("rewrite_last_closure {:?}", expr); + if let ast::ExprKind::Closure(ref closure) = expr.kind { let ast::Closure { ref binder, From 3df823283201b703f70acb5612bb9d5d36a76961 Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Mon, 10 Feb 2025 21:03:30 +0900 Subject: [PATCH 2/8] fix: labels on the closure body can be disappearing --- src/closures.rs | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/src/closures.rs b/src/closures.rs index 182eb6e3865..8dc4bc867a1 100644 --- a/src/closures.rs +++ b/src/closures.rs @@ -1,4 +1,4 @@ -use rustc_ast::{ast, ptr}; +use rustc_ast::{Label, ast, ptr}; use rustc_span::Span; use thin_vec::thin_vec; use tracing::debug; @@ -55,7 +55,7 @@ pub(crate) fn rewrite_closure( // 1 = space between `|...|` and body. let body_shape = shape.offset_left(extra_offset, span)?; - if let ast::ExprKind::Block(ref block, _) = body.kind { + if let ast::ExprKind::Block(ref block, ref label) = body.kind { // The body of the closure is an empty block. if block.stmts.is_empty() && !block_contains_comment(context, block) { return body @@ -72,7 +72,7 @@ pub(crate) fn rewrite_closure( result.or_else(|_| { // Either we require a block, or tried without and failed. - rewrite_closure_block(block, &prefix, context, body_shape) + rewrite_closure_block(block, label, &prefix, context, body_shape) }) } else { rewrite_closure_expr(body, &prefix, context, body_shape).or_else(|_| { @@ -104,8 +104,8 @@ fn get_inner_expr<'a>( prefix: &str, context: &RewriteContext<'_>, ) -> &'a ast::Expr { - if let ast::ExprKind::Block(ref block, _) = expr.kind { - if !needs_block(block, prefix, context) { + if let ast::ExprKind::Block(ref block, ref label) = expr.kind { + if !needs_block(block, label, prefix, context) { // block.stmts.len() == 1 except with `|| {{}}`; // https://github.com/rust-lang/rustfmt/issues/3844 if let Some(expr) = block.stmts.first().and_then(stmt_expr) { @@ -118,7 +118,12 @@ fn get_inner_expr<'a>( } // Figure out if a block is necessary. -fn needs_block(block: &ast::Block, prefix: &str, context: &RewriteContext<'_>) -> bool { +fn needs_block( + block: &ast::Block, + label: &Option