Skip to content

Make sure we don't lose default struct value when formatting struct #134668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/tools/rustfmt/src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1944,6 +1944,11 @@ pub(crate) fn rewrite_struct_field(
shape: Shape,
lhs_max_width: usize,
) -> RewriteResult {
// FIXME(default_field_values): Implement formatting.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also should go above the contains_skip since that would mess up:

struct Foo2 {
    #[rustfmt::skip]
    default_field:    Spacing =    /* uwu */ 0,
}

which I added a test for.

if field.default.is_some() {
return Err(RewriteError::Unknown);
}

if contains_skip(&field.attrs) {
return Ok(context.snippet(field.span()).to_owned());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to change this to return Err(RewriteError::Unknown); for now. I don't think the field.span() contains the default values. At least I still saw the error reported in rust-lang/rustfmt#6424 when I built rustfmt from this branch and ran it on

frame_support::construct_runtime!(
    pub struct Test {
        System: frame_system = 0,
        SelfDomainId: pallet_domain_id = 1,
    }
);

If possible, could you also include the above snippet in the test case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, the field span doesn't include the optional const. I just didn't actually re-test my test.

I'm not going to include the snippet because it's identical in behavior to the snippet I included, though. I'll adjust the behavior though, and make sure to re-test it.

}
Expand Down
1 change: 1 addition & 0 deletions src/tools/rustfmt/src/spanned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ impl Spanned for ast::GenericParam {

impl Spanned for ast::FieldDef {
fn span(&self) -> Span {
// FIXME(default_field_values): This needs to be adjusted.
span_with_attrs_lo_hi!(self, self.span.lo(), self.ty.span.hi())
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/tools/rustfmt/tests/source/default-field-values.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![feature(default_struct_values)]

// Test for now that nightly default field values are left alone for now.

struct Foo {
default_field: Spacing = /* uwu */ 0,
}

struct Foo2 {
#[rustfmt::skip]
default_field: Spacing = /* uwu */ 0,
}

a_macro!(
struct Foo2 {
default_field: Spacing = /* uwu */ 0,
}
);
18 changes: 18 additions & 0 deletions src/tools/rustfmt/tests/target/default-field-values.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![feature(default_struct_values)]

// Test for now that nightly default field values are left alone for now.

struct Foo {
default_field: Spacing = /* uwu */ 0,
}

struct Foo2 {
#[rustfmt::skip]
default_field: Spacing = /* uwu */ 0,
}

a_macro!(
struct Foo2 {
default_field: Spacing = /* uwu */ 0,
}
);
Loading