rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
5.85k stars 860 forks source link

Rustfmt misaligns doc comment between rustfmt::skip modules #6176

Open dtolnay opened 1 month ago

dtolnay commented 1 month ago
mod modules {
    #[rustfmt::skip]
    #[path = "/dev/null"]
    mod first;

    /// ...
    /// ...
    #[rustfmt::skip]
    #[path = "/dev/null"]
    mod second;
}

Running rustfmt on this file turns it into:

mod modules {
    #[rustfmt::skip]
    #[path = "/dev/null"]
    mod first; /// ...
               /// ...
    #[rustfmt::skip]
    #[path = "/dev/null"]
    mod second;
}
<     mod first;
<
<     /// ...
<     /// ...
---
>     mod first; /// ...
>                /// ...

Observations:

dtolnay commented 1 month ago

This currently affects syn as of https://github.com/dtolnay/syn/tree/2.0.66. Running cargo fmt in that repo produces the incorrect diff seen in https://github.com/dtolnay/syn/pull/1672.

The oldest version of rustfmt that exhibits this issue is the one that shipped with Rust 1.38; rustfmt in older Rust releases leaves the original code unmodified.

dtolnay commented 1 month ago

According to bisect, this regressed between nightly-2019-07-25 and nightly-2019-07-31 (the dates in between are missing the rustfmt component). The only PR touching rust-lang/rust's src/tools/rustfmt in that range is https://github.com/rust-lang/rust/pull/62805, so the relevant rustfmt commit range is https://github.com/rust-lang/rustfmt/compare/66c27c9161b2aa70c2902807be12952bd4a0a62b...9e960e7d6a0c6b7c46c195acbd6f92ade6eec3ba.

https://github.com/rust-lang/rustfmt/pull/3691 looks suspiciously relevant based on these changes it causes to some of the tests.

image