Skip to content

Commit

Permalink
Auto merge of #118441 - GuillaumeGomez:display-stability-version, r=r…
Browse files Browse the repository at this point in the history
…ustdoc

Always display stability version even if it's the same as the containing item

Fixes #118439.

Currently, if the containing item's version is the same as the item's version (like a method), we don't display it on the item.

This was something done on purpose as you can see [here](https://github.com/rust-lang/rust/blob/e9b7bf011478aa8c19ac49afc99853a66ba04319/src/librustdoc/html/render/mod.rs#L949-L955). It was implemented in #30686.

I think we should change this because on pages with a lot of items, if someone arrives (through the search or a link) to an item far below the page, they won't know the stability version unless they scroll to the top, which isn't great.

You can see the result [here](https://rustdoc.crud.net/imperio/display-stability-version/std/pin/struct.Pin.html#method.new).

r? `@notriddle`
  • Loading branch information
bors committed Apr 19, 2024
2 parents 07d0d7c + a333572 commit d1a0fa5
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 66 deletions.
7 changes: 0 additions & 7 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,13 +600,6 @@ impl Item {
}
}

pub(crate) fn const_stable_since(&self, tcx: TyCtxt<'_>) -> Option<StableSince> {
match self.const_stability(tcx)?.level {
StabilityLevel::Stable { since, .. } => Some(since),
StabilityLevel::Unstable { .. } => None,
}
}

pub(crate) fn is_non_exhaustive(&self) -> bool {
self.attrs.other_attrs.iter().any(|a| a.has_name(sym::non_exhaustive))
}
Expand Down
62 changes: 13 additions & 49 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,32 +996,20 @@ fn assoc_method(
/// consequence of the above rules.
fn render_stability_since_raw_with_extra(
w: &mut Buffer,
ver: Option<StableSince>,
stable_version: Option<StableSince>,
const_stability: Option<ConstStability>,
containing_ver: Option<StableSince>,
containing_const_ver: Option<StableSince>,
extra_class: &str,
) -> bool {
let stable_version = if ver != containing_ver
&& let Some(ver) = &ver
{
since_to_string(ver)
} else {
None
};

let mut title = String::new();
let mut stability = String::new();

if let Some(ver) = stable_version {
stability.push_str(ver.as_str());
title.push_str(&format!("Stable since Rust version {ver}"));
if let Some(version) = stable_version.and_then(|version| since_to_string(&version)) {
stability.push_str(&version);
title.push_str(&format!("Stable since Rust version {version}"));
}

let const_title_and_stability = match const_stability {
Some(ConstStability { level: StabilityLevel::Stable { since, .. }, .. })
if Some(since) != containing_const_ver =>
{
Some(ConstStability { level: StabilityLevel::Stable { since, .. }, .. }) => {
since_to_string(&since)
.map(|since| (format!("const since {since}"), format!("const: {since}")))
}
Expand Down Expand Up @@ -1076,17 +1064,8 @@ fn render_stability_since_raw(
w: &mut Buffer,
ver: Option<StableSince>,
const_stability: Option<ConstStability>,
containing_ver: Option<StableSince>,
containing_const_ver: Option<StableSince>,
) -> bool {
render_stability_since_raw_with_extra(
w,
ver,
const_stability,
containing_ver,
containing_const_ver,
"",
)
render_stability_since_raw_with_extra(w, ver, const_stability, "")
}

fn render_assoc_item(
Expand Down Expand Up @@ -1585,7 +1564,6 @@ fn render_impl(
cx: &mut Context<'_>,
item: &clean::Item,
parent: &clean::Item,
containing_item: &clean::Item,
link: AssocItemLink<'_>,
render_mode: RenderMode,
is_default_item: bool,
Expand Down Expand Up @@ -1681,7 +1659,7 @@ fn render_impl(
})
.map(|item| format!("{}.{name}", item.type_()));
write!(w, "<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">");
render_rightside(w, cx, item, containing_item, render_mode);
render_rightside(w, cx, item, render_mode);
if trait_.is_some() {
// Anchors are only used on trait impls.
write!(w, "<a href=\"#{id}\" class=\"anchor\">§</a>");
Expand All @@ -1703,7 +1681,7 @@ fn render_impl(
let source_id = format!("{item_type}.{name}");
let id = cx.derive_id(&source_id);
write!(w, "<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">");
render_rightside(w, cx, item, containing_item, render_mode);
render_rightside(w, cx, item, render_mode);
if trait_.is_some() {
// Anchors are only used on trait impls.
write!(w, "<a href=\"#{id}\" class=\"anchor\">§</a>");
Expand Down Expand Up @@ -1789,7 +1767,6 @@ fn render_impl(
cx,
trait_item,
if trait_.is_some() { &i.impl_item } else { parent },
parent,
link,
render_mode,
false,
Expand All @@ -1805,7 +1782,6 @@ fn render_impl(
t: &clean::Trait,
i: &clean::Impl,
parent: &clean::Item,
containing_item: &clean::Item,
render_mode: RenderMode,
rendering_params: ImplRenderingParameters,
) {
Expand Down Expand Up @@ -1833,7 +1809,6 @@ fn render_impl(
cx,
trait_item,
parent,
containing_item,
assoc_link,
render_mode,
true,
Expand All @@ -1856,7 +1831,6 @@ fn render_impl(
t,
i.inner_impl(),
&i.impl_item,
parent,
render_mode,
rendering_params,
);
Expand All @@ -1878,7 +1852,6 @@ fn render_impl(
cx,
i,
parent,
parent,
rendering_params.show_def_docs,
use_absolute,
aliases,
Expand Down Expand Up @@ -1926,20 +1899,14 @@ fn render_impl(

// Render the items that appear on the right side of methods, impls, and
// associated types. For example "1.0.0 (const: 1.39.0) · source".
fn render_rightside(
w: &mut Buffer,
cx: &Context<'_>,
item: &clean::Item,
containing_item: &clean::Item,
render_mode: RenderMode,
) {
fn render_rightside(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, render_mode: RenderMode) {
let tcx = cx.tcx();

// 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 => (item.const_stability(tcx), containing_item.const_stable_since(tcx)),
RenderMode::ForDeref { .. } => (None, None),
let const_stability = match render_mode {
RenderMode::Normal => item.const_stability(tcx),
RenderMode::ForDeref { .. } => None,
};
let src_href = cx.src_href(item);
let has_src_ref = src_href.is_some();
Expand All @@ -1949,8 +1916,6 @@ fn render_rightside(
&mut rightside,
item.stable_since(tcx),
const_stability,
containing_item.stable_since(tcx),
const_stable_since,
if has_src_ref { "" } else { " rightside" },
);
if let Some(link) = src_href {
Expand All @@ -1972,7 +1937,6 @@ pub(crate) fn render_impl_summary(
cx: &mut Context<'_>,
i: &Impl,
parent: &clean::Item,
containing_item: &clean::Item,
show_def_docs: bool,
use_absolute: Option<bool>,
// This argument is used to reference same type with different paths to avoid duplication
Expand All @@ -1987,7 +1951,7 @@ pub(crate) fn render_impl_summary(
format!(" data-aliases=\"{}\"", aliases.join(","))
};
write!(w, "<section id=\"{id}\" class=\"impl\"{aliases}>");
render_rightside(w, cx, &i.impl_item, containing_item, RenderMode::Normal);
render_rightside(w, cx, &i.impl_item, RenderMode::Normal);
write!(
w,
"<a href=\"#{id}\" class=\"anchor\">§</a>\
Expand Down
6 changes: 1 addition & 5 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,6 @@ pub(super) fn print_item(cx: &mut Context<'_>, item: &clean::Item, buf: &mut Buf
&mut stability_since_raw,
item.stable_since(cx.tcx()),
item.const_stability(cx.tcx()),
None,
None,
);
let stability_since_raw: String = stability_since_raw.into_inner();

Expand Down Expand Up @@ -825,7 +823,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
write!(w, "<details class=\"toggle{method_toggle_class}\" open><summary>");
}
write!(w, "<section id=\"{id}\" class=\"method\">");
render_rightside(w, cx, m, t, RenderMode::Normal);
render_rightside(w, cx, m, RenderMode::Normal);
write!(w, "<h4 class=\"code-header\">");
render_assoc_item(
w,
Expand Down Expand Up @@ -1686,8 +1684,6 @@ fn item_variants(
w,
variant.stable_since(tcx),
variant.const_stability(tcx),
it.stable_since(tcx),
it.const_stable_since(tcx),
" rightside",
);
w.write_str("<h3 class=\"code-header\">");
Expand Down
4 changes: 2 additions & 2 deletions tests/rustdoc/const-display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ impl Foo {
pub struct Bar;

impl Bar {
// Do not show non-const stabilities that are the same as the enclosing item.
// @matches 'foo/struct.Bar.html' '//span[@class="since"]' '^const: 1.2.0$'
// Show non-const stabilities that are the same as the enclosing item.
// @has 'foo/struct.Bar.html' '//span[@class="since"]' '1.0.0 (const: 1.2.0)'
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const2", since = "1.2.0")]
pub const fn stable_impl() -> u32 { 42 }
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc/deref/deref-const-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct Foo {

// @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"]' '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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc/ensure-src-link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

// This test ensures that the [src] link is present on traits items.

// @has foo/trait.Iterator.html '//*[@id="method.zip"]//a[@class="src rightside"]' "source"
// @has foo/trait.Iterator.html '//*[@id="method.zip"]//a[@class="src"]' "source"
pub use std::iter::Iterator;
2 changes: 1 addition & 1 deletion tests/rustdoc/implementor-stable-version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ pub struct Foo;
#[stable(feature = "foobar", since = "4.4.4")]
impl Bar for Foo {}

// @!has foo/trait.Baz.html '//div[@id="implementors-list"]//span[@class="since"]' '3.3.3'
// @has foo/trait.Baz.html '//div[@id="implementors-list"]//span[@class="since"]' '3.3.3'
#[stable(feature = "foobaz", since = "3.3.3")]
impl Baz for Foo {}

0 comments on commit d1a0fa5

Please sign in to comment.