rust-lang / style-team

Home of the Rust style team
Apache License 2.0
454 stars 56 forks source link

How Should Formatters Add Line Breaks for Long Type Alias Bounds #193

Open ytmimi opened 2 months ago

ytmimi commented 2 months ago

This is related to https://github.com/rust-lang/rustfmt/issues/5892. To summarize, there are inconsistencies when formatting Type Alias Bounds with rustfmt when using different style_edition values.

Using style_edition={2015|2018|2021} produces the following output:

type AAAAAAAAAAAAA: BBBBBBBBBBBBBBB<
    CCCCCCCCCCCCCCCCC,
    DDDDDDDDDDDDDDDDD,
    EEEEEEEEEEEEEEEEE,
    FFFFFFFFFFFFFFFFF,
    GGGGGGGGGGGGGGGGG,
    HHHHHHHHHHHHHHHHH,
    IIIIIIIIIIIIIIIII,
>;

and using style_edition=2024 produces the following output:

type AAAAAAAAAAAAA: BBBBBBBBBBBBBBB<
        CCCCCCCCCCCCCCCCC,
        DDDDDDDDDDDDDDDDD,
        EEEEEEEEEEEEEEEEE,
        FFFFFFFFFFFFFFFFF,
        GGGGGGGGGGGGGGGGG,
        HHHHHHHHHHHHHHHHH,
        IIIIIIIIIIIIIIIII,
    >;

The Type Alias Section of the Style Guide, doesn't give any prescription for how to break or handle the indentation of the bounds.

In https://github.com/rust-lang/rustfmt/pull/6293, it was suggested that the Type Alias Bounds should be similar to those for Trait Bounds, and therefore produce the following formatting:

type AAAAAAAAAAAAA:
    BBBBBBBBBBBBBBB<
        CCCCCCCCCCCCCCCCC,
        DDDDDDDDDDDDDDDDD,
        EEEEEEEEEEEEEEEEE,
        FFFFFFFFFFFFFFFFF,
    >;

So I'm wondering what the correct formatting should be, and if the Style Guide should be updated to provide clearer guidance on this.

Another question I have is how should Type Alias Bounds be formatted in this following case:

// This is the formatting rustfmt produces for all style editions
type AAAAAAAAAAAAA: BBBBBBBBBBBBBBB<
        CCCCCCCCCCCCCCCCC,
        DDDDDDDDDDDDDDDDD,
        EEEEEEEEEEEEEEEEE,
        FFFFFFFFFFFFFFFFF,
        GGGGGGGGGGGGGGGGG,
        HHHHHHHHHHHHHHHHH,
        IIIIIIIIIIIIIIIII,
    > + Copy
    + Debug;
calebcartwright commented 2 months ago

Just as a reference to another section of the guide that may be operative here: https://doc.rust-lang.org/nightly/style-guide/types.html#line-breaks

calebcartwright commented 2 months ago

this was the sole topic we discussed in the t-style meeting today and it was one we discussed at length

Given the pending deadlines for the 2024 Edition work, our position is that for now, the second example:

type AAAAAAAAAAAAA: BBBBBBBBBBBBBBB<
        CCCCCCCCCCCCCCCCC,
        DDDDDDDDDDDDDDDDD,
        EEEEEEEEEEEEEEEEE,
        FFFFFFFFFFFFFFFFF,
        GGGGGGGGGGGGGGGGG,
        HHHHHHHHHHHHHHHHH,
        IIIIIIIIIIIIIIIII,
    >;

can be considered correct and we're pleased that the formatting is no longer dependent upon the presence/absence of additional bounds (as it was in 2021 and below style editions)

based on the discussion though, it's very conceivable that we'd want to do something different for the next style edition so because of that as well as the desire to start freezing the 2024 edition, we're not going to update the style guide text at this time

please feel free to weigh in and correct me @rust-lang/style but wanted to post a summary to unlock the rustfmt team