Skip to content

Commit 433d243

Browse files
committed
extra_info_tags don't return string, use display_fn
1 parent 1f01433 commit 433d243

File tree

1 file changed

+56
-35
lines changed

1 file changed

+56
-35
lines changed

src/librustdoc/html/render/print_item.rs

+56-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clean::AttributesExt;
22

3+
use rustc_data_structures::captures::Captures;
34
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
45
use rustc_hir as hir;
56
use rustc_hir::def::CtorKind;
@@ -28,8 +29,8 @@ use crate::formats::item_type::ItemType;
2829
use crate::formats::{AssocItemRender, Impl, RenderMode};
2930
use crate::html::escape::Escape;
3031
use crate::html::format::{
31-
join_with_double_colon, print_abi_with_space, print_constness_with_space, print_where_clause,
32-
visibility_print_with_space, Buffer, Ending, PrintWithSpace,
32+
display_fn, join_with_double_colon, print_abi_with_space, print_constness_with_space,
33+
print_where_clause, visibility_print_with_space, Buffer, Ending, PrintWithSpace,
3334
};
3435
use crate::html::layout::Page;
3536
use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
@@ -367,7 +368,7 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
367368
..myitem.clone()
368369
};
369370

370-
let stab_tags = Some(extra_info_tags(&import_item, item, cx.tcx()));
371+
let stab_tags = Some(extra_info_tags(&import_item, item, cx.tcx()).to_string());
371372
stab_tags
372373
} else {
373374
None
@@ -461,42 +462,62 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
461462

462463
/// Render the stability, deprecation and portability tags that are displayed in the item's summary
463464
/// at the module level.
464-
fn extra_info_tags(item: &clean::Item, parent: &clean::Item, tcx: TyCtxt<'_>) -> String {
465-
let mut tags = String::new();
466-
467-
fn tag_html(class: &str, title: &str, contents: &str) -> String {
468-
format!(r#"<span class="stab {}" title="{}">{}</span>"#, class, Escape(title), contents)
469-
}
470-
471-
// The trailing space after each tag is to space it properly against the rest of the docs.
472-
if let Some(depr) = &item.deprecation(tcx) {
473-
let message = if stability::deprecation_in_effect(depr) {
474-
"Deprecated"
475-
} else {
476-
"Deprecation planned"
477-
};
478-
tags += &tag_html("deprecated", "", message);
479-
}
465+
fn extra_info_tags<'a, 'tcx: 'a>(
466+
item: &'a clean::Item,
467+
parent: &'a clean::Item,
468+
tcx: TyCtxt<'tcx>,
469+
) -> impl fmt::Display + 'a + Captures<'tcx> {
470+
display_fn(move |f| {
471+
fn tag_html<'a>(
472+
class: &'a str,
473+
title: &'a str,
474+
contents: &'a str,
475+
) -> impl fmt::Display + 'a {
476+
display_fn(move |f| {
477+
write!(
478+
f,
479+
r#"<span class="stab {}" title="{}">{}</span>"#,
480+
class,
481+
Escape(title),
482+
contents
483+
)
484+
})
485+
}
480486

481-
// The "rustc_private" crates are permanently unstable so it makes no sense
482-
// to render "unstable" everywhere.
483-
if item.stability(tcx).as_ref().map(|s| s.is_unstable() && s.feature != sym::rustc_private)
484-
== Some(true)
485-
{
486-
tags += &tag_html("unstable", "", "Experimental");
487-
}
487+
// The trailing space after each tag is to space it properly against the rest of the docs.
488+
if let Some(depr) = &item.deprecation(tcx) {
489+
let message = if stability::deprecation_in_effect(depr) {
490+
"Deprecated"
491+
} else {
492+
"Deprecation planned"
493+
};
494+
write!(f, "{}", tag_html("deprecated", "", message))?;
495+
}
488496

489-
let cfg = match (&item.cfg, parent.cfg.as_ref()) {
490-
(Some(cfg), Some(parent_cfg)) => cfg.simplify_with(parent_cfg),
491-
(cfg, _) => cfg.as_deref().cloned(),
492-
};
497+
// The "rustc_private" crates are permanently unstable so it makes no sense
498+
// to render "unstable" everywhere.
499+
if item.stability(tcx).as_ref().map(|s| s.is_unstable() && s.feature != sym::rustc_private)
500+
== Some(true)
501+
{
502+
write!(f, "{}", tag_html("unstable", "", "Experimental"))?;
503+
}
493504

494-
debug!("Portability name={:?} {:?} - {:?} = {:?}", item.name, item.cfg, parent.cfg, cfg);
495-
if let Some(ref cfg) = cfg {
496-
tags += &tag_html("portability", &cfg.render_long_plain(), &cfg.render_short_html());
497-
}
505+
let cfg = match (&item.cfg, parent.cfg.as_ref()) {
506+
(Some(cfg), Some(parent_cfg)) => cfg.simplify_with(parent_cfg),
507+
(cfg, _) => cfg.as_deref().cloned(),
508+
};
498509

499-
tags
510+
debug!("Portability name={:?} {:?} - {:?} = {:?}", item.name, item.cfg, parent.cfg, cfg);
511+
if let Some(ref cfg) = cfg {
512+
write!(
513+
f,
514+
"{}",
515+
tag_html("portability", &cfg.render_long_plain(), &cfg.render_short_html())
516+
)
517+
} else {
518+
Ok(())
519+
}
520+
})
500521
}
501522

502523
fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &clean::Function) {

0 commit comments

Comments
 (0)