From b2a6936d00d51bf8b5adfe548c89559851a6f460 Mon Sep 17 00:00:00 2001 From: Ayaz Hafiz Date: Sat, 15 Aug 2020 13:34:06 -0700 Subject: [PATCH 1/2] 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 --- src/formatting/closures.rs | 7 ++++++- src/formatting/expr.rs | 12 +----------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/formatting/closures.rs b/src/formatting/closures.rs index 7dad434a7f3..37ea9a27918 100644 --- a/src/formatting/closures.rs +++ b/src/formatting/closures.rs @@ -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, @@ -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)?; diff --git a/src/formatting/expr.rs b/src/formatting/expr.rs index eff86f54cb3..25d13b14c9c 100644 --- a/src/formatting/expr.rs +++ b/src/formatting/expr.rs @@ -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); From 331091f43a517faa7b4379bfb032396d0a48cbc6 Mon Sep 17 00:00:00 2001 From: Ayaz Hafiz Date: Sat, 15 Aug 2020 13:38:06 -0700 Subject: [PATCH 2/2] fixup! Correctly create artificial span for formatting closure body --- tests/source/issue-4382.rs | 4 ++++ tests/target/issue-4382.rs | 10 ++++++++++ 2 files changed, 14 insertions(+) create mode 100644 tests/source/issue-4382.rs create mode 100644 tests/target/issue-4382.rs diff --git a/tests/source/issue-4382.rs b/tests/source/issue-4382.rs new file mode 100644 index 00000000000..cbf0c4ed6d4 --- /dev/null +++ b/tests/source/issue-4382.rs @@ -0,0 +1,4 @@ +pub const NAME_MAX: usize = { + #[cfg(target_os = "linux")] { 1024 } + #[cfg(target_os = "freebsd")] { 255 } +}; diff --git a/tests/target/issue-4382.rs b/tests/target/issue-4382.rs new file mode 100644 index 00000000000..740fa9bfe0a --- /dev/null +++ b/tests/target/issue-4382.rs @@ -0,0 +1,10 @@ +pub const NAME_MAX: usize = { + #[cfg(target_os = "linux")] + { + 1024 + } + #[cfg(target_os = "freebsd")] + { + 255 + } +};