Skip to content

Commit 5b9d468

Browse files
committed
I forget why I made this change
1 parent 4c813a7 commit 5b9d468

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/librustdoc/html/render/context.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::cell::RefCell;
1+
use std::cell::{RefCell, RefMut};
22
use std::collections::BTreeMap;
33
use std::error::Error as StdError;
44
use std::io;
@@ -55,7 +55,10 @@ crate struct Context<'tcx> {
5555
/// publicly reused items to redirect to the right location.
5656
pub(super) render_redirect_pages: bool,
5757
/// The map used to ensure all generated 'id=' attributes are unique.
58-
pub(super) id_map: RefCell<IdMap>,
58+
///
59+
/// INVARIANT: there is always at least one map in this list (because `mod_item_out` is never
60+
/// called without first calling `mod_item_in`).
61+
pub(super) id_maps: Vec<RefCell<IdMap>>,
5962
/// Shared mutable state.
6063
///
6164
/// Issue for improving the situation: [#82381][]
@@ -70,7 +73,7 @@ crate struct Context<'tcx> {
7073

7174
// `Context` is cloned a lot, so we don't want the size to grow unexpectedly.
7275
#[cfg(target_arch = "x86_64")]
73-
rustc_data_structures::static_assert_size!(Context<'_>, 104);
76+
rustc_data_structures::static_assert_size!(Context<'_>, 88);
7477

7578
/// Shared mutable state used in [`Context`] and elsewhere.
7679
crate struct SharedContext<'tcx> {
@@ -161,9 +164,12 @@ impl<'tcx> Context<'tcx> {
161164
&self.shared.tcx.sess
162165
}
163166

167+
pub(super) fn id_map(&self) -> RefMut<'_, IdMap> {
168+
self.id_maps.last().unwrap().borrow_mut()
169+
}
170+
164171
pub(super) fn derive_id(&self, id: String) -> String {
165-
let mut map = self.id_map.borrow_mut();
166-
map.derive(id)
172+
self.id_map().derive(id)
167173
}
168174

169175
/// String representation of how to get back to the root path of the 'doc/'
@@ -502,7 +508,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
502508
current: Vec::new(),
503509
dst,
504510
render_redirect_pages: false,
505-
id_map: RefCell::new(id_map),
511+
id_maps: vec![RefCell::new(id_map)],
506512
shared: Rc::new(scx),
507513
include_sources,
508514
};
@@ -608,6 +614,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
608614
}
609615

610616
fn mod_item_in(&mut self, item: &clean::Item) -> Result<(), Error> {
617+
self.id_maps.push(RefCell::new(IdMap::new()));
611618
// Stripped modules survive the rustdoc passes (i.e., `strip-private`)
612619
// if they contain impls for public types. These modules can also
613620
// contain items such as publicly re-exported structures.
@@ -653,6 +660,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
653660
// Go back to where we were at
654661
self.dst.pop();
655662
self.current.pop();
663+
self.id_maps.pop();
656664
Ok(())
657665
}
658666

src/librustdoc/html/render/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -485,14 +485,13 @@ fn document(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, parent: Option
485485

486486
/// Render md_text as markdown.
487487
fn render_markdown(w: &mut Buffer, cx: &Context<'_>, md_text: &str, links: Vec<RenderedLink>) {
488-
let mut ids = cx.id_map.borrow_mut();
489488
write!(
490489
w,
491490
"<div class=\"docblock\">{}</div>",
492491
Markdown(
493492
md_text,
494493
&links,
495-
&mut ids,
494+
&mut cx.id_map(),
496495
cx.shared.codes,
497496
cx.shared.edition(),
498497
&cx.shared.playground
@@ -622,7 +621,7 @@ fn short_item_info(
622621

623622
if let Some(note) = note {
624623
let note = note.as_str();
625-
let mut ids = cx.id_map.borrow_mut();
624+
let mut ids = cx.id_map();
626625
let html = MarkdownHtml(
627626
&note,
628627
&mut ids,
@@ -661,13 +660,12 @@ fn short_item_info(
661660
message.push_str(&format!(" ({})", feature));
662661

663662
if let Some(unstable_reason) = reason {
664-
let mut ids = cx.id_map.borrow_mut();
665663
message = format!(
666664
"<details><summary>{}</summary>{}</details>",
667665
message,
668666
MarkdownHtml(
669667
&unstable_reason.as_str(),
670-
&mut ids,
668+
&mut cx.id_map(),
671669
error_codes,
672670
cx.shared.edition(),
673671
&cx.shared.playground,
@@ -1537,14 +1535,13 @@ fn render_impl(
15371535
}
15381536

15391537
if let Some(ref dox) = cx.shared.maybe_collapsed_doc_value(&i.impl_item) {
1540-
let mut ids = cx.id_map.borrow_mut();
15411538
write!(
15421539
w,
15431540
"<div class=\"docblock\">{}</div>",
15441541
Markdown(
15451542
&*dox,
15461543
&i.impl_item.links(cx),
1547-
&mut ids,
1544+
&mut cx.id_map(),
15481545
cx.shared.codes,
15491546
cx.shared.edition(),
15501547
&cx.shared.playground

0 commit comments

Comments
 (0)