From 1d205a44127a5641f16513353b2be4d4ae4fd48a Mon Sep 17 00:00:00 2001 From: Milo <50248166+Milo123459@users.noreply.github.com> Date: Thu, 2 Dec 2021 17:18:15 +0000 Subject: [PATCH 1/8] intial work on this --- src/librustdoc/html/render/context.rs | 32 ++++---- src/librustdoc/html/render/mod.rs | 94 ++++++++++++------------ src/librustdoc/html/render/print_item.rs | 82 ++++++++++----------- src/librustdoc/scrape_examples.rs | 6 +- 4 files changed, 108 insertions(+), 106 deletions(-) diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 069862efde640..0fd1eb6be2d07 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -43,6 +43,8 @@ use crate::try_err; /// It is intended that this context is a lightweight object which can be fairly /// easily cloned because it is cloned per work-job (about once per item in the /// rustdoc tree). +/// +#[derive(Clone)] crate struct Context<'tcx> { /// Current hierarchy of components leading down to what's currently being /// rendered @@ -56,9 +58,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 +75,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 +168,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/' @@ -212,10 +213,11 @@ impl<'tcx> Context<'tcx> { } else { tyname.as_str() }; + let clone = self.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: clone.shared.static_root_path.as_deref(), title: &title, description: &desc, keywords: &keywords, @@ -229,8 +231,8 @@ impl<'tcx> Context<'tcx> { &self.shared.templates, &self.shared.layout, &page, - |buf: &mut _| print_sidebar(self, it, buf), - |buf: &mut _| print_item(self, &self.shared.templates, it, buf, &page), + |buf: &mut _| print_sidebar(&mut self.clone(), it, buf), + |buf: &mut _| print_item(&self, &self.shared.templates, it, buf, &page), &self.shared.style_files, ) } else { @@ -515,8 +517,8 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { current: Vec::new(), dst, render_redirect_pages: false, - id_map: RefCell::new(id_map), - deref_id_map: RefCell::new(FxHashMap::default()), + id_map: id_map, + deref_id_map: FxHashMap::default(), shared: Rc::new(scx), include_sources, }; @@ -540,8 +542,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, } @@ -644,7 +646,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); @@ -656,7 +658,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 25fef114d95fd..46e4daeac8dd9 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -181,7 +181,7 @@ crate struct StylePath { crate disabled: bool, } -fn write_srclink(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer) { +fn write_srclink(cx: &mut Context<'_>, item: &clean::Item, buf: &mut Buffer) { if let Some(l) = cx.src_href(item) { write!(buf, "[src]", l) } @@ -482,7 +482,7 @@ fn settings(root_path: &str, suffix: &str, themes: &[StylePath]) -> Result, + cx: &mut Context<'_>, item: &clean::Item, parent: Option<&clean::Item>, heading_offset: HeadingOffset, @@ -501,19 +501,19 @@ fn document( /// Render md_text as markdown. fn render_markdown( w: &mut Buffer, - cx: &Context<'_>, + cx: &mut Context<'_>, md_text: &str, 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: ids, error_codes: cx.shared.codes, edition: cx.shared.edition(), playground: &cx.shared.playground, @@ -528,7 +528,7 @@ fn render_markdown( fn document_short( w: &mut Buffer, item: &clean::Item, - cx: &Context<'_>, + cx: &mut Context<'_>, link: AssocItemLink<'_>, parent: &clean::Item, show_def_docs: bool, @@ -557,7 +557,7 @@ fn document_short( fn document_full_collapsible( w: &mut Buffer, item: &clean::Item, - cx: &Context<'_>, + cx: &mut Context<'_>, heading_offset: HeadingOffset, ) { document_full_inner(w, item, cx, true, heading_offset); @@ -566,7 +566,7 @@ fn document_full_collapsible( fn document_full( w: &mut Buffer, item: &clean::Item, - cx: &Context<'_>, + cx: &mut Context<'_>, heading_offset: HeadingOffset, ) { document_full_inner(w, item, cx, false, heading_offset); @@ -575,7 +575,7 @@ fn document_full( fn document_full_inner( w: &mut Buffer, item: &clean::Item, - cx: &Context<'_>, + cx: &mut Context<'_>, is_collapsible: bool, heading_offset: HeadingOffset, ) { @@ -611,7 +611,7 @@ fn document_full_inner( /// * Required features (through the `doc_cfg` feature) fn document_item_info( w: &mut Buffer, - cx: &Context<'_>, + cx: &mut Context<'_>, item: &clean::Item, parent: Option<&clean::Item>, ) { @@ -640,7 +640,7 @@ fn portability(item: &clean::Item, parent: Option<&clean::Item>) -> Option, + cx: &mut Context<'_>, parent: Option<&clean::Item>, ) -> Vec { let mut extra_info = vec![]; @@ -668,7 +668,7 @@ fn short_item_info( if let Some(note) = note { let note = note.as_str(); - let mut ids = cx.id_map.borrow_mut(); + let mut ids = &cx.id_map; let html = MarkdownHtml( ¬e, &mut ids, @@ -707,7 +707,7 @@ fn short_item_info( message.push_str(&format!(" ({})", feature)); if let Some(unstable_reason) = reason { - let mut ids = cx.id_map.borrow_mut(); + let mut ids = &cx.id_map; message = format!( "
{}{}
", message, @@ -734,7 +734,7 @@ 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() @@ -767,7 +767,7 @@ fn render_impls(cx: &Context<'_>, w: &mut Buffer, impls: &[&&Impl], containing_i w.write_str(&rendered_impls.join("")); } -fn naive_assoc_href(it: &clean::Item, link: AssocItemLink<'_>, cx: &Context<'_>) -> String { +fn naive_assoc_href(it: &clean::Item, link: AssocItemLink<'_>, cx: &mut Context<'_>) -> String { use crate::formats::item_type::ItemType::*; let name = it.name.as_ref().unwrap(); @@ -793,7 +793,7 @@ fn assoc_const( _default: Option<&String>, link: AssocItemLink<'_>, extra: &str, - cx: &Context<'_>, + cx: &mut Context<'_>, ) { write!( w, @@ -813,7 +813,7 @@ fn assoc_type( default: Option<&clean::Type>, link: AssocItemLink<'_>, extra: &str, - cx: &Context<'_>, + cx: &mut Context<'_>, ) { write!( w, @@ -888,7 +888,7 @@ fn render_assoc_item( item: &clean::Item, link: AssocItemLink<'_>, parent: ItemType, - cx: &Context<'_>, + cx: &mut Context<'_>, ) { fn method( w: &mut Buffer, @@ -898,7 +898,7 @@ fn render_assoc_item( d: &clean::FnDecl, link: AssocItemLink<'_>, parent: ItemType, - cx: &Context<'_>, + cx: &mut Context<'_>, ) { let name = meth.name.as_ref().unwrap(); let href = match link { @@ -1050,7 +1050,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<'_>, @@ -1062,7 +1062,7 @@ 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<'_>, @@ -1090,7 +1090,7 @@ fn render_assoc_items_inner( 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, @@ -1193,7 +1193,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, @@ -1260,7 +1260,7 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool, tcx: TyCtxt<'_>) -> } } -fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String { +fn notable_traits_decl(decl: &clean::FnDecl, cx: &mut Context<'_>) -> String { let mut out = Buffer::html(); if let Some(did) = decl.output.as_return().and_then(|t| t.def_id(cx.cache())) { @@ -1326,7 +1326,7 @@ struct ImplRenderingParameters { fn render_impl( w: &mut Buffer, - cx: &Context<'_>, + cx: &mut Context<'_>, i: &Impl, parent: &clean::Item, link: AssocItemLink<'_>, @@ -1348,7 +1348,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, @@ -1385,7 +1385,7 @@ fn render_impl( // because impls can't have a stability. if item.doc_value().is_some() { document_item_info(&mut info_buffer, cx, it, Some(parent)); - document_full(&mut doc_buffer, item, cx, HeadingOffset::H5); + document_full(&mut doc_buffer, item, &mut cx, HeadingOffset::H5); short_documented = false; } else { // In case the item isn't documented, @@ -1403,7 +1403,7 @@ fn render_impl( } else { document_item_info(&mut info_buffer, cx, item, Some(parent)); if rendering_params.show_def_docs { - document_full(&mut doc_buffer, item, cx, HeadingOffset::H5); + document_full(&mut doc_buffer, item, &mut cx, HeadingOffset::H5); short_documented = false; } } @@ -1554,7 +1554,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, @@ -1633,7 +1633,7 @@ 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 mut ids = &cx.id_map; write!( w, "
{}
", @@ -1663,7 +1663,7 @@ fn render_impl( // associated types. For example "1.0.0 (const: 1.39.0) [src]". fn render_rightside( w: &mut Buffer, - cx: &Context<'_>, + cx: &mut Context<'_>, item: &clean::Item, containing_item: &clean::Item, ) { @@ -1684,7 +1684,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, @@ -1741,7 +1741,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() @@ -1967,7 +1967,7 @@ fn small_url_encode(s: String) -> String { } } -fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) { +fn sidebar_assoc_items(cx: &mut Context<'_>, out: &mut Buffer, it: &clean::Item) { let did = it.def_id.expect_def_id(); let cache = cx.cache(); if let Some(v) = cache.impls.get(&did) { @@ -2095,7 +2095,7 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) { } fn sidebar_deref_methods( - cx: &Context<'_>, + cx: &mut Context<'_>, out: &mut Buffer, impl_: &Impl, v: &[Impl], @@ -2143,7 +2143,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" @@ -2182,7 +2182,7 @@ fn sidebar_deref_methods( } } -fn sidebar_struct(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, s: &clean::Struct) { +fn sidebar_struct(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item, s: &clean::Struct) { let mut sidebar = Buffer::new(); let fields = get_struct_fields_name(&s.fields); @@ -2214,12 +2214,12 @@ fn sidebar_struct(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, s: &clea fn get_id_for_impl_on_foreign_type( for_: &clean::Type, trait_: &clean::Path, - cx: &Context<'_>, + cx: &mut Context<'_>, ) -> String { small_url_encode(format!("impl-{:#}-for-{:#}", trait_.print(cx), for_.print(cx))) } -fn extract_for_impl_name(item: &clean::Item, cx: &Context<'_>) -> Option<(String, String)> { +fn extract_for_impl_name(item: &clean::Item, cx: &mut Context<'_>) -> Option<(String, String)> { match *item.kind { clean::ItemKind::ImplItem(ref i) => { i.trait_.as_ref().map(|trait_| { @@ -2235,7 +2235,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( @@ -2341,7 +2341,7 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean buf.push_str("
") } -fn sidebar_primitive(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item) { +fn sidebar_primitive(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item) { let mut sidebar = Buffer::new(); sidebar_assoc_items(cx, &mut sidebar, it); @@ -2350,7 +2350,7 @@ fn sidebar_primitive(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item) { } } -fn sidebar_typedef(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item) { +fn sidebar_typedef(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item) { let mut sidebar = Buffer::new(); sidebar_assoc_items(cx, &mut sidebar, it); @@ -2371,7 +2371,7 @@ fn get_struct_fields_name(fields: &[clean::Item]) -> Vec { fields } -fn sidebar_union(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, u: &clean::Union) { +fn sidebar_union(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item, u: &clean::Union) { let mut sidebar = Buffer::new(); let fields = get_struct_fields_name(&u.fields); @@ -2395,7 +2395,7 @@ fn sidebar_union(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, u: &clean } } -fn sidebar_enum(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, e: &clean::Enum) { +fn sidebar_enum(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item, e: &clean::Enum) { let mut sidebar = Buffer::new(); let mut variants = e @@ -2502,7 +2502,7 @@ fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) { } } -fn sidebar_foreign_type(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item) { +fn sidebar_foreign_type(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item) { let mut sidebar = Buffer::new(); sidebar_assoc_items(cx, &mut sidebar, it); @@ -2571,7 +2571,7 @@ const MAX_FULL_EXAMPLES: usize = 5; const NUM_VISIBLE_LINES: usize = 10; /// Generates the HTML for example call locations generated via the --scrape-examples flag. -fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) { +fn render_call_locations(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item) { let tcx = cx.tcx(); let def_id = item.def_id.expect_def_id(); let key = tcx.def_path_hash(def_id); @@ -2583,7 +2583,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 d07ef6db4c6b0..270dfd38d9628 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -148,7 +148,7 @@ pub(super) fn print_item( buf.write_str(&heading); match *item.kind { - clean::ModuleItem(ref m) => item_module(buf, cx, item, &m.items), + clean::ModuleItem(ref m) => item_module(buf, &mut cx, item, &m.items), clean::FunctionItem(ref f) | clean::ForeignFunctionItem(ref f) => { item_function(buf, cx, item, f) } @@ -193,8 +193,8 @@ fn toggle_close(w: &mut Buffer) { w.write_str(""); } -fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) { - document(w, cx, item, None, HeadingOffset::H2); +fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items: &[clean::Item]) { + document(w, &mut cx, item, None, HeadingOffset::H2); let mut indices = (0..items.len()).filter(|i| !items[*i].is_stripped()).collect::>(); @@ -499,11 +499,11 @@ fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean:: 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), + notable_traits = notable_traits_decl(&f.decl, &mut cx), ); }); }); - document(w, cx, it, None, HeadingOffset::H2) + document(w, &mut cx, it, None, HeadingOffset::H2) } fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Trait) { @@ -556,7 +556,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra ); } for t in &types { - render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx); + render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, &mut cx); w.write_str(";\n"); } // If there are too many associated constants, hide everything after them @@ -580,7 +580,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra w.write_str("\n"); } for t in &consts { - render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx); + render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, &mut cx); w.write_str(";\n"); } if !toggle && should_hide_fields(count_methods) { @@ -591,7 +591,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra w.write_str("\n"); } for (pos, m) in required.iter().enumerate() { - render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait, cx); + render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait, &mut cx); w.write_str(";\n"); if pos < required.len() - 1 { @@ -602,7 +602,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra w.write_str("\n"); } for (pos, m) in provided.iter().enumerate() { - render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait, cx); + render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait, &mut cx); match *m.kind { clean::MethodItem(ref inner, _) if !inner.generics.where_predicates.is_empty() => @@ -626,7 +626,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra }); // Trait documentation - document(w, cx, it, None, HeadingOffset::H2); + document(w, &mut cx, it, None, HeadingOffset::H2); fn write_small_section_header(w: &mut Buffer, id: &str, title: &str, extra_content: &str) { write!( @@ -644,7 +644,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra let item_type = m.type_(); let id = cx.derive_id(format!("{}.{}", item_type, name)); let mut content = Buffer::empty_from(w); - document(&mut content, cx, m, Some(t), HeadingOffset::H5); + document(&mut content, &mut cx, m, Some(t), HeadingOffset::H5); let toggled = !content.is_empty(); if toggled { write!(w, "
"); @@ -652,10 +652,10 @@ 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(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl, cx); + render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl, &mut cx); w.write_str("

"); w.write_str("
"); if toggled { @@ -718,7 +718,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); + render_assoc_items(w, &mut cx, it, it.def_id.expect_def_id(), AssocItemRender::All); let cache = cx.cache(); if let Some(implementors) = cache.implementors.get(&it.def_id.expect_def_id()) { @@ -760,7 +760,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra AssocItemLink::GotoSource(implementor.impl_item.def_id, &provided_methods); render_impl( w, - cx, + &mut cx, implementor, it, assoc_link, @@ -860,13 +860,13 @@ fn item_trait_alias(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clea }); }); - document(w, cx, it, None, HeadingOffset::H2); + document(w, &mut cx, it, None, HeadingOffset::H2); // Render any items associated directly to this alias, as otherwise they // won't be visible anywhere in the docs. It would be nice to also show // associated items from the aliased type (see discussion in #32077), but // we need #14072 to make sense of the generics. - render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All) + render_assoc_items(w, &mut 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) { @@ -884,13 +884,13 @@ fn item_opaque_ty(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean: }); }); - document(w, cx, it, None, HeadingOffset::H2); + document(w, &mut cx, it, None, HeadingOffset::H2); // Render any items associated directly to this alias, as otherwise they // won't be visible anywhere in the docs. It would be nice to also show // associated items from the aliased type (see discussion in #32077), but // we need #14072 to make sense of the generics. - render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All) + render_assoc_items(w, &mut cx, it, it.def_id.expect_def_id(), AssocItemRender::All) } fn item_typedef( @@ -932,14 +932,14 @@ fn item_typedef( }); } - 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 // won't be visible anywhere in the docs. It would be nice to also show // associated items from the aliased type (see discussion in #32077), but // we need #14072 to make sense of the generics. - render_assoc_items(w, cx, it, def_id, AssocItemRender::All); + render_assoc_items(w, &mut cx, it, def_id, AssocItemRender::All); } fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Union) { @@ -950,7 +950,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni }); }); - document(w, cx, it, None, HeadingOffset::H2); + document(w, &mut cx, it, None, HeadingOffset::H2); let mut fields = s .fields @@ -983,11 +983,11 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni if let Some(stability_class) = field.stability_class(cx.tcx()) { write!(w, "", stab = stability_class); } - document(w, cx, field, Some(it), HeadingOffset::H3); + document(w, &mut cx, field, Some(it), HeadingOffset::H3); } } 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); } @@ -1065,7 +1065,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum }); }); - document(w, cx, it, None, HeadingOffset::H2); + document(w, &mut cx, it, None, HeadingOffset::H2); if !e.variants.is_empty() { write!( @@ -1094,7 +1094,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum w.write_str(""); render_stability_since(w, variant, it, cx.tcx()); w.write_str("
"); - document(w, cx, variant, Some(it), HeadingOffset::H3); + document(w, &mut cx, variant, Some(it), HeadingOffset::H3); document_non_exhaustive(w, variant); use crate::clean::Variant; @@ -1134,7 +1134,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum f = field.name.as_ref().unwrap(), t = ty.print(cx) ); - document(w, cx, field, Some(variant), HeadingOffset::H4); + document(w, &mut cx, field, Some(variant), HeadingOffset::H4); } _ => unreachable!(), } @@ -1144,7 +1144,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum } } 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); } @@ -1162,7 +1162,7 @@ fn item_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Mac None, ); }); - document(w, cx, it, None, HeadingOffset::H2) + document(w, &mut cx, it, None, HeadingOffset::H2) } fn item_proc_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, m: &clean::ProcMacro) { @@ -1194,12 +1194,12 @@ fn item_proc_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, m: &clean } } }); - document(w, cx, it, None, HeadingOffset::H2) + document(w, &mut cx, it, None, HeadingOffset::H2) } fn item_primitive(w: &mut Buffer, cx: &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) + document(w, &mut cx, it, None, HeadingOffset::H2); + render_assoc_items(w, &mut cx, it, it.def_id.expect_def_id(), AssocItemRender::All) } fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean::Constant) { @@ -1239,7 +1239,7 @@ fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean:: }); }); - document(w, cx, it, None, HeadingOffset::H2) + document(w, &mut cx, it, None, HeadingOffset::H2) } fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Struct) { @@ -1250,7 +1250,7 @@ fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St }); }); - document(w, cx, it, None, HeadingOffset::H2); + document(w, &mut cx, it, None, HeadingOffset::H2); let mut fields = s .fields @@ -1286,12 +1286,12 @@ fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St name = field_name, ty = ty.print(cx) ); - document(w, cx, field, Some(it), HeadingOffset::H3); + document(w, &mut cx, field, Some(it), HeadingOffset::H3); } } } 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); } @@ -1309,7 +1309,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St ); }); }); - document(w, cx, it, None, HeadingOffset::H2) + document(w, &mut cx, it, None, HeadingOffset::H2) } fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) { @@ -1326,13 +1326,13 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) { }); }); - document(w, cx, it, None, HeadingOffset::H2); + document(w, &mut cx, it, None, HeadingOffset::H2); - render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All) + render_assoc_items(w, &mut cx, it, it.def_id.expect_def_id(), AssocItemRender::All) } fn item_keyword(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) { - document(w, cx, it, None, HeadingOffset::H2) + document(w, &mut cx, it, None, HeadingOffset::H2) } /// Compare two strings treating multi-digit numbers as single units (i.e. natural sort order). @@ -1464,7 +1464,7 @@ fn render_implementor( }; render_impl( w, - cx, + &mut cx, implementor, trait_, AssocItemLink::Anchor(None), diff --git a/src/librustdoc/scrape_examples.rs b/src/librustdoc/scrape_examples.rs index 05e746573f479..bc0587d22fce2 100644 --- a/src/librustdoc/scrape_examples.rs +++ b/src/librustdoc/scrape_examples.rs @@ -114,7 +114,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, } @@ -214,7 +214,7 @@ 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. @@ -237,7 +237,7 @@ 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()); // Save output to provided path From 8bedeaf269a0a241eda625f2331e00d4b51c1414 Mon Sep 17 00:00:00 2001 From: Milo <50248166+Milo123459@users.noreply.github.com> Date: Thu, 2 Dec 2021 21:14:28 +0000 Subject: [PATCH 2/8] no errors --- src/librustdoc/html/render/context.rs | 17 +-- src/librustdoc/html/render/mod.rs | 48 ++++---- src/librustdoc/html/render/print_item.rs | 134 +++++++++++------------ 3 files changed, 103 insertions(+), 96 deletions(-) diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 0fd1eb6be2d07..b38494421e8ba 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -178,7 +178,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()); @@ -214,6 +214,7 @@ impl<'tcx> Context<'tcx> { tyname.as_str() }; let clone = self.clone(); + let shared_clone = self.shared.clone(); let page = layout::Page { css_class: tyname_s, root_path: &self.root_path(), @@ -221,18 +222,20 @@ impl<'tcx> Context<'tcx> { title: &title, description: &desc, keywords: &keywords, - resource_suffix: &self.shared.resource_suffix, + resource_suffix: &*shared_clone.resource_suffix, extra_scripts: &[], static_extra_scripts: &[], }; - + let clone_1 = &mut self.clone(); + let clone_2 = &mut self.clone(); + let shared_templates = &self.shared.clone().templates; if !self.render_redirect_pages { layout::render( - &self.shared.templates, - &self.shared.layout, + &self.clone().shared.templates, + &self.clone().shared.layout, &page, - |buf: &mut _| print_sidebar(&mut self.clone(), it, buf), - |buf: &mut _| print_item(&self, &self.shared.templates, it, buf, &page), + |buf: &mut _| print_sidebar(clone_1, it, buf), + |buf: &mut _| print_item(clone_2, shared_templates, it, buf, &page), &self.shared.style_files, ) } else { diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 46e4daeac8dd9..0d0e8f77054b4 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -668,10 +668,10 @@ fn short_item_info( if let Some(note) = note { let note = note.as_str(); - let mut ids = &cx.id_map; + let ids = &mut cx.id_map; let html = MarkdownHtml( ¬e, - &mut ids, + ids, error_codes, cx.shared.edition(), &cx.shared.playground, @@ -707,13 +707,13 @@ fn short_item_info( message.push_str(&format!(" ({})", feature)); if let Some(unstable_reason) = reason { - let mut ids = &cx.id_map; + let ids = &mut cx.id_map; message = format!( "
{}{}
", message, MarkdownHtml( &unstable_reason.as_str(), - &mut ids, + ids, error_codes, cx.shared.edition(), &cx.shared.playground, @@ -799,7 +799,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.clone()), naive_assoc_href(it, link, cx), it.name.as_ref().unwrap(), ty.print(cx) @@ -964,8 +964,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.clone()), + decl = d.full_print(header_len, indent, header.asyncness, &cx.clone()), notable_traits = notable_traits_decl(d, cx), where_clause = print_where_clause(g, cx, indent, end_newline), ) @@ -1069,7 +1069,8 @@ fn render_assoc_items_inner( derefs: &mut FxHashSet, ) { info!("Documenting associated items of {:?}", containing_item.name); - let cache = cx.cache(); + let cx_clone = cx.clone(); + let cache = cx_clone.cache(); let v = match cache.impls.get(&it) { Some(v) => v, None => return, @@ -1088,9 +1089,9 @@ fn render_assoc_items_inner( } AssocItemRender::DerefFor { trait_, type_, deref_mut_ } => { let id = - cx.derive_id(small_url_encode(format!("deref-methods-{:#}", type_.print(cx)))); + cx.derive_id(small_url_encode(format!("deref-methods-{:#}", type_.print(&cx.clone())))); if let Some(def_id) = type_.def_id(cx.cache()) { - &cx.deref_id_map.insert(def_id, id.clone()); + cx.deref_id_map.insert(def_id, id.clone()); } write!( tmp_buf, @@ -1264,7 +1265,7 @@ fn notable_traits_decl(decl: &clean::FnDecl, cx: &mut Context<'_>) -> String { let mut out = Buffer::html(); if let Some(did) = decl.output.as_return().and_then(|t| t.def_id(cx.cache())) { - if let Some(impls) = cx.cache().impls.get(&did) { + if let Some(impls) = cx.clone().cache().impls.get(&did) { for i in impls { let impl_ = i.inner_impl(); if let Some(trait_) = &impl_.trait_ { @@ -1335,7 +1336,8 @@ fn render_impl( aliases: &[String], rendering_params: ImplRenderingParameters, ) { - let cache = cx.cache(); + let cx_clone = cx.clone(); + let cache = cx_clone.cache(); let traits = &cache.traits; let trait_ = i.trait_did().map(|did| &traits[&did]); let mut close_tags = String::new(); @@ -1385,7 +1387,7 @@ fn render_impl( // because impls can't have a stability. if item.doc_value().is_some() { document_item_info(&mut info_buffer, cx, it, Some(parent)); - document_full(&mut doc_buffer, item, &mut cx, HeadingOffset::H5); + document_full(&mut doc_buffer, item, cx, HeadingOffset::H5); short_documented = false; } else { // In case the item isn't documented, @@ -1403,7 +1405,7 @@ fn render_impl( } else { document_item_info(&mut info_buffer, cx, item, Some(parent)); if rendering_params.show_def_docs { - document_full(&mut doc_buffer, item, &mut cx, HeadingOffset::H5); + document_full(&mut doc_buffer, item, cx, HeadingOffset::H5); short_documented = false; } } @@ -1568,7 +1570,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.clone().tcx()); let assoc_link = AssocItemLink::GotoSource(did.into(), &provided_methods); doc_impl_item( @@ -1633,14 +1635,14 @@ fn render_impl( } if let Some(ref dox) = cx.shared.maybe_collapsed_doc_value(&i.impl_item) { - let mut ids = &cx.id_map; + let ids = &mut cx.clone().id_map; write!( w, "
{}
", Markdown { content: &*dox, links: &i.impl_item.links(cx), - ids: &mut ids, + ids, error_codes: cx.shared.codes, edition: cx.shared.edition(), playground: &cx.shared.playground, @@ -1695,7 +1697,7 @@ pub(crate) fn render_impl_summary( // in documentation pages for trait with automatic implementations like "Send" and "Sync". aliases: &[String], ) { - let id = cx.derive_id(match i.inner_impl().trait_ { + let id = cx.clone().derive_id(match i.inner_impl().trait_ { Some(ref t) => { if is_on_foreign_type { get_id_for_impl_on_foreign_type(&i.inner_impl().for_, t, cx) @@ -1969,7 +1971,8 @@ fn small_url_encode(s: String) -> String { fn sidebar_assoc_items(cx: &mut Context<'_>, out: &mut Buffer, it: &clean::Item) { let did = it.def_id.expect_def_id(); - let cache = cx.cache(); + let cx_clone = cx.clone(); + let cache = cx_clone.cache(); if let Some(v) = cache.impls.get(&did) { let mut used_links = FxHashSet::default(); @@ -2101,7 +2104,8 @@ fn sidebar_deref_methods( v: &[Impl], derefs: &mut FxHashSet, ) { - let c = cx.cache(); + let cx_clone = cx.clone(); + let c = cx_clone.cache(); debug!("found Deref: {:?}", impl_); if let Some((target, real_target)) = @@ -2303,8 +2307,8 @@ fn sidebar_trait(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &c |out, sym| write!(out, "{0}", sym), "", ); - - let cache = cx.cache(); + let cx_clone = cx.clone(); + let cache = cx_clone.cache(); if let Some(implementors) = cache.implementors.get(&it.def_id.expect_def_id()) { let mut res = implementors .iter() diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 270dfd38d9628..7aee29a65a070 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(), }; @@ -148,7 +148,7 @@ pub(super) fn print_item( buf.write_str(&heading); match *item.kind { - clean::ModuleItem(ref m) => item_module(buf, &mut cx, item, &m.items), + clean::ModuleItem(ref m) => item_module(buf, cx, item, &m.items), clean::FunctionItem(ref f) | clean::ForeignFunctionItem(ref f) => { item_function(buf, cx, item, f) } @@ -194,7 +194,7 @@ fn toggle_close(w: &mut Buffer) { } fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items: &[clean::Item]) { - document(w, &mut cx, item, None, HeadingOffset::H2); + document(w, cx, item, None, HeadingOffset::H2); let mut indices = (0..items.len()).filter(|i| !items[*i].is_stripped()).collect::>(); @@ -464,7 +464,7 @@ fn extra_info_tags(item: &clean::Item, parent: &clean::Item, tcx: TyCtxt<'_>) -> tags } -fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) { +fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &clean::Function) { let vis = it.visibility.print_with_space(it.def_id, cx).to_string(); let constness = print_constness_with_space(&f.header.constness, it.const_stability(cx.tcx())); let asyncness = f.header.asyncness.print_with_space(); @@ -496,17 +496,17 @@ 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), - notable_traits = notable_traits_decl(&f.decl, &mut cx), + generics = f.generics.print(&cx.clone()), + where_clause = print_where_clause(&f.generics, &cx.clone(), 0, true), + decl = f.decl.full_print(header_len, 0, f.header.asyncness, &cx.clone()), + notable_traits = notable_traits_decl(&f.decl, cx), ); }); }); - document(w, &mut cx, it, None, HeadingOffset::H2) + 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::>(); @@ -556,7 +556,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra ); } for t in &types { - render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, &mut cx); + render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx); w.write_str(";\n"); } // If there are too many associated constants, hide everything after them @@ -580,7 +580,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra w.write_str("\n"); } for t in &consts { - render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, &mut cx); + render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx); w.write_str(";\n"); } if !toggle && should_hide_fields(count_methods) { @@ -591,7 +591,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra w.write_str("\n"); } for (pos, m) in required.iter().enumerate() { - render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait, &mut cx); + render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait, cx); w.write_str(";\n"); if pos < required.len() - 1 { @@ -602,7 +602,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra w.write_str("\n"); } for (pos, m) in provided.iter().enumerate() { - render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait, &mut cx); + render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait, cx); match *m.kind { clean::MethodItem(ref inner, _) if !inner.generics.where_predicates.is_empty() => @@ -626,7 +626,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra }); // Trait documentation - document(w, &mut cx, it, None, HeadingOffset::H2); + document(w, cx, it, None, HeadingOffset::H2); fn write_small_section_header(w: &mut Buffer, id: &str, title: &str, extra_content: &str) { write!( @@ -638,13 +638,13 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra ) } - fn trait_item(w: &mut Buffer, cx: &Context<'_>, m: &clean::Item, t: &clean::Item) { + fn trait_item(w: &mut Buffer, cx: &mut Context<'_>, m: &clean::Item, t: &clean::Item) { let name = m.name.as_ref().unwrap(); info!("Documenting {} on {:?}", name, t.name); let item_type = m.type_(); let id = cx.derive_id(format!("{}.{}", item_type, name)); let mut content = Buffer::empty_from(w); - document(&mut content, &mut cx, m, Some(t), HeadingOffset::H5); + document(&mut content, cx, m, Some(t), HeadingOffset::H5); let toggled = !content.is_empty(); if toggled { write!(w, "
"); @@ -652,10 +652,10 @@ 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(&mut cx, m, w); + write_srclink(&mut cx.clone(), m, w); write!(w, "
"); write!(w, "

"); - render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl, &mut cx); + render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl, cx); w.write_str("

"); w.write_str("
"); if toggled { @@ -718,9 +718,9 @@ 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, &mut cx, it, it.def_id.expect_def_id(), AssocItemRender::All); - - let cache = cx.cache(); + render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All); + let cx_clone = cx.clone(); + let cache = cx_clone.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. @@ -760,7 +760,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra AssocItemLink::GotoSource(implementor.impl_item.def_id, &provided_methods); render_impl( w, - &mut cx, + cx, implementor, it, assoc_link, @@ -845,7 +845,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, ""); @@ -860,13 +860,13 @@ fn item_trait_alias(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clea }); }); - document(w, &mut cx, it, None, HeadingOffset::H2); + document(w, cx, it, None, HeadingOffset::H2); // Render any items associated directly to this alias, as otherwise they // won't be visible anywhere in the docs. It would be nice to also show // associated items from the aliased type (see discussion in #32077), but // we need #14072 to make sense of the generics. - render_assoc_items(w, &mut cx, it, it.def_id.expect_def_id(), AssocItemRender::All) + 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) { @@ -884,25 +884,25 @@ fn item_opaque_ty(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean: }); }); - document(w, &mut cx, it, None, HeadingOffset::H2); + document(w, &mut cx.clone(), it, None, HeadingOffset::H2); // Render any items associated directly to this alias, as otherwise they // won't be visible anywhere in the docs. It would be nice to also show // associated items from the aliased type (see discussion in #32077), but // we need #14072 to make sense of the generics. - render_assoc_items(w, &mut cx, it, it.def_id.expect_def_id(), AssocItemRender::All) + render_assoc_items(w, &mut cx.clone(), it, it.def_id.expect_def_id(), AssocItemRender::All) } fn item_typedef( w: &mut Buffer, - cx: &Context<'_>, + cx: &mut Context<'_>, it: &clean::Item, t: &clean::Typedef, is_associated: bool, ) { fn write_content( w: &mut Buffer, - cx: &Context<'_>, + cx: &mut Context<'_>, it: &clean::Item, t: &clean::Typedef, is_associated: bool, @@ -925,24 +925,24 @@ 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.clone(), it, t, is_associated); } else { wrap_into_docblock(w, |w| { - write_content(w, cx, it, t, is_associated); + write_content(w, &mut cx.clone(), it, t, is_associated); }); } - document(w, &mut cx, it, None, HeadingOffset::H2); + document(w, &mut cx.clone(), it, None, HeadingOffset::H2); let def_id = it.def_id.expect_def_id(); // Render any items associated directly to this alias, as otherwise they // won't be visible anywhere in the docs. It would be nice to also show // associated items from the aliased type (see discussion in #32077), but // we need #14072 to make sense of the generics. - render_assoc_items(w, &mut cx, it, def_id, AssocItemRender::All); + 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, ""); @@ -950,7 +950,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni }); }); - document(w, &mut cx, it, None, HeadingOffset::H2); + document(w, &mut cx.clone(), it, None, HeadingOffset::H2); let mut fields = s .fields @@ -983,11 +983,11 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni if let Some(stability_class) = field.stability_class(cx.tcx()) { write!(w, "", stab = stability_class); } - document(w, &mut cx, field, Some(it), HeadingOffset::H3); + document(w, cx, field, Some(it), HeadingOffset::H3); } } let def_id = it.def_id.expect_def_id(); - render_assoc_items(w, &mut cx, it, def_id, AssocItemRender::All); + render_assoc_items(w, &mut cx.clone(), it, def_id, AssocItemRender::All); document_type_layout(w, cx, def_id); } @@ -1004,7 +1004,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, ""); @@ -1065,7 +1065,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum }); }); - document(w, &mut cx, it, None, HeadingOffset::H2); + document(w, cx, it, None, HeadingOffset::H2); if !e.variants.is_empty() { write!( @@ -1094,7 +1094,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum w.write_str(""); render_stability_since(w, variant, it, cx.tcx()); w.write_str(""); - document(w, &mut cx, variant, Some(it), HeadingOffset::H3); + document(w, cx, variant, Some(it), HeadingOffset::H3); document_non_exhaustive(w, variant); use crate::clean::Variant; @@ -1134,7 +1134,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum f = field.name.as_ref().unwrap(), t = ty.print(cx) ); - document(w, &mut cx, field, Some(variant), HeadingOffset::H4); + document(w, cx, field, Some(variant), HeadingOffset::H4); } _ => unreachable!(), } @@ -1144,11 +1144,11 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum } } let def_id = it.def_id.expect_def_id(); - render_assoc_items(w, &mut cx, it, def_id, AssocItemRender::All); + render_assoc_items(w, cx, it, def_id, AssocItemRender::All); document_type_layout(w, cx, def_id); } -fn item_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Macro) { +fn item_macro(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::Macro) { wrap_into_docblock(w, |w| { highlight::render_with_highlighting( &t.source, @@ -1162,10 +1162,10 @@ fn item_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Mac None, ); }); - document(w, &mut cx, it, None, HeadingOffset::H2) + document(w, cx, it, None, HeadingOffset::H2) } -fn item_proc_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, m: &clean::ProcMacro) { +fn item_proc_macro(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, m: &clean::ProcMacro) { wrap_into_docblock(w, |w| { let name = it.name.as_ref().expect("proc-macros always have names"); match m.kind { @@ -1194,15 +1194,15 @@ fn item_proc_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, m: &clean } } }); - document(w, &mut cx, it, None, HeadingOffset::H2) + document(w, cx, it, None, HeadingOffset::H2) } -fn item_primitive(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) { - document(w, &mut cx, it, None, HeadingOffset::H2); - render_assoc_items(w, &mut cx, it, it.def_id.expect_def_id(), AssocItemRender::All) +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) } -fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean::Constant) { +fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &clean::Constant) { wrap_into_docblock(w, |w| { wrap_item(w, "const", |w| { render_attributes_in_code(w, it); @@ -1239,10 +1239,10 @@ fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean:: }); }); - document(w, &mut cx, it, None, HeadingOffset::H2) + 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); @@ -1250,7 +1250,7 @@ fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St }); }); - document(w, &mut cx, it, None, HeadingOffset::H2); + document(w, cx, it, None, HeadingOffset::H2); let mut fields = s .fields @@ -1286,16 +1286,16 @@ fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St name = field_name, ty = ty.print(cx) ); - document(w, &mut cx, field, Some(it), HeadingOffset::H3); + document(w, cx, field, Some(it), HeadingOffset::H3); } } } let def_id = it.def_id.expect_def_id(); - render_assoc_items(w, &mut cx, it, def_id, AssocItemRender::All); + render_assoc_items(w, cx, it, def_id, AssocItemRender::All); document_type_layout(w, cx, def_id); } -fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Static) { +fn item_static(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Static) { wrap_into_docblock(w, |w| { wrap_item(w, "static", |w| { render_attributes_in_code(w, it); @@ -1309,10 +1309,10 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St ); }); }); - document(w, &mut cx, it, None, HeadingOffset::H2) + 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"); @@ -1326,13 +1326,13 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) { }); }); - document(w, &mut cx, it, None, HeadingOffset::H2); + document(w, cx, it, None, HeadingOffset::H2); - render_assoc_items(w, &mut cx, it, it.def_id.expect_def_id(), AssocItemRender::All) + render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All) } -fn item_keyword(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) { - document(w, &mut cx, it, None, HeadingOffset::H2) +fn item_keyword(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) { + document(w, cx, it, None, HeadingOffset::H2) } /// Compare two strings treating multi-digit numbers as single units (i.e. natural sort order). @@ -1464,7 +1464,7 @@ fn render_implementor( }; render_impl( w, - &mut cx, + &mut cx.clone(), implementor, trait_, AssocItemLink::Anchor(None), From 1e7e431da713d1e69b5785932dd54fa3dc85ddc2 Mon Sep 17 00:00:00 2001 From: Milo <50248166+Milo123459@users.noreply.github.com> Date: Thu, 2 Dec 2021 21:48:44 +0000 Subject: [PATCH 3/8] fix ci --- src/librustdoc/html/render/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 4544384d98eed..2d0a071f6ed5f 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -1279,7 +1279,7 @@ fn notable_traits_decl(decl: &clean::FnDecl, cx: &mut 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.clone()); out.push_str(";"); } } From 426eabd68937523cb1fd5db8a6d1705afaa28df1 Mon Sep 17 00:00:00 2001 From: Milo <50248166+Milo123459@users.noreply.github.com> Date: Thu, 2 Dec 2021 21:53:05 +0000 Subject: [PATCH 4/8] fix style and remove broken comment --- src/librustdoc/html/render/context.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 22592da7a0b15..2165714a9ef27 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -43,7 +43,7 @@ use crate::try_err; /// It is intended that this context is a lightweight object which can be fairly /// easily cloned because it is cloned per work-job (about once per item in the /// rustdoc tree). -/// + #[derive(Clone)] crate struct Context<'tcx> { /// Current hierarchy of components leading down to what's currently being From a04a6ad70d19184e6cb871147a740c98491a3b00 Mon Sep 17 00:00:00 2001 From: Milo <50248166+Milo123459@users.noreply.github.com> Date: Thu, 2 Dec 2021 22:03:00 +0000 Subject: [PATCH 5/8] format --- src/librustdoc/html/render/mod.rs | 32 ++++++++++++++++++++----------- src/librustdoc/scrape_examples.rs | 6 ++++-- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 2d0a071f6ed5f..0be0ca0012d6b 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -657,13 +657,8 @@ fn short_item_info( if let Some(note) = note { let note = note.as_str(); let ids = &mut cx.id_map; - let html = MarkdownHtml( - ¬e, - ids, - error_codes, - cx.shared.edition(), - &cx.shared.playground, - ); + let html = + MarkdownHtml(¬e, ids, error_codes, cx.shared.edition(), &cx.shared.playground); message.push_str(&format!(": {}", html.into_string())); } extra_info.push(format!( @@ -705,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: &mut 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() @@ -1067,8 +1067,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.clone())))); + let id = cx.derive_id(small_url_encode(format!( + "deref-methods-{:#}", + type_.print(&cx.clone()) + ))); if let Some(def_id) = type_.def_id(cx.cache()) { cx.deref_id_map.insert(def_id, id.clone()); } @@ -1279,7 +1281,15 @@ fn notable_traits_decl(decl: &clean::FnDecl, cx: &mut 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, "", &mut cx.clone()); + assoc_type( + &mut out, + it, + &[], + Some(&tydef.type_), + src_link, + "", + &mut cx.clone(), + ); out.push_str(";"); } } diff --git a/src/librustdoc/scrape_examples.rs b/src/librustdoc/scrape_examples.rs index cc45425905a7a..3a3385614d56e 100644 --- a/src/librustdoc/scrape_examples.rs +++ b/src/librustdoc/scrape_examples.rs @@ -230,7 +230,8 @@ crate fn run( ) -> interface::Result<()> { let inner = move || -> Result<(), String> { // Generates source files for examples - let (mut 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: &mut 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 From ac3eb3fa160c7f5fff46be7378f89fbaa1e2659a Mon Sep 17 00:00:00 2001 From: Milo <50248166+Milo123459@users.noreply.github.com> Date: Thu, 2 Dec 2021 22:14:57 +0000 Subject: [PATCH 6/8] Update src/librustdoc/html/render/mod.rs Co-authored-by: Joshua Nelson --- src/librustdoc/html/render/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 0be0ca0012d6b..6fae579479896 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -501,7 +501,7 @@ fn render_markdown( Markdown { content: md_text, links: &links, - ids: ids, + ids, error_codes: cx.shared.codes, edition: cx.shared.edition(), playground: &cx.shared.playground, From 3d05feddd666e5f52943d81c01737771d6161909 Mon Sep 17 00:00:00 2001 From: Milo <50248166+Milo123459@users.noreply.github.com> Date: Sat, 4 Dec 2021 22:06:02 +0000 Subject: [PATCH 7/8] remove blankline --- src/librustdoc/html/render/context.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 2165714a9ef27..38ff49029878c 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -43,7 +43,6 @@ use crate::try_err; /// It is intended that this context is a lightweight object which can be fairly /// easily cloned because it is cloned per work-job (about once per item in the /// rustdoc tree). - #[derive(Clone)] crate struct Context<'tcx> { /// Current hierarchy of components leading down to what's currently being From 9be2e3c61071d926c94a8fe9d7f8f1a6bc280aef Mon Sep 17 00:00:00 2001 From: Milo <50248166+Milo123459@users.noreply.github.com> Date: Thu, 30 Dec 2021 03:25:01 +0000 Subject: [PATCH 8/8] start of work --- src/librustdoc/html/format.rs | 16 ++-- src/librustdoc/html/layout.rs | 20 ++--- src/librustdoc/html/render/context.rs | 37 ++++----- src/librustdoc/html/render/mod.rs | 93 ++++++++++------------ src/librustdoc/html/render/print_item.rs | 45 +++++------ src/librustdoc/html/render/write_shared.rs | 13 +-- src/librustdoc/html/sources.rs | 14 ++-- 7 files changed, 110 insertions(+), 128 deletions(-) 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 38ff49029878c..a383e7cad666d 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -43,7 +43,6 @@ use crate::try_err; /// It is intended that this context is a lightweight object which can be fairly /// easily cloned because it is cloned per work-job (about once per item in the /// rustdoc tree). -#[derive(Clone)] crate struct Context<'tcx> { /// Current hierarchy of components leading down to what's currently being /// rendered @@ -212,12 +211,11 @@ impl<'tcx> Context<'tcx> { } else { tyname.as_str() }; - let clone = self.clone(); let shared_clone = self.shared.clone(); let page = layout::Page { css_class: tyname_s, root_path: &self.root_path(), - static_root_path: clone.shared.static_root_path.as_deref(), + static_root_path: (&*shared_clone).static_root_path.as_deref(), title: &title, description: &desc, keywords: &keywords, @@ -225,17 +223,16 @@ impl<'tcx> Context<'tcx> { extra_scripts: &[], static_extra_scripts: &[], }; - let clone_1 = &mut self.clone(); - let clone_2 = &mut self.clone(); let shared_templates = &self.shared.clone().templates; if !self.render_redirect_pages { layout::render( - &self.clone().shared.templates, - &self.clone().shared.layout, + self, + &(&*self).shared, &page, - |buf: &mut _| print_sidebar(clone_1, it, buf), - |buf: &mut _| print_item(clone_2, 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()) { @@ -515,11 +512,11 @@ 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: id_map, + id_map, deref_id_map: FxHashMap::default(), shared: Rc::new(scx), include_sources, @@ -534,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 { @@ -586,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)?; @@ -608,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( @@ -617,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 { diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 6fae579479896..92bd4a5f35f9a 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -180,7 +180,7 @@ impl StylePath { } } -fn write_srclink(cx: &mut Context<'_>, item: &clean::Item, buf: &mut Buffer) { +fn write_srclink(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer) { if let Some(l) = cx.src_href(item) { write!(buf, "[src]", l) } @@ -470,7 +470,7 @@ fn settings(root_path: &str, suffix: &str, theme_names: Vec) -> Result, + cx: &Context<'_>, item: &clean::Item, parent: Option<&clean::Item>, heading_offset: HeadingOffset, @@ -489,7 +489,7 @@ fn document( /// Render md_text as markdown. fn render_markdown( w: &mut Buffer, - cx: &mut Context<'_>, + cx: &Context<'_>, md_text: &str, links: Vec, heading_offset: HeadingOffset, @@ -516,7 +516,7 @@ fn render_markdown( fn document_short( w: &mut Buffer, item: &clean::Item, - cx: &mut Context<'_>, + cx: &Context<'_>, link: AssocItemLink<'_>, parent: &clean::Item, show_def_docs: bool, @@ -545,7 +545,7 @@ fn document_short( fn document_full_collapsible( w: &mut Buffer, item: &clean::Item, - cx: &mut Context<'_>, + cx: &Context<'_>, heading_offset: HeadingOffset, ) { document_full_inner(w, item, cx, true, heading_offset); @@ -554,7 +554,7 @@ fn document_full_collapsible( fn document_full( w: &mut Buffer, item: &clean::Item, - cx: &mut Context<'_>, + cx: &Context<'_>, heading_offset: HeadingOffset, ) { document_full_inner(w, item, cx, false, heading_offset); @@ -563,7 +563,7 @@ fn document_full( fn document_full_inner( w: &mut Buffer, item: &clean::Item, - cx: &mut Context<'_>, + cx: &Context<'_>, is_collapsible: bool, heading_offset: HeadingOffset, ) { @@ -599,7 +599,7 @@ fn document_full_inner( /// * Required features (through the `doc_cfg` feature) fn document_item_info( w: &mut Buffer, - cx: &mut Context<'_>, + cx: &Context<'_>, item: &clean::Item, parent: Option<&clean::Item>, ) { @@ -628,7 +628,7 @@ fn portability(item: &clean::Item, parent: Option<&clean::Item>) -> Option, + cx: &Context<'_>, parent: Option<&clean::Item>, ) -> Vec { let mut extra_info = vec![]; @@ -738,7 +738,7 @@ fn render_impls( w.write_str(&rendered_impls.join("")); } -fn naive_assoc_href(it: &clean::Item, link: AssocItemLink<'_>, cx: &mut Context<'_>) -> String { +fn naive_assoc_href(it: &clean::Item, link: AssocItemLink<'_>, cx: &Context<'_>) -> String { use crate::formats::item_type::ItemType::*; let name = it.name.as_ref().unwrap(); @@ -764,13 +764,13 @@ fn assoc_const( _default: Option<&String>, link: AssocItemLink<'_>, extra: &str, - cx: &mut Context<'_>, + cx: &Context<'_>, ) { write!( w, "{}{}const {}: {}", extra, - it.visibility.print_with_space(it.def_id, &cx.clone()), + it.visibility.print_with_space(it.def_id, &*cx), naive_assoc_href(it, link, cx), it.name.as_ref().unwrap(), ty.print(cx) @@ -784,7 +784,7 @@ fn assoc_type( default: Option<&clean::Type>, link: AssocItemLink<'_>, extra: &str, - cx: &mut Context<'_>, + cx: &Context<'_>, ) { write!( w, @@ -859,7 +859,7 @@ fn render_assoc_item( item: &clean::Item, link: AssocItemLink<'_>, parent: ItemType, - cx: &mut Context<'_>, + cx: &Context<'_>, render_mode: RenderMode, ) { fn method( @@ -870,7 +870,7 @@ fn render_assoc_item( d: &clean::FnDecl, link: AssocItemLink<'_>, parent: ItemType, - cx: &mut Context<'_>, + cx: &Context<'_>, render_mode: RenderMode, ) { let name = meth.name.as_ref().unwrap(); @@ -943,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.clone()), - decl = d.full_print(header_len, indent, header.asyncness, &cx.clone()), + 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), ) @@ -1048,8 +1048,7 @@ fn render_assoc_items_inner( derefs: &mut FxHashSet, ) { info!("Documenting associated items of {:?}", containing_item.name); - let cx_clone = cx.clone(); - let cache = cx_clone.cache(); + let cache = &*cx.cache(); let v = match cache.impls.get(&it) { Some(v) => v, None => return, @@ -1067,10 +1066,8 @@ 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.clone()) - ))); + 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.insert(def_id, id.clone()); } @@ -1242,7 +1239,7 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool, tcx: TyCtxt<'_>) -> } } -fn notable_traits_decl(decl: &clean::FnDecl, cx: &mut Context<'_>) -> String { +fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String { let mut out = Buffer::html(); if let Some((did, ty)) = decl.output.as_return().and_then(|t| Some((t.def_id(cx.cache())?, t))) @@ -1288,7 +1285,7 @@ fn notable_traits_decl(decl: &clean::FnDecl, cx: &mut Context<'_>) -> String { Some(&tydef.type_), src_link, "", - &mut cx.clone(), + &mut (*cx), ); out.push_str(";"); } @@ -1332,8 +1329,7 @@ fn render_impl( aliases: &[String], rendering_params: ImplRenderingParameters, ) { - let cx_clone = cx.clone(); - let cache = cx_clone.cache(); + let cache = &*cx.cache(); let traits = &cache.traits; let trait_ = i.trait_did().map(|did| &traits[&did]); let mut close_tags = String::new(); @@ -1567,7 +1563,7 @@ fn render_impl( continue; } let did = i.trait_.as_ref().unwrap().def_id(); - let provided_methods = i.provided_trait_methods(cx.clone().tcx()); + let provided_methods = i.provided_trait_methods((&*cx).tcx()); let assoc_link = AssocItemLink::GotoSource(did.into(), &provided_methods); doc_impl_item( @@ -1632,13 +1628,13 @@ fn render_impl( } if let Some(ref dox) = cx.shared.maybe_collapsed_doc_value(&i.impl_item) { - let ids = &mut cx.clone().id_map; + let ids = &mut (*cx).id_map; write!( w, "
{}
", Markdown { content: &*dox, - links: &i.impl_item.links(cx), + links: &i.impl_item.links(&*cx), ids, error_codes: cx.shared.codes, edition: cx.shared.edition(), @@ -1662,7 +1658,7 @@ fn render_impl( // associated types. For example "1.0.0 (const: 1.39.0) [src]". fn render_rightside( w: &mut Buffer, - cx: &mut Context<'_>, + cx: &Context<'_>, item: &clean::Item, containing_item: &clean::Item, render_mode: RenderMode, @@ -1702,7 +1698,7 @@ pub(crate) fn render_impl_summary( // in documentation pages for trait with automatic implementations like "Send" and "Sync". aliases: &[String], ) { - let id = cx.clone().derive_id(match i.inner_impl().trait_ { + let id = cx.derive_id(match i.inner_impl().trait_ { Some(ref t) => { if is_on_foreign_type { get_id_for_impl_on_foreign_type(&i.inner_impl().for_, t, cx) @@ -1974,10 +1970,9 @@ fn small_url_encode(s: String) -> String { } } -fn sidebar_assoc_items(cx: &mut Context<'_>, out: &mut Buffer, it: &clean::Item) { +fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) { let did = it.def_id.expect_def_id(); - let cx_clone = cx.clone(); - let cache = cx_clone.cache(); + let cache = (&*cx).cache(); if let Some(v) = cache.impls.get(&did) { let mut used_links = FxHashSet::default(); @@ -2102,14 +2097,13 @@ fn sidebar_assoc_items(cx: &mut Context<'_>, out: &mut Buffer, it: &clean::Item) } fn sidebar_deref_methods( - cx: &mut Context<'_>, + cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &[Impl], derefs: &mut FxHashSet, ) { - let cx_clone = cx.clone(); - let c = cx_clone.cache(); + let c = (&*cx).cache(); debug!("found Deref: {:?}", impl_); if let Some((target, real_target)) = @@ -2190,7 +2184,7 @@ fn sidebar_deref_methods( } } -fn sidebar_struct(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item, s: &clean::Struct) { +fn sidebar_struct(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, s: &clean::Struct) { let mut sidebar = Buffer::new(); let fields = get_struct_fields_name(&s.fields); @@ -2222,12 +2216,12 @@ fn sidebar_struct(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item, s: & fn get_id_for_impl_on_foreign_type( for_: &clean::Type, trait_: &clean::Path, - cx: &mut Context<'_>, + cx: &Context<'_>, ) -> String { small_url_encode(format!("impl-{:#}-for-{:#}", trait_.print(cx), for_.print(cx))) } -fn extract_for_impl_name(item: &clean::Item, cx: &mut Context<'_>) -> Option<(String, String)> { +fn extract_for_impl_name(item: &clean::Item, cx: &Context<'_>) -> Option<(String, String)> { match *item.kind { clean::ItemKind::ImplItem(ref i) => { i.trait_.as_ref().map(|trait_| { @@ -2311,15 +2305,14 @@ fn sidebar_trait(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &c |out, sym| write!(out, "{0}", sym), "", ); - let cx_clone = cx.clone(); - let cache = cx_clone.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() { @@ -2349,7 +2342,7 @@ fn sidebar_trait(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &c buf.push_str("") } -fn sidebar_primitive(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item) { +fn sidebar_primitive(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item) { let mut sidebar = Buffer::new(); sidebar_assoc_items(cx, &mut sidebar, it); @@ -2358,7 +2351,7 @@ fn sidebar_primitive(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item) { } } -fn sidebar_typedef(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item) { +fn sidebar_typedef(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item) { let mut sidebar = Buffer::new(); sidebar_assoc_items(cx, &mut sidebar, it); @@ -2379,7 +2372,7 @@ fn get_struct_fields_name(fields: &[clean::Item]) -> Vec { fields } -fn sidebar_union(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item, u: &clean::Union) { +fn sidebar_union(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, u: &clean::Union) { let mut sidebar = Buffer::new(); let fields = get_struct_fields_name(&u.fields); @@ -2403,7 +2396,7 @@ fn sidebar_union(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item, u: &c } } -fn sidebar_enum(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item, e: &clean::Enum) { +fn sidebar_enum(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, e: &clean::Enum) { let mut sidebar = Buffer::new(); let mut variants = e @@ -2510,7 +2503,7 @@ fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) { } } -fn sidebar_foreign_type(cx: &mut Context<'_>, buf: &mut Buffer, it: &clean::Item) { +fn sidebar_foreign_type(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item) { let mut sidebar = Buffer::new(); sidebar_assoc_items(cx, &mut sidebar, it); @@ -2579,7 +2572,7 @@ const MAX_FULL_EXAMPLES: usize = 5; const NUM_VISIBLE_LINES: usize = 10; /// Generates the HTML for example call locations generated via the --scrape-examples flag. -fn render_call_locations(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item) { +fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) { let tcx = cx.tcx(); let def_id = item.def_id.expect_def_id(); let key = tcx.def_path_hash(def_id); diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 37244443918c6..711d3eb916390 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -193,7 +193,7 @@ fn toggle_close(w: &mut Buffer) { w.write_str("
"); } -fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items: &[clean::Item]) { +fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) { document(w, cx, item, None, HeadingOffset::H2); let mut indices = (0..items.len()).filter(|i| !items[*i].is_stripped()).collect::>(); @@ -464,7 +464,7 @@ fn extra_info_tags(item: &clean::Item, parent: &clean::Item, tcx: TyCtxt<'_>) -> tags } -fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &clean::Function) { +fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) { let vis = it.visibility.print_with_space(it.def_id, cx).to_string(); let constness = print_constness_with_space(&f.header.constness, it.const_stability(cx.tcx())); let asyncness = f.header.asyncness.print_with_space(); @@ -496,9 +496,9 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle unsafety = unsafety, abi = abi, name = name, - generics = f.generics.print(&cx.clone()), - where_clause = print_where_clause(&f.generics, &cx.clone(), 0, true), - decl = f.decl.full_print(header_len, 0, f.header.asyncness, &cx.clone()), + 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), ); }); @@ -666,7 +666,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean: ) } - fn trait_item(w: &mut Buffer, cx: &mut Context<'_>, m: &clean::Item, t: &clean::Item) { + fn trait_item(w: &mut Buffer, cx: &Context<'_>, m: &clean::Item, t: &clean::Item) { let name = m.name.as_ref().unwrap(); info!("Documenting {} on {:?}", name, t.name); let item_type = m.type_(); @@ -680,7 +680,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean: write!(w, "
", id); write!(w, "
"); render_stability_since(w, m, t, cx.tcx()); - write_srclink(&mut cx.clone(), 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: &mut Context<'_>, it: &clean::Item, t: &clean: // 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 cx_clone = cx.clone(); - let cache = cx_clone.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. @@ -905,7 +904,7 @@ fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: & 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, ""); @@ -920,13 +919,13 @@ fn item_opaque_ty(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean: }); }); - document(w, &mut cx.clone(), it, None, HeadingOffset::H2); + document(w, cx, it, None, HeadingOffset::H2); // Render any items associated directly to this alias, as otherwise they // won't be visible anywhere in the docs. It would be nice to also show // associated items from the aliased type (see discussion in #32077), but // we need #14072 to make sense of the generics. - render_assoc_items(w, &mut cx.clone(), it, it.def_id.expect_def_id(), AssocItemRender::All) + render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All) } fn item_typedef( @@ -938,7 +937,7 @@ fn item_typedef( ) { fn write_content( w: &mut Buffer, - cx: &mut Context<'_>, + cx: &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, &mut cx.clone(), it, t, is_associated); + write_content(w, &mut (*cx), it, t, is_associated); } else { wrap_into_docblock(w, |w| { - write_content(w, &mut cx.clone(), it, t, is_associated); + write_content(w, &mut (*cx), it, t, is_associated); }); } - document(w, &mut cx.clone(), 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 @@ -986,7 +985,7 @@ fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean: }); }); - document(w, &mut cx.clone(), 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: &mut Context<'_>, it: &clean::Item, s: &clean: } } let def_id = it.def_id.expect_def_id(); - render_assoc_items(w, &mut cx.clone(), it, def_id, AssocItemRender::All); + render_assoc_items(w, &mut *cx, it, def_id, AssocItemRender::All); document_type_layout(w, cx, def_id); } @@ -1182,7 +1181,7 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean:: document_type_layout(w, cx, def_id); } -fn item_macro(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::Macro) { +fn item_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Macro) { wrap_into_docblock(w, |w| { highlight::render_with_highlighting( &t.source, @@ -1199,7 +1198,7 @@ fn item_macro(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean: document(w, cx, it, None, HeadingOffset::H2) } -fn item_proc_macro(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, m: &clean::ProcMacro) { +fn item_proc_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, m: &clean::ProcMacro) { wrap_into_docblock(w, |w| { let name = it.name.as_ref().expect("proc-macros always have names"); match m.kind { @@ -1236,7 +1235,7 @@ fn item_primitive(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) { render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All) } -fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &clean::Constant) { +fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean::Constant) { wrap_into_docblock(w, |w| { wrap_item(w, "const", |w| { render_attributes_in_code(w, it); @@ -1329,7 +1328,7 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean document_type_layout(w, cx, def_id); } -fn item_static(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Static) { +fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Static) { wrap_into_docblock(w, |w| { wrap_item(w, "static", |w| { render_attributes_in_code(w, it); @@ -1365,7 +1364,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) { render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All) } -fn item_keyword(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) { +fn item_keyword(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) { document(w, cx, it, None, HeadingOffset::H2) } 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);