about summary refs log tree commit diff
diff options
context:
space:
mode:
authorrChaser53 <tayoshizawa29@gmail.com>2019-05-30 07:31:48 +0900
committerSeiichi Uchida <seuchida@gmail.com>2019-05-30 07:31:48 +0900
commitfbd9f335f9fa53f50da89d63571117b5bfe8e65d (patch)
treef48a91d2ef9f5686654af1e395e24772fb15a695
parent1c210eb3c673cea0479ec1307a91b4b36245dfa1 (diff)
fix Erasing inner attributes in struct (#3593)
-rw-r--r--src/expr.rs39
-rw-r--r--src/patterns.rs3
-rw-r--r--tests/target/issue-3592.rs13
3 files changed, 43 insertions, 12 deletions
diff --git a/src/expr.rs b/src/expr.rs
index afd9465c73c..23628d929e8 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -102,6 +102,7 @@ pub(crate) fn format_expr(
             path,
             fields,
             base.as_ref().map(|e| &**e),
+            &expr.attrs,
             expr.span,
             shape,
         ),
@@ -1570,6 +1571,7 @@ fn rewrite_struct_lit<'a>(
     path: &ast::Path,
     fields: &'a [ast::Field],
     base: Option<&'a ast::Expr>,
+    attrs: &[ast::Attribute],
     span: Span,
     shape: Shape,
 ) -> Option<String> {
@@ -1664,7 +1666,8 @@ fn rewrite_struct_lit<'a>(
         write_list(&item_vec, &fmt)?
     };
 
-    let fields_str = wrap_struct_field(context, &fields_str, shape, v_shape, one_line_width);
+    let fields_str =
+        wrap_struct_field(context, &attrs, &fields_str, shape, v_shape, one_line_width)?;
     Some(format!("{} {{{}}}", path_str, fields_str))
 
     // FIXME if context.config.indent_style() == Visual, but we run out
@@ -1673,25 +1676,39 @@ fn rewrite_struct_lit<'a>(
 
 pub(crate) fn wrap_struct_field(
     context: &RewriteContext<'_>,
+    attrs: &[ast::Attribute],
     fields_str: &str,
     shape: Shape,
     nested_shape: Shape,
     one_line_width: usize,
-) -> String {
-    if context.config.indent_style() == IndentStyle::Block
+) -> Option<String> {
+    let should_vertical = context.config.indent_style() == IndentStyle::Block
         && (fields_str.contains('\n')
             || !context.config.struct_lit_single_line()
-            || fields_str.len() > one_line_width)
-    {
-        format!(
-            "{}{}{}",
+            || fields_str.len() > one_line_width);
+
+    let inner_attrs = &inner_attributes(attrs);
+    if inner_attrs.is_empty() {
+        if should_vertical {
+            Some(format!(
+                "{}{}{}",
+                nested_shape.indent.to_string_with_newline(context.config),
+                fields_str,
+                shape.indent.to_string_with_newline(context.config)
+            ))
+        } else {
+            // One liner or visual indent.
+            Some(format!(" {} ", fields_str))
+        }
+    } else {
+        Some(format!(
+            "{}{}{}{}{}",
+            nested_shape.indent.to_string_with_newline(context.config),
+            inner_attrs.rewrite(context, shape)?,
             nested_shape.indent.to_string_with_newline(context.config),
             fields_str,
             shape.indent.to_string_with_newline(context.config)
-        )
-    } else {
-        // One liner or visual indent.
-        format!(" {} ", fields_str)
+        ))
     }
 }
 
diff --git a/src/patterns.rs b/src/patterns.rs
index 73ff2c0f7ab..c26451f6d69 100644
--- a/src/patterns.rs
+++ b/src/patterns.rs
@@ -226,7 +226,8 @@ fn rewrite_struct_pat(
         }
     }
 
-    let fields_str = wrap_struct_field(context, &fields_str, shape, v_shape, one_line_width);
+    // ast::Pat doesn't have attrs so use &[]
+    let fields_str = wrap_struct_field(context, &[], &fields_str, shape, v_shape, one_line_width)?;
     Some(format!("{} {{{}}}", path_str, fields_str))
 }
 
diff --git a/tests/target/issue-3592.rs b/tests/target/issue-3592.rs
new file mode 100644
index 00000000000..3142268e081
--- /dev/null
+++ b/tests/target/issue-3592.rs
@@ -0,0 +1,13 @@
+fn r() -> (Biz, ()) {
+    (
+        Biz {
+            #![cfg(unix)]
+            field: 9
+        },
+        Biz {
+            #![cfg(not(unix))]
+            field: 200
+        },
+        (),
+    )
+}