Skip to content

Commit fdb1eef

Browse files
authored
Rollup merge of #109269 - klensy:rdoc-s, r=notriddle
rustdoc: cleanup some intermediate allocs First commit self contained, second one use `display_fn` for `extra_info_tags`
2 parents c076799 + 433d243 commit fdb1eef

File tree

5 files changed

+59
-40
lines changed

5 files changed

+59
-40
lines changed

src/librustdoc/html/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,7 @@ pub(crate) fn visibility_print_with_space<'a, 'tcx: 'a>(
14801480
debug!("path={:?}", path);
14811481
// modified from `resolved_path()` to work with `DefPathData`
14821482
let last_name = path.data.last().unwrap().data.get_opt_name().unwrap();
1483-
let anchor = anchor(vis_did, last_name, cx).to_string();
1483+
let anchor = anchor(vis_did, last_name, cx);
14841484

14851485
let mut s = "pub(in ".to_owned();
14861486
for seg in &path.data[..path.data.len() - 1] {

src/librustdoc/html/render/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ impl<'tcx> Context<'tcx> {
352352
},
353353
);
354354

355-
path = href.into_inner().to_string_lossy().to_string();
355+
path = href.into_inner().to_string_lossy().into_owned();
356356

357357
if let Some(c) = path.as_bytes().last() && *c != b'/' {
358358
path.push('/');

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) {

src/librustdoc/html/sources.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl LocalSourcesCollector<'_, '_> {
8585
},
8686
);
8787

88-
let mut href = href.into_inner().to_string_lossy().to_string();
88+
let mut href = href.into_inner().to_string_lossy().into_owned();
8989
if let Some(c) = href.as_bytes().last() && *c != b'/' {
9090
href.push('/');
9191
}

src/librustdoc/passes/collect_intra_doc_links.rs

-2
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
286286
split.next().map(|f| Symbol::intern(f)).ok_or_else(no_res)?;
287287
let path = split
288288
.next()
289-
.map(|f| f.to_owned())
290289
// If there's no third component, we saw `[a::b]` before and it failed to resolve.
291290
// So there's no partial res.
292291
.ok_or_else(no_res)?;
@@ -429,7 +428,6 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
429428
let item_name = Symbol::intern(item_str);
430429
let path_root = split
431430
.next()
432-
.map(|f| f.to_owned())
433431
// If there's no `::`, it's not an associated item.
434432
// So we can be sure that `rustc_resolve` was accurate when it said it wasn't resolved.
435433
.ok_or_else(|| {

0 commit comments

Comments
 (0)