Skip to content

Commit 4c51a66

Browse files
committed
Don't share id_map and deref_id_map
All the tests passed, so it doesn't seem they need to be shared. Plus they should be item/page-specific. I'm not sure why they were shared before. I think the reason `id_map` worked as a shared value before is that it is cleared before rendering each item (in `render_item`). And then I'm guessing `deref_id_map` worked because it's a hashmap keyed by `DefId`, so there was no overlap (though I'm guessing we could have had issues in the future). Note that `id_map` currently still has to be cleared because otherwise child items would inherit the `id_map` of their parent. I'm hoping to figure out a way to stop cloning `Context`, but until then we have to reset `id_map`.
1 parent 3bc879e commit 4c51a66

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

src/librustdoc/html/render/context.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::rc::Rc;
66
use std::sync::mpsc::channel;
77

88
use rustc_data_structures::fx::FxHashMap;
9-
use rustc_hir::def_id::LOCAL_CRATE;
9+
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1010
use rustc_middle::ty::TyCtxt;
1111
use rustc_session::Session;
1212
use rustc_span::edition::Edition;
@@ -30,7 +30,7 @@ use crate::formats::item_type::ItemType;
3030
use crate::formats::FormatRenderer;
3131
use crate::html::escape::Escape;
3232
use crate::html::format::Buffer;
33-
use crate::html::markdown::{self, plain_text_summary, ErrorCodes};
33+
use crate::html::markdown::{self, plain_text_summary, ErrorCodes, IdMap};
3434
use crate::html::{layout, sources};
3535

3636
/// Major driving force in all rustdoc rendering. This contains information
@@ -52,6 +52,11 @@ crate struct Context<'tcx> {
5252
/// real location of an item. This is used to allow external links to
5353
/// publicly reused items to redirect to the right location.
5454
pub(super) render_redirect_pages: bool,
55+
/// The map used to ensure all generated 'id=' attributes are unique.
56+
pub(super) id_map: RefCell<IdMap>,
57+
/// Tracks section IDs for `Deref` targets so they match in both the main
58+
/// body and the sidebar.
59+
pub(super) deref_id_map: RefCell<FxHashMap<DefId, String>>,
5560
/// Shared mutable state.
5661
///
5762
/// Issue for improving the situation: [#82381][]
@@ -72,7 +77,7 @@ crate struct Context<'tcx> {
7277

7378
// `Context` is cloned a lot, so we don't want the size to grow unexpectedly.
7479
#[cfg(target_arch = "x86_64")]
75-
rustc_data_structures::static_assert_size!(Context<'_>, 72);
80+
rustc_data_structures::static_assert_size!(Context<'_>, 152);
7681

7782
impl<'tcx> Context<'tcx> {
7883
pub(super) fn path(&self, filename: &str) -> PathBuf {
@@ -95,7 +100,7 @@ impl<'tcx> Context<'tcx> {
95100
}
96101

97102
pub(super) fn derive_id(&self, id: String) -> String {
98-
let mut map = self.shared.id_map.borrow_mut();
103+
let mut map = self.id_map.borrow_mut();
99104
map.derive(id)
100105
}
101106

@@ -153,8 +158,8 @@ impl<'tcx> Context<'tcx> {
153158
};
154159

155160
{
156-
self.shared.id_map.borrow_mut().reset();
157-
self.shared.id_map.borrow_mut().populate(&INITIAL_IDS);
161+
self.id_map.borrow_mut().reset();
162+
self.id_map.borrow_mut().populate(&INITIAL_IDS);
158163
}
159164

160165
if !self.render_redirect_pages {
@@ -387,8 +392,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
387392
edition,
388393
codes: ErrorCodes::from(unstable_features.is_nightly_build()),
389394
playground,
390-
id_map: RefCell::new(id_map),
391-
deref_id_map: RefCell::new(FxHashMap::default()),
392395
all: RefCell::new(AllTypes::new()),
393396
errors: receiver,
394397
redirections: if generate_redirect_map { Some(Default::default()) } else { None },
@@ -418,6 +421,8 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
418421
current: Vec::new(),
419422
dst,
420423
render_redirect_pages: false,
424+
id_map: RefCell::new(id_map),
425+
deref_id_map: RefCell::new(FxHashMap::default()),
421426
shared: Rc::new(scx),
422427
cache: Rc::new(cache),
423428
};

src/librustdoc/html/render/mod.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use crate::html::format::{
7070
PrintWithSpace, WhereClause,
7171
};
7272
use crate::html::layout;
73-
use crate::html::markdown::{self, ErrorCodes, IdMap, Markdown, MarkdownHtml, MarkdownSummaryLine};
73+
use crate::html::markdown::{self, ErrorCodes, Markdown, MarkdownHtml, MarkdownSummaryLine};
7474

7575
/// A pair of name and its optional document.
7676
crate type NameDoc = (String, Option<String>);
@@ -121,11 +121,6 @@ crate struct SharedContext<'tcx> {
121121
crate edition: Edition,
122122
codes: ErrorCodes,
123123
playground: Option<markdown::Playground>,
124-
/// The map used to ensure all generated 'id=' attributes are unique.
125-
id_map: RefCell<IdMap>,
126-
/// Tracks section IDs for `Deref` targets so they match in both the main
127-
/// body and the sidebar.
128-
deref_id_map: RefCell<FxHashMap<DefId, String>>,
129124
all: RefCell<AllTypes>,
130125
/// Storage for the errors produced while generating documentation so they
131126
/// can be printed together at the end.
@@ -650,7 +645,7 @@ fn render_markdown(
650645
prefix: &str,
651646
is_hidden: bool,
652647
) {
653-
let mut ids = cx.shared.id_map.borrow_mut();
648+
let mut ids = cx.id_map.borrow_mut();
654649
write!(
655650
w,
656651
"<div class=\"docblock{}\">{}{}</div>",
@@ -810,7 +805,7 @@ fn short_item_info(
810805

811806
if let Some(note) = note {
812807
let note = note.as_str();
813-
let mut ids = cx.shared.id_map.borrow_mut();
808+
let mut ids = cx.id_map.borrow_mut();
814809
let html = MarkdownHtml(
815810
&note,
816811
&mut ids,
@@ -849,7 +844,7 @@ fn short_item_info(
849844
message.push_str(&format!(" ({})", feature));
850845

851846
if let Some(unstable_reason) = reason {
852-
let mut ids = cx.shared.id_map.borrow_mut();
847+
let mut ids = cx.id_map.borrow_mut();
853848
message = format!(
854849
"<details><summary>{}</summary>{}</details>",
855850
message,
@@ -1189,8 +1184,7 @@ fn render_assoc_items(
11891184
type_.print(cx.cache())
11901185
)));
11911186
debug!("Adding {} to deref id map", type_.print(cx.cache()));
1192-
cx.shared
1193-
.deref_id_map
1187+
cx.deref_id_map
11941188
.borrow_mut()
11951189
.insert(type_.def_id_full(cx.cache()).unwrap(), id.clone());
11961190
write!(
@@ -1497,7 +1491,7 @@ fn render_impl(
14971491
}
14981492

14991493
if let Some(ref dox) = cx.shared.maybe_collapsed_doc_value(&i.impl_item) {
1500-
let mut ids = cx.shared.id_map.borrow_mut();
1494+
let mut ids = cx.id_map.borrow_mut();
15011495
write!(
15021496
w,
15031497
"<div class=\"docblock\">{}</div>",
@@ -2046,7 +2040,7 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &V
20462040
.flat_map(|i| get_methods(i.inner_impl(), true, &mut used_links, deref_mut, c))
20472041
.collect::<Vec<_>>();
20482042
if !ret.is_empty() {
2049-
let deref_id_map = cx.shared.deref_id_map.borrow();
2043+
let deref_id_map = cx.deref_id_map.borrow();
20502044
let id = deref_id_map
20512045
.get(&real_target.def_id_full(cx.cache()).unwrap())
20522046
.expect("Deref section without derived id");

0 commit comments

Comments
 (0)