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

Fix formatting of comments in empty structs #5171

Conversation

Szymongib
Copy link
Contributor

Currently, comments in empty structs are formatted weirdly. This PR attempts to fix it by indenting all one-line comments to the same one-depth indentation.

Examples can be found in the issue and tests.

Fixes: #4854

Reopen of #4875 due to branch changes (apologies for inconveniences).

@Szymongib
Copy link
Contributor Author

@ytmimi I have made the changes you suggested in this PR, sorry for the inconvenience I did not realize that branches changed.

@ytmimi
Copy link
Contributor

ytmimi commented Jan 14, 2022

No inconvenience at all! Thank you very much for the work you put into your original solution, making these suggested changes, and for updating all those tests!

Everything looks good to me and I'm happy to move forward.

@calebcartwright
Copy link
Member

Thanks for the quick turn around! Would you mind adding a few more tests?

@Szymongib
Copy link
Contributor Author

Ah sorry @calebcartwright I thought I had the original commit with tests but probably lost it when rebasing and changing brnach. Added them now.

So formating of block comments changed from:

struct Foo { 
    /* bar */
}

to

struct Foo {/* bar */}

Let me know if that is ok.

@ytmimi
Copy link
Contributor

ytmimi commented Jan 18, 2022

@Szymongib I believe that's the correct behavior! However, I do think there's an issue with some of the examples that include multi line block comments. Here are the examples you have:

struct Struct5 {
    /*
    Comment block
    with many lines.
    */}

struct Struct6(
    /*
    Comment block
    with many lines.
    */);

struct Struct7 {
    /*
    Invalid
    format
    */}

struct Struct8(
    /*
    Invalid
    format
    */);

struct Struct10 {
    /* bar
    baz
    */}

And here's what I think the issue is; On line 1380 we check if we need to push an indented newline before adding the content of the string, however on line 1387 before deciding to add a newline we do a similar but slightly different check.

rustfmt/src/items.rs

Lines 1380 to 1390 in 5056f4c

if !is_single_line(s) || first_line_contains_single_line_comment(s) {
let nested_indent_str = offset
.block_indent(context.config)
.to_string_with_newline(context.config);
result.push_str(&nested_indent_str);
}
result.push_str(s);
if last_line_contains_single_line_comment(s) {
result.push_str(&offset.to_string_with_newline(context.config));
}
}

I think if we updated Line 1387 to include the !is_single_line (multi-line) check it would fix the issue that we're seeing and move the closing bracket to the right spot. I had something like this in mind:

+      let is_multi_line = !is_single_line(s);
+      if is_multi_line || first_line_contains_single_line_comment(s) {
-      if !is_single_line(s) || first_line_contains_single_line_comment(s) 
          let nested_indent_str = offset 
              .block_indent(context.config) 
              .to_string_with_newline(context.config); 
          result.push_str(&nested_indent_str); 
      } 
      result.push_str(s);
+      if is_multi_line || last_line_contains_single_line_comment(s)
-      if last_line_contains_single_line_comment(s) { 
          result.push_str(&offset.to_string_with_newline(context.config)); 
      } 
  } 

@calebcartwright
Copy link
Member

Let me know if that is ok.

We actually cannot make such a "breaking" formatting change, at least not without version gating it. We should try to find a way of preserving this type of formatting (comment on next line), but if not it will have to be gated. We've got some other code (or at least PRs) with examples of maintaining that type of comment behavior that I'm sure we could dig up as reference info

@ytmimi
Copy link
Contributor

ytmimi commented Jan 19, 2022

@calebcartwright correct me if my understanding is off, but your saying that we can't format single line block comments like this?

struct Foo {                            struct Foo {/* bar */}
    /* bar */        ---------->
}

I just checked and on the current master rustfmt 1.4.38-nightly (5056f4cf 2022-01-05) that's how things are formatted, so this PR doesn't introduce that change.

Is my comment about the closing brace also something that would be considered breaking?

@Szymongib
Copy link
Contributor Author

Thanks @ytmimi.

Indeed master version does format single-line comments like that, so this behavior does not change.

struct Foo {/* bar */}

@calebcartwright
Copy link
Member

My apologies as it seems I misunderstood, thank you both! So long as the bug fix doesn't introduce any idempotency issues for existing "valid" (see, non-buggy indentation) formatting it's fine

Copy link
Member

@calebcartwright calebcartwright left a comment

Choose a reason for hiding this comment

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

One minor question inline re: existing tests but otherwise LGTM and ready to merge

Thanks so much for sticking with this one @Szymongib, it will be a solid first contribution!

Comment on lines 16 to 17
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
// Expand as needed, numbers should be ascending according to the stage
// through the inclusion pipeline, or according to the descriptions
// Expand as needed, numbers should be ascending according to the stage
// through the inclusion pipeline, or according to the descriptions
Copy link
Member

Choose a reason for hiding this comment

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

Was it necessary to modify these files? It's typically a flag when we have to change existing inputs to tests, but I'm guessing this isn't actually necessary, perhaps wasn't even explicitly intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah sorry, I just thought we might want to remove the // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed part of the comment as the issue should be addressed with the PR. It makes sense to leave it as it is tho, so I will revert that change.

Copy link
Contributor

Choose a reason for hiding this comment

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

@calebcartwright are you trying to highlight that this file is the source file and that we don't need to update the formatting since it'll get checked against the corresponding target? Would we not want to remove the comment about issue 4854 to not confuse anyone down the line about whether or not the issue is fixed?

Copy link
Member

Choose a reason for hiding this comment

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

I was really just speaking to changing of the indentation, I actually hadn't read the the comment text word for word. I'm okay with removing that first line though would prefer to defer that to a follow up PR if anyone is interested given how long this PR (and it's predecessor) have been basically ready, but would still be good in that potential follow up to leave the source contents with incorrect indentation

@calebcartwright
Copy link
Member

Thanks again!

@calebcartwright calebcartwright merged commit b4a4bf0 into rust-lang:master Jan 29, 2022
@Szymongib Szymongib deleted the fix/fix-formating-of-comments-in-empty-structs branch January 29, 2022 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Comments after the first one in empty struct are not formatted nicely
3 participants