Skip to content

Commit ce13ff1

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 dd32de7 commit ce13ff1

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

Diff for: src/closures.rs

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

135135
// Rewrite closure with a single expression wrapping its body with block.
136+
// || { #[attr] foo() } -> Block { #[attr] foo() }
136137
fn rewrite_closure_with_block(
137138
body: &ast::Expr,
138139
prefix: &str,
@@ -154,8 +155,12 @@ fn rewrite_closure_with_block(
154155
}],
155156
id: ast::NodeId::root(),
156157
rules: ast::BlockCheckMode::Default,
157-
span: body.span,
158158
tokens: None,
159+
span: body
160+
.attrs
161+
.first()
162+
.map(|attr| attr.span.to(body.span))
163+
.unwrap_or(body.span),
159164
};
160165
let block = crate::expr::rewrite_block_with_visitor(
161166
context,

Diff for: src/expr.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -528,17 +528,7 @@ pub(crate) fn rewrite_block_with_visitor(
528528
let open_pos = snippet.find_uncommented("{")?;
529529
visitor.last_pos = block.span.lo() + BytePos(open_pos as u32)
530530
}
531-
(ast::BlockCheckMode::Default, None) => {
532-
visitor.last_pos = block.span.lo();
533-
if let Some(attrs) = attrs {
534-
if let Some(first) = attrs.first() {
535-
let first_lo_span = first.span.lo();
536-
if first_lo_span < visitor.last_pos {
537-
visitor.last_pos = first_lo_span;
538-
}
539-
}
540-
}
541-
}
531+
(ast::BlockCheckMode::Default, None) => visitor.last_pos = block.span.lo(),
542532
}
543533

544534
let inner_attrs = attrs.map(inner_attributes);

0 commit comments

Comments
 (0)