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

Amend style guide section for formatting where clauses in type aliases #114901

Merged
merged 3 commits into from
Sep 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions src/doc/style-guide/src/items.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,26 +367,52 @@ where
## Type aliases

Keep type aliases on one line when they fit. If necessary to break the line, do
compiler-errors marked this conversation as resolved.
Show resolved Hide resolved
so after the `=`, and block-indent the right-hand side:
so before the `=`, and block-indent the right-hand side:

```rust
pub type Foo = Bar<T>;

// If multi-line is required
type VeryLongType<T, U: SomeBound> =
AnEvenLongerType<T, U, Foo<T>>;
type VeryLongType<T, U: SomeBound>
= AnEvenLongerType<T, U, Foo<T>>;
```

Where possible avoid `where` clauses and keep type constraints inline. Where
that is not possible split the line before and after the `where` clause (and
split the `where` clause as normal), e.g.,
When there is a trailing `where` clause after the type, and no `where` clause
present before the type, break before the `=` and indent. Then break before the
`where` keyword and format the clauses normally, e.g.,

```rust
// With only a trailing where clause
type VeryLongType<T, U>
= AnEvenLongerType<T, U, Foo<T>>
where
T: U::AnAssociatedType,
U: SomeBound;
```

When there is a `where` clause before the type, format it normally, and break
after the last clause. Do not indent before the `=` to leave it visually
distinct from the indented clauses that precede it. If there is additionally a
`where` clause after the type, break before the `where` keyword and format the
clauses normally.

```rust
// With only a preceding where clause.
type WithPrecedingWC<T, U>
where
T: U::AnAssociatedType,
U: SomeBound,
= AnEvenLongerType<T, U, Foo<T>>;

// Or with both a preceding and trailing where clause.
type WithPrecedingWC<T, U>
where
T: U::AnAssociatedType,
U: SomeBound,
= AnEvenLongerType<T, U, Foo<T>>
where
T: U::AnAssociatedType2,
U: SomeBound2;
```

## Associated types
Expand Down