Skip to content

Commit 5fa1653

Browse files
Move SharedContext out of Rc
1 parent 2e242f8 commit 5fa1653

File tree

6 files changed

+171
-162
lines changed

6 files changed

+171
-162
lines changed

src/librustdoc/html/render/context.rs

+20-25
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::cell::RefCell;
22
use std::collections::BTreeMap;
33
use std::io;
44
use std::path::{Path, PathBuf};
5-
use std::rc::Rc;
65
use std::sync::mpsc::{Receiver, channel};
76

87
use rinja::Template;
@@ -51,17 +50,17 @@ pub(crate) struct Context<'tcx> {
5150
pub(crate) dst: PathBuf,
5251
/// Tracks section IDs for `Deref` targets so they match in both the main
5352
/// body and the sidebar.
54-
pub(super) deref_id_map: DefIdMap<String>,
53+
pub(super) deref_id_map: RefCell<DefIdMap<String>>,
5554
/// The map used to ensure all generated 'id=' attributes are unique.
56-
pub(super) id_map: IdMap,
55+
pub(super) id_map: RefCell<IdMap>,
5756
/// Shared mutable state.
5857
///
5958
/// Issue for improving the situation: [#82381][]
6059
///
6160
/// [#82381]: https://github.com/rust-lang/rust/issues/82381
62-
pub(crate) shared: Rc<SharedContext<'tcx>>,
61+
pub(crate) shared: SharedContext<'tcx>,
6362
/// Collection of all types with notable traits referenced in the current module.
64-
pub(crate) types_with_notable_traits: FxIndexSet<clean::Type>,
63+
pub(crate) types_with_notable_traits: RefCell<FxIndexSet<clean::Type>>,
6564
/// Contains information that needs to be saved and reset after rendering an item which is
6665
/// not a module.
6766
pub(crate) info: ContextInfo,
@@ -170,8 +169,8 @@ impl<'tcx> Context<'tcx> {
170169
self.shared.tcx.sess
171170
}
172171

173-
pub(super) fn derive_id<S: AsRef<str> + ToString>(&mut self, id: S) -> String {
174-
self.id_map.derive(id)
172+
pub(super) fn derive_id<S: AsRef<str> + ToString>(&self, id: S) -> String {
173+
self.id_map.borrow_mut().derive(id)
175174
}
176175

177176
/// String representation of how to get back to the root path of the 'doc/'
@@ -230,24 +229,23 @@ impl<'tcx> Context<'tcx> {
230229
};
231230

232231
if !render_redirect_pages {
233-
let clone_shared = Rc::clone(&self.shared);
232+
let mut page_buffer = Buffer::html();
233+
print_item(self, it, &mut page_buffer);
234234
let page = layout::Page {
235235
css_class: tyname_s,
236236
root_path: &self.root_path(),
237-
static_root_path: clone_shared.static_root_path.as_deref(),
237+
static_root_path: self.shared.static_root_path.as_deref(),
238238
title: &title,
239239
description: &desc,
240-
resource_suffix: &clone_shared.resource_suffix,
240+
resource_suffix: &self.shared.resource_suffix,
241241
rust_logo: has_doc_flag(self.tcx(), LOCAL_CRATE.as_def_id(), sym::rust_logo),
242242
};
243-
let mut page_buffer = Buffer::html();
244-
print_item(self, it, &mut page_buffer);
245243
layout::render(
246-
&clone_shared.layout,
244+
&self.shared.layout,
247245
&page,
248246
|buf: &mut _| print_sidebar(self, it, buf),
249247
move |buf: &mut Buffer| buf.push_buffer(page_buffer),
250-
&clone_shared.style_files,
248+
&self.shared.style_files,
251249
)
252250
} else {
253251
if let Some(&(ref names, ty)) = self.cache().paths.get(&it.item_id.expect_def_id()) {
@@ -572,10 +570,10 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
572570
let mut cx = Context {
573571
current: Vec::new(),
574572
dst,
575-
id_map,
573+
id_map: RefCell::new(id_map),
576574
deref_id_map: Default::default(),
577-
shared: Rc::new(scx),
578-
types_with_notable_traits: FxIndexSet::default(),
575+
shared: scx,
576+
types_with_notable_traits: RefCell::new(FxIndexSet::default()),
579577
info: ContextInfo::new(include_sources),
580578
};
581579

@@ -591,9 +589,9 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
591589
}
592590

593591
fn make_child_renderer(&mut self) -> Self::InfoType {
594-
self.deref_id_map.clear();
595-
self.id_map.clear();
596-
self.types_with_notable_traits.clear();
592+
self.deref_id_map.borrow_mut().clear();
593+
self.id_map.borrow_mut().clear();
594+
self.types_with_notable_traits.borrow_mut().clear();
597595
self.info
598596
}
599597

@@ -612,7 +610,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
612610
if !root_path.ends_with('/') {
613611
root_path.push('/');
614612
}
615-
let shared = Rc::clone(&self.shared);
613+
let shared = &self.shared;
616614
let mut page = layout::Page {
617615
title: "List of all items in this crate",
618616
css_class: "mod sys",
@@ -759,11 +757,8 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
759757
shared.fs.write(redirect_map_path, paths)?;
760758
}
761759

762-
// No need for it anymore.
763-
drop(shared);
764-
765760
// Flush pending errors.
766-
Rc::get_mut(&mut self.shared).unwrap().fs.close();
761+
self.shared.fs.close();
767762
let nb_errors = self.shared.errors.iter().map(|err| self.tcx().dcx().err(err)).count();
768763
if nb_errors > 0 {
769764
Err(Error::new(io::Error::new(io::ErrorKind::Other, "I/O error"), ""))

0 commit comments

Comments
 (0)