Skip to content

Commit 68c46e1

Browse files
Make sure we don't lose default struct value when formatting struct
1 parent b22856d commit 68c46e1

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

src/tools/rustfmt/src/items.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1944,6 +1944,11 @@ pub(crate) fn rewrite_struct_field(
19441944
shape: Shape,
19451945
lhs_max_width: usize,
19461946
) -> RewriteResult {
1947+
// FIXME(default_field_values): Implement formatting.
1948+
if field.default.is_some() {
1949+
return Err(RewriteError::Unknown);
1950+
}
1951+
19471952
if contains_skip(&field.attrs) {
19481953
return Ok(context.snippet(field.span()).to_owned());
19491954
}

src/tools/rustfmt/src/spanned.rs

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ impl Spanned for ast::GenericParam {
144144

145145
impl Spanned for ast::FieldDef {
146146
fn span(&self) -> Span {
147+
// FIXME(default_field_values): This needs to be adjusted.
147148
span_with_attrs_lo_hi!(self, self.span.lo(), self.ty.span.hi())
148149
}
149150
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![feature(default_struct_values)]
2+
3+
// Test for now that nightly default field values are left alone for now.
4+
5+
struct Foo {
6+
default_field: Spacing = /* uwu */ 0,
7+
}
8+
9+
struct Foo2 {
10+
#[rustfmt::skip]
11+
default_field: Spacing = /* uwu */ 0,
12+
}
13+
14+
a_macro!(
15+
struct Foo2 {
16+
default_field: Spacing = /* uwu */ 0,
17+
}
18+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![feature(default_struct_values)]
2+
3+
// Test for now that nightly default field values are left alone for now.
4+
5+
struct Foo {
6+
default_field: Spacing = /* uwu */ 0,
7+
}
8+
9+
struct Foo2 {
10+
#[rustfmt::skip]
11+
default_field: Spacing = /* uwu */ 0,
12+
}
13+
14+
a_macro!(
15+
struct Foo2 {
16+
default_field: Spacing = /* uwu */ 0,
17+
}
18+
);

0 commit comments

Comments
 (0)