From 5619b64461f6bb1ad9e2bfa6584736e4aab1f379 Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Thu, 13 Feb 2025 01:51:40 +0900 Subject: [PATCH] Don't remove block label from closure body (#6468) --- src/closures.rs | 30 ++-- tests/source/closure-block-labels.rs | 192 +++++++++++++++++++++++++ tests/target/closure-block-labels.rs | 204 +++++++++++++++++++++++++++ 3 files changed, 418 insertions(+), 8 deletions(-) create mode 100644 tests/source/closure-block-labels.rs create mode 100644 tests/target/closure-block-labels.rs diff --git a/src/closures.rs b/src/closures.rs index 1f59fbc2960..296b7407e40 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; @@ -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(body, &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