Skip to content

Commit

Permalink
fix Erasing inner attributes in struct (rust-lang#3593)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchaser53 authored and topecongiro committed May 29, 2019
1 parent 1c210eb commit fbd9f33
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
39 changes: 28 additions & 11 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub(crate) fn format_expr(
path,
fields,
base.as_ref().map(|e| &**e),
&expr.attrs,
expr.span,
shape,
),
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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
Expand All @@ -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)
))
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
13 changes: 13 additions & 0 deletions tests/target/issue-3592.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn r() -> (Biz, ()) {
(
Biz {
#![cfg(unix)]
field: 9
},
Biz {
#![cfg(not(unix))]
field: 200
},
(),
)
}

0 comments on commit fbd9f33

Please sign in to comment.