Skip to content

Commit 5e3fc6b

Browse files
Add new "hide deprecated items" setting in rustdoc
1 parent 1b39278 commit 5e3fc6b

12 files changed

Lines changed: 107 additions & 23 deletions

File tree

src/librustdoc/clean/auto_trait.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ fn synthesize_auto_trait_impl<'tcx>(
126126
items: Vec::new(),
127127
polarity,
128128
kind: clean::ImplKind::Auto,
129+
is_deprecated: false,
129130
})),
130131
item_id: clean::ItemId::Auto { trait_: trait_def_id, for_: item_def_id },
131132
cfg: None,

src/librustdoc/clean/blanket_impl.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ pub(crate) fn synthesize_blanket_impls(
117117
None,
118118
None,
119119
))),
120+
is_deprecated: tcx
121+
.lookup_deprecation(impl_def_id)
122+
.is_some_and(|deprecation| deprecation.is_in_effect()),
120123
})),
121124
cfg: None,
122125
inline_stmt_id: None,

src/librustdoc/clean/inline.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,9 @@ pub(crate) fn build_impl(
645645
} else {
646646
ImplKind::Normal
647647
},
648+
is_deprecated: tcx
649+
.lookup_deprecation(did)
650+
.is_some_and(|deprecation| deprecation.is_in_effect()),
648651
})),
649652
merged_attrs,
650653
cfg,

src/librustdoc/clean/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2882,6 +2882,9 @@ fn clean_impl<'tcx>(
28822882
)),
28832883
_ => None,
28842884
});
2885+
let is_deprecated = tcx
2886+
.lookup_deprecation(def_id.to_def_id())
2887+
.is_some_and(|deprecation| deprecation.is_in_effect());
28852888
let mut make_item = |trait_: Option<Path>, for_: Type, items: Vec<Item>| {
28862889
let kind = ImplItem(Box::new(Impl {
28872890
safety: match impl_.of_trait {
@@ -2902,6 +2905,7 @@ fn clean_impl<'tcx>(
29022905
} else {
29032906
ImplKind::Normal
29042907
},
2908+
is_deprecated,
29052909
}));
29062910
Item::from_def_id_and_parts(def_id.to_def_id(), None, kind, cx)
29072911
};

src/librustdoc/clean/types.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,10 @@ impl Item {
427427
})
428428
}
429429

430+
pub(crate) fn is_deprecated(&self, tcx: TyCtxt<'_>) -> bool {
431+
self.deprecation(tcx).is_some_and(|deprecation| deprecation.is_in_effect())
432+
}
433+
430434
pub(crate) fn inner_docs(&self, tcx: TyCtxt<'_>) -> bool {
431435
self.item_id.as_def_id().map(|did| inner_docs(tcx.get_all_attrs(did))).unwrap_or(false)
432436
}
@@ -1270,6 +1274,9 @@ impl Trait {
12701274
pub(crate) fn is_dyn_compatible(&self, tcx: TyCtxt<'_>) -> bool {
12711275
tcx.is_dyn_compatible(self.def_id)
12721276
}
1277+
pub(crate) fn is_deprecated(&self, tcx: TyCtxt<'_>) -> bool {
1278+
tcx.lookup_deprecation(self.def_id).is_some_and(|deprecation| deprecation.is_in_effect())
1279+
}
12731280
}
12741281

12751282
#[derive(Clone, Debug)]
@@ -2254,6 +2261,7 @@ pub(crate) struct Impl {
22542261
pub(crate) items: Vec<Item>,
22552262
pub(crate) polarity: ty::ImplPolarity,
22562263
pub(crate) kind: ImplKind,
2264+
pub(crate) is_deprecated: bool,
22572265
}
22582266

22592267
impl Impl {

src/librustdoc/html/render/mod.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,12 +1794,14 @@ fn render_impl(
17941794
let mut info_buffer = String::new();
17951795
let mut short_documented = true;
17961796

1797+
let mut trait_item_deprecated = false;
17971798
if render_method_item {
17981799
if !is_default_item {
17991800
if let Some(t) = trait_ {
18001801
// The trait item may have been stripped so we might not
18011802
// find any documentation or stability for it.
18021803
if let Some(it) = t.items.iter().find(|i| i.name == item.name) {
1804+
trait_item_deprecated = it.is_deprecated(cx.tcx());
18031805
// We need the stability of the item from the trait
18041806
// because impls can't have a stability.
18051807
if !item.doc_value().is_empty() {
@@ -1839,10 +1841,20 @@ fn render_impl(
18391841
Either::Right(boring)
18401842
};
18411843

1844+
let mut deprecation_attr = if trait_item_deprecated || item.is_deprecated(cx.tcx()) {
1845+
" deprecated"
1846+
} else {
1847+
""
1848+
};
1849+
18421850
let toggled = !doc_buffer.is_empty();
18431851
if toggled {
18441852
let method_toggle_class = if item_type.is_method() { " method-toggle" } else { "" };
1845-
write!(w, "<details class=\"toggle{method_toggle_class}\" open><summary>")?;
1853+
write!(
1854+
w,
1855+
"<details class=\"toggle{method_toggle_class}{deprecation_attr}\" open><summary>"
1856+
)?;
1857+
deprecation_attr = "";
18461858
}
18471859
match &item.kind {
18481860
clean::MethodItem(..) | clean::RequiredMethodItem(_) => {
@@ -1859,7 +1871,7 @@ fn render_impl(
18591871
.map(|item| format!("{}.{name}", item.type_()));
18601872
write!(
18611873
w,
1862-
"<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">\
1874+
"<section id=\"{id}\" class=\"{item_type}{in_trait_class}{deprecation_attr}\">\
18631875
{}",
18641876
render_rightside(cx, item, render_mode)
18651877
)?;
@@ -1885,7 +1897,7 @@ fn render_impl(
18851897
let id = cx.derive_id(&source_id);
18861898
write!(
18871899
w,
1888-
"<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">\
1900+
"<section id=\"{id}\" class=\"{item_type}{in_trait_class}{deprecation_attr}\">\
18891901
{}",
18901902
render_rightside(cx, item, render_mode)
18911903
)?;
@@ -1912,7 +1924,7 @@ fn render_impl(
19121924
let id = cx.derive_id(&source_id);
19131925
write!(
19141926
w,
1915-
"<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">\
1927+
"<section id=\"{id}\" class=\"{item_type}{in_trait_class}{deprecation_attr}\">\
19161928
{}",
19171929
render_rightside(cx, item, render_mode),
19181930
)?;
@@ -1944,7 +1956,7 @@ fn render_impl(
19441956
let id = cx.derive_id(&source_id);
19451957
write!(
19461958
w,
1947-
"<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">\
1959+
"<section id=\"{id}\" class=\"{item_type}{in_trait_class}{deprecation_attr}\">\
19481960
{}",
19491961
render_rightside(cx, item, render_mode),
19501962
)?;
@@ -1971,7 +1983,7 @@ fn render_impl(
19711983
let id = cx.derive_id(&source_id);
19721984
write!(
19731985
w,
1974-
"<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">\
1986+
"<section id=\"{id}\" class=\"{item_type}{in_trait_class}{deprecation_attr}\">\
19751987
{}",
19761988
render_rightside(cx, item, render_mode),
19771989
)?;
@@ -2143,11 +2155,18 @@ fn render_impl(
21432155
}
21442156
if render_mode == RenderMode::Normal {
21452157
let toggled = !(impl_items.is_empty() && default_impl_items.is_empty());
2158+
let deprecation_attr = if impl_.is_deprecated
2159+
|| trait_.is_some_and(|trait_| trait_.is_deprecated(cx.tcx()))
2160+
{
2161+
" deprecated"
2162+
} else {
2163+
""
2164+
};
21462165
if toggled {
21472166
close_tags.push("</details>");
21482167
write!(
21492168
w,
2150-
"<details class=\"toggle implementors-toggle\"{}>\
2169+
"<details class=\"toggle implementors-toggle{deprecation_attr}\"{}>\
21512170
<summary>",
21522171
if rendering_params.toggle_open_by_default { " open" } else { "" }
21532172
)?;

src/librustdoc/html/render/print_item.rs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ fn toggle_close(mut w: impl fmt::Write) {
217217
}
218218

219219
fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> impl fmt::Display {
220+
fn deprecation_class(is_deprecated: bool) -> &'static str {
221+
if is_deprecated { " class=\"deprecated\"" } else { "" }
222+
}
223+
220224
fmt::from_fn(|w| {
221225
write!(w, "{}", document(cx, item, None, HeadingOffset::H2))?;
222226

@@ -370,11 +374,18 @@ fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> i
370374
write!(w, "</code></dt>")?
371375
}
372376
clean::ImportItem(ref import) => {
373-
let stab_tags =
374-
import.source.did.map_or_else(String::new, |import_def_id| {
375-
print_extra_info_tags(tcx, myitem, item, Some(import_def_id))
376-
.to_string()
377-
});
377+
let (stab_tags, deprecation) = match import.source.did {
378+
Some(import_def_id) => {
379+
let stab_tags =
380+
print_extra_info_tags(tcx, myitem, item, Some(import_def_id))
381+
.to_string();
382+
let deprecation = tcx
383+
.lookup_deprecation(import_def_id)
384+
.is_some_and(|deprecation| deprecation.is_in_effect());
385+
(stab_tags, deprecation)
386+
}
387+
None => (String::new(), item.is_deprecated(tcx)),
388+
};
378389
let id = match import.kind {
379390
clean::ImportKind::Simple(s) => {
380391
format!(" id=\"{}\"", cx.derive_id(format!("reexport.{s}")))
@@ -383,8 +394,8 @@ fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> i
383394
};
384395
write!(
385396
w,
386-
"<dt{id}>\
387-
<code>"
397+
"<dt{id}{deprecation_attr}><code>",
398+
deprecation_attr = deprecation_class(deprecation)
388399
)?;
389400
render_attributes_in_code(w, myitem, "", cx)?;
390401
write!(
@@ -396,9 +407,7 @@ fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> i
396407
)?;
397408
}
398409
_ => {
399-
if myitem.name.is_none() {
400-
continue;
401-
}
410+
let Some(item_name) = myitem.name else { continue };
402411

403412
let unsafety_flag = match myitem.kind {
404413
clean::FunctionItem(_) | clean::ForeignFunctionItem(..)
@@ -431,9 +440,10 @@ fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> i
431440
.into_string();
432441
let (docs_before, docs_after) =
433442
if docs.is_empty() { ("", "") } else { ("<dd>", "</dd>") };
443+
let deprecation_attr = deprecation_class(myitem.is_deprecated(tcx));
434444
write!(
435445
w,
436-
"<dt>\
446+
"<dt{deprecation_attr}>\
437447
<a class=\"{class}\" href=\"{href}\" title=\"{title1} {title2}\">\
438448
{name}\
439449
</a>\
@@ -442,12 +452,12 @@ fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> i
442452
{stab_tags}\
443453
</dt>\
444454
{docs_before}{docs}{docs_after}",
445-
name = EscapeBodyTextWithWbr(myitem.name.unwrap().as_str()),
455+
name = EscapeBodyTextWithWbr(item_name.as_str()),
446456
visibility_and_hidden = visibility_and_hidden,
447457
stab_tags = print_extra_info_tags(tcx, myitem, item, None),
448458
class = type_,
449459
unsafety_flag = unsafety_flag,
450-
href = print_item_path(type_, myitem.name.unwrap().as_str()),
460+
href = print_item_path(type_, item_name.as_str()),
451461
title1 = myitem.type_(),
452462
title2 = full_path(cx, myitem),
453463
)?;
@@ -778,15 +788,24 @@ fn item_trait(cx: &Context<'_>, it: &clean::Item, t: &clean::Trait) -> impl fmt:
778788

779789
let content = document_full(m, cx, HeadingOffset::H5).to_string();
780790

791+
let mut deprecation_attr =
792+
if m.is_deprecated(cx.tcx()) { " deprecated" } else { "" };
793+
781794
let toggled = !content.is_empty();
782795
if toggled {
783796
let method_toggle_class =
784797
if item_type.is_method() { " method-toggle" } else { "" };
785-
write!(w, "<details class=\"toggle{method_toggle_class}\" open><summary>")?;
798+
write!(
799+
w,
800+
"<details \
801+
class=\"toggle{method_toggle_class}{deprecation_attr}\" \
802+
open><summary>"
803+
)?;
804+
deprecation_attr = "";
786805
}
787806
write!(
788807
w,
789-
"<section id=\"{id}\" class=\"method\">\
808+
"<section id=\"{id}\" class=\"method{deprecation_attr}\">\
790809
{}\
791810
<h4 class=\"code-header\">{}</h4></section>",
792811
render_rightside(cx, m, RenderMode::Normal),

src/librustdoc/html/static/css/rustdoc.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,6 +2642,15 @@ However, it's not needed with smaller screen width because the doc/code block is
26422642
}
26432643
}
26442644

2645+
/* Items on module pages */
2646+
.hide-deprecated-items dt.deprecated,
2647+
.hide-deprecated-items dt.deprecated + dd,
2648+
/* Items on item pages */
2649+
.hide-deprecated-items .deprecated,
2650+
.hide-deprecated-items .deprecated + .item-info {
2651+
display: none;
2652+
}
2653+
26452654
/*
26462655
WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY
26472656
If you update this line, then you also need to update the line with the same warning

src/librustdoc/html/static/js/search.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4920,6 +4920,9 @@ async function addTab(results, query, display, finishedCallback, isTypeSearch) {
49204920

49214921
const link = document.createElement("a");
49224922
link.className = "result-" + type;
4923+
if (obj.item.deprecated) {
4924+
link.className += " deprecated";
4925+
}
49234926
link.href = obj.href;
49244927

49254928
const resultName = document.createElement("span");

src/librustdoc/html/static/js/settings.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@
7878
removeClass(document.documentElement, "word-wrap-source-code");
7979
}
8080
break;
81+
case "hide-deprecated-items":
82+
if (value === true) {
83+
addClass(document.documentElement, "hide-deprecated-items");
84+
} else {
85+
removeClass(document.documentElement, "hide-deprecated-items");
86+
}
8187
}
8288
}
8389

@@ -274,6 +280,11 @@
274280
"js_name": "word-wrap-source-code",
275281
"default": false,
276282
},
283+
{
284+
"name": "Hide deprecated items",
285+
"js_name": "hide-deprecated-items",
286+
"default": false,
287+
},
277288
];
278289

279290
// Then we build the DOM.

0 commit comments

Comments
 (0)