From 3742a4bd902f5d6efd2b401a6dab5e4087070d5d Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Tue, 2 Jan 2024 04:59:24 -0800 Subject: [PATCH 1/5] style-guide: Format single associated type `where` clauses on the same line In particular, lifetime-generic associated types often have a `where Self: 'a` bound, which we can format on the same line. --- src/doc/style-guide/src/editions.md | 1 + src/doc/style-guide/src/items.md | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/doc/style-guide/src/editions.md b/src/doc/style-guide/src/editions.md index b9a89c20cee40..9d593f8081025 100644 --- a/src/doc/style-guide/src/editions.md +++ b/src/doc/style-guide/src/editions.md @@ -43,6 +43,7 @@ include: - Miscellaneous `rustfmt` bugfixes. - Use version-sort (sort `x8`, `x16`, `x32`, `x64`, `x128` in that order). - Change "ASCIIbetical" sort to Unicode-aware "non-lowercase before lowercase". +- Format single associated type `where` clauses on the same line if they fit. ## Rust 2015/2018/2021 style edition diff --git a/src/doc/style-guide/src/items.md b/src/doc/style-guide/src/items.md index 0066a4bacb956..06bac12987191 100644 --- a/src/doc/style-guide/src/items.md +++ b/src/doc/style-guide/src/items.md @@ -421,9 +421,20 @@ Format associated types like type aliases. Where an associated type has a bound, put a space after the colon but not before: ```rust -pub type Foo: Bar; +type Foo: Bar; ``` +If an associated type has no `=`, and has a `where` clause with only one entry, +format the entire type declaration including the `where` clause on the same +line if it fits: + +```rust +type Item<'a> where Self: 'a; +``` + +If the associated type has a `=`, or if the `where` clause contains multiple +entries, format it across multiple lines as with a type alias. + ## extern items When writing extern items (such as `extern "C" fn`), always specify the ABI. From 2f20bb4a976e7dbd11c5329b48a2d402ff81fca9 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Wed, 10 Jan 2024 19:11:17 -0800 Subject: [PATCH 2/5] style-guide: Give a second example for associated type formatting Show an example that has bounds. --- src/doc/style-guide/src/items.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/doc/style-guide/src/items.md b/src/doc/style-guide/src/items.md index 06bac12987191..718180191895a 100644 --- a/src/doc/style-guide/src/items.md +++ b/src/doc/style-guide/src/items.md @@ -430,6 +430,7 @@ line if it fits: ```rust type Item<'a> where Self: 'a; +type Item<'a>: PartialEq + Send where Self: 'a; ``` If the associated type has a `=`, or if the `where` clause contains multiple From 2af29af710aa4613e4ff670e5902d7ea6725988f Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Wed, 10 Jan 2024 19:13:15 -0800 Subject: [PATCH 3/5] style-guide: Not all where clauses can be written as inline bounds --- src/doc/style-guide/src/items.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/style-guide/src/items.md b/src/doc/style-guide/src/items.md index 718180191895a..095e5512ea923 100644 --- a/src/doc/style-guide/src/items.md +++ b/src/doc/style-guide/src/items.md @@ -347,7 +347,7 @@ where ``` If a `where` clause is very short, prefer using an inline bound on the type -parameter. +parameter if possible. If a component of a `where` clause does not fit and contains `+`, break it before each `+` and block-indent the continuation lines. Put each bound on its From 9ff777bf50cb26107496c1bfdd178c56c5a57cca Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Wed, 10 Jan 2024 19:21:27 -0800 Subject: [PATCH 4/5] style-guide: Also format where clauses on one line for short function decls --- src/doc/style-guide/src/items.md | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/doc/style-guide/src/items.md b/src/doc/style-guide/src/items.md index 095e5512ea923..9b8cdaed88bee 100644 --- a/src/doc/style-guide/src/items.md +++ b/src/doc/style-guide/src/items.md @@ -295,8 +295,18 @@ Prefer to use single-letter names for generic parameters. These rules apply for `where` clauses on any item. -If immediately following a closing bracket of any kind, write the keyword -`where` on the same line, with a space before it. +If a where clause is short, and appears on a short one-line function +declaration with no body or a short associated type with no `=`, format it on +the same line as the declaration: + +```rust +fn new(&self) -> Self where Self: Sized; + +type Item<'a>: SomeTrait where Self: 'a; +``` + +Otherwise, if immediately following a closing bracket of any kind, write the +keyword `where` on the same line, with a space before it. Otherwise, put `where` on a new line at the same indentation level. Put each component of a `where` clause on its own line, block-indented. Use a trailing @@ -424,9 +434,9 @@ bound, put a space after the colon but not before: type Foo: Bar; ``` -If an associated type has no `=`, and has a `where` clause with only one entry, -format the entire type declaration including the `where` clause on the same -line if it fits: +If an associated type is short, has no `=`, and has a `where` clause with only +one entry, format the entire type declaration including the `where` clause on +the same line if it fits: ```rust type Item<'a> where Self: 'a; From 163b1a66152faffb5543f5a7ed5d80251f4d466c Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Wed, 24 Jan 2024 12:48:11 -0800 Subject: [PATCH 5/5] Reword formatting for where clauses Suggested-by: Caleb Cartwright --- src/doc/style-guide/src/items.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/style-guide/src/items.md b/src/doc/style-guide/src/items.md index 9b8cdaed88bee..c0628691b7734 100644 --- a/src/doc/style-guide/src/items.md +++ b/src/doc/style-guide/src/items.md @@ -296,7 +296,7 @@ Prefer to use single-letter names for generic parameters. These rules apply for `where` clauses on any item. If a where clause is short, and appears on a short one-line function -declaration with no body or a short associated type with no `=`, format it on +declaration with no body or on a short type with no `=`, format it on the same line as the declaration: ```rust