From 588a99bbddc0a4d1816df1c1f9fa23daeea2368d Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 27 Nov 2021 14:37:30 +0100 Subject: [PATCH 1/4] Fix display for "const" deref methods in rustdoc --- src/librustdoc/html/render/mod.rs | 39 +++++++++++++++----- src/librustdoc/html/render/print_item.rs | 45 +++++++++++++++++++++--- 2 files changed, 70 insertions(+), 14 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 08022d526fefb..5a2c1ea214dfa 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -861,6 +861,7 @@ fn render_assoc_item( link: AssocItemLink<'_>, parent: ItemType, cx: &Context<'_>, + render_mode: RenderMode, ) { fn method( w: &mut Buffer, @@ -871,6 +872,7 @@ fn render_assoc_item( link: AssocItemLink<'_>, parent: ItemType, cx: &Context<'_>, + render_mode: RenderMode, ) { let name = meth.name.as_ref().unwrap(); let href = match link { @@ -893,8 +895,14 @@ fn render_assoc_item( } }; let vis = meth.visibility.print_with_space(meth.def_id, cx).to_string(); - let constness = - print_constness_with_space(&header.constness, meth.const_stability(cx.tcx())); + // FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove + // this condition. + let constness = match render_mode { + RenderMode::Normal => { + print_constness_with_space(&header.constness, meth.const_stability(cx.tcx())) + } + RenderMode::ForDeref { .. } => "", + }; let asyncness = header.asyncness.print_with_space(); let unsafety = header.unsafety.print_with_space(); let defaultness = print_default_space(meth.is_default()); @@ -945,10 +953,10 @@ fn render_assoc_item( match *item.kind { clean::StrippedItem(..) => {} clean::TyMethodItem(ref m) => { - method(w, item, m.header, &m.generics, &m.decl, link, parent, cx) + method(w, item, m.header, &m.generics, &m.decl, link, parent, cx, render_mode) } clean::MethodItem(ref m, _) => { - method(w, item, m.header, &m.generics, &m.decl, link, parent, cx) + method(w, item, m.header, &m.generics, &m.decl, link, parent, cx, render_mode) } clean::AssocConstItem(ref ty, ref default) => assoc_const( w, @@ -1415,7 +1423,7 @@ fn render_impl( "
", id, item_type, in_trait_class, ); - render_rightside(w, cx, item, containing_item); + render_rightside(w, cx, item, containing_item, render_mode); write!(w, "", id); w.write_str("

"); render_assoc_item( @@ -1424,6 +1432,7 @@ fn render_impl( link.anchor(source_id.as_ref().unwrap_or(&id)), ItemType::Impl, cx, + render_mode, ); w.write_str("

"); w.write_str("
"); @@ -1459,7 +1468,7 @@ fn render_impl( "
", id, item_type, in_trait_class ); - render_rightside(w, cx, item, containing_item); + render_rightside(w, cx, item, containing_item, render_mode); write!(w, "", id); w.write_str("

"); assoc_const( @@ -1638,16 +1647,28 @@ fn render_rightside( cx: &Context<'_>, item: &clean::Item, containing_item: &clean::Item, + render_mode: RenderMode, ) { let tcx = cx.tcx(); + let const_stable_since; + // FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove + // this condition. + let (const_stability, const_stable_since) = match render_mode { + RenderMode::Normal => { + const_stable_since = containing_item.const_stable_since(tcx); + (item.const_stability(tcx), const_stable_since.as_deref()) + } + RenderMode::ForDeref { .. } => (None, None), + }; + write!(w, "
"); render_stability_since_raw( w, item.stable_since(tcx).as_deref(), - item.const_stability(tcx), + const_stability, containing_item.stable_since(tcx).as_deref(), - containing_item.const_stable_since(tcx).as_deref(), + const_stable_since, ); write_srclink(cx, item, w); @@ -1683,7 +1704,7 @@ pub(crate) fn render_impl_summary( format!(" data-aliases=\"{}\"", aliases.join(",")) }; write!(w, "
", id, aliases); - render_rightside(w, cx, &i.impl_item, containing_item); + render_rightside(w, cx, &i.impl_item, containing_item, RenderMode::Normal); write!(w, "", id); write!(w, "

"); diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index d3738cfa3e781..fc65b1d63cb05 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -556,7 +556,14 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra ); } for t in &types { - render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx); + render_assoc_item( + w, + t, + AssocItemLink::Anchor(None), + ItemType::Trait, + cx, + RenderMode::Normal, + ); w.write_str(";\n"); } // If there are too many associated constants, hide everything after them @@ -580,7 +587,14 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra w.write_str("\n"); } for t in &consts { - render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx); + render_assoc_item( + w, + t, + AssocItemLink::Anchor(None), + ItemType::Trait, + cx, + RenderMode::Normal, + ); w.write_str(";\n"); } if !toggle && should_hide_fields(count_methods) { @@ -591,7 +605,14 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra w.write_str("\n"); } for (pos, m) in required.iter().enumerate() { - render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait, cx); + render_assoc_item( + w, + m, + AssocItemLink::Anchor(None), + ItemType::Trait, + cx, + RenderMode::Normal, + ); w.write_str(";\n"); if pos < required.len() - 1 { @@ -602,7 +623,14 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra w.write_str("\n"); } for (pos, m) in provided.iter().enumerate() { - render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait, cx); + render_assoc_item( + w, + m, + AssocItemLink::Anchor(None), + ItemType::Trait, + cx, + RenderMode::Normal, + ); match *m.kind { clean::MethodItem(ref inner, _) if !inner.generics.where_predicates.is_empty() => @@ -655,7 +683,14 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra write_srclink(cx, m, w); write!(w, "

"); write!(w, "

"); - render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl, cx); + render_assoc_item( + w, + m, + AssocItemLink::Anchor(Some(&id)), + ItemType::Impl, + cx, + RenderMode::Normal, + ); w.write_str("

"); w.write_str("
"); if toggled { From 0c1b0f77576fe9b539a53322298dc26b75e255e8 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 27 Nov 2021 14:38:15 +0100 Subject: [PATCH 2/4] Add test for const deref methods display --- src/test/rustdoc/deref-const-fn.rs | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/test/rustdoc/deref-const-fn.rs diff --git a/src/test/rustdoc/deref-const-fn.rs b/src/test/rustdoc/deref-const-fn.rs new file mode 100644 index 0000000000000..ca51f3c7b5af0 --- /dev/null +++ b/src/test/rustdoc/deref-const-fn.rs @@ -0,0 +1,38 @@ +// This test ensures that the const methods from Deref aren't shown as const. +// For more information, see https://github.com/rust-lang/rust/issues/90855. + +#![crate_name = "foo"] + +#![feature(staged_api)] + +#![stable(feature = "rust1", since = "1.0.0")] + +// @has 'foo/struct.Bar.html' +#[stable(feature = "rust1", since = "1.0.0")] +pub struct Bar; + +impl Bar { + // @has - '//*[@id="method.len"]' 'pub const fn len(&self) -> usize' + // @has - '//*[@id="method.len"]//span[@class="since"]' '1.0.0 (const: 1.0.0)' + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_stable(feature = "rust1", since = "1.0.0")] + pub const fn len(&self) -> usize { 0 } +} + +#[stable(feature = "rust1", since = "1.0.0")] +pub struct Foo { + value: Bar, +} + +// @has 'foo/struct.Foo.html' +// @has - '//*[@id="method.len"]' 'pub fn len(&self) -> usize' +// @!has - '//*[@id="method.len"]//span[@class="since"]' '1.0.0' +// @!has - '//*[@id="method.len"]//span[@class="since"]' '(const: 1.0.0)' +#[stable(feature = "rust1", since = "1.0.0")] +impl std::ops::Deref for Foo { + type Target = Bar; + + fn deref(&self) -> &Self::Target { + &self.value + } +} From b647c902620f862106da2baf6eb5575289cb447f Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 29 Nov 2021 10:20:11 +0100 Subject: [PATCH 3/4] Improve code by replacing &str with Symbol in render_stability_since_raw --- src/librustdoc/clean/types.rs | 8 ++++---- src/librustdoc/html/render/mod.rs | 18 +++++++----------- src/librustdoc/html/render/print_item.rs | 8 ++++---- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 37acf68defd25..aac7dbaca2b81 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -602,16 +602,16 @@ impl Item { }) } - crate fn stable_since(&self, tcx: TyCtxt<'_>) -> Option { + crate fn stable_since(&self, tcx: TyCtxt<'_>) -> Option { match self.stability(tcx)?.level { - StabilityLevel::Stable { since, .. } => Some(since.as_str()), + StabilityLevel::Stable { since, .. } => Some(since), StabilityLevel::Unstable { .. } => None, } } - crate fn const_stable_since(&self, tcx: TyCtxt<'_>) -> Option { + crate fn const_stable_since(&self, tcx: TyCtxt<'_>) -> Option { match self.const_stability(tcx)?.level { - StabilityLevel::Stable { since, .. } => Some(since.as_str()), + StabilityLevel::Stable { since, .. } => Some(since), StabilityLevel::Unstable { .. } => None, } } diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 5a2c1ea214dfa..eaeda4d178bb1 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -804,17 +804,17 @@ fn assoc_type( fn render_stability_since_raw( w: &mut Buffer, - ver: Option<&str>, + ver: Option, const_stability: Option<&ConstStability>, - containing_ver: Option<&str>, - containing_const_ver: Option<&str>, + containing_ver: Option, + containing_const_ver: Option, ) { let ver = ver.filter(|inner| !inner.is_empty()); match (ver, const_stability) { // stable and const stable (Some(v), Some(ConstStability { level: StabilityLevel::Stable { since }, .. })) - if Some(since.as_str()).as_deref() != containing_const_ver => + if Some(*since) != containing_const_ver => { write!( w, @@ -1651,23 +1651,19 @@ fn render_rightside( ) { let tcx = cx.tcx(); - let const_stable_since; // FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove // this condition. let (const_stability, const_stable_since) = match render_mode { - RenderMode::Normal => { - const_stable_since = containing_item.const_stable_since(tcx); - (item.const_stability(tcx), const_stable_since.as_deref()) - } + RenderMode::Normal => (item.const_stability(tcx), containing_item.const_stable_since(tcx)), RenderMode::ForDeref { .. } => (None, None), }; write!(w, "
"); render_stability_since_raw( w, - item.stable_since(tcx).as_deref(), + item.stable_since(tcx), const_stability, - containing_item.stable_since(tcx).as_deref(), + containing_item.stable_since(tcx), const_stable_since, ); diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index fc65b1d63cb05..da66cd20d62b2 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -101,7 +101,7 @@ pub(super) fn print_item( let mut stability_since_raw = Buffer::new(); render_stability_since_raw( &mut stability_since_raw, - item.stable_since(cx.tcx()).as_deref(), + item.stable_since(cx.tcx()), item.const_stability(cx.tcx()), None, None, @@ -1462,10 +1462,10 @@ fn render_stability_since( ) { render_stability_since_raw( w, - item.stable_since(tcx).as_deref(), + item.stable_since(tcx), item.const_stability(tcx), - containing_item.stable_since(tcx).as_deref(), - containing_item.const_stable_since(tcx).as_deref(), + containing_item.stable_since(tcx), + containing_item.const_stable_since(tcx), ) } From 02782bba604e3edc5d356dd3e153667cf1474949 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 29 Nov 2021 10:24:51 +0100 Subject: [PATCH 4/4] Return ConstStability instead of &ConstStability in Item::const_stability --- src/librustdoc/clean/types.rs | 4 ++-- src/librustdoc/html/format.rs | 5 +---- src/librustdoc/html/render/mod.rs | 4 ++-- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index aac7dbaca2b81..5aaaa99dadb40 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -376,8 +376,8 @@ impl Item { self.def_id.as_def_id().and_then(|did| tcx.lookup_stability(did)) } - crate fn const_stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<&'tcx ConstStability> { - self.def_id.as_def_id().and_then(|did| tcx.lookup_const_stability(did)) + crate fn const_stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option { + self.def_id.as_def_id().and_then(|did| tcx.lookup_const_stability(did)).map(|cs| *cs) } crate fn deprecation(&self, tcx: TyCtxt<'_>) -> Option { diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 34742fac0e4b7..fdb52703edf77 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -1349,10 +1349,7 @@ impl PrintWithSpace for hir::Mutability { } } -crate fn print_constness_with_space( - c: &hir::Constness, - s: Option<&ConstStability>, -) -> &'static str { +crate fn print_constness_with_space(c: &hir::Constness, s: Option) -> &'static str { match (c, s) { // const stable or when feature(staged_api) is not set ( diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index eaeda4d178bb1..a1db66a4d52db 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -805,7 +805,7 @@ fn assoc_type( fn render_stability_since_raw( w: &mut Buffer, ver: Option, - const_stability: Option<&ConstStability>, + const_stability: Option, containing_ver: Option, containing_const_ver: Option, ) { @@ -814,7 +814,7 @@ fn render_stability_since_raw( match (ver, const_stability) { // stable and const stable (Some(v), Some(ConstStability { level: StabilityLevel::Stable { since }, .. })) - if Some(*since) != containing_const_ver => + if Some(since) != containing_const_ver => { write!( w,