Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit fbd9f33

Browse files
rchaser53topecongiro
authored andcommitted
fix Erasing inner attributes in struct (rust-lang#3593)
1 parent 1c210eb commit fbd9f33

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

src/expr.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pub(crate) fn format_expr(
102102
path,
103103
fields,
104104
base.as_ref().map(|e| &**e),
105+
&expr.attrs,
105106
expr.span,
106107
shape,
107108
),
@@ -1570,6 +1571,7 @@ fn rewrite_struct_lit<'a>(
15701571
path: &ast::Path,
15711572
fields: &'a [ast::Field],
15721573
base: Option<&'a ast::Expr>,
1574+
attrs: &[ast::Attribute],
15731575
span: Span,
15741576
shape: Shape,
15751577
) -> Option<String> {
@@ -1664,7 +1666,8 @@ fn rewrite_struct_lit<'a>(
16641666
write_list(&item_vec, &fmt)?
16651667
};
16661668

1667-
let fields_str = wrap_struct_field(context, &fields_str, shape, v_shape, one_line_width);
1669+
let fields_str =
1670+
wrap_struct_field(context, &attrs, &fields_str, shape, v_shape, one_line_width)?;
16681671
Some(format!("{} {{{}}}", path_str, fields_str))
16691672

16701673
// FIXME if context.config.indent_style() == Visual, but we run out
@@ -1673,25 +1676,39 @@ fn rewrite_struct_lit<'a>(
16731676

16741677
pub(crate) fn wrap_struct_field(
16751678
context: &RewriteContext<'_>,
1679+
attrs: &[ast::Attribute],
16761680
fields_str: &str,
16771681
shape: Shape,
16781682
nested_shape: Shape,
16791683
one_line_width: usize,
1680-
) -> String {
1681-
if context.config.indent_style() == IndentStyle::Block
1684+
) -> Option<String> {
1685+
let should_vertical = context.config.indent_style() == IndentStyle::Block
16821686
&& (fields_str.contains('\n')
16831687
|| !context.config.struct_lit_single_line()
1684-
|| fields_str.len() > one_line_width)
1685-
{
1686-
format!(
1687-
"{}{}{}",
1688+
|| fields_str.len() > one_line_width);
1689+
1690+
let inner_attrs = &inner_attributes(attrs);
1691+
if inner_attrs.is_empty() {
1692+
if should_vertical {
1693+
Some(format!(
1694+
"{}{}{}",
1695+
nested_shape.indent.to_string_with_newline(context.config),
1696+
fields_str,
1697+
shape.indent.to_string_with_newline(context.config)
1698+
))
1699+
} else {
1700+
// One liner or visual indent.
1701+
Some(format!(" {} ", fields_str))
1702+
}
1703+
} else {
1704+
Some(format!(
1705+
"{}{}{}{}{}",
1706+
nested_shape.indent.to_string_with_newline(context.config),
1707+
inner_attrs.rewrite(context, shape)?,
16881708
nested_shape.indent.to_string_with_newline(context.config),
16891709
fields_str,
16901710
shape.indent.to_string_with_newline(context.config)
1691-
)
1692-
} else {
1693-
// One liner or visual indent.
1694-
format!(" {} ", fields_str)
1711+
))
16951712
}
16961713
}
16971714

src/patterns.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ fn rewrite_struct_pat(
226226
}
227227
}
228228

229-
let fields_str = wrap_struct_field(context, &fields_str, shape, v_shape, one_line_width);
229+
// ast::Pat doesn't have attrs so use &[]
230+
let fields_str = wrap_struct_field(context, &[], &fields_str, shape, v_shape, one_line_width)?;
230231
Some(format!("{} {{{}}}", path_str, fields_str))
231232
}
232233

tests/target/issue-3592.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn r() -> (Biz, ()) {
2+
(
3+
Biz {
4+
#![cfg(unix)]
5+
field: 9
6+
},
7+
Biz {
8+
#![cfg(not(unix))]
9+
field: 200
10+
},
11+
(),
12+
)
13+
}

0 commit comments

Comments
 (0)