From e1b6bb214fbc9d2a9af5c029679822e90dfd9660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Thu, 11 Mar 2021 03:31:54 +0100 Subject: [PATCH 1/8] fix links from trait impl methods to trait declaration --- src/librustdoc/html/render/mod.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index fbe799e718482..a95cfc1136725 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -912,10 +912,9 @@ fn render_assoc_item( let cache = cx.cache(); let tcx = cx.tcx(); let name = meth.name.as_ref().unwrap(); - let anchor = format!("#{}.{}", meth.type_(), name); let href = match link { AssocItemLink::Anchor(Some(ref id)) => format!("#{}", id), - AssocItemLink::Anchor(None) => anchor, + AssocItemLink::Anchor(None) => format!("#{}.{}", meth.type_(), name), AssocItemLink::GotoSource(did, provided_methods) => { // We're creating a link from an impl-item to the corresponding // trait-item and need to map the anchored type accordingly. @@ -925,7 +924,9 @@ fn render_assoc_item( ItemType::TyMethod }; - href(did, cache).map(|p| format!("{}#{}.{}", p.0, ty, name)).unwrap_or(anchor) + href(did, cache) + .map(|p| format!("{}#{}.{}", p.0, ty, name)) + .unwrap_or_else(|| format!("#{}.{}", ty, name)) } }; let vis = meth.visibility.print_with_space(tcx, meth.def_id, cache).to_string(); From 45964368f4a2e31c94e9bcf1cef933c087d21544 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Thu, 11 Mar 2021 03:32:30 +0100 Subject: [PATCH 2/8] add anchors links on hover to items from trait impl --- src/librustdoc/html/render/mod.rs | 19 +++++++++++++------ src/librustdoc/html/static/rustdoc.css | 5 +++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index a95cfc1136725..9f9a3acd8523b 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -1453,12 +1453,13 @@ fn render_impl( } else { (true, " hidden") }; + let in_trait_class = if trait_.is_some() { " trait-impl" } else { "" }; match *item.kind { clean::MethodItem(..) | clean::TyMethodItem(_) => { // Only render when the method is not static or we allow static methods if render_method_item { let id = cx.derive_id(format!("{}.{}", item_type, name)); - write!(w, "

", id, item_type, extra_class); + write!(w, "

", id, item_type, extra_class, in_trait_class); w.write_str(""); render_assoc_item(w, item, link.anchor(&id), ItemType::Impl, cx); w.write_str(""); @@ -1469,13 +1470,14 @@ fn render_impl( outer_version, outer_const_version, ); + write!(w, "", id); write_srclink(cx, item, w); w.write_str("

"); } } clean::TypedefItem(ref tydef, _) => { let id = cx.derive_id(format!("{}.{}", ItemType::AssocType, name)); - write!(w, "

", id, item_type, extra_class); + write!(w, "

", id, item_type, extra_class, in_trait_class); assoc_type( w, item, @@ -1486,11 +1488,13 @@ fn render_impl( cx.cache(), tcx, ); - w.write_str("

"); + w.write_str("
"); + write!(w, "", id); + w.write_str("

"); } clean::AssocConstItem(ref ty, ref default) => { let id = cx.derive_id(format!("{}.{}", item_type, name)); - write!(w, "

", id, item_type, extra_class); + write!(w, "

", id, item_type, extra_class, in_trait_class); assoc_const(w, item, ty, default.as_ref(), link.anchor(&id), "", cx); w.write_str(""); render_stability_since_raw( @@ -1500,12 +1504,13 @@ fn render_impl( outer_version, outer_const_version, ); + write!(w, "", id); write_srclink(cx, item, w); w.write_str("

"); } clean::AssocTypeItem(ref bounds, ref default) => { let id = cx.derive_id(format!("{}.{}", item_type, name)); - write!(w, "

", id, item_type, extra_class); + write!(w, "

", id, item_type, extra_class, in_trait_class); assoc_type( w, item, @@ -1516,7 +1521,9 @@ fn render_impl( cx.cache(), tcx, ); - w.write_str("

"); + w.write_str("
"); + write!(w, "", id); + w.write_str("

"); } clean::StrippedItem(..) => return, _ => panic!("can't make docs for trait item with name {:?}", item.name), diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index 585b7459bd717..2f9f8fb733508 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -133,7 +133,7 @@ h3.impl, h3.method, h4.method, h3.type, h4.type, h4.associatedconstant { margin-bottom: 10px; position: relative; } -h3.impl, h3.method, h3.type { +h3.impl, h3.method, h4.method.trait-impl, h3.type, h4.type.trait-impl, h4.associatedconstant.trait-impl { padding-left: 15px; } @@ -655,7 +655,8 @@ a { display: initial; } -.in-band:hover > .anchor, .impl:hover > .anchor { +.in-band:hover > .anchor, .impl:hover > .anchor, .method.trait-impl:hover > .anchor, +.type.trait-impl:hover > .anchor, .associatedconstant.trait-impl:hover > .anchor { display: inline-block; position: absolute; } From 838c2ef1112b0aeff1152f71caec12b2606df3f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Sat, 13 Mar 2021 01:40:13 +0100 Subject: [PATCH 3/8] fix source link when in a trait implementation --- src/librustdoc/html/render/mod.rs | 38 ++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 9f9a3acd8523b..1eb70eb6364cc 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -1459,9 +1459,20 @@ fn render_impl( // Only render when the method is not static or we allow static methods if render_method_item { let id = cx.derive_id(format!("{}.{}", item_type, name)); + let source_id = trait_ + .and_then(|trait_| trait_ + .items.iter() + .find(|item| item.name.map(|n| n.as_str().eq(&name.as_str())).unwrap_or(false)) + ).map(|item| format!("{}.{}", item.type_(), name)); write!(w, "

", id, item_type, extra_class, in_trait_class); w.write_str(""); - render_assoc_item(w, item, link.anchor(&id), ItemType::Impl, cx); + render_assoc_item( + w, + item, + link.anchor(source_id.as_ref().unwrap_or(&id)), + ItemType::Impl, + cx + ); w.write_str(""); render_stability_since_raw( w, @@ -1476,14 +1487,15 @@ fn render_impl( } } clean::TypedefItem(ref tydef, _) => { - let id = cx.derive_id(format!("{}.{}", ItemType::AssocType, name)); + let source_id = format!("{}.{}", ItemType::AssocType, name); + let id = cx.derive_id(source_id.clone()); write!(w, "

", id, item_type, extra_class, in_trait_class); assoc_type( w, item, &Vec::new(), Some(&tydef.type_), - link.anchor(&id), + link.anchor(if trait_.is_some() { &source_id } else { &id }), "", cx.cache(), tcx, @@ -1493,9 +1505,18 @@ fn render_impl( w.write_str("

"); } clean::AssocConstItem(ref ty, ref default) => { - let id = cx.derive_id(format!("{}.{}", item_type, name)); + let source_id = format!("{}.{}", item_type, name); + let id = cx.derive_id(source_id.clone()); write!(w, "

", id, item_type, extra_class, in_trait_class); - assoc_const(w, item, ty, default.as_ref(), link.anchor(&id), "", cx); + assoc_const( + w, + item, + ty, + default.as_ref(), + link.anchor(if trait_.is_some() { &source_id } else { &id }), + "", + cx + ); w.write_str(""); render_stability_since_raw( w, @@ -1509,14 +1530,15 @@ fn render_impl( w.write_str("

"); } clean::AssocTypeItem(ref bounds, ref default) => { - let id = cx.derive_id(format!("{}.{}", item_type, name)); + let source_id = format!("{}.{}", item_type, name); + let id = cx.derive_id(source_id.clone()); write!(w, "

", id, item_type, extra_class, in_trait_class); assoc_type( w, item, bounds, default.as_ref(), - link.anchor(&id), + link.anchor(if trait_.is_some() { &source_id } else { &id }), "", cx.cache(), tcx, @@ -1613,7 +1635,7 @@ fn render_impl( true, outer_version, outer_const_version, - None, + Some(t), show_def_docs, ); } From df100f7c5388175605ccf8450e7077bd5939629a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Sat, 13 Mar 2021 02:13:00 +0100 Subject: [PATCH 4/8] format css --- src/librustdoc/html/static/rustdoc.css | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index 2f9f8fb733508..705ae17f3eb32 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -133,7 +133,8 @@ h3.impl, h3.method, h4.method, h3.type, h4.type, h4.associatedconstant { margin-bottom: 10px; position: relative; } -h3.impl, h3.method, h4.method.trait-impl, h3.type, h4.type.trait-impl, h4.associatedconstant.trait-impl { +h3.impl, h3.method, h4.method.trait-impl, h3.type, +h4.type.trait-impl, h4.associatedconstant.trait-impl { padding-left: 15px; } From b9f9a2a68feb99f0c3076e78f43b4459cb4e8968 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Sat, 13 Mar 2021 02:33:04 +0100 Subject: [PATCH 5/8] tidy format rust --- src/librustdoc/html/render/mod.rs | 38 +++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 1eb70eb6364cc..efd453f96b8e7 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -1460,18 +1460,24 @@ fn render_impl( if render_method_item { let id = cx.derive_id(format!("{}.{}", item_type, name)); let source_id = trait_ - .and_then(|trait_| trait_ - .items.iter() - .find(|item| item.name.map(|n| n.as_str().eq(&name.as_str())).unwrap_or(false)) - ).map(|item| format!("{}.{}", item.type_(), name)); - write!(w, "

", id, item_type, extra_class, in_trait_class); + .and_then(|trait_| { + trait_.items.iter().find(|item| { + item.name.map(|n| n.as_str().eq(&name.as_str())).unwrap_or(false) + }) + }) + .map(|item| format!("{}.{}", item.type_(), name)); + write!( + w, + "

", + id, item_type, extra_class, in_trait_class, + ); w.write_str(""); render_assoc_item( w, item, link.anchor(source_id.as_ref().unwrap_or(&id)), ItemType::Impl, - cx + cx, ); w.write_str(""); render_stability_since_raw( @@ -1489,7 +1495,11 @@ fn render_impl( clean::TypedefItem(ref tydef, _) => { let source_id = format!("{}.{}", ItemType::AssocType, name); let id = cx.derive_id(source_id.clone()); - write!(w, "

", id, item_type, extra_class, in_trait_class); + write!( + w, + "

", + id, item_type, extra_class, in_trait_class + ); assoc_type( w, item, @@ -1507,7 +1517,11 @@ fn render_impl( clean::AssocConstItem(ref ty, ref default) => { let source_id = format!("{}.{}", item_type, name); let id = cx.derive_id(source_id.clone()); - write!(w, "

", id, item_type, extra_class, in_trait_class); + write!( + w, + "

", + id, item_type, extra_class, in_trait_class + ); assoc_const( w, item, @@ -1515,7 +1529,7 @@ fn render_impl( default.as_ref(), link.anchor(if trait_.is_some() { &source_id } else { &id }), "", - cx + cx, ); w.write_str(""); render_stability_since_raw( @@ -1532,7 +1546,11 @@ fn render_impl( clean::AssocTypeItem(ref bounds, ref default) => { let source_id = format!("{}.{}", item_type, name); let id = cx.derive_id(source_id.clone()); - write!(w, "

", id, item_type, extra_class, in_trait_class); + write!( + w, + "

", + id, item_type, extra_class, in_trait_class + ); assoc_type( w, item, From 26ffef061f5d054b60ca9da2e32a7e8a466b4665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Sat, 13 Mar 2021 15:04:12 +0100 Subject: [PATCH 6/8] add test --- .../trait-impl-items-links-and-anchors.rs | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/test/rustdoc/trait-impl-items-links-and-anchors.rs diff --git a/src/test/rustdoc/trait-impl-items-links-and-anchors.rs b/src/test/rustdoc/trait-impl-items-links-and-anchors.rs new file mode 100644 index 0000000000000..b61495b5d7f86 --- /dev/null +++ b/src/test/rustdoc/trait-impl-items-links-and-anchors.rs @@ -0,0 +1,65 @@ +// ignore-tidy-linelength + +pub trait MyTrait { + type Assoc; + const VALUE: u32; + fn trait_function(&self); + fn defaulted(&self) {} + fn defaulted_override(&self) {} +} + + +impl MyTrait for String { + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="associatedtype.Assoc-1"]//a[@class="type"]/@href' #associatedtype.Assoc + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="associatedtype.Assoc-1"]//a[@class="anchor"]/@href' #associatedtype.Assoc-1 + type Assoc = (); + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="associatedconstant.VALUE-1"]//a[@class="constant"]/@href' #associatedconstant.VALUE + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="associatedconstant.VALUE-1"]//a[@class="anchor"]/@href' #associatedconstant.VALUE-1 + const VALUE: u32 = 5; + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="method.trait_function"]//a[@class="fnname"]/@href' #tymethod.trait_function + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="method.trait_function"]//a[@class="anchor"]/@href' #method.trait_function + fn trait_function(&self) {} + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="method.defaulted_override-1"]//a[@class="fnname"]/@href' #method.defaulted_override + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="method.defaulted_override-1"]//a[@class="anchor"]/@href' #method.defaulted_override-1 + fn defaulted_override(&self) {} +} + +impl MyTrait for Vec { + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="associatedtype.Assoc-2"]//a[@class="type"]/@href' #associatedtype.Assoc + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="associatedtype.Assoc-2"]//a[@class="anchor"]/@href' #associatedtype.Assoc-2 + type Assoc = (); + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="associatedconstant.VALUE-2"]//a[@class="constant"]/@href' #associatedconstant.VALUE + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="associatedconstant.VALUE-2"]//a[@class="anchor"]/@href' #associatedconstant.VALUE-2 + const VALUE: u32 = 5; + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="method.trait_function"]//a[@class="fnname"]/@href' #tymethod.trait_function + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="method.trait_function-1"]//a[@class="anchor"]/@href' #method.trait_function-1 + fn trait_function(&self) {} + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="method.defaulted_override-2"]//a[@class="fnname"]/@href' #method.defaulted_override + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="method.defaulted_override-2"]//a[@class="anchor"]/@href' #method.defaulted_override-2 + fn defaulted_override(&self) {} +} + +impl MyTrait for MyStruct { + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="associatedtype.Assoc-3"]//a[@class="type"]/@href' #associatedtype.Assoc + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="associatedtype.Assoc-3"]//a[@class="anchor"]/@href' #associatedtype.Assoc-3 + // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//h4[@id="associatedtype.Assoc"]//a[@class="type"]/@href' ../trait_impl_items_links_and_anchors/trait.MyTrait.html#associatedtype.Assoc + // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//h4[@id="associatedtype.Assoc"]//a[@class="anchor"]/@href' #associatedtype.Assoc + type Assoc = bool; + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="associatedconstant.VALUE-3"]//a[@class="constant"]/@href' #associatedconstant.VALUE + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="associatedconstant.VALUE-3"]//a[@class="anchor"]/@href' #associatedconstant.VALUE-3 + // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//h4[@id="associatedconstant.VALUE"]//a[@class="constant"]/@href' ../trait_impl_items_links_and_anchors/trait.MyTrait.html#associatedconstant.VALUE + // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//h4[@id="associatedconstant.VALUE"]//a[@class="anchor"]/@href' #associatedconstant.VALUE + const VALUE: u32 = 20; + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="method.trait_function-2"]//a[@class="fnname"]/@href' #tymethod.trait_function + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="method.trait_function-2"]//a[@class="anchor"]/@href' #method.trait_function-2 + // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//h4[@id="method.trait_function"]//a[@class="fnname"]/@href' ../trait_impl_items_links_and_anchors/trait.MyTrait.html#tymethod.trait_function + // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//h4[@id="method.trait_function"]//a[@class="anchor"]/@href' #method.trait_function + fn trait_function(&self) {} + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="method.defaulted_override-3"]//a[@class="fnname"]/@href' #method.defaulted_override + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//h4[@id="method.defaulted_override-3"]//a[@class="anchor"]/@href' #method.defaulted_override-3 + // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//h4[@id="method.defaulted_override"]//a[@class="fnname"]/@href' ../trait_impl_items_links_and_anchors/trait.MyTrait.html#method.defaulted_override + // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//h4[@id="method.defaulted_override"]//a[@class="anchor"]/@href' #method.defaulted_override + fn defaulted_override(&self) {} +} + +pub struct MyStruct; From 7f395302d3ad5f8da50e440924bb086c8c4b531d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Sat, 13 Mar 2021 15:24:00 +0100 Subject: [PATCH 7/8] forgot test assertions for default method --- src/test/rustdoc/trait-impl-items-links-and-anchors.rs | 2 ++ 1 file changed, 2 insertions(+) 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 b61495b5d7f86..b063c58cc409d 100644 --- a/src/test/rustdoc/trait-impl-items-links-and-anchors.rs +++ b/src/test/rustdoc/trait-impl-items-links-and-anchors.rs @@ -60,6 +60,8 @@ impl MyTrait for MyStruct { // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//h4[@id="method.defaulted_override"]//a[@class="fnname"]/@href' ../trait_impl_items_links_and_anchors/trait.MyTrait.html#method.defaulted_override // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//h4[@id="method.defaulted_override"]//a[@class="anchor"]/@href' #method.defaulted_override fn defaulted_override(&self) {} + // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//h4[@id="method.defaulted"]//a[@class="fnname"]/@href' ../trait_impl_items_links_and_anchors/trait.MyTrait.html#method.defaulted + // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//h4[@id="method.defaulted"]//a[@class="anchor"]/@href' #method.defaulted } pub struct MyStruct; From e36ca09101830c9b1968891fc1adfe88654f6a1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Tue, 13 Apr 2021 23:32:05 +0200 Subject: [PATCH 8/8] remove line length ignore --- src/test/rustdoc/trait-impl-items-links-and-anchors.rs | 2 -- 1 file changed, 2 deletions(-) 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 b063c58cc409d..6c09be1144a83 100644 --- a/src/test/rustdoc/trait-impl-items-links-and-anchors.rs +++ b/src/test/rustdoc/trait-impl-items-links-and-anchors.rs @@ -1,5 +1,3 @@ -// ignore-tidy-linelength - pub trait MyTrait { type Assoc; const VALUE: u32;