diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 74cdf9b9b79..a59cfbea885 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -353,7 +353,11 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>( } else { let mut br_with_padding = String::with_capacity(6 * indent + 28); br_with_padding.push_str("
"); - for _ in 0..indent + 4 { + + let padding_amout = + if ending == Ending::Newline { indent + 4 } else { indent + "fn where ".len() }; + + for _ in 0..padding_amout { br_with_padding.push_str(" "); } let where_preds = where_preds.to_string().replace("
", &br_with_padding); @@ -367,8 +371,12 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>( if indent == 0 { format!("
where{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); + let mut clause = br_with_padding; - clause.truncate(clause.len() - 4); + clause.truncate(clause.len() - "where ".len()); + write!(clause, "where{where_preds}")?; clause } diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 9a7a08ab806..c2d24e51484 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -645,6 +645,10 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean: if count_consts != 0 && count_methods != 0 { w.write_str("\n"); } + + if !required_methods.is_empty() { + write!(w, " // Required method{}\n", pluralize(required_methods.len())); + } for (pos, m) in required_methods.iter().enumerate() { render_assoc_item( w, @@ -663,6 +667,10 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean: if !required_methods.is_empty() && !provided_methods.is_empty() { w.write_str("\n"); } + + if !provided_methods.is_empty() { + write!(w, " // Provided method{}\n", pluralize(provided_methods.len())); + } for (pos, m) in provided_methods.iter().enumerate() { render_assoc_item( w, @@ -672,16 +680,8 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean: cx, RenderMode::Normal, ); - match *m.kind { - clean::MethodItem(ref inner, _) - if !inner.generics.where_predicates.is_empty() => - { - w.write_str(",\n { ... }\n"); - } - _ => { - w.write_str(" { ... }\n"); - } - } + + w.write_str(" { ... }\n"); if pos < provided_methods.len() - 1 { w.write_str(""); diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index a1e91118303..2a9548712f0 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -1176,6 +1176,7 @@ a.test-arrow:hover { .item-spacer { width: 100%; height: 12px; + display: block; } .out-of-band > span.since { diff --git a/tests/rustdoc/auxiliary/inline-default-methods.rs b/tests/rustdoc/auxiliary/inline-default-methods.rs index 8a636f44921..f06a20b27dc 100644 --- a/tests/rustdoc/auxiliary/inline-default-methods.rs +++ b/tests/rustdoc/auxiliary/inline-default-methods.rs @@ -4,3 +4,15 @@ pub trait Foo { fn bar(&self); fn foo(&mut self) {} } + +pub trait Bar { + fn bar(&self); + fn foo1(&mut self) {} + fn foo2(&mut self) {} +} + +pub trait Baz { + fn bar1(&self); + fn bar2(&self); + fn foo(&mut self) {} +} diff --git a/tests/rustdoc/decl-trailing-whitespace.declaration.html b/tests/rustdoc/decl-trailing-whitespace.declaration.html index 02b51b34461..a2500de79a0 100644 --- a/tests/rustdoc/decl-trailing-whitespace.declaration.html +++ b/tests/rustdoc/decl-trailing-whitespace.declaration.html @@ -1,7 +1,9 @@ pub trait Write { + // Required methods fn poll_write(
        self: Option<String>,
        cx: &mut Option<String>,
        buf: &mut [usize]
    ) -> Option<Result<usize, Error>>; fn poll_flush(
        self: Option<String>,
        cx: &mut Option<String>
    ) -> Option<Result<(), Error>>; fn poll_close(
        self: Option<String>,
        cx: &mut Option<String>
    ) -> Option<Result<(), Error>>; + // Provided method fn poll_write_vectored(
        self: Option<String>,
        cx: &mut Option<String>,
        bufs: &[usize]
    ) -> Option<Result<usize, Error>> { ... } -}
\ No newline at end of file +} diff --git a/tests/rustdoc/inline-default-methods.rs b/tests/rustdoc/inline-default-methods.rs index e6468316f58..7706cb139ac 100644 --- a/tests/rustdoc/inline-default-methods.rs +++ b/tests/rustdoc/inline-default-methods.rs @@ -4,6 +4,18 @@ extern crate inline_default_methods; // @has inline_default_methods/trait.Foo.html -// @has - '//pre[@class="rust item-decl"]' 'fn bar(&self);' -// @has - '//pre[@class="rust item-decl"]' 'fn foo(&mut self) { ... }' +// @has - '//pre[@class="rust item-decl"]' '// Required method fn bar(&self);' +// @has - '//pre[@class="rust item-decl"]' '// Provided method fn foo(&mut self)' pub use inline_default_methods::Foo; + +// @has inline_default_methods/trait.Bar.html +// @has - '//pre[@class="rust item-decl"]' '// Required method fn bar(&self);' +// @has - '//pre[@class="rust item-decl"]' '// Provided methods fn foo1(&mut self)' +// @has - '//pre[@class="rust item-decl"]' 'fn foo2(&mut self)' +pub use inline_default_methods::Bar; + +// @has inline_default_methods/trait.Baz.html +// @has - '//pre[@class="rust item-decl"]' '// Required methods fn bar1(&self);' +// @has - '//pre[@class="rust item-decl"]' 'fn bar2(&self);' +// @has - '//pre[@class="rust item-decl"]' '// Provided method fn foo(&mut self)' +pub use inline_default_methods::Baz; diff --git a/tests/rustdoc/issue-85454.rs b/tests/rustdoc/issue-85454.rs index 2d410a5974a..5a49a9d0651 100644 --- a/tests/rustdoc/issue-85454.rs +++ b/tests/rustdoc/issue-85454.rs @@ -5,7 +5,7 @@ extern crate issue_85454; // @has foo/trait.FromResidual.html -// @has - '//pre[@class="rust item-decl"]' 'pub trait FromResidual::Residual> { fn from_residual(residual: R) -> Self; }' +// @has - '//pre[@class="rust item-decl"]' 'pub trait FromResidual::Residual> { // Required method fn from_residual(residual: R) -> Self; }' pub trait FromResidual::Residual> { fn from_residual(residual: R) -> Self; } @@ -24,6 +24,6 @@ pub enum ControlFlow { pub mod reexport { // @has foo/reexport/trait.FromResidual.html - // @has - '//pre[@class="rust item-decl"]' 'pub trait FromResidual::Residual> { fn from_residual(residual: R) -> Self; }' + // @has - '//pre[@class="rust item-decl"]' 'pub trait FromResidual::Residual> { // Required method fn from_residual(residual: R) -> Self; }' pub use issue_85454::*; } diff --git a/tests/rustdoc/where.SWhere_TraitWhere_item-decl.html b/tests/rustdoc/where.SWhere_TraitWhere_item-decl.html index 11df902f372..858bc142f66 100644 --- a/tests/rustdoc/where.SWhere_TraitWhere_item-decl.html +++ b/tests/rustdoc/where.SWhere_TraitWhere_item-decl.html @@ -1,8 +1,8 @@
pub trait TraitWhere {
-    type Item<'a>
    where
        Self: 'a
; + type Item<'a>
       where Self: 'a; - fn func(self)
    where
        Self: Sized
, - { ... } - fn lines(self) -> Lines<Self>
    where
        Self: Sized
, - { ... } + // Provided methods + fn func(self)
       where Self: Sized { ... } + fn lines(self) -> Lines<Self>
       where Self: Sized { ... } + fn merge<T>(self, a: T)
       where Self: Sized,
             T: Sized
{ ... } }
\ No newline at end of file diff --git a/tests/rustdoc/where.rs b/tests/rustdoc/where.rs index 4c34c7e51d9..af3239b6947 100644 --- a/tests/rustdoc/where.rs +++ b/tests/rustdoc/where.rs @@ -41,6 +41,12 @@ fn lines(self) -> Lines where Self: Sized, { todo!() } + + fn merge(self, a: T) + where + Self: Sized, + T: Sized, + { todo!() } } // @has foo/struct.Echo.html '//*[@class="impl"]//h3[@class="code-header"]' \ diff --git a/tests/rustdoc/whitespace-after-where-clause.trait.html b/tests/rustdoc/whitespace-after-where-clause.trait.html index bc3653de52d..8a78e82dc71 100644 --- a/tests/rustdoc/whitespace-after-where-clause.trait.html +++ b/tests/rustdoc/whitespace-after-where-clause.trait.html @@ -1,6 +1,7 @@
pub trait ToOwned<T>where
    T: Clone,
{ type Owned; + // Required methods fn to_owned(&self) -> Self::Owned; fn whatever(&self) -> T; }
\ No newline at end of file diff --git a/tests/rustdoc/whitespace-after-where-clause.trait2.html b/tests/rustdoc/whitespace-after-where-clause.trait2.html index eda4ca72acc..e6fafde1eff 100644 --- a/tests/rustdoc/whitespace-after-where-clause.trait2.html +++ b/tests/rustdoc/whitespace-after-where-clause.trait2.html @@ -1,6 +1,7 @@
pub trait ToOwned2<T: Clone> {
     type Owned;
 
+    // Required methods
     fn to_owned(&self) -> Self::Owned;
     fn whatever(&self) -> T;
 }
\ No newline at end of file