rust-lang / rust-lang/rust

Formatting of associated types with GATs very verbose

Open
#152,762 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

E-help-wanted I-style-radar T-style
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

Initially filed as a rustfmt bug, but I have been redirected here, as it appears that rustfmt is correctly following the official style guide. This issue pertains to the section of the Rust Style Guide on type aliases with where clauses.

Rustfmt (nightly) has recently been updated to be capable of formatting associated type aliases (definitions of associated types in implementations of traits where there is an = to define the type). This means that a number of associated types that were previously left unformatted are now being formatted according to the section of the guide above.

This change is making a number of associated type implementations in my codebase that were previously a single line (and fit within the 100 character limit) now take up 4 lines. And I am concerned that the new formatting will make it harder for me to justify including associated types in my code due to the verbosity it imposes both on my own codebase and on implementors of traits I create.

As an example:

    type GridItemStyle<'a> = &'a Style where Self: 'a;

is now

    type GridItemStyle<'a>
        = &'a Style
    where
        Self: 'a;

I feel like this style perhaps made sense when where clauses on associated types were a rare case and would often be quite long. But now with GATs, where: Self: 'a is not only common but mandatory. And I think the style guide ought to be updated to account for this.

In particular:

  • There seems to be no need to split the name and value onto separate lines when they fit on one line. For let bindings and function definitions we have a smart heuristic based on line length. I think we could do the same here.
  • the where Self: 'a is boilerplate code that conveys no useful information. I think it would be better if it was hidden off the end of the line if possible, or else taking up a single line if not.
Proposal

I concede that where clauses are typically formatted onto separate lines in Rust, but I think that where Self: 'a could be special cased to some degree. So I would propose:

The entirely single line form is used if the full line fits within the width limit:

    type GridItemStyle<'a> = &'a Style where Self: 'a;

Else the following form is used if the definition minus the where clause fits within the width limit:

    type GridItemStyle<'a> = &'a Style
    where
        Self: 'a;

And finally the existing form is used as a last resort if the width limit is exceeded by just the type part of the definition:

    type GridItemStyle<'a>
        = &'a Style
    where
        Self: 'a;

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the Rust Style Guide section on type aliases with where clauses at src/doc/style-guide/src/items.md#L390-L401, then reproduce the nightly rustfmt output for the associated-type examples in the issue. Done means agreeing on and implementing a revised formatting rule that preserves the proposed compact forms while respecting the width limit.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.