Skip to content

Commit 1de4858

Browse files
ayazhafizcalebcartwright
authored andcommitted
Correctly create artificial span for formatting closure body
This commit partially reverts #3934, opting to create a span that covers the entire body of a closure when formatting a closure body with a block-formatting strategy, rather than having the block-formatting code determine if the visitor pointer should be rewound. The problem with rewinding the visitor pointer is it may be incorrect for other (i.e. non-artificial) AST nodes, as in the case of #4382. Closes #4382
1 parent 688e472 commit 1de4858

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

Diff for: src/formatting/closures.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ fn veto_block(e: &ast::Expr) -> bool {
140140
}
141141

142142
// Rewrite closure with a single expression wrapping its body with block.
143+
// || { #[attr] foo() } -> Block { #[attr] foo() }
143144
fn rewrite_closure_with_block(
144145
body: &ast::Expr,
145146
prefix: &str,
@@ -160,7 +161,11 @@ fn rewrite_closure_with_block(
160161
}],
161162
id: ast::NodeId::root(),
162163
rules: ast::BlockCheckMode::Default,
163-
span: body.span,
164+
span: body
165+
.attrs
166+
.first()
167+
.map(|attr| attr.span.to(body.span))
168+
.unwrap_or(body.span),
164169
};
165170
let block =
166171
rewrite_block_with_visitor(context, "", &block, Some(&body.attrs), None, shape, false)?;

Diff for: src/formatting/expr.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -525,17 +525,7 @@ pub(crate) fn rewrite_block_with_visitor(
525525
let open_pos = snippet.find_uncommented("{")?;
526526
visitor.last_pos = block.span.lo() + BytePos(open_pos as u32)
527527
}
528-
(ast::BlockCheckMode::Default, None) => {
529-
visitor.last_pos = block.span.lo();
530-
if let Some(attrs) = attrs {
531-
if let Some(first) = attrs.first() {
532-
let first_lo_span = first.span.lo();
533-
if first_lo_span < visitor.last_pos {
534-
visitor.last_pos = first_lo_span;
535-
}
536-
}
537-
}
538-
}
528+
(ast::BlockCheckMode::Default, None) => visitor.last_pos = block.span.lo(),
539529
}
540530

541531
let inner_attrs = attrs.map(inner_attributes);

0 commit comments

Comments
 (0)