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

rustfmt removes comment between doc comment and generic params #5320

Closed
ytmimi opened this issue Apr 24, 2022 · 6 comments · Fixed by #5780
Closed

rustfmt removes comment between doc comment and generic params #5320

ytmimi opened this issue Apr 24, 2022 · 6 comments · Fixed by #5780

Comments

@ytmimi
Copy link
Contributor

ytmimi commented Apr 24, 2022

This was an issue discovered when implementing a fix for #5122 in PR #5124 (comment). The issue is reproducible on rustfmt 1.4.38-nightly (8572b343 2022-04-24) and uses the rustfmt.toml defined in the rustfmt repo.

# rustfmt.toml
error_on_line_overflow = true
error_on_unformatted = true
version = "Two"

This input:

// Non-doc pre-comment of Foo
/// doc of Foo
// Non-doc post-comment of Foo
struct Foo<
    // Non-doc pre-comment of 'a
    /// doc of 'a
    // Non-doc post-comment of 'a
    'a,
    // Non-doc pre-comment of T
    /// doc of T
    // Non-doc post-comment of T
    T,
    // Non-doc pre-comment of N
    /// doc of N
    // Non-doc post-comment of N
    const N: item,
>;

Gets formatted as follows:

// Non-doc pre-comment of Foo
/// doc of Foo
// Non-doc post-comment of Foo
struct Foo<
    // Non-doc pre-comment of 'a
    /// doc of 'a
    'a,
    // Non-doc pre-comment of T
    /// doc of T
    T,
    // Non-doc pre-comment of N
    /// doc of N
    const N: item,
>;
@ytmimi
Copy link
Contributor Author

ytmimi commented Apr 24, 2022

#5215 is somewhat related. It mostly deals with not properly adding a newline between comments and struct fields when there is a doc comment preceding the item instead of dropping the comment altogether, but I imagine the we'll need to take a similar approach when recovering the comments to prevent the issue described in #5215.

@calebcartwright
Copy link
Member

Thanks for capturing this so we don't lose track!

@ytmimi
Copy link
Contributor Author

ytmimi commented Apr 24, 2022

Just a happy coincidence that I came across this as I was making changes to #5305, and peeking into various issues / PRs to see if I could reword any of the CHANGELOG entries.

@ytmimi ytmimi changed the title Remove comment between doc comment and generic params rustfmt removes comment between doc comment and generic params Apr 25, 2022
@Rudxain
Copy link

Rudxain commented Dec 22, 2022

Related bug (inline block comment):

in

pub struct Foo;
impl /*core*/std::error::Error for Foo {}

out

pub struct Foo;
impl std::error::Error for Foo {}

Rudxain added a commit to Rudxain/SV3G that referenced this issue Dec 23, 2022
@ytmimi
Copy link
Contributor Author

ytmimi commented Dec 23, 2022

@Rudxain thanks for reaching out! There are several places where rustfmt isn't expected to find comments and doesn't try to recover them when reformatting your code. Between the impl keyword and the trait name is one of them. Although you're observing a similar issue it's happening in a different context so it's unfortunately unrelated to this issue.

Feel free to open up a separate issue to cover the case you're running into.

@Rudxain
Copy link

Rudxain commented Dec 23, 2022

There are several places where rustfmt isn't expected to find comments and doesn't try to recover them when reformatting your code.

Oh. I didn't expect that 😅

Feel free to open up a separate issue to cover the case you're running into.

I'll probably do that. I commented /*core*/ to remind myself to replace std by core when core Errors get stabilized

BTW, thanks for the info!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants