Auto merge of #137425 - yotamofek:pr/rustdoc/return-impl-display-redux, r=GuillaumeGomez

`librustdoc`: return `impl fmt::Display` in more places instead of writing to strings

Continuation of #136784 , another attempt at landing the larger parts of #136748 .
I'd like to, gradually, make all of the building blocks for rendering docs in `librustdoc` return `impl fmt::Display` instead of returning `Strings`, or receiving a `&mut String` (or `&mut impl fmt::Write`). Another smaller end goal is to be able to get rid of [`write_str`](8dac72bb1d/src/librustdoc/html/format.rs (L40-L42)).
This PR is a large step in that direction.

Most of the changes are quite mechanical, and split up into separate commits for easier reviewing (hopefully). I took `print_item` and then started by converting all the functions it called (and their dependencies), and the last commit does the conversion for `print_item` itself. Ignoring whitespace should make reviewing a bit easier.

And most importantly, perf run shows pretty good results locally, hopefully CI will also show green 😁

r? `@GuillaumeGomez` , if you feel like it.
This commit is contained in:
bors 2025-03-02 12:35:48 +00:00
commit 7c4a55c2ac
5 changed files with 2522 additions and 2320 deletions

View file

@ -30,7 +30,7 @@ use super::url_parts_builder::{UrlPartsBuilder, estimate_item_path_byte_length};
use crate::clean::types::ExternalLocation;
use crate::clean::utils::find_nearest_parent_module;
use crate::clean::{self, ExternalCrate, PrimitiveType};
use crate::display::Joined as _;
use crate::display::{Joined as _, MaybeDisplay as _};
use crate::formats::cache::Cache;
use crate::formats::item_type::ItemType;
use crate::html::escape::{Escape, EscapeBodyText};
@ -178,12 +178,12 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
cx: &'a Context<'tcx>,
indent: usize,
ending: Ending,
) -> impl Display + 'a + Captures<'tcx> {
fmt::from_fn(move |f| {
if gens.where_predicates.is_empty() {
return Ok(());
}
) -> Option<impl Display + 'a + Captures<'tcx>> {
if gens.where_predicates.is_empty() {
return None;
}
Some(fmt::from_fn(move |f| {
let where_preds = fmt::from_fn(|f| {
gens.where_predicates
.iter()
@ -246,7 +246,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
}
};
write!(f, "{clause}")
})
}))
}
impl clean::Lifetime {
@ -1179,7 +1179,7 @@ impl clean::Impl {
self.print_type(&self.for_, f, use_absolute, cx)?;
}
print_where_clause(&self.generics, cx, 0, Ending::Newline).fmt(f)
print_where_clause(&self.generics, cx, 0, Ending::Newline).maybe_display().fmt(f)
})
}
fn print_type<'a, 'tcx: 'a>(

View file

@ -237,8 +237,7 @@ impl<'tcx> Context<'tcx> {
};
if !render_redirect_pages {
let mut page_buffer = String::new();
print_item(self, it, &mut page_buffer);
let content = print_item(self, it);
let page = layout::Page {
css_class: tyname_s,
root_path: &self.root_path(),
@ -254,7 +253,7 @@ impl<'tcx> Context<'tcx> {
BufDisplay(|buf: &mut String| {
print_sidebar(self, it, buf);
}),
page_buffer,
content,
&self.shared.style_files,
)
} else {

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -636,26 +636,22 @@ impl TypeAliasPart {
} else {
AssocItemLink::Anchor(None)
};
let text = {
let mut buf = String::new();
super::render_impl(
&mut buf,
cx,
impl_,
type_alias_item,
assoc_link,
RenderMode::Normal,
None,
&[],
ImplRenderingParameters {
show_def_docs: true,
show_default_items: true,
show_non_assoc_items: true,
toggle_open_by_default: true,
},
);
buf
};
let text = super::render_impl(
cx,
impl_,
type_alias_item,
assoc_link,
RenderMode::Normal,
None,
&[],
ImplRenderingParameters {
show_def_docs: true,
show_default_items: true,
show_non_assoc_items: true,
toggle_open_by_default: true,
},
)
.to_string();
let type_alias_fqp = (*type_alias_fqp).iter().join("::");
if Some(&text) == ret.last().map(|s: &AliasSerializableImpl| &s.text) {
ret.last_mut()