From 8553c199e7c7764dce12eff38ee4235a0fe455d6 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 30 Aug 2021 16:04:28 +0200 Subject: [PATCH 1/5] Put back display of associated items (types and consts) --- src/librustdoc/html/render/mod.rs | 31 ++++++++++++++++++------ src/librustdoc/html/render/print_item.rs | 13 +++++++--- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 7704abc9a72c8..0b060468d05c5 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -714,7 +714,9 @@ fn render_impls( None, false, true, + true, &[], + true, ); buffer.into_inner() }) @@ -1053,7 +1055,9 @@ fn render_assoc_items( None, false, true, + true, &[], + true, ); } } @@ -1254,9 +1258,12 @@ fn render_impl( use_absolute: Option, is_on_foreign_type: bool, show_default_items: bool, + // It'll exclude methods. + show_non_assoc_items: bool, // This argument is used to reference same type with different paths to avoid duplication // in documentation pages for trait with automatic implementations like "Send" and "Sync". aliases: &[String], + toggle_open_by_default: bool, ) { let cache = cx.cache(); let traits = &cache.traits; @@ -1280,16 +1287,18 @@ fn render_impl( is_default_item: bool, trait_: Option<&clean::Trait>, show_def_docs: bool, + show_non_assoc_items: bool, ) { let item_type = item.type_(); let name = item.name.as_ref().unwrap(); - let render_method_item = match render_mode { - RenderMode::Normal => true, - RenderMode::ForDeref { mut_: deref_mut_ } => { - should_render_item(&item, deref_mut_, cx.cache()) - } - }; + let render_method_item = show_non_assoc_items + && match render_mode { + RenderMode::Normal => true, + RenderMode::ForDeref { mut_: deref_mut_ } => { + should_render_item(&item, deref_mut_, cx.cache()) + } + }; let in_trait_class = if trait_.is_some() { " trait-impl" } else { "" }; @@ -1456,6 +1465,7 @@ fn render_impl( false, trait_.map(|t| &t.trait_), show_def_docs, + show_non_assoc_items, ); } @@ -1469,6 +1479,7 @@ fn render_impl( containing_item: &clean::Item, render_mode: RenderMode, show_def_docs: bool, + show_non_assoc_items: bool, ) { for trait_item in &t.items { let n = trait_item.name; @@ -1491,6 +1502,7 @@ fn render_impl( true, Some(t), show_def_docs, + show_non_assoc_items, ); } } @@ -1511,6 +1523,7 @@ fn render_impl( parent, render_mode, show_def_docs, + show_non_assoc_items, ); } } @@ -1518,7 +1531,11 @@ fn render_impl( let toggled = !(impl_items.is_empty() && default_impl_items.is_empty()); if toggled { close_tags.insert_str(0, ""); - write!(w, "
"); + write!( + w, + "
", + if toggle_open_by_default { " open" } else { "" } + ); write!(w, "") } render_impl_summary( diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 8f4857a693928..5bc500498ffbf 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -16,8 +16,7 @@ use rustc_span::symbol::{kw, sym, Symbol}; use super::{ collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_strs, notable_traits_decl, render_assoc_item, render_assoc_items, render_attributes_in_code, render_attributes_in_pre, - render_impl, render_impl_summary, render_stability_since_raw, write_srclink, AssocItemLink, - Context, + render_impl, render_stability_since_raw, write_srclink, AssocItemLink, Context, }; use crate::clean::{self, GetDefId}; use crate::formats::item_type::ItemType; @@ -740,7 +739,9 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra None, true, false, + true, &[], + false, ); } } @@ -1388,16 +1389,20 @@ fn render_implementor( } => implementor_dups[&path.last()].1, _ => false, }; - render_impl_summary( + render_impl( w, cx, implementor, trait_, - trait_, + AssocItemLink::Anchor(None), + RenderMode::Normal, false, Some(use_absolute), false, + false, + false, aliases, + false, ); } From eb913664333036b185bd5d5f3df3ce270864aaa6 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 30 Aug 2021 16:11:10 +0200 Subject: [PATCH 2/5] Put back "auto-collapse-implementors" setting --- src/librustdoc/html/render/mod.rs | 1 + src/librustdoc/html/static/js/main.js | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 0b060468d05c5..70f1e6e024d3d 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -453,6 +453,7 @@ fn settings(root_path: &str, suffix: &str, themes: &[StylePath]) -> Result Date: Mon, 30 Aug 2021 16:47:17 +0200 Subject: [PATCH 3/5] Add tests for implementors associated types display --- src/test/rustdoc-gui/implementors.goml | 18 +++++++++--------- .../rustdoc-gui/src/lib2/implementors/lib.rs | 6 +++++- src/test/rustdoc-gui/src/lib2/lib.rs | 4 +++- src/test/rustdoc-gui/toggle-implementors.goml | 13 +++++++++++++ .../trait-impl-items-links-and-anchors.rs | 11 ++++++++++- 5 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 src/test/rustdoc-gui/toggle-implementors.goml diff --git a/src/test/rustdoc-gui/implementors.goml b/src/test/rustdoc-gui/implementors.goml index 87e4f2a7bd141..c9042eb4813ab 100644 --- a/src/test/rustdoc-gui/implementors.goml +++ b/src/test/rustdoc-gui/implementors.goml @@ -3,14 +3,14 @@ goto: file://|DOC_PATH|/implementors/trait.Whatever.html assert: "#implementors-list" // There are supposed to be two implementors listed. -assert-count: ("#implementors-list > .impl", 2) +assert-count: ("#implementors-list .impl", 2) // Now we check that both implementors have an anchor, an ID and a similar DOM. -assert: ("#implementors-list > .impl:nth-child(1) > a.anchor") -assert-attribute: ("#implementors-list > .impl:nth-child(1)", {"id": "impl-Whatever"}) -assert-attribute: ("#implementors-list > .impl:nth-child(1) > a.anchor", {"href": "#impl-Whatever"}) -assert: "#implementors-list > .impl:nth-child(1) > .code-header.in-band" +assert: ("#implementors-list .impl:nth-child(1) > a.anchor") +assert-attribute: ("#implementors-list .impl:nth-child(1)", {"id": "impl-Whatever"}) +assert-attribute: ("#implementors-list .impl:nth-child(1) > a.anchor", {"href": "#impl-Whatever"}) +assert: "#implementors-list .impl:nth-child(1) > .code-header.in-band" -assert: ("#implementors-list > .impl:nth-child(2) > a.anchor") -assert-attribute: ("#implementors-list > .impl:nth-child(2)", {"id": "impl-Whatever-1"}) -assert-attribute: ("#implementors-list > .impl:nth-child(2) > a.anchor", {"href": "#impl-Whatever-1"}) -assert: "#implementors-list > .impl:nth-child(2) > .code-header.in-band" +assert: ("#implementors-list .impl:nth-child(2) > a.anchor") +assert-attribute: ("#implementors-list .impl:nth-child(2)", {"id": "impl-Whatever-1"}) +assert-attribute: ("#implementors-list .impl:nth-child(2) > a.anchor", {"href": "#impl-Whatever-1"}) +assert: "#implementors-list .impl:nth-child(2) > .code-header.in-band" diff --git a/src/test/rustdoc-gui/src/lib2/implementors/lib.rs b/src/test/rustdoc-gui/src/lib2/implementors/lib.rs index 4b2f6962e30c5..6417a6ac5af6d 100644 --- a/src/test/rustdoc-gui/src/lib2/implementors/lib.rs +++ b/src/test/rustdoc-gui/src/lib2/implementors/lib.rs @@ -1,7 +1,11 @@ pub trait Whatever { + type Foo; + fn method() {} } pub struct Struct; -impl Whatever for Struct {} +impl Whatever for Struct { + type Foo = u8; +} diff --git a/src/test/rustdoc-gui/src/lib2/lib.rs b/src/test/rustdoc-gui/src/lib2/lib.rs index 36373d24971c9..cb63a9f600227 100644 --- a/src/test/rustdoc-gui/src/lib2/lib.rs +++ b/src/test/rustdoc-gui/src/lib2/lib.rs @@ -38,7 +38,9 @@ impl Trait for Foo { } -impl implementors::Whatever for Foo {} +impl implementors::Whatever for Foo { + type Foo = u32; +} pub mod sub_mod { /// ```txt diff --git a/src/test/rustdoc-gui/toggle-implementors.goml b/src/test/rustdoc-gui/toggle-implementors.goml new file mode 100644 index 0000000000000..4b2a266c7128a --- /dev/null +++ b/src/test/rustdoc-gui/toggle-implementors.goml @@ -0,0 +1,13 @@ +// This test ensures two things: +// +// 1. The implementors toggle are not open by default. +// 2. The "auto-collapse-implementors" setting is working as expected. +goto: file://|DOC_PATH|/implementors/trait.Whatever.html + +// First, checking that they are not open by default. +assert-attribute-false: ("#implementors-list > details", {"open": ""}, ALL) + +// Second, checking "auto-collapse-implementors" setting. +local-storage: {"rustdoc-auto-collapse-implementors": false} +reload: +assert-attribute: ("#implementors-list > details", {"open": ""}, ALL) diff --git a/src/test/rustdoc/trait-impl-items-links-and-anchors.rs b/src/test/rustdoc/trait-impl-items-links-and-anchors.rs index ddbe93febdc25..4d25835bf08fd 100644 --- a/src/test/rustdoc/trait-impl-items-links-and-anchors.rs +++ b/src/test/rustdoc/trait-impl-items-links-and-anchors.rs @@ -1,6 +1,6 @@ pub trait MyTrait { type Assoc; - const VALUE: u32; + const VALUE: u32 = 12; fn trait_function(&self); fn defaulted(&self) {} fn defaulted_override(&self) {} @@ -38,9 +38,11 @@ impl MyTrait for Vec { } impl MyTrait for MyStruct { + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="associatedtype.Assoc-3"]//a[@class="anchor"]/@href' #associatedtype.Assoc-3 // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="associatedtype.Assoc"]//a[@class="type"]/@href' trait.MyTrait.html#associatedtype.Assoc // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="associatedtype.Assoc"]//a[@class="anchor"]/@href' #associatedtype.Assoc type Assoc = bool; + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="associatedconstant.VALUE-3"]//a[@class="anchor"]/@href' #associatedconstant.VALUE-3 // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="associatedconstant.VALUE"]//a[@class="constant"]/@href' trait.MyTrait.html#associatedconstant.VALUE // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="associatedconstant.VALUE"]//a[@class="anchor"]/@href' #associatedconstant.VALUE const VALUE: u32 = 20; @@ -55,3 +57,10 @@ impl MyTrait for MyStruct { } pub struct MyStruct; + +// We check that associated items with default values aren't generated in the implementors list. +impl MyTrait for (u8, u8) { + // @!has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="associatedconstant.VALUE-4"]' + type Assoc = bool; + fn trait_function(&self) {} +} From fba4149bf1d8959299379a5dc351a61bdb5b8c92 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 31 Aug 2021 12:20:02 +0200 Subject: [PATCH 4/5] Clean render_impl arguments --- src/librustdoc/html/render/mod.rs | 88 ++++++++++++++---------- src/librustdoc/html/render/print_item.rs | 25 ++++--- 2 files changed, 67 insertions(+), 46 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 70f1e6e024d3d..a0a67610481ea 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -711,13 +711,15 @@ fn render_impls( containing_item, assoc_link, RenderMode::Normal, - true, None, - false, - true, - true, &[], - true, + ImplRenderingParameters { + show_def_docs: true, + is_on_foreign_type: false, + show_default_items: true, + show_non_assoc_items: true, + toggle_open_by_default: true, + }, ); buffer.into_inner() }) @@ -1052,13 +1054,15 @@ fn render_assoc_items( containing_item, AssocItemLink::Anchor(None), render_mode, - true, None, - false, - true, - true, &[], - true, + ImplRenderingParameters { + show_def_docs: true, + is_on_foreign_type: false, + show_default_items: true, + show_non_assoc_items: true, + toggle_open_by_default: true, + }, ); } } @@ -1248,6 +1252,16 @@ fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String { out.into_inner() } +#[derive(Clone, Copy, Debug)] +struct ImplRenderingParameters { + show_def_docs: bool, + is_on_foreign_type: bool, + show_default_items: bool, + /// Whether or not to show methods. + show_non_assoc_items: bool, + toggle_open_by_default: bool, +} + fn render_impl( w: &mut Buffer, cx: &Context<'_>, @@ -1255,16 +1269,9 @@ fn render_impl( parent: &clean::Item, link: AssocItemLink<'_>, render_mode: RenderMode, - show_def_docs: bool, use_absolute: Option, - is_on_foreign_type: bool, - show_default_items: bool, - // It'll exclude methods. - show_non_assoc_items: bool, - // This argument is used to reference same type with different paths to avoid duplication - // in documentation pages for trait with automatic implementations like "Send" and "Sync". aliases: &[String], - toggle_open_by_default: bool, + rendering_params: ImplRenderingParameters, ) { let cache = cx.cache(); let traits = &cache.traits; @@ -1287,13 +1294,12 @@ fn render_impl( render_mode: RenderMode, is_default_item: bool, trait_: Option<&clean::Trait>, - show_def_docs: bool, - show_non_assoc_items: bool, + rendering_params: ImplRenderingParameters, ) { let item_type = item.type_(); let name = item.name.as_ref().unwrap(); - let render_method_item = show_non_assoc_items + let render_method_item = rendering_params.show_non_assoc_items && match render_mode { RenderMode::Normal => true, RenderMode::ForDeref { mut_: deref_mut_ } => { @@ -1322,18 +1328,32 @@ fn render_impl( } else { // In case the item isn't documented, // provide short documentation from the trait. - document_short(&mut doc_buffer, it, cx, link, parent, show_def_docs); + document_short( + &mut doc_buffer, + it, + cx, + link, + parent, + rendering_params.show_def_docs, + ); } } } else { document_item_info(&mut info_buffer, cx, item, Some(parent)); - if show_def_docs { + if rendering_params.show_def_docs { document_full(&mut doc_buffer, item, cx); short_documented = false; } } } else { - document_short(&mut doc_buffer, item, cx, link, parent, show_def_docs); + document_short( + &mut doc_buffer, + item, + cx, + link, + parent, + rendering_params.show_def_docs, + ); } } let w = if short_documented && trait_.is_some() { interesting } else { boring }; @@ -1465,8 +1485,7 @@ fn render_impl( render_mode, false, trait_.map(|t| &t.trait_), - show_def_docs, - show_non_assoc_items, + rendering_params, ); } @@ -1479,8 +1498,7 @@ fn render_impl( parent: &clean::Item, containing_item: &clean::Item, render_mode: RenderMode, - show_def_docs: bool, - show_non_assoc_items: bool, + rendering_params: ImplRenderingParameters, ) { for trait_item in &t.items { let n = trait_item.name; @@ -1502,8 +1520,7 @@ fn render_impl( render_mode, true, Some(t), - show_def_docs, - show_non_assoc_items, + rendering_params, ); } } @@ -1512,7 +1529,7 @@ fn render_impl( // default items which weren't overridden in the implementation block. // We don't emit documentation for default items if they appear in the // Implementations on Foreign Types or Implementors sections. - if show_default_items { + if rendering_params.show_default_items { if let Some(t) = trait_ { render_default_items( &mut default_impl_items, @@ -1523,8 +1540,7 @@ fn render_impl( &i.impl_item, parent, render_mode, - show_def_docs, - show_non_assoc_items, + rendering_params, ); } } @@ -1535,7 +1551,7 @@ fn render_impl( write!( w, "
", - if toggle_open_by_default { " open" } else { "" } + if rendering_params.toggle_open_by_default { " open" } else { "" } ); write!(w, "") } @@ -1545,9 +1561,9 @@ fn render_impl( i, parent, parent, - show_def_docs, + rendering_params.show_def_docs, use_absolute, - is_on_foreign_type, + rendering_params.is_on_foreign_type, aliases, ); if toggled { diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 5bc500498ffbf..dac866c276779 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -17,6 +17,7 @@ use super::{ collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_strs, notable_traits_decl, render_assoc_item, render_assoc_items, render_attributes_in_code, render_attributes_in_pre, render_impl, render_stability_since_raw, write_srclink, AssocItemLink, Context, + ImplRenderingParameters, }; use crate::clean::{self, GetDefId}; use crate::formats::item_type::ItemType; @@ -735,13 +736,15 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra it, assoc_link, RenderMode::Normal, - false, None, - true, - false, - true, &[], - false, + ImplRenderingParameters { + show_def_docs: false, + is_on_foreign_type: true, + show_default_items: false, + show_non_assoc_items: true, + toggle_open_by_default: false, + }, ); } } @@ -1396,13 +1399,15 @@ fn render_implementor( trait_, AssocItemLink::Anchor(None), RenderMode::Normal, - false, Some(use_absolute), - false, - false, - false, aliases, - false, + ImplRenderingParameters { + show_def_docs: false, + is_on_foreign_type: false, + show_default_items: false, + show_non_assoc_items: false, + toggle_open_by_default: false, + }, ); } From 92dae39b64224ca9f706f5de8df09a72559947c5 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 1 Sep 2021 20:34:51 +0200 Subject: [PATCH 5/5] Remove implementors setting --- src/librustdoc/html/render/mod.rs | 1 - src/librustdoc/html/static/js/main.js | 5 ----- src/test/rustdoc-gui/toggle-implementors.goml | 11 +---------- 3 files changed, 1 insertion(+), 16 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index a0a67610481ea..c17ddcc1f1bdb 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -453,7 +453,6 @@ fn settings(root_path: &str, suffix: &str, themes: &[StylePath]) -> Result details", {"open": ""}, ALL) - -// Second, checking "auto-collapse-implementors" setting. -local-storage: {"rustdoc-auto-collapse-implementors": false} -reload: -assert-attribute: ("#implementors-list > details", {"open": ""}, ALL)