From 171813dfb25f3e8a9f80c1ae9242a539deed78df Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Sun, 25 Jun 2023 14:02:05 -0700 Subject: [PATCH 01/10] Remove useless `truncate` call I cannot find a reason for this `truncate` call, and all tests pass with it removed. I assume it was added to reduce the allocation size (capacity) of the `String`, but the docs explicitly say it has no effect on capacity. --- src/librustdoc/html/format.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index b710311c85872..f551eb5e9b259 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -375,12 +375,8 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>( // put the first one on the same line as the 'where' keyword let where_preds = where_preds.replacen(&br_with_padding, " ", 1); - let mut clause = br_with_padding; - // +1 is for `\n`. - clause.truncate(indent + 1 + where_indent); - - write!(clause, "where{where_preds}")?; - clause + write!(br_with_padding, "where{where_preds}")?; + br_with_padding } } }; From ec35eeecbd89ea86c396ed00d7fca5e1d6ab081f Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Sun, 25 Jun 2023 14:33:16 -0700 Subject: [PATCH 02/10] Remove some `alternate` formatting code It was never used. --- src/librustdoc/html/format.rs | 97 ++++++++++++----------------------- 1 file changed, 33 insertions(+), 64 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index f551eb5e9b259..45443e9d5e592 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -281,11 +281,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>( !matches!(pred, clean::WherePredicate::BoundPredicate { bounds, .. } if bounds.is_empty()) }).map(|pred| { display_fn(move |f| { - if f.alternate() { - f.write_str(" ")?; - } else { - f.write_str("\n")?; - } + f.write_str("\n")?; match pred { clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => { @@ -293,25 +289,13 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>( let generic_bounds = print_generic_bounds(bounds, cx); if bound_params.is_empty() { - if f.alternate() { - write!(f, "{ty_cx:#}: {generic_bounds:#}") - } else { - write!(f, "{ty_cx}: {generic_bounds}") - } + write!(f, "{ty_cx}: {generic_bounds}") } else { - if f.alternate() { - write!( - f, - "for<{:#}> {ty_cx:#}: {generic_bounds:#}", - comma_sep(bound_params.iter().map(|lt| lt.print(cx)), true) - ) - } else { - write!( - f, - "for<{}> {ty_cx}: {generic_bounds}", - comma_sep(bound_params.iter().map(|lt| lt.print(cx)), true) - ) - } + write!( + f, + "for<{}> {ty_cx}: {generic_bounds}", + comma_sep(bound_params.iter().map(|lt| lt.print(cx)), true) + ) } } clean::WherePredicate::RegionPredicate { lifetime, bounds } => { @@ -324,11 +308,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>( } // FIXME(fmease): Render bound params. clean::WherePredicate::EqPredicate { lhs, rhs, bound_params: _ } => { - if f.alternate() { - write!(f, "{:#} == {:#}", lhs.print(cx), rhs.print(cx)) - } else { - write!(f, "{} == {}", lhs.print(cx), rhs.print(cx)) - } + write!(f, "{} == {}", lhs.print(cx), rhs.print(cx)) } } }) @@ -339,45 +319,34 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>( } let where_preds = comma_sep(where_predicates, false); - let clause = if f.alternate() { - if ending == Ending::Newline { - format!(" where{where_preds},") - } else { - format!(" where{where_preds}") - } + let mut br_with_padding = String::with_capacity(6 * indent + 28); + br_with_padding.push('\n'); + let where_indent = 3; + let padding_amount = if ending == Ending::Newline { + indent + 4 + } else if indent == 0 { + 4 } else { - let mut br_with_padding = String::with_capacity(6 * indent + 28); - br_with_padding.push('\n'); - - let where_indent = 3; - let padding_amount = if ending == Ending::Newline { - indent + 4 - } else if indent == 0 { - 4 - } else { - indent + where_indent + "where ".len() - }; - - for _ in 0..padding_amount { - br_with_padding.push(' '); - } - let where_preds = where_preds.to_string().replace('\n', &br_with_padding); - - if ending == Ending::Newline { - let mut clause = " ".repeat(indent.saturating_sub(1)); - write!(clause, "where{where_preds},")?; - clause + indent + where_indent + "where ".len() + }; + for _ in 0..padding_amount { + br_with_padding.push(' '); + } + let where_preds = where_preds.to_string().replace('\n', &br_with_padding); + let clause = if ending == Ending::Newline { + let mut clause = " ".repeat(indent.saturating_sub(1)); + write!(clause, "where{where_preds},")?; + clause + } else { + // insert a newline after a single space but before multiple spaces at the start + if indent == 0 { + format!("\nwhere{where_preds}") } else { - // insert a newline after a single space but before multiple spaces at the start - if indent == 0 { - format!("\nwhere{where_preds}") - } else { - // put the first one on the same line as the 'where' keyword - let where_preds = where_preds.replacen(&br_with_padding, " ", 1); + // put the first one on the same line as the 'where' keyword + let where_preds = where_preds.replacen(&br_with_padding, " ", 1); - write!(br_with_padding, "where{where_preds}")?; - br_with_padding - } + write!(br_with_padding, "where{where_preds}")?; + br_with_padding } }; write!(f, "{clause}") From 7e115570a3f4239b864e0280cf8d3b71d4fcc3a5 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Sun, 25 Jun 2023 18:02:29 -0700 Subject: [PATCH 03/10] Extract closure as helper function --- src/librustdoc/html/format.rs | 77 +++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 45443e9d5e592..adf57378c7f8e 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -9,7 +9,7 @@ use std::borrow::Cow; use std::cell::Cell; -use std::fmt::{self, Write}; +use std::fmt::{self, Display, Write}; use std::iter::{self, once}; use rustc_ast as ast; @@ -279,40 +279,9 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>( display_fn(move |f| { let mut where_predicates = gens.where_predicates.iter().filter(|pred| { !matches!(pred, clean::WherePredicate::BoundPredicate { bounds, .. } if bounds.is_empty()) - }).map(|pred| { - display_fn(move |f| { - f.write_str("\n")?; - - match pred { - clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => { - let ty_cx = ty.print(cx); - let generic_bounds = print_generic_bounds(bounds, cx); - - if bound_params.is_empty() { - write!(f, "{ty_cx}: {generic_bounds}") - } else { - write!( - f, - "for<{}> {ty_cx}: {generic_bounds}", - comma_sep(bound_params.iter().map(|lt| lt.print(cx)), true) - ) - } - } - clean::WherePredicate::RegionPredicate { lifetime, bounds } => { - let mut bounds_display = String::new(); - for bound in bounds.iter().map(|b| b.print(cx)) { - write!(bounds_display, "{bound} + ")?; - } - bounds_display.truncate(bounds_display.len() - " + ".len()); - write!(f, "{}: {bounds_display}", lifetime.print()) - } - // FIXME(fmease): Render bound params. - clean::WherePredicate::EqPredicate { lhs, rhs, bound_params: _ } => { - write!(f, "{} == {}", lhs.print(cx), rhs.print(cx)) - } - } - }) - }).peekable(); + }).map(|pred| + print_where_pred(cx,pred) + ).peekable(); if where_predicates.peek().is_none() { return Ok(()); @@ -353,6 +322,44 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>( }) } +fn print_where_pred<'a, 'tcx: 'a>( + cx: &'a Context<'tcx>, + pred: &'a clean::WherePredicate, +) -> impl Display + Captures<'a> + Captures<'tcx> { + display_fn(move |f| { + f.write_str("\n")?; + + match pred { + clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => { + let ty_cx = ty.print(cx); + let generic_bounds = print_generic_bounds(bounds, cx); + + if bound_params.is_empty() { + write!(f, "{ty_cx}: {generic_bounds}") + } else { + write!( + f, + "for<{}> {ty_cx}: {generic_bounds}", + comma_sep(bound_params.iter().map(|lt| lt.print(cx)), true) + ) + } + } + clean::WherePredicate::RegionPredicate { lifetime, bounds } => { + let mut bounds_display = String::new(); + for bound in bounds.iter().map(|b| b.print(cx)) { + write!(bounds_display, "{bound} + ")?; + } + bounds_display.truncate(bounds_display.len() - " + ".len()); + write!(f, "{}: {bounds_display}", lifetime.print()) + } + // FIXME(fmease): Render bound params. + clean::WherePredicate::EqPredicate { lhs, rhs, bound_params: _ } => { + write!(f, "{} == {}", lhs.print(cx), rhs.print(cx)) + } + } + }) +} + impl clean::Lifetime { pub(crate) fn print(&self) -> impl fmt::Display + '_ { self.0.as_str() From 4b7aed8faf2fd37e79181eeafa3258160ea7a293 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Sun, 25 Jun 2023 18:08:14 -0700 Subject: [PATCH 04/10] Simplify some code --- src/librustdoc/html/format.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index adf57378c7f8e..71aa0b030edc0 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -345,11 +345,7 @@ fn print_where_pred<'a, 'tcx: 'a>( } } clean::WherePredicate::RegionPredicate { lifetime, bounds } => { - let mut bounds_display = String::new(); - for bound in bounds.iter().map(|b| b.print(cx)) { - write!(bounds_display, "{bound} + ")?; - } - bounds_display.truncate(bounds_display.len() - " + ".len()); + let bounds_display = bounds.iter().map(|b| b.print(cx)).join(" + "); write!(f, "{}: {bounds_display}", lifetime.print()) } // FIXME(fmease): Render bound params. From c6948b524caa4803e40ad3782b8efda04b708c23 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Sun, 25 Jun 2023 19:07:06 -0700 Subject: [PATCH 05/10] Further simplify `print_where_clause` This commit (or one of the prior ones) mostly fixes #112901, though it causes one case (item-decl for trait assoc ty) to regress in that issue. --- src/librustdoc/html/format.rs | 64 ++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 71aa0b030edc0..2a39eb2a4b2dc 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -276,32 +276,39 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>( indent: usize, ending: Ending, ) -> impl fmt::Display + 'a + Captures<'tcx> { + let where_indent = 3; + let padding_amount = if ending == Ending::Newline { + indent + 4 + } else if indent == 0 { + 4 + } else { + indent + where_indent + "where ".len() + }; + display_fn(move |f| { - let mut where_predicates = gens.where_predicates.iter().filter(|pred| { - !matches!(pred, clean::WherePredicate::BoundPredicate { bounds, .. } if bounds.is_empty()) - }).map(|pred| - print_where_pred(cx,pred) - ).peekable(); + let mut where_predicates = gens + .where_predicates + .iter() + .filter(|pred| { + !matches!(pred, clean::WherePredicate::BoundPredicate { bounds, .. } if bounds.is_empty()) + }) + .enumerate() + .map(|(i, pred)| { + let (padding_amount, add_newline) = if i == 0 && ending == Ending::NoNewline && indent > 0 { + // put the first one on the same line as the 'where' keyword + (1, false) + } else { + (padding_amount, true) + }; + print_where_pred_helper(cx, pred, padding_amount, add_newline) + }) + .peekable(); if where_predicates.peek().is_none() { return Ok(()); } - let where_preds = comma_sep(where_predicates, false); - let mut br_with_padding = String::with_capacity(6 * indent + 28); - br_with_padding.push('\n'); - let where_indent = 3; - let padding_amount = if ending == Ending::Newline { - indent + 4 - } else if indent == 0 { - 4 - } else { - indent + where_indent + "where ".len() - }; - for _ in 0..padding_amount { - br_with_padding.push(' '); - } - let where_preds = where_preds.to_string().replace('\n', &br_with_padding); + let where_preds = comma_sep(where_predicates, false).to_string(); let clause = if ending == Ending::Newline { let mut clause = " ".repeat(indent.saturating_sub(1)); write!(clause, "where{where_preds},")?; @@ -311,23 +318,26 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>( if indent == 0 { format!("\nwhere{where_preds}") } else { - // put the first one on the same line as the 'where' keyword - let where_preds = where_preds.replacen(&br_with_padding, " ", 1); - - write!(br_with_padding, "where{where_preds}")?; - br_with_padding + let padding_str: String = + iter::once("\n").chain(iter::repeat(" ").take(padding_amount)).collect(); + format!("\n{padding_str}where{where_preds}") } }; write!(f, "{clause}") }) } -fn print_where_pred<'a, 'tcx: 'a>( +fn print_where_pred_helper<'a, 'tcx: 'a>( cx: &'a Context<'tcx>, pred: &'a clean::WherePredicate, + indent_len: usize, + add_newline: bool, ) -> impl Display + Captures<'a> + Captures<'tcx> { display_fn(move |f| { - f.write_str("\n")?; + if add_newline { + f.write_str("\n")?; + } + f.write_str(&" ".repeat(indent_len))?; match pred { clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => { From 30d2127c0964613873659dd2b0743be5a10a961c Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Sun, 25 Jun 2023 19:15:32 -0700 Subject: [PATCH 06/10] More simplification --- src/librustdoc/html/format.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 2a39eb2a4b2dc..88e27a254d781 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -286,7 +286,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>( }; display_fn(move |f| { - let mut where_predicates = gens + let where_preds: String = gens .where_predicates .iter() .filter(|pred| { @@ -302,28 +302,25 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>( }; print_where_pred_helper(cx, pred, padding_amount, add_newline) }) - .peekable(); + .join(","); - if where_predicates.peek().is_none() { + if where_preds.is_empty() { return Ok(()); } - let where_preds = comma_sep(where_predicates, false).to_string(); - let clause = if ending == Ending::Newline { - let mut clause = " ".repeat(indent.saturating_sub(1)); - write!(clause, "where{where_preds},")?; - clause + if ending == Ending::Newline { + let indent_str = " ".repeat(indent.saturating_sub(1)); + write!(f, "{indent_str}where{where_preds},") } else { // insert a newline after a single space but before multiple spaces at the start if indent == 0 { - format!("\nwhere{where_preds}") + write!(f, "\nwhere{where_preds}") } else { let padding_str: String = iter::once("\n").chain(iter::repeat(" ").take(padding_amount)).collect(); - format!("\n{padding_str}where{where_preds}") + write!(f, "\n{padding_str}where{where_preds}") } - }; - write!(f, "{clause}") + } }) } From 782fbb87fbe8a16c21d45ec41ee268dbffdb2ff5 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Sun, 25 Jun 2023 19:51:54 -0700 Subject: [PATCH 07/10] Make `where` clauses render the same --- src/librustdoc/html/format.rs | 24 ++++--------------- tests/rustdoc/where.SWhere_Echo_impl.html | 2 +- .../rustdoc/where.SWhere_Simd_item-decl.html | 3 +-- .../where.SWhere_TraitWhere_item-decl.html | 10 ++++---- tests/rustdoc/where.alpha_trait_decl.html | 3 +-- tests/rustdoc/where.bravo_trait_decl.html | 2 +- tests/rustdoc/where.charlie_fn_decl.html | 2 +- tests/rustdoc/where.golf_type_alias_decl.html | 2 +- 8 files changed, 16 insertions(+), 32 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 88e27a254d781..d1ebb06d2de1f 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -276,14 +276,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>( indent: usize, ending: Ending, ) -> impl fmt::Display + 'a + Captures<'tcx> { - let where_indent = 3; - let padding_amount = if ending == Ending::Newline { - indent + 4 - } else if indent == 0 { - 4 - } else { - indent + where_indent + "where ".len() - }; + let padding_amount = indent + "where ".len(); display_fn(move |f| { let where_preds: String = gens @@ -294,7 +287,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>( }) .enumerate() .map(|(i, pred)| { - let (padding_amount, add_newline) = if i == 0 && ending == Ending::NoNewline && indent > 0 { + let (padding_amount, add_newline) = if i == 0 && ending == Ending::NoNewline { // put the first one on the same line as the 'where' keyword (1, false) } else { @@ -309,17 +302,10 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>( } if ending == Ending::Newline { - let indent_str = " ".repeat(indent.saturating_sub(1)); - write!(f, "{indent_str}where{where_preds},") + write!(f, "where{where_preds},") } else { - // insert a newline after a single space but before multiple spaces at the start - if indent == 0 { - write!(f, "\nwhere{where_preds}") - } else { - let padding_str: String = - iter::once("\n").chain(iter::repeat(" ").take(padding_amount)).collect(); - write!(f, "\n{padding_str}where{where_preds}") - } + let indent_str = " ".repeat(indent); + write!(f, "\n{indent_str}where{where_preds}") } }) } diff --git a/tests/rustdoc/where.SWhere_Echo_impl.html b/tests/rustdoc/where.SWhere_Echo_impl.html index 7517eb090f496..936bb2f0b5565 100644 --- a/tests/rustdoc/where.SWhere_Echo_impl.html +++ b/tests/rustdoc/where.SWhere_Echo_impl.html @@ -1,2 +1,2 @@

impl<D> Delta<D>where - D: MyTrait,

\ No newline at end of file + D: MyTrait, \ No newline at end of file diff --git a/tests/rustdoc/where.SWhere_Simd_item-decl.html b/tests/rustdoc/where.SWhere_Simd_item-decl.html index 3e72ba2b74fe2..0a363fb0cd55e 100644 --- a/tests/rustdoc/where.SWhere_Simd_item-decl.html +++ b/tests/rustdoc/where.SWhere_Simd_item-decl.html @@ -1,3 +1,2 @@
pub struct Simd<T>(_)
-where
-    T: MyTrait;
+where T: MyTrait; \ No newline at end of file diff --git a/tests/rustdoc/where.SWhere_TraitWhere_item-decl.html b/tests/rustdoc/where.SWhere_TraitWhere_item-decl.html index e8ab061e679dd..e072e5c14ba1d 100644 --- a/tests/rustdoc/where.SWhere_TraitWhere_item-decl.html +++ b/tests/rustdoc/where.SWhere_TraitWhere_item-decl.html @@ -1,13 +1,13 @@
pub trait TraitWhere {
     type Item<'a>
-       where Self: 'a;
+    where Self: 'a;
 
     // Provided methods
     fn func(self)
-       where Self: Sized { ... }
+    where Self: Sized { ... }
     fn lines(self) -> Lines<Self>
-       where Self: Sized { ... }
+    where Self: Sized { ... }
     fn merge<T>(self, a: T)
-       where Self: Sized,
-             T: Sized { ... }
+    where Self: Sized,
+          T: Sized { ... }
 }
\ No newline at end of file diff --git a/tests/rustdoc/where.alpha_trait_decl.html b/tests/rustdoc/where.alpha_trait_decl.html index a7700055c9a74..b7f85b47ae6dc 100644 --- a/tests/rustdoc/where.alpha_trait_decl.html +++ b/tests/rustdoc/where.alpha_trait_decl.html @@ -1,3 +1,2 @@ pub struct Alpha<A>(_) -where - A: MyTrait; \ No newline at end of file +where A: MyTrait; \ No newline at end of file diff --git a/tests/rustdoc/where.bravo_trait_decl.html b/tests/rustdoc/where.bravo_trait_decl.html index 00524201a8aca..e3ce64fbae63f 100644 --- a/tests/rustdoc/where.bravo_trait_decl.html +++ b/tests/rustdoc/where.bravo_trait_decl.html @@ -1,5 +1,5 @@ pub trait Bravo<B>where - B: MyTrait,{ + B: MyTrait,{ // Required method fn get(&self, B: B); } \ No newline at end of file diff --git a/tests/rustdoc/where.charlie_fn_decl.html b/tests/rustdoc/where.charlie_fn_decl.html index 8e3bc8b01ecbd..df194d8e983f0 100644 --- a/tests/rustdoc/where.charlie_fn_decl.html +++ b/tests/rustdoc/where.charlie_fn_decl.html @@ -1,2 +1,2 @@ pub fn charlie<C>()where - C: MyTrait, \ No newline at end of file + C: MyTrait, \ No newline at end of file diff --git a/tests/rustdoc/where.golf_type_alias_decl.html b/tests/rustdoc/where.golf_type_alias_decl.html index 8da5402f90073..27c4eb3e0c7fd 100644 --- a/tests/rustdoc/where.golf_type_alias_decl.html +++ b/tests/rustdoc/where.golf_type_alias_decl.html @@ -1,2 +1,2 @@ pub type Golf<T>where - T: Clone, = (T, T); \ No newline at end of file + T: Clone, = (T, T); \ No newline at end of file From bc0759d923c37f80118957fc0fb7bf09bcbc3230 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Sun, 25 Jun 2023 19:56:34 -0700 Subject: [PATCH 08/10] Put all `where` bounds on individual lines, including first --- src/librustdoc/html/format.rs | 18 ++++-------------- tests/rustdoc/where.SWhere_Echo_impl.html | 2 +- tests/rustdoc/where.SWhere_Simd_item-decl.html | 3 ++- .../where.SWhere_TraitWhere_item-decl.html | 14 +++++++++----- tests/rustdoc/where.alpha_trait_decl.html | 3 ++- tests/rustdoc/where.bravo_trait_decl.html | 2 +- tests/rustdoc/where.charlie_fn_decl.html | 2 +- tests/rustdoc/where.golf_type_alias_decl.html | 2 +- 8 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index d1ebb06d2de1f..0edaf6ec1f3ff 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -276,7 +276,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>( indent: usize, ending: Ending, ) -> impl fmt::Display + 'a + Captures<'tcx> { - let padding_amount = indent + "where ".len(); + let padding_amount = indent + 4; display_fn(move |f| { let where_preds: String = gens @@ -285,15 +285,8 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>( .filter(|pred| { !matches!(pred, clean::WherePredicate::BoundPredicate { bounds, .. } if bounds.is_empty()) }) - .enumerate() - .map(|(i, pred)| { - let (padding_amount, add_newline) = if i == 0 && ending == Ending::NoNewline { - // put the first one on the same line as the 'where' keyword - (1, false) - } else { - (padding_amount, true) - }; - print_where_pred_helper(cx, pred, padding_amount, add_newline) + .map(|pred| { + print_where_pred_helper(cx, pred, padding_amount) }) .join(","); @@ -314,12 +307,9 @@ fn print_where_pred_helper<'a, 'tcx: 'a>( cx: &'a Context<'tcx>, pred: &'a clean::WherePredicate, indent_len: usize, - add_newline: bool, ) -> impl Display + Captures<'a> + Captures<'tcx> { display_fn(move |f| { - if add_newline { - f.write_str("\n")?; - } + f.write_str("\n")?; f.write_str(&" ".repeat(indent_len))?; match pred { diff --git a/tests/rustdoc/where.SWhere_Echo_impl.html b/tests/rustdoc/where.SWhere_Echo_impl.html index 936bb2f0b5565..7517eb090f496 100644 --- a/tests/rustdoc/where.SWhere_Echo_impl.html +++ b/tests/rustdoc/where.SWhere_Echo_impl.html @@ -1,2 +1,2 @@

impl<D> Delta<D>where - D: MyTrait,

\ No newline at end of file + D: MyTrait, \ No newline at end of file diff --git a/tests/rustdoc/where.SWhere_Simd_item-decl.html b/tests/rustdoc/where.SWhere_Simd_item-decl.html index 0a363fb0cd55e..a2e76a548e260 100644 --- a/tests/rustdoc/where.SWhere_Simd_item-decl.html +++ b/tests/rustdoc/where.SWhere_Simd_item-decl.html @@ -1,2 +1,3 @@
pub struct Simd<T>(_)
-where T: MyTrait;
\ No newline at end of file +where + T: MyTrait; \ No newline at end of file diff --git a/tests/rustdoc/where.SWhere_TraitWhere_item-decl.html b/tests/rustdoc/where.SWhere_TraitWhere_item-decl.html index e072e5c14ba1d..3af87a00c4271 100644 --- a/tests/rustdoc/where.SWhere_TraitWhere_item-decl.html +++ b/tests/rustdoc/where.SWhere_TraitWhere_item-decl.html @@ -1,13 +1,17 @@
pub trait TraitWhere {
     type Item<'a>
-    where Self: 'a;
+    where
+        Self: 'a;
 
     // Provided methods
     fn func(self)
-    where Self: Sized { ... }
+    where
+        Self: Sized { ... }
     fn lines(self) -> Lines<Self>
-    where Self: Sized { ... }
+    where
+        Self: Sized { ... }
     fn merge<T>(self, a: T)
-    where Self: Sized,
-          T: Sized { ... }
+    where
+        Self: Sized,
+        T: Sized { ... }
 }
\ No newline at end of file diff --git a/tests/rustdoc/where.alpha_trait_decl.html b/tests/rustdoc/where.alpha_trait_decl.html index b7f85b47ae6dc..a7700055c9a74 100644 --- a/tests/rustdoc/where.alpha_trait_decl.html +++ b/tests/rustdoc/where.alpha_trait_decl.html @@ -1,2 +1,3 @@ pub struct Alpha<A>(_) -where A: MyTrait; \ No newline at end of file +where + A: MyTrait; \ No newline at end of file diff --git a/tests/rustdoc/where.bravo_trait_decl.html b/tests/rustdoc/where.bravo_trait_decl.html index e3ce64fbae63f..00524201a8aca 100644 --- a/tests/rustdoc/where.bravo_trait_decl.html +++ b/tests/rustdoc/where.bravo_trait_decl.html @@ -1,5 +1,5 @@ pub trait Bravo<B>where - B: MyTrait,{ + B: MyTrait,{ // Required method fn get(&self, B: B); } \ No newline at end of file diff --git a/tests/rustdoc/where.charlie_fn_decl.html b/tests/rustdoc/where.charlie_fn_decl.html index df194d8e983f0..8e3bc8b01ecbd 100644 --- a/tests/rustdoc/where.charlie_fn_decl.html +++ b/tests/rustdoc/where.charlie_fn_decl.html @@ -1,2 +1,2 @@ pub fn charlie<C>()where - C: MyTrait, \ No newline at end of file + C: MyTrait, \ No newline at end of file diff --git a/tests/rustdoc/where.golf_type_alias_decl.html b/tests/rustdoc/where.golf_type_alias_decl.html index 27c4eb3e0c7fd..8da5402f90073 100644 --- a/tests/rustdoc/where.golf_type_alias_decl.html +++ b/tests/rustdoc/where.golf_type_alias_decl.html @@ -1,2 +1,2 @@ pub type Golf<T>where - T: Clone, = (T, T); \ No newline at end of file + T: Clone, = (T, T); \ No newline at end of file From 89c0e58a80225382621c3e8027ef0d8f87f01ec4 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Sun, 25 Jun 2023 20:04:55 -0700 Subject: [PATCH 09/10] Apply smaller font size to all `where` clauses --- src/librustdoc/html/static/css/rustdoc.css | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 21c1eb631e07b..86df811874174 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -640,13 +640,16 @@ pre, .rustdoc.source .example-wrap { background: var(--table-alt-row-background-color); } +.where { + font-size: 0.875rem; +} + /* Shift "where ..." part of method or fn definition down a line */ .method .where, .fn .where, .where.fmt-newline { display: block; white-space: pre-wrap; - font-size: 0.875rem; } .item-info { From 35b357703edac9ce8c64a24ea1cd8ff96324849c Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Sun, 25 Jun 2023 21:42:10 -0700 Subject: [PATCH 10/10] Add test for #112901 It's a lot of snapshots, but it's quite hard to test this sort of formatting with other ways. --- ...ue-112901-where-clause.assoc-S-impl-F.html | 3 ++ .../issue-112901-where-clause.assoc-Tr-F.html | 3 ++ ...sue-112901-where-clause.assoc-Tr-decl.html | 5 +++ ...e-112901-where-clause.assoc-Tr-impl-F.html | 3 ++ tests/rustdoc/issue-112901-where-clause.rs | 36 +++++++++++++++++++ .../issue-112901-where-clause.structs-A.html | 3 ++ .../issue-112901-where-clause.structs-S.html | 3 ++ 7 files changed, 56 insertions(+) create mode 100644 tests/rustdoc/issue-112901-where-clause.assoc-S-impl-F.html create mode 100644 tests/rustdoc/issue-112901-where-clause.assoc-Tr-F.html create mode 100644 tests/rustdoc/issue-112901-where-clause.assoc-Tr-decl.html create mode 100644 tests/rustdoc/issue-112901-where-clause.assoc-Tr-impl-F.html create mode 100644 tests/rustdoc/issue-112901-where-clause.rs create mode 100644 tests/rustdoc/issue-112901-where-clause.structs-A.html create mode 100644 tests/rustdoc/issue-112901-where-clause.structs-S.html diff --git a/tests/rustdoc/issue-112901-where-clause.assoc-S-impl-F.html b/tests/rustdoc/issue-112901-where-clause.assoc-S-impl-F.html new file mode 100644 index 0000000000000..b3a3d9e6c1427 --- /dev/null +++ b/tests/rustdoc/issue-112901-where-clause.assoc-S-impl-F.html @@ -0,0 +1,3 @@ +
§

type F<T> = T +where + T: Clone

\ No newline at end of file diff --git a/tests/rustdoc/issue-112901-where-clause.assoc-Tr-F.html b/tests/rustdoc/issue-112901-where-clause.assoc-Tr-F.html new file mode 100644 index 0000000000000..f28d6369e7a9e --- /dev/null +++ b/tests/rustdoc/issue-112901-where-clause.assoc-Tr-F.html @@ -0,0 +1,3 @@ +
source

type F<T> +where + T: Clone

\ No newline at end of file diff --git a/tests/rustdoc/issue-112901-where-clause.assoc-Tr-decl.html b/tests/rustdoc/issue-112901-where-clause.assoc-Tr-decl.html new file mode 100644 index 0000000000000..5389c50cf0e4c --- /dev/null +++ b/tests/rustdoc/issue-112901-where-clause.assoc-Tr-decl.html @@ -0,0 +1,5 @@ +
pub trait Tr {
+    type F<T>
+    where
+        T: Clone;
+}
\ No newline at end of file diff --git a/tests/rustdoc/issue-112901-where-clause.assoc-Tr-impl-F.html b/tests/rustdoc/issue-112901-where-clause.assoc-Tr-impl-F.html new file mode 100644 index 0000000000000..cbb07a3e884f5 --- /dev/null +++ b/tests/rustdoc/issue-112901-where-clause.assoc-Tr-impl-F.html @@ -0,0 +1,3 @@ +
§

type F<T> = T +where + T: Clone

\ No newline at end of file diff --git a/tests/rustdoc/issue-112901-where-clause.rs b/tests/rustdoc/issue-112901-where-clause.rs new file mode 100644 index 0000000000000..ee9af37917b78 --- /dev/null +++ b/tests/rustdoc/issue-112901-where-clause.rs @@ -0,0 +1,36 @@ +#![crate_name = "foo"] +#![feature(trivial_bounds)] + +pub mod structs { + // @has foo/structs/struct.A.html + // @snapshot structs-A - '//pre[@class="rust item-decl"]' + pub struct A(T) + where + T: Copy; + + // @has foo/structs/struct.S.html + // @snapshot structs-S - '//pre[@class="rust item-decl"]' + pub struct S + where + String: Clone; +} + +pub mod assoc { + // @has foo/assoc/struct.S.html + // @snapshot assoc-S-impl-F - '//section[@id="associatedtype.F"]' + pub struct S; + + // @has foo/assoc/trait.Tr.html + // @snapshot assoc-Tr-decl - '//pre[@class="rust item-decl"]' + // @snapshot assoc-Tr-F - '//section[@id="associatedtype.F"]' + // @snapshot assoc-Tr-impl-F - '//section[@id="associatedtype.F-1"]' + pub trait Tr { + type F + where + T: Clone; + } + + impl Tr for S { + type F = T where T: Clone; + } +} diff --git a/tests/rustdoc/issue-112901-where-clause.structs-A.html b/tests/rustdoc/issue-112901-where-clause.structs-A.html new file mode 100644 index 0000000000000..397e604a71ff3 --- /dev/null +++ b/tests/rustdoc/issue-112901-where-clause.structs-A.html @@ -0,0 +1,3 @@ +
pub struct A<T>(_)
+where
+    T: Copy;
\ No newline at end of file diff --git a/tests/rustdoc/issue-112901-where-clause.structs-S.html b/tests/rustdoc/issue-112901-where-clause.structs-S.html new file mode 100644 index 0000000000000..7b1a31d7dd471 --- /dev/null +++ b/tests/rustdoc/issue-112901-where-clause.structs-S.html @@ -0,0 +1,3 @@ +
pub struct S
+where
+    String: Clone;
\ No newline at end of file