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
This commit is contained in:
Ayaz Hafiz 2020-08-15 13:34:06 -07:00 committed by Caleb Cartwright
parent dd32de74ce
commit ce13ff15c3
2 changed files with 7 additions and 12 deletions

View File

@ -133,6 +133,7 @@ fn veto_block(e: &ast::Expr) -> bool {
}
// Rewrite closure with a single expression wrapping its body with block.
// || { #[attr] foo() } -> Block { #[attr] foo() }
fn rewrite_closure_with_block(
body: &ast::Expr,
prefix: &str,
@ -154,8 +155,12 @@ fn rewrite_closure_with_block(
}],
id: ast::NodeId::root(),
rules: ast::BlockCheckMode::Default,
span: body.span,
tokens: None,
span: body
.attrs
.first()
.map(|attr| attr.span.to(body.span))
.unwrap_or(body.span),
};
let block = crate::expr::rewrite_block_with_visitor(
context,

View File

@ -528,17 +528,7 @@ pub(crate) fn rewrite_block_with_visitor(
let open_pos = snippet.find_uncommented("{")?;
visitor.last_pos = block.span.lo() + BytePos(open_pos as u32)
}
(ast::BlockCheckMode::Default, None) => {
visitor.last_pos = block.span.lo();
if let Some(attrs) = attrs {
if let Some(first) = attrs.first() {
let first_lo_span = first.span.lo();
if first_lo_span < visitor.last_pos {
visitor.last_pos = first_lo_span;
}
}
}
}
(ast::BlockCheckMode::Default, None) => visitor.last_pos = block.span.lo(),
}
let inner_attrs = attrs.map(inner_attributes);