From 85254c9dd8266f24658071b773fbc3ba2da7b35c Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 16 Aug 2023 16:40:12 +0000 Subject: [PATCH 1/3] bugfix: reflect how rustfmt formats type aliases --- src/doc/style-guide/src/items.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/doc/style-guide/src/items.md b/src/doc/style-guide/src/items.md index a6d941f6d0454..b93f2fbde4d84 100644 --- a/src/doc/style-guide/src/items.md +++ b/src/doc/style-guide/src/items.md @@ -367,14 +367,14 @@ where ## Type aliases Keep type aliases on one line when they fit. If necessary to break the line, do -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; // If multi-line is required -type VeryLongType = - AnEvenLongerType>; +type VeryLongType + = AnEvenLongerType>; ``` Where possible avoid `where` clauses and keep type constraints inline. Where From f707b9cd707d84768687caa025d483cef61ed440 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 16 Aug 2023 16:41:49 +0000 Subject: [PATCH 2/3] Describe how to format trailing where clauses --- src/doc/style-guide/src/items.md | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/doc/style-guide/src/items.md b/src/doc/style-guide/src/items.md index b93f2fbde4d84..dc10f4ff15d35 100644 --- a/src/doc/style-guide/src/items.md +++ b/src/doc/style-guide/src/items.md @@ -378,15 +378,38 @@ type VeryLongType ``` 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., +that is not possible, prefer a trailing `where` clause over one that precedes +the type. Split the line before and after a trailing `where` clause (and split +the `where` clause as normal) and indent before the `=` and type, e.g., ```rust type VeryLongType + = AnEvenLongerType> +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. + +``` +type WithPrecedingWC where T: U::AnAssociatedType, U: SomeBound, = AnEvenLongerType>; + +// Or with both a preceding and trailing where clause. +type WithPrecedingWC +where + T: U::AnAssociatedType, + U: SomeBound, += AnEvenLongerType> +where + T: U::AnAssociatedType2, + U: SomeBound2; ``` ## Associated types From 2ff14b0050145b79769bf207b7d0bee5c98292b8 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 23 Aug 2023 18:25:09 +0000 Subject: [PATCH 3/3] Remove opinions from style guide about where clauses in type alias items --- src/doc/style-guide/src/items.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/doc/style-guide/src/items.md b/src/doc/style-guide/src/items.md index dc10f4ff15d35..b215de6ad28a2 100644 --- a/src/doc/style-guide/src/items.md +++ b/src/doc/style-guide/src/items.md @@ -377,12 +377,12 @@ type VeryLongType = AnEvenLongerType>; ``` -Where possible avoid `where` clauses and keep type constraints inline. Where -that is not possible, prefer a trailing `where` clause over one that precedes -the type. Split the line before and after a trailing `where` clause (and split -the `where` clause as normal) and indent before the `=` and type, 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 = AnEvenLongerType> where @@ -392,9 +392,12 @@ where 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. +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 where T: U::AnAssociatedType,