Skip to content

Commit d1a0fa5

Browse files
committed
Auto merge of #118441 - GuillaumeGomez:display-stability-version, r=rustdoc
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`
2 parents 07d0d7c + a333572 commit d1a0fa5

File tree

7 files changed

+19
-66
lines changed

7 files changed

+19
-66
lines changed

src/librustdoc/clean/types.rs

-7
Original file line numberDiff line numberDiff line change
@@ -600,13 +600,6 @@ impl Item {
600600
}
601601
}
602602

603-
pub(crate) fn const_stable_since(&self, tcx: TyCtxt<'_>) -> Option<StableSince> {
604-
match self.const_stability(tcx)?.level {
605-
StabilityLevel::Stable { since, .. } => Some(since),
606-
StabilityLevel::Unstable { .. } => None,
607-
}
608-
}
609-
610603
pub(crate) fn is_non_exhaustive(&self) -> bool {
611604
self.attrs.other_attrs.iter().any(|a| a.has_name(sym::non_exhaustive))
612605
}

src/librustdoc/html/render/mod.rs

+13-49
Original file line numberDiff line numberDiff line change
@@ -996,32 +996,20 @@ fn assoc_method(
996996
/// consequence of the above rules.
997997
fn render_stability_since_raw_with_extra(
998998
w: &mut Buffer,
999-
ver: Option<StableSince>,
999+
stable_version: Option<StableSince>,
10001000
const_stability: Option<ConstStability>,
1001-
containing_ver: Option<StableSince>,
1002-
containing_const_ver: Option<StableSince>,
10031001
extra_class: &str,
10041002
) -> bool {
1005-
let stable_version = if ver != containing_ver
1006-
&& let Some(ver) = &ver
1007-
{
1008-
since_to_string(ver)
1009-
} else {
1010-
None
1011-
};
1012-
10131003
let mut title = String::new();
10141004
let mut stability = String::new();
10151005

1016-
if let Some(ver) = stable_version {
1017-
stability.push_str(ver.as_str());
1018-
title.push_str(&format!("Stable since Rust version {ver}"));
1006+
if let Some(version) = stable_version.and_then(|version| since_to_string(&version)) {
1007+
stability.push_str(&version);
1008+
title.push_str(&format!("Stable since Rust version {version}"));
10191009
}
10201010

10211011
let const_title_and_stability = match const_stability {
1022-
Some(ConstStability { level: StabilityLevel::Stable { since, .. }, .. })
1023-
if Some(since) != containing_const_ver =>
1024-
{
1012+
Some(ConstStability { level: StabilityLevel::Stable { since, .. }, .. }) => {
10251013
since_to_string(&since)
10261014
.map(|since| (format!("const since {since}"), format!("const: {since}")))
10271015
}
@@ -1076,17 +1064,8 @@ fn render_stability_since_raw(
10761064
w: &mut Buffer,
10771065
ver: Option<StableSince>,
10781066
const_stability: Option<ConstStability>,
1079-
containing_ver: Option<StableSince>,
1080-
containing_const_ver: Option<StableSince>,
10811067
) -> bool {
1082-
render_stability_since_raw_with_extra(
1083-
w,
1084-
ver,
1085-
const_stability,
1086-
containing_ver,
1087-
containing_const_ver,
1088-
"",
1089-
)
1068+
render_stability_since_raw_with_extra(w, ver, const_stability, "")
10901069
}
10911070

10921071
fn render_assoc_item(
@@ -1585,7 +1564,6 @@ fn render_impl(
15851564
cx: &mut Context<'_>,
15861565
item: &clean::Item,
15871566
parent: &clean::Item,
1588-
containing_item: &clean::Item,
15891567
link: AssocItemLink<'_>,
15901568
render_mode: RenderMode,
15911569
is_default_item: bool,
@@ -1681,7 +1659,7 @@ fn render_impl(
16811659
})
16821660
.map(|item| format!("{}.{name}", item.type_()));
16831661
write!(w, "<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">");
1684-
render_rightside(w, cx, item, containing_item, render_mode);
1662+
render_rightside(w, cx, item, render_mode);
16851663
if trait_.is_some() {
16861664
// Anchors are only used on trait impls.
16871665
write!(w, "<a href=\"#{id}\" class=\"anchor\">§</a>");
@@ -1703,7 +1681,7 @@ fn render_impl(
17031681
let source_id = format!("{item_type}.{name}");
17041682
let id = cx.derive_id(&source_id);
17051683
write!(w, "<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">");
1706-
render_rightside(w, cx, item, containing_item, render_mode);
1684+
render_rightside(w, cx, item, render_mode);
17071685
if trait_.is_some() {
17081686
// Anchors are only used on trait impls.
17091687
write!(w, "<a href=\"#{id}\" class=\"anchor\">§</a>");
@@ -1789,7 +1767,6 @@ fn render_impl(
17891767
cx,
17901768
trait_item,
17911769
if trait_.is_some() { &i.impl_item } else { parent },
1792-
parent,
17931770
link,
17941771
render_mode,
17951772
false,
@@ -1805,7 +1782,6 @@ fn render_impl(
18051782
t: &clean::Trait,
18061783
i: &clean::Impl,
18071784
parent: &clean::Item,
1808-
containing_item: &clean::Item,
18091785
render_mode: RenderMode,
18101786
rendering_params: ImplRenderingParameters,
18111787
) {
@@ -1833,7 +1809,6 @@ fn render_impl(
18331809
cx,
18341810
trait_item,
18351811
parent,
1836-
containing_item,
18371812
assoc_link,
18381813
render_mode,
18391814
true,
@@ -1856,7 +1831,6 @@ fn render_impl(
18561831
t,
18571832
i.inner_impl(),
18581833
&i.impl_item,
1859-
parent,
18601834
render_mode,
18611835
rendering_params,
18621836
);
@@ -1878,7 +1852,6 @@ fn render_impl(
18781852
cx,
18791853
i,
18801854
parent,
1881-
parent,
18821855
rendering_params.show_def_docs,
18831856
use_absolute,
18841857
aliases,
@@ -1926,20 +1899,14 @@ fn render_impl(
19261899

19271900
// Render the items that appear on the right side of methods, impls, and
19281901
// associated types. For example "1.0.0 (const: 1.39.0) · source".
1929-
fn render_rightside(
1930-
w: &mut Buffer,
1931-
cx: &Context<'_>,
1932-
item: &clean::Item,
1933-
containing_item: &clean::Item,
1934-
render_mode: RenderMode,
1935-
) {
1902+
fn render_rightside(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, render_mode: RenderMode) {
19361903
let tcx = cx.tcx();
19371904

19381905
// FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove
19391906
// this condition.
1940-
let (const_stability, const_stable_since) = match render_mode {
1941-
RenderMode::Normal => (item.const_stability(tcx), containing_item.const_stable_since(tcx)),
1942-
RenderMode::ForDeref { .. } => (None, None),
1907+
let const_stability = match render_mode {
1908+
RenderMode::Normal => item.const_stability(tcx),
1909+
RenderMode::ForDeref { .. } => None,
19431910
};
19441911
let src_href = cx.src_href(item);
19451912
let has_src_ref = src_href.is_some();
@@ -1949,8 +1916,6 @@ fn render_rightside(
19491916
&mut rightside,
19501917
item.stable_since(tcx),
19511918
const_stability,
1952-
containing_item.stable_since(tcx),
1953-
const_stable_since,
19541919
if has_src_ref { "" } else { " rightside" },
19551920
);
19561921
if let Some(link) = src_href {
@@ -1972,7 +1937,6 @@ pub(crate) fn render_impl_summary(
19721937
cx: &mut Context<'_>,
19731938
i: &Impl,
19741939
parent: &clean::Item,
1975-
containing_item: &clean::Item,
19761940
show_def_docs: bool,
19771941
use_absolute: Option<bool>,
19781942
// This argument is used to reference same type with different paths to avoid duplication
@@ -1987,7 +1951,7 @@ pub(crate) fn render_impl_summary(
19871951
format!(" data-aliases=\"{}\"", aliases.join(","))
19881952
};
19891953
write!(w, "<section id=\"{id}\" class=\"impl\"{aliases}>");
1990-
render_rightside(w, cx, &i.impl_item, containing_item, RenderMode::Normal);
1954+
render_rightside(w, cx, &i.impl_item, RenderMode::Normal);
19911955
write!(
19921956
w,
19931957
"<a href=\"#{id}\" class=\"anchor\">§</a>\

src/librustdoc/html/render/print_item.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,6 @@ pub(super) fn print_item(cx: &mut Context<'_>, item: &clean::Item, buf: &mut Buf
214214
&mut stability_since_raw,
215215
item.stable_since(cx.tcx()),
216216
item.const_stability(cx.tcx()),
217-
None,
218-
None,
219217
);
220218
let stability_since_raw: String = stability_since_raw.into_inner();
221219

@@ -825,7 +823,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
825823
write!(w, "<details class=\"toggle{method_toggle_class}\" open><summary>");
826824
}
827825
write!(w, "<section id=\"{id}\" class=\"method\">");
828-
render_rightside(w, cx, m, t, RenderMode::Normal);
826+
render_rightside(w, cx, m, RenderMode::Normal);
829827
write!(w, "<h4 class=\"code-header\">");
830828
render_assoc_item(
831829
w,
@@ -1686,8 +1684,6 @@ fn item_variants(
16861684
w,
16871685
variant.stable_since(tcx),
16881686
variant.const_stability(tcx),
1689-
it.stable_since(tcx),
1690-
it.const_stable_since(tcx),
16911687
" rightside",
16921688
);
16931689
w.write_str("<h3 class=\"code-header\">");

tests/rustdoc/const-display.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ impl Foo {
7272
pub struct Bar;
7373

7474
impl Bar {
75-
// Do not show non-const stabilities that are the same as the enclosing item.
76-
// @matches 'foo/struct.Bar.html' '//span[@class="since"]' '^const: 1.2.0$'
75+
// Show non-const stabilities that are the same as the enclosing item.
76+
// @has 'foo/struct.Bar.html' '//span[@class="since"]' '1.0.0 (const: 1.2.0)'
7777
#[stable(feature = "rust1", since = "1.0.0")]
7878
#[rustc_const_stable(feature = "const2", since = "1.2.0")]
7979
pub const fn stable_impl() -> u32 { 42 }

tests/rustdoc/deref/deref-const-fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct Foo {
2626

2727
// @has 'foo/struct.Foo.html'
2828
// @has - '//*[@id="method.len"]' 'pub fn len(&self) -> usize'
29-
// @!has - '//*[@id="method.len"]//span[@class="since"]' '1.0.0'
29+
// @has - '//*[@id="method.len"]//span[@class="since"]' '1.0.0'
3030
// @!has - '//*[@id="method.len"]//span[@class="since"]' '(const: 1.0.0)'
3131
#[stable(feature = "rust1", since = "1.0.0")]
3232
impl std::ops::Deref for Foo {

tests/rustdoc/ensure-src-link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

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

5-
// @has foo/trait.Iterator.html '//*[@id="method.zip"]//a[@class="src rightside"]' "source"
5+
// @has foo/trait.Iterator.html '//*[@id="method.zip"]//a[@class="src"]' "source"
66
pub use std::iter::Iterator;

tests/rustdoc/implementor-stable-version.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ pub struct Foo;
1616
#[stable(feature = "foobar", since = "4.4.4")]
1717
impl Bar for Foo {}
1818

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

0 commit comments

Comments
 (0)