Skip to content

Commit 4d962f0

Browse files
committed
Auto merge of #118441 - GuillaumeGomez:display-stability-version, r=<try>
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 abe34e9 + a333572 commit 4d962f0

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
@@ -592,13 +592,6 @@ impl Item {
592592
}
593593
}
594594

595-
pub(crate) fn const_stable_since(&self, tcx: TyCtxt<'_>) -> Option<StableSince> {
596-
match self.const_stability(tcx)?.level {
597-
StabilityLevel::Stable { since, .. } => Some(since),
598-
StabilityLevel::Unstable { .. } => None,
599-
}
600-
}
601-
602595
pub(crate) fn is_non_exhaustive(&self) -> bool {
603596
self.attrs.other_attrs.iter().any(|a| a.has_name(sym::non_exhaustive))
604597
}

src/librustdoc/html/render/mod.rs

+13-49
Original file line numberDiff line numberDiff line change
@@ -955,32 +955,20 @@ fn assoc_method(
955955
/// consequence of the above rules.
956956
fn render_stability_since_raw_with_extra(
957957
w: &mut Buffer,
958-
ver: Option<StableSince>,
958+
stable_version: Option<StableSince>,
959959
const_stability: Option<ConstStability>,
960-
containing_ver: Option<StableSince>,
961-
containing_const_ver: Option<StableSince>,
962960
extra_class: &str,
963961
) -> bool {
964-
let stable_version = if ver != containing_ver
965-
&& let Some(ver) = &ver
966-
{
967-
since_to_string(ver)
968-
} else {
969-
None
970-
};
971-
972962
let mut title = String::new();
973963
let mut stability = String::new();
974964

975-
if let Some(ver) = stable_version {
976-
stability.push_str(ver.as_str());
977-
title.push_str(&format!("Stable since Rust version {ver}"));
965+
if let Some(version) = stable_version.and_then(|version| since_to_string(&version)) {
966+
stability.push_str(&version);
967+
title.push_str(&format!("Stable since Rust version {version}"));
978968
}
979969

980970
let const_title_and_stability = match const_stability {
981-
Some(ConstStability { level: StabilityLevel::Stable { since, .. }, .. })
982-
if Some(since) != containing_const_ver =>
983-
{
971+
Some(ConstStability { level: StabilityLevel::Stable { since, .. }, .. }) => {
984972
since_to_string(&since)
985973
.map(|since| (format!("const since {since}"), format!("const: {since}")))
986974
}
@@ -1035,17 +1023,8 @@ fn render_stability_since_raw(
10351023
w: &mut Buffer,
10361024
ver: Option<StableSince>,
10371025
const_stability: Option<ConstStability>,
1038-
containing_ver: Option<StableSince>,
1039-
containing_const_ver: Option<StableSince>,
10401026
) -> bool {
1041-
render_stability_since_raw_with_extra(
1042-
w,
1043-
ver,
1044-
const_stability,
1045-
containing_ver,
1046-
containing_const_ver,
1047-
"",
1048-
)
1027+
render_stability_since_raw_with_extra(w, ver, const_stability, "")
10491028
}
10501029

10511030
fn render_assoc_item(
@@ -1535,7 +1514,6 @@ fn render_impl(
15351514
cx: &mut Context<'_>,
15361515
item: &clean::Item,
15371516
parent: &clean::Item,
1538-
containing_item: &clean::Item,
15391517
link: AssocItemLink<'_>,
15401518
render_mode: RenderMode,
15411519
is_default_item: bool,
@@ -1630,7 +1608,7 @@ fn render_impl(
16301608
})
16311609
.map(|item| format!("{}.{name}", item.type_()));
16321610
write!(w, "<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">");
1633-
render_rightside(w, cx, item, containing_item, render_mode);
1611+
render_rightside(w, cx, item, render_mode);
16341612
if trait_.is_some() {
16351613
// Anchors are only used on trait impls.
16361614
write!(w, "<a href=\"#{id}\" class=\"anchor\">§</a>");
@@ -1652,7 +1630,7 @@ fn render_impl(
16521630
let source_id = format!("{item_type}.{name}");
16531631
let id = cx.derive_id(&source_id);
16541632
write!(w, "<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">");
1655-
render_rightside(w, cx, item, containing_item, render_mode);
1633+
render_rightside(w, cx, item, render_mode);
16561634
if trait_.is_some() {
16571635
// Anchors are only used on trait impls.
16581636
write!(w, "<a href=\"#{id}\" class=\"anchor\">§</a>");
@@ -1738,7 +1716,6 @@ fn render_impl(
17381716
cx,
17391717
trait_item,
17401718
if trait_.is_some() { &i.impl_item } else { parent },
1741-
parent,
17421719
link,
17431720
render_mode,
17441721
false,
@@ -1754,7 +1731,6 @@ fn render_impl(
17541731
t: &clean::Trait,
17551732
i: &clean::Impl,
17561733
parent: &clean::Item,
1757-
containing_item: &clean::Item,
17581734
render_mode: RenderMode,
17591735
rendering_params: ImplRenderingParameters,
17601736
) {
@@ -1782,7 +1758,6 @@ fn render_impl(
17821758
cx,
17831759
trait_item,
17841760
parent,
1785-
containing_item,
17861761
assoc_link,
17871762
render_mode,
17881763
true,
@@ -1805,7 +1780,6 @@ fn render_impl(
18051780
t,
18061781
i.inner_impl(),
18071782
&i.impl_item,
1808-
parent,
18091783
render_mode,
18101784
rendering_params,
18111785
);
@@ -1827,7 +1801,6 @@ fn render_impl(
18271801
cx,
18281802
i,
18291803
parent,
1830-
parent,
18311804
rendering_params.show_def_docs,
18321805
use_absolute,
18331806
aliases,
@@ -1875,20 +1848,14 @@ fn render_impl(
18751848

18761849
// Render the items that appear on the right side of methods, impls, and
18771850
// associated types. For example "1.0.0 (const: 1.39.0) · source".
1878-
fn render_rightside(
1879-
w: &mut Buffer,
1880-
cx: &Context<'_>,
1881-
item: &clean::Item,
1882-
containing_item: &clean::Item,
1883-
render_mode: RenderMode,
1884-
) {
1851+
fn render_rightside(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, render_mode: RenderMode) {
18851852
let tcx = cx.tcx();
18861853

18871854
// FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove
18881855
// this condition.
1889-
let (const_stability, const_stable_since) = match render_mode {
1890-
RenderMode::Normal => (item.const_stability(tcx), containing_item.const_stable_since(tcx)),
1891-
RenderMode::ForDeref { .. } => (None, None),
1856+
let const_stability = match render_mode {
1857+
RenderMode::Normal => item.const_stability(tcx),
1858+
RenderMode::ForDeref { .. } => None,
18921859
};
18931860
let src_href = cx.src_href(item);
18941861
let has_src_ref = src_href.is_some();
@@ -1898,8 +1865,6 @@ fn render_rightside(
18981865
&mut rightside,
18991866
item.stable_since(tcx),
19001867
const_stability,
1901-
containing_item.stable_since(tcx),
1902-
const_stable_since,
19031868
if has_src_ref { "" } else { " rightside" },
19041869
);
19051870
if let Some(link) = src_href {
@@ -1921,7 +1886,6 @@ pub(crate) fn render_impl_summary(
19211886
cx: &mut Context<'_>,
19221887
i: &Impl,
19231888
parent: &clean::Item,
1924-
containing_item: &clean::Item,
19251889
show_def_docs: bool,
19261890
use_absolute: Option<bool>,
19271891
// This argument is used to reference same type with different paths to avoid duplication
@@ -1936,7 +1900,7 @@ pub(crate) fn render_impl_summary(
19361900
format!(" data-aliases=\"{}\"", aliases.join(","))
19371901
};
19381902
write!(w, "<section id=\"{id}\" class=\"impl\"{aliases}>");
1939-
render_rightside(w, cx, &i.impl_item, containing_item, RenderMode::Normal);
1903+
render_rightside(w, cx, &i.impl_item, RenderMode::Normal);
19401904
write!(
19411905
w,
19421906
"<a href=\"#{id}\" class=\"anchor\">§</a>\

src/librustdoc/html/render/print_item.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,6 @@ pub(super) fn print_item(
224224
&mut stability_since_raw,
225225
item.stable_since(cx.tcx()),
226226
item.const_stability(cx.tcx()),
227-
None,
228-
None,
229227
);
230228
let stability_since_raw: String = stability_since_raw.into_inner();
231229

@@ -847,7 +845,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
847845
write!(w, "<details class=\"toggle{method_toggle_class}\" open><summary>");
848846
}
849847
write!(w, "<section id=\"{id}\" class=\"method\">");
850-
render_rightside(w, cx, m, t, RenderMode::Normal);
848+
render_rightside(w, cx, m, RenderMode::Normal);
851849
write!(w, "<h4 class=\"code-header\">");
852850
render_assoc_item(
853851
w,
@@ -1708,8 +1706,6 @@ fn item_variants(
17081706
w,
17091707
variant.stable_since(tcx),
17101708
variant.const_stability(tcx),
1711-
it.stable_since(tcx),
1712-
it.const_stable_since(tcx),
17131709
" rightside",
17141710
);
17151711
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)