Skip to content

Commit 7b4b1b0

Browse files
committed
Auto merge of rust-lang#114901 - compiler-errors:style-guide-wc, r=calebcartwright
Amend style guide section for formatting where clauses in type aliases This PR has two parts: 1. Amend wording about breaking before or after the `=`, which is a style guide bugfix to align it with current rustfmt behavior. 2. Explain how to format trailing (rust-lang#89122) where clauses, which are preferred in both GATs (rust-lang#90076) and type aliases (rust-lang#114662). r? `@joshtriplett`
2 parents d4589a4 + 2ff14b0 commit 7b4b1b0

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

src/doc/style-guide/src/items.md

+32-6
Original file line numberDiff line numberDiff line change
@@ -367,26 +367,52 @@ where
367367
## Type aliases
368368

369369
Keep type aliases on one line when they fit. If necessary to break the line, do
370-
so after the `=`, and block-indent the right-hand side:
370+
so before the `=`, and block-indent the right-hand side:
371371

372372
```rust
373373
pub type Foo = Bar<T>;
374374

375375
// If multi-line is required
376-
type VeryLongType<T, U: SomeBound> =
377-
AnEvenLongerType<T, U, Foo<T>>;
376+
type VeryLongType<T, U: SomeBound>
377+
= AnEvenLongerType<T, U, Foo<T>>;
378378
```
379379

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

384384
```rust
385+
// With only a trailing where clause
385386
type VeryLongType<T, U>
387+
= AnEvenLongerType<T, U, Foo<T>>
388+
where
389+
T: U::AnAssociatedType,
390+
U: SomeBound;
391+
```
392+
393+
When there is a `where` clause before the type, format it normally, and break
394+
after the last clause. Do not indent before the `=` to leave it visually
395+
distinct from the indented clauses that precede it. If there is additionally a
396+
`where` clause after the type, break before the `where` keyword and format the
397+
clauses normally.
398+
399+
```rust
400+
// With only a preceding where clause.
401+
type WithPrecedingWC<T, U>
386402
where
387403
T: U::AnAssociatedType,
388404
U: SomeBound,
389405
= AnEvenLongerType<T, U, Foo<T>>;
406+
407+
// Or with both a preceding and trailing where clause.
408+
type WithPrecedingWC<T, U>
409+
where
410+
T: U::AnAssociatedType,
411+
U: SomeBound,
412+
= AnEvenLongerType<T, U, Foo<T>>
413+
where
414+
T: U::AnAssociatedType2,
415+
U: SomeBound2;
390416
```
391417

392418
## Associated types

0 commit comments

Comments
 (0)