Skip to content

Commit c9c90dc

Browse files
authored
Rollup merge of rust-lang#134668 - compiler-errors:default-struct-value-fmt, r=ytmimi
Make sure we don't lose default struct value when formatting struct The reason why rust-lang/rustfmt#6424 broke when rust-lang#129514 landed is because the parser now *successfully* parses default struct values. That means that where we used to fail in `rewrite_macro_inner`: https://github.com/rust-lang/rust/blob/e108481f74ff123ad98a63bd107a18d13035b275/src/tools/rustfmt/src/macros.rs#L263-L267 ... we now succeed, and we now proceed to format the inner struct as a macro arg. Since formatting was never implemented for default struct values, this means that we’ll always rewrite the struct to exclude the default value. This PR makes it so that we simply don’t rewrite struct fields if they have a default value. r? `@ytmimi`
2 parents fde85a8 + 68c46e1 commit c9c90dc

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)