Skip to content
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

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

compiler-errors
Copy link
Member

The reason why rust-lang/rustfmt#6424 broke when #129514 landed is because the parser now successfully parses default struct values. That means that where we used to fail in rewrite_macro_inner:

let ParsedMacroArgs {
args: arg_vec,
vec_with_semi,
trailing_comma,
} = match parse_macro_args(context, ts, style, is_forced_bracket) {

... 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

@rustbot
Copy link
Collaborator

rustbot commented Dec 22, 2024

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 22, 2024
Comment on lines 1951 to 1954
if field.default.is_some() {
return Err(RewriteError::SkipFormatting);
}
Copy link
Contributor

@ytmimi ytmimi Dec 22, 2024

Choose a reason for hiding this comment

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

RewriteError::SkipFormatting is used to represent #[rustfmt::skip]. I think in this case we could just return Ok(context.snippet(field.span()).to_owned()); (assuming that the span contains the default), or Err(RewriteError::Unknown. At some point I want to add a RewriteError variant for unstable features that don't have defined formatting yet.

@@ -1947,6 +1947,10 @@ pub(crate) fn rewrite_struct_field(
if contains_skip(&field.attrs) {
return Ok(context.snippet(field.span()).to_owned());
}
// FIXME(default_field_values): Implement formatting.
if field.default.is_some() {
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.

@rust-log-analyzer

This comment has been minimized.

@compiler-errors compiler-errors force-pushed the default-struct-value-fmt branch from 3a5362c to 68c46e1 Compare December 22, 2024 23:55
@@ -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.

Copy link
Contributor

@ytmimi ytmimi left a comment

Choose a reason for hiding this comment

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

Good catch on the #[rustfmt::skip] case, and thanks for adding the #![feature(default_struct_values)]. I think we're ready to go here.

@ytmimi
Copy link
Contributor

ytmimi commented Dec 23, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Dec 23, 2024

@ytmimi: 🔑 Insufficient privileges: Not in reviewers

@compiler-errors
Copy link
Member Author

@bors r=ytmimi

@bors
Copy link
Contributor

bors commented Dec 23, 2024

📌 Commit 68c46e1 has been approved by ytmimi

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 23, 2024
tgross35 added a commit to tgross35/rust that referenced this pull request Dec 23, 2024
…ue-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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants