rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

Rustfmt deletes comments in first position of trait bound

Open sffc opened this issue 1 year ago • 4 comments

Example code:

    pub fn try_new_with_date_length_unstable<P>(
        provider: &P,
        locale: &DataLocale,
        length: length::Date,
    ) -> Result<Self, Error>
    where
        P: // Explanatory comment about the bounds
            ?Sized
            + DataProvider<BuddhistDatePatternV1Marker>
            + DataProvider<BuddhistYearNamesV1Marker>
            + DataProvider<BuddhistMonthNamesV1Marker>
    { ... }

Running this through rustfmt deletes the // Explanatory comment about the bounds.

Workaround: move the ?Sized into the first position. If your bound is not ?Sized, you can use Sized. The comment is retained:

    pub fn try_new_with_date_length_unstable<P>(
        provider: &P,
        locale: &DataLocale,
        length: length::Date,
    ) -> Result<Self, Error>
    where
        P: ?Sized
            // Explanatory comment about the bounds
            + DataProvider<BuddhistDatePatternV1Marker>
            + DataProvider<BuddhistYearNamesV1Marker>
            + DataProvider<BuddhistMonthNamesV1Marker>
    { ... }

Possibly related to https://github.com/rust-lang/rustfmt/issues/3669, https://github.com/rust-lang/rustfmt/pull/4666, https://github.com/rust-lang/rustfmt/pull/5059 but the examples are in different contexts so I thought I would file a new issue with this test case.

CC @Manishearth

sffc avatar Jan 31 '24 23:01 sffc

I just hit this:

diff --git a/tests/rustdoc/rfc-2632-const-trait-impl.rs b/tests/rustdoc/rfc-2632-const-trait-impl.rs
index 6f264969e54..0fec059f951 100644
--- a/tests/rustdoc/rfc-2632-const-trait-impl.rs
+++ b/tests/rustdoc/rfc-2632-const-trait-impl.rs
@@ -24,9 +24,9 @@ pub trait Tr<T> {
     // @has - '//section[@id="method.a"]/h4[@class="code-header"]/a[@class="trait"]' 'Fn'
     // @!has - '//section[@id="method.a"]/h4[@class="code-header"]/span[@class="where"]' '~const'
     // @has - '//section[@id="method.a"]/h4[@class="code-header"]/div[@class="where"]' ': Fn'
-    fn a<A: /* ~const */ Fn() + ~const Destruct>()
+    fn a<A: Fn() + ~const Destruct>()
     where
-        Option<A>: /* ~const */ Fn() + ~const Destruct,
+        Option<A>: Fn() + ~const Destruct,
     {
     }
 }

I have a comment removed in both trait bounds: within the generic params, and in the where clause.

nnethercote avatar Jun 03 '24 22:06 nnethercote

Another repro:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f590ab120d35f6aa1d296e9da5ce9d0f

@rustbot label +C-bug @rustbot label +p-high

Not sure Rustbot will let me set the p-high label, but I feel I should try. After all this is a case where rustfmt corrupts the code, lossily, in a way that is unlikely to be detected.

ijackson avatar Jul 18 '24 13:07 ijackson

Error: The feature relabel is not enabled in this repository. To enable it add its section in the triagebot.toml in the root of the repository.

The feature relabel is not enabled in this repository. To enable it add its section in the triagebot.toml in the root of the repository.

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

rustbot avatar Jul 18 '24 13:07 rustbot