Skip to content

Correctly create artificial span for formatting closure body #4387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/formatting/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,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,
Expand All @@ -160,7 +161,11 @@ fn rewrite_closure_with_block(
}],
id: ast::NodeId::root(),
rules: ast::BlockCheckMode::Default,
span: body.span,
span: body
.attrs
.first()
.map(|attr| attr.span.to(body.span))
.unwrap_or(body.span),
};
let block =
rewrite_block_with_visitor(context, "", &block, Some(&body.attrs), None, shape, false)?;
Expand Down
12 changes: 1 addition & 11 deletions src/formatting/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,17 +525,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);
Expand Down
4 changes: 4 additions & 0 deletions tests/source/issue-4382.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub const NAME_MAX: usize = {
#[cfg(target_os = "linux")] { 1024 }
#[cfg(target_os = "freebsd")] { 255 }
};
10 changes: 10 additions & 0 deletions tests/target/issue-4382.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub const NAME_MAX: usize = {
#[cfg(target_os = "linux")]
{
1024
}
#[cfg(target_os = "freebsd")]
{
255
}
};