diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index fdb52703edf77..abb3ff4f1926b 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -28,26 +28,26 @@ use crate::html::render::cache::ExternalLocation; use crate::html::render::Context; crate trait Print { - fn print(self, buffer: &mut Buffer); + fn print(self, buffer: &mut Buffer, cx: &mut Context<'_>); } impl Print for F where - F: FnOnce(&mut Buffer), + F: FnOnce(&mut Buffer, &mut Context<'_>), { - fn print(self, buffer: &mut Buffer) { - (self)(buffer) + fn print(self, buffer: &mut Buffer, cx: &mut Context<'_>) { + (self)(buffer, cx) } } impl Print for String { - fn print(self, buffer: &mut Buffer) { + fn print(self, buffer: &mut Buffer, _: &mut Context<'_>) { buffer.write_str(&self); } } impl Print for &'_ str { - fn print(self, buffer: &mut Buffer) { + fn print(self, buffer: &mut Buffer, _: &mut Context<'_>) { buffer.write_str(self); } } @@ -106,8 +106,8 @@ impl Buffer { self.buffer.write_fmt(v).unwrap(); } - crate fn to_display(mut self, t: T) -> String { - t.print(&mut self); + crate fn to_display(mut self, t: T, cx: &mut Context<'_>) -> String { + t.print(&mut self, cx); self.into_inner() } diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index 3d3fa3aaeaa51..b2caec7947fe6 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -9,6 +9,8 @@ use crate::html::render::{ensure_trailing_slash, StylePath}; use serde::Serialize; +use super::render::{Context, SharedContext}; + #[derive(Clone, Serialize)] crate struct Layout { crate logo: String, @@ -58,28 +60,28 @@ struct PageLayout<'a> { } crate fn render( - templates: &tera::Tera, - layout: &Layout, + cx: &mut Context<'_>, + shared: &SharedContext<'_>, page: &Page<'_>, sidebar: S, t: T, - style_files: &[StylePath], ) -> String { let static_root_path = page.get_static_root_path(); - let krate_with_trailing_slash = ensure_trailing_slash(&layout.krate).to_string(); - let mut themes: Vec = style_files + let krate_with_trailing_slash = ensure_trailing_slash(&shared.layout.krate).to_string(); + let mut themes: Vec = shared + .style_files .iter() .map(StylePath::basename) .collect::>() .unwrap_or_default(); themes.sort(); let rustdoc_version = rustc_interface::util::version_str().unwrap_or("unknown version"); - let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar. - let sidebar = Buffer::html().to_display(sidebar); + let content = Buffer::html().to_display(t, cx); // Note: This must happen before making the sidebar. + let sidebar = Buffer::html().to_display(sidebar, cx); let teractx = tera::Context::from_serialize(PageLayout { static_root_path, page, - layout, + layout: &shared.layout, themes, sidebar, content, @@ -87,7 +89,7 @@ crate fn render( rustdoc_version, }) .unwrap(); - templates.render("page.html", &teractx).unwrap() + shared.templates.render("page.html", &teractx).unwrap() } crate fn redirect(url: &str) -> String { diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 365d959ad9f3b..a383e7cad666d 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -56,9 +56,9 @@ crate struct Context<'tcx> { pub(super) render_redirect_pages: bool, /// Tracks section IDs for `Deref` targets so they match in both the main /// body and the sidebar. - pub(super) deref_id_map: RefCell>, + pub(super) deref_id_map: FxHashMap, /// The map used to ensure all generated 'id=' attributes are unique. - pub(super) id_map: RefCell, + pub(super) id_map: IdMap, /// Shared mutable state. /// /// Issue for improving the situation: [#82381][] @@ -73,7 +73,7 @@ crate struct Context<'tcx> { // `Context` is cloned a lot, so we don't want the size to grow unexpectedly. #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(Context<'_>, 144); +rustc_data_structures::static_assert_size!(Context<'_>, 128); /// Shared mutable state used in [`Context`] and elsewhere. crate struct SharedContext<'tcx> { @@ -166,9 +166,8 @@ impl<'tcx> Context<'tcx> { self.shared.tcx.sess } - pub(super) fn derive_id(&self, id: String) -> String { - let mut map = self.id_map.borrow_mut(); - map.derive(id) + pub(super) fn derive_id(&mut self, id: String) -> String { + self.id_map.derive(id) } /// String representation of how to get back to the root path of the 'doc/' @@ -177,7 +176,7 @@ impl<'tcx> Context<'tcx> { "../".repeat(self.current.len()) } - fn render_item(&self, it: &clean::Item, is_module: bool) -> String { + fn render_item(&mut self, it: &clean::Item, is_module: bool) -> String { let mut title = String::new(); if !is_module { title.push_str(&it.name.unwrap().as_str()); @@ -212,26 +211,28 @@ impl<'tcx> Context<'tcx> { } else { tyname.as_str() }; + let shared_clone = self.shared.clone(); let page = layout::Page { css_class: tyname_s, root_path: &self.root_path(), - static_root_path: self.shared.static_root_path.as_deref(), + static_root_path: (&*shared_clone).static_root_path.as_deref(), title: &title, description: &desc, keywords: &keywords, - resource_suffix: &self.shared.resource_suffix, + resource_suffix: &*shared_clone.resource_suffix, extra_scripts: &[], static_extra_scripts: &[], }; - + let shared_templates = &self.shared.clone().templates; if !self.render_redirect_pages { layout::render( - &self.shared.templates, - &self.shared.layout, + self, + &(&*self).shared, &page, - |buf: &mut _| print_sidebar(self, it, buf), - |buf: &mut _| print_item(self, &self.shared.templates, it, buf, &page), - &self.shared.style_files, + |buf: &mut _, cx: &mut Context<'_>| print_sidebar(cx, it, buf), + |buf: &mut _, cx: &mut Context<'_>| { + print_item(cx, shared_templates, it, buf, &page) + }, ) } else { if let Some(&(ref names, ty)) = self.cache().paths.get(&it.def_id.expect_def_id()) { @@ -511,12 +512,12 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { let dst = output; scx.ensure_dir(&dst)?; - let mut cx = Context { + let cx = &mut Context { current: Vec::new(), dst, render_redirect_pages: false, - id_map: RefCell::new(id_map), - deref_id_map: RefCell::new(FxHashMap::default()), + id_map, + deref_id_map: FxHashMap::default(), shared: Rc::new(scx), include_sources, }; @@ -530,9 +531,9 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { // Write shared runs within a flock; disable thread dispatching of IO temporarily. Rc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(true); - write_shared(&cx, &krate, index, &md_opts)?; + write_shared(cx, &krate, index, &md_opts)?; Rc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(false); - Ok((cx, krate)) + Ok((*cx, krate)) } fn make_child_renderer(&self) -> Self { @@ -540,8 +541,8 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { current: self.current.clone(), dst: self.dst.clone(), render_redirect_pages: self.render_redirect_pages, - deref_id_map: RefCell::new(FxHashMap::default()), - id_map: RefCell::new(IdMap::new()), + deref_id_map: FxHashMap::default(), + id_map: IdMap::new(), shared: Rc::clone(&self.shared), include_sources: self.include_sources, } @@ -582,12 +583,11 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { }; let all = self.shared.all.replace(AllTypes::new()); let v = layout::render( - &self.shared.templates, - &self.shared.layout, + self, + &self.shared, &page, sidebar, - |buf: &mut Buffer| all.print(buf), - &self.shared.style_files, + |buf: &mut Buffer, _: &mut Context<'_>| all.print(buf), ); self.shared.fs.write(final_file, v)?; @@ -604,8 +604,8 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { .map(StylePath::basename) .collect::>()?; let v = layout::render( - &self.shared.templates, - &self.shared.layout, + self, + &*self.shared.clone(), &page, sidebar, settings( @@ -613,7 +613,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { &self.shared.resource_suffix, theme_names, )?, - &self.shared.style_files, ); self.shared.fs.write(settings_file, v)?; if let Some(ref redirections) = self.shared.redirections { @@ -648,7 +647,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { if !self.render_redirect_pages { self.render_redirect_pages = item.is_stripped(); } - let scx = &self.shared; + let scx = self.shared.clone(); let item_name = item.name.as_ref().unwrap().to_string(); self.dst.push(&item_name); self.current.push(item_name); @@ -660,7 +659,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { if !buf.is_empty() { self.shared.ensure_dir(&self.dst)?; let joint_dst = self.dst.join("index.html"); - scx.fs.write(joint_dst, buf)?; + scx.clone().fs.write(joint_dst, buf)?; } // Render sidebar-items.js used throughout this module. diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 166e084012724..92bd4a5f35f9a 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -494,14 +494,14 @@ fn render_markdown( links: Vec, heading_offset: HeadingOffset, ) { - let mut ids = cx.id_map.borrow_mut(); + let ids = &mut cx.id_map; write!( w, "
{}
", Markdown { content: md_text, links: &links, - ids: &mut ids, + ids, error_codes: cx.shared.codes, edition: cx.shared.edition(), playground: &cx.shared.playground, @@ -656,14 +656,9 @@ fn short_item_info( if let Some(note) = note { let note = note.as_str(); - let mut ids = cx.id_map.borrow_mut(); - let html = MarkdownHtml( - ¬e, - &mut ids, - error_codes, - cx.shared.edition(), - &cx.shared.playground, - ); + let ids = &mut cx.id_map; + let html = + MarkdownHtml(¬e, ids, error_codes, cx.shared.edition(), &cx.shared.playground); message.push_str(&format!(": {}", html.into_string())); } extra_info.push(format!( @@ -693,7 +688,6 @@ fn short_item_info( } message.push_str(&format!(" ({})", feature)); - extra_info.push(format!("
{}
", message)); } @@ -706,7 +700,12 @@ fn short_item_info( // Render the list of items inside one of the sections "Trait Implementations", // "Auto Trait Implementations," "Blanket Trait Implementations" (on struct/enum pages). -fn render_impls(cx: &Context<'_>, w: &mut Buffer, impls: &[&&Impl], containing_item: &clean::Item) { +fn render_impls( + cx: &mut Context<'_>, + w: &mut Buffer, + impls: &[&&Impl], + containing_item: &clean::Item, +) { let tcx = cx.tcx(); let mut rendered_impls = impls .iter() @@ -771,7 +770,7 @@ fn assoc_const( w, "{}{}const {}: {}", extra, - it.visibility.print_with_space(it.def_id, cx), + it.visibility.print_with_space(it.def_id, &*cx), naive_assoc_href(it, link, cx), it.name.as_ref().unwrap(), ty.print(cx) @@ -944,8 +943,8 @@ fn render_assoc_item( // links without a href are valid - https://www.w3schools.com/tags/att_a_href.asp href = href.map(|href| format!("href=\"{}\"", href)).unwrap_or_else(|| "".to_string()), name = name, - generics = g.print(cx), - decl = d.full_print(header_len, indent, header.asyncness, cx), + generics = g.print(&*cx), + decl = d.full_print(header_len, indent, header.asyncness, &*cx), notable_traits = notable_traits_decl(d, cx), where_clause = print_where_clause(g, cx, indent, end_newline), ) @@ -1030,7 +1029,7 @@ impl<'a> AssocItemLink<'a> { fn render_assoc_items( w: &mut Buffer, - cx: &Context<'_>, + cx: &mut Context<'_>, containing_item: &clean::Item, it: DefId, what: AssocItemRender<'_>, @@ -1042,14 +1041,14 @@ fn render_assoc_items( fn render_assoc_items_inner( w: &mut Buffer, - cx: &Context<'_>, + cx: &mut Context<'_>, containing_item: &clean::Item, it: DefId, what: AssocItemRender<'_>, derefs: &mut FxHashSet, ) { info!("Documenting associated items of {:?}", containing_item.name); - let cache = cx.cache(); + let cache = &*cx.cache(); let v = match cache.impls.get(&it) { Some(v) => v, None => return, @@ -1067,10 +1066,10 @@ fn render_assoc_items_inner( RenderMode::Normal } AssocItemRender::DerefFor { trait_, type_, deref_mut_ } => { - let id = - cx.derive_id(small_url_encode(format!("deref-methods-{:#}", type_.print(cx)))); + let id = cx + .derive_id(small_url_encode(format!("deref-methods-{:#}", type_.print(&*cx)))); if let Some(def_id) = type_.def_id(cx.cache()) { - cx.deref_id_map.borrow_mut().insert(def_id, id.clone()); + cx.deref_id_map.insert(def_id, id.clone()); } write!( tmp_buf, @@ -1173,7 +1172,7 @@ fn render_assoc_items_inner( fn render_deref_methods( w: &mut Buffer, - cx: &Context<'_>, + cx: &mut Context<'_>, impl_: &Impl, container_item: &clean::Item, deref_mut: bool, @@ -1279,7 +1278,15 @@ fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String { let empty_set = FxHashSet::default(); let src_link = AssocItemLink::GotoSource(trait_did.into(), &empty_set); - assoc_type(&mut out, it, &[], Some(&tydef.type_), src_link, "", cx); + assoc_type( + &mut out, + it, + &[], + Some(&tydef.type_), + src_link, + "", + &mut (*cx), + ); out.push_str(";"); } } @@ -1313,7 +1320,7 @@ struct ImplRenderingParameters { fn render_impl( w: &mut Buffer, - cx: &Context<'_>, + cx: &mut Context<'_>, i: &Impl, parent: &clean::Item, link: AssocItemLink<'_>, @@ -1322,7 +1329,7 @@ fn render_impl( aliases: &[String], rendering_params: ImplRenderingParameters, ) { - let cache = cx.cache(); + let cache = &*cx.cache(); let traits = &cache.traits; let trait_ = i.trait_did().map(|did| &traits[&did]); let mut close_tags = String::new(); @@ -1335,7 +1342,7 @@ fn render_impl( fn doc_impl_item( boring: &mut Buffer, interesting: &mut Buffer, - cx: &Context<'_>, + cx: &mut Context<'_>, item: &clean::Item, parent: &clean::Item, containing_item: &clean::Item, @@ -1542,7 +1549,7 @@ fn render_impl( fn render_default_items( boring: &mut Buffer, interesting: &mut Buffer, - cx: &Context<'_>, + cx: &mut Context<'_>, t: &clean::Trait, i: &clean::Impl, parent: &clean::Item, @@ -1556,7 +1563,7 @@ fn render_impl( continue; } let did = i.trait_.as_ref().unwrap().def_id(); - let provided_methods = i.provided_trait_methods(cx.tcx()); + let provided_methods = i.provided_trait_methods((&*cx).tcx()); let assoc_link = AssocItemLink::GotoSource(did.into(), &provided_methods); doc_impl_item( @@ -1621,14 +1628,14 @@ fn render_impl( } if let Some(ref dox) = cx.shared.maybe_collapsed_doc_value(&i.impl_item) { - let mut ids = cx.id_map.borrow_mut(); + let ids = &mut (*cx).id_map; write!( w, "
{}
", Markdown { content: &*dox, - links: &i.impl_item.links(cx), - ids: &mut ids, + links: &i.impl_item.links(&*cx), + ids, error_codes: cx.shared.codes, edition: cx.shared.edition(), playground: &cx.shared.playground, @@ -1680,7 +1687,7 @@ fn render_rightside( pub(crate) fn render_impl_summary( w: &mut Buffer, - cx: &Context<'_>, + cx: &mut Context<'_>, i: &Impl, parent: &clean::Item, containing_item: &clean::Item, @@ -1737,7 +1744,7 @@ pub(crate) fn render_impl_summary( w.write_str(""); } -fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) { +fn print_sidebar(cx: &mut Context<'_>, it: &clean::Item, buffer: &mut Buffer) { let parentlen = cx.current.len() - if it.is_mod() { 1 } else { 0 }; if it.is_struct() @@ -1965,7 +1972,7 @@ fn small_url_encode(s: String) -> String { fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) { let did = it.def_id.expect_def_id(); - let cache = cx.cache(); + let cache = (&*cx).cache(); if let Some(v) = cache.impls.get(&did) { let mut used_links = FxHashSet::default(); @@ -2096,7 +2103,7 @@ fn sidebar_deref_methods( v: &[Impl], derefs: &mut FxHashSet, ) { - let c = cx.cache(); + let c = (&*cx).cache(); debug!("found Deref: {:?}", impl_); if let Some((target, real_target)) = @@ -2138,7 +2145,7 @@ fn sidebar_deref_methods( if !ret.is_empty() { let map; let id = if let Some(target_def_id) = real_target.def_id(c) { - map = cx.deref_id_map.borrow(); + map = &cx.deref_id_map; map.get(&target_def_id).expect("Deref section without derived id") } else { "deref-methods" @@ -2230,7 +2237,7 @@ fn extract_for_impl_name(item: &clean::Item, cx: &Context<'_>) -> Option<(String } } -fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean::Trait) { +fn sidebar_trait(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean::Trait) { buf.write_str("
"); fn print_sidebar_section( @@ -2298,15 +2305,14 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean |out, sym| write!(out, "{0}", sym), "
", ); - - let cache = cx.cache(); + let cache = *&cx.cache(); if let Some(implementors) = cache.implementors.get(&it.def_id.expect_def_id()) { let mut res = implementors .iter() .filter(|i| { i.inner_impl().for_.def_id(cache).map_or(false, |d| !cache.paths.contains_key(&d)) }) - .filter_map(|i| extract_for_impl_name(&i.impl_item, cx)) + .filter_map(|i| extract_for_impl_name(&i.impl_item, &mut *cx)) .collect::>(); if !res.is_empty() { @@ -2578,7 +2584,7 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) { }; // Generate a unique ID so users can link to this section for a given method - let id = cx.id_map.borrow_mut().derive("scraped-examples"); + let id = &cx.id_map.derive("scraped-examples"); write!( w, "
\ diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index e139ac8581e72..711d3eb916390 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -59,7 +59,7 @@ struct ItemVars<'a> { } pub(super) fn print_item( - cx: &Context<'_>, + cx: &mut Context<'_>, templates: &tera::Tera, item: &clean::Item, buf: &mut Buffer, @@ -133,12 +133,12 @@ pub(super) fn print_item( }; let item_vars = ItemVars { - page: page, + page, static_root_path: page.get_static_root_path(), - typ: typ, + typ, name: &item.name.as_ref().unwrap().as_str(), item_type: &item.type_().to_string(), - path_components: path_components, + path_components, stability_since_raw: &stability_since_raw, src_href: src_href.as_deref(), }; @@ -496,9 +496,9 @@ fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean:: unsafety = unsafety, abi = abi, name = name, - generics = f.generics.print(cx), - where_clause = print_where_clause(&f.generics, cx, 0, true), - decl = f.decl.full_print(header_len, 0, f.header.asyncness, cx), + generics = f.generics.print(&*cx), + where_clause = print_where_clause(&f.generics, &*cx, 0, true), + decl = f.decl.full_print(header_len, 0, f.header.asyncness, &*cx), notable_traits = notable_traits_decl(&f.decl, cx), ); }); @@ -506,7 +506,7 @@ fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean:: document(w, cx, it, None, HeadingOffset::H2) } -fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Trait) { +fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::Trait) { let bounds = bounds(&t.bounds, false, cx); let types = t.items.iter().filter(|m| m.is_associated_type()).collect::>(); let consts = t.items.iter().filter(|m| m.is_associated_const()).collect::>(); @@ -680,7 +680,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra write!(w, "
", id); write!(w, "
"); render_stability_since(w, m, t, cx.tcx()); - write_srclink(cx, m, w); + write_srclink(&mut (*cx), m, w); write!(w, "
"); write!(w, "

"); render_assoc_item( @@ -754,8 +754,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra // If there are methods directly on this trait object, render them here. render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All); - - let cache = cx.cache(); + let cache = (&*cx).cache(); if let Some(implementors) = cache.implementors.get(&it.def_id.expect_def_id()) { // The DefId is for the first Type found with that name. The bool is // if any Types with the same name but different DefId have been found. @@ -881,7 +880,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra ); } -fn item_trait_alias(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::TraitAlias) { +fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::TraitAlias) { wrap_into_docblock(w, |w| { wrap_item(w, "trait-alias", |w| { render_attributes_in_pre(w, it, ""); @@ -905,7 +904,7 @@ fn item_trait_alias(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clea render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All) } -fn item_opaque_ty(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::OpaqueTy) { +fn item_opaque_ty(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::OpaqueTy) { wrap_into_docblock(w, |w| { wrap_item(w, "opaque", |w| { render_attributes_in_pre(w, it, ""); @@ -931,7 +930,7 @@ fn item_opaque_ty(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean: fn item_typedef( w: &mut Buffer, - cx: &Context<'_>, + cx: &mut Context<'_>, it: &clean::Item, t: &clean::Typedef, is_associated: bool, @@ -961,14 +960,14 @@ fn item_typedef( // If this is an associated typedef, we don't want to wrap it into a docblock. if is_associated { - write_content(w, cx, it, t, is_associated); + write_content(w, &mut (*cx), it, t, is_associated); } else { wrap_into_docblock(w, |w| { - write_content(w, cx, it, t, is_associated); + write_content(w, &mut (*cx), it, t, is_associated); }); } - document(w, cx, it, None, HeadingOffset::H2); + document(w, &mut (*cx), it, None, HeadingOffset::H2); let def_id = it.def_id.expect_def_id(); // Render any items associated directly to this alias, as otherwise they @@ -978,7 +977,7 @@ fn item_typedef( render_assoc_items(w, cx, it, def_id, AssocItemRender::All); } -fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Union) { +fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Union) { wrap_into_docblock(w, |w| { wrap_item(w, "union", |w| { render_attributes_in_pre(w, it, ""); @@ -986,7 +985,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni }); }); - document(w, cx, it, None, HeadingOffset::H2); + document(w, &*cx, it, None, HeadingOffset::H2); let mut fields = s .fields @@ -1023,7 +1022,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni } } let def_id = it.def_id.expect_def_id(); - render_assoc_items(w, cx, it, def_id, AssocItemRender::All); + render_assoc_items(w, &mut *cx, it, def_id, AssocItemRender::All); document_type_layout(w, cx, def_id); } @@ -1040,7 +1039,7 @@ fn print_tuple_struct_fields(w: &mut Buffer, cx: &Context<'_>, s: &[clean::Item] } } -fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum) { +fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::Enum) { wrap_into_docblock(w, |w| { wrap_item(w, "enum", |w| { render_attributes_in_pre(w, it, ""); @@ -1231,7 +1230,7 @@ fn item_proc_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, m: &clean document(w, cx, it, None, HeadingOffset::H2) } -fn item_primitive(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) { +fn item_primitive(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) { document(w, cx, it, None, HeadingOffset::H2); render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All) } @@ -1276,7 +1275,7 @@ fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean:: document(w, cx, it, None, HeadingOffset::H2) } -fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Struct) { +fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Struct) { wrap_into_docblock(w, |w| { wrap_item(w, "struct", |w| { render_attributes_in_code(w, it); @@ -1346,7 +1345,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St document(w, cx, it, None, HeadingOffset::H2) } -fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) { +fn item_foreign_type(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) { wrap_into_docblock(w, |w| { wrap_item(w, "foreigntype", |w| { w.write_str("extern {\n"); @@ -1498,7 +1497,7 @@ fn render_implementor( }; render_impl( w, - cx, + &mut cx.clone(), implementor, trait_, AssocItemLink::Anchor(None), diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs index 0d5ba8e80d242..a7958300cc4a5 100644 --- a/src/librustdoc/html/render/write_shared.rs +++ b/src/librustdoc/html/render/write_shared.rs @@ -144,7 +144,7 @@ impl Context<'_> { } pub(super) fn write_shared( - cx: &Context<'_>, + cx: &mut Context<'_>, krate: &Crate, search_index: String, options: &RenderOptions, @@ -471,7 +471,7 @@ pub(super) fn write_shared( title: "Index of crates", css_class: "mod", root_path: "./", - static_root_path: cx.shared.static_root_path.as_deref(), + static_root_path: &*cx.shared.static_root_path.as_deref(), description: "List of crates", keywords: BASIC_KEYWORDS, resource_suffix: &cx.shared.resource_suffix, @@ -494,14 +494,7 @@ pub(super) fn write_shared( }) .collect::() ); - let v = layout::render( - &cx.shared.templates, - &cx.shared.layout, - &page, - "", - content, - &cx.shared.style_files, - ); + let v = layout::render(cx, &cx.shared, &page, "", content); cx.shared.fs.write(dst, v)?; } } diff --git a/src/librustdoc/html/sources.rs b/src/librustdoc/html/sources.rs index c8e93374e63cc..63b681112528e 100644 --- a/src/librustdoc/html/sources.rs +++ b/src/librustdoc/html/sources.rs @@ -191,11 +191,12 @@ impl SourceCollector<'_, 'tcx> { let title = format!("{} - source", src_fname.to_string_lossy()); let desc = format!("Source of the Rust file `{}`.", filename.prefer_remapped()); + let shared_clone = self.cx.shared.clone(); let page = layout::Page { title: &title, css_class: "source", root_path: &root_path, - static_root_path: self.cx.shared.static_root_path.as_deref(), + static_root_path: shared_clone.static_root_path.as_deref(), description: &desc, keywords: BASIC_KEYWORDS, resource_suffix: &self.cx.shared.resource_suffix, @@ -203,23 +204,22 @@ impl SourceCollector<'_, 'tcx> { static_extra_scripts: &[&format!("source-script{}", self.cx.shared.resource_suffix)], }; let v = layout::render( - &self.cx.shared.templates, - &self.cx.shared.layout, + self.cx, + &self.cx.shared, &page, "", - |buf: &mut _| { + |buf: &mut _, cx: &mut Context<'_>| { print_src( buf, contents, - self.cx.shared.edition(), + cx.shared.edition(), file_span, - self.cx, + cx, &root_path, None, SourceContext::Standalone, ) }, - &self.cx.shared.style_files, ); self.cx.shared.fs.write(cur, v)?; self.emitted_local_sources.insert(p); diff --git a/src/librustdoc/scrape_examples.rs b/src/librustdoc/scrape_examples.rs index 10b6fdf87f419..3a3385614d56e 100644 --- a/src/librustdoc/scrape_examples.rs +++ b/src/librustdoc/scrape_examples.rs @@ -108,7 +108,7 @@ crate type AllCallLocations = FxHashMap; struct FindCalls<'a, 'tcx> { tcx: TyCtxt<'tcx>, map: Map<'tcx>, - cx: Context<'tcx>, + cx: &'a mut Context<'tcx>, target_crates: Vec, calls: &'a mut AllCallLocations, } @@ -230,7 +230,8 @@ crate fn run( ) -> interface::Result<()> { let inner = move || -> Result<(), String> { // Generates source files for examples - let (cx, _) = Context::init(krate, renderopts, cache, tcx).map_err(|e| e.to_string())?; + let (mut cx, _) = + Context::init(krate, renderopts, cache, tcx).map_err(|e| e.to_string())?; // Collect CrateIds corresponding to provided target crates // If two different versions of the crate in the dependency tree, then examples will be collcted from both. @@ -253,7 +254,8 @@ crate fn run( // Run call-finder on all items let mut calls = FxHashMap::default(); - let mut finder = FindCalls { calls: &mut calls, tcx, map: tcx.hir(), cx, target_crates }; + let mut finder = + FindCalls { calls: &mut calls, tcx, map: tcx.hir(), cx: &mut cx, target_crates }; tcx.hir().visit_all_item_likes(&mut finder.as_deep_visitor()); // Sort call locations within a given file in document order