Skip to content

Commit 331465a

Browse files
committed
rustdoc: Pass DocContext to Cache::populate
This will allow removing `Crate.externs`.
1 parent 548c108 commit 331465a

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

src/librustdoc/core.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -508,14 +508,12 @@ crate fn run_global_ctxt(
508508
rustc_errors::FatalError.raise();
509509
}
510510

511-
let render_options = ctxt.render_options;
512-
let mut cache = ctxt.cache;
513-
krate = tcx.sess.time("create_format_cache", || cache.populate(krate, tcx, &render_options));
511+
krate = tcx.sess.time("create_format_cache", || Cache::populate(&mut ctxt, krate));
514512

515513
// The main crate doc comments are always collapsed.
516514
krate.collapsed = true;
517515

518-
(krate, render_options, cache)
516+
(krate, ctxt.render_options, ctxt.cache)
519517
}
520518

521519
/// Due to <https://github.com/rust-lang/rust/pull/73566>,

src/librustdoc/formats/cache.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_span::symbol::sym;
88

99
use crate::clean::{self, ItemId, PrimitiveType};
1010
use crate::config::RenderOptions;
11+
use crate::core::DocContext;
1112
use crate::fold::DocFolder;
1213
use crate::formats::item_type::ItemType;
1314
use crate::formats::Impl;
@@ -136,15 +137,13 @@ impl Cache {
136137

137138
/// Populates the `Cache` with more data. The returned `Crate` will be missing some data that was
138139
/// in `krate` due to the data being moved into the `Cache`.
139-
crate fn populate(
140-
&mut self,
141-
mut krate: clean::Crate,
142-
tcx: TyCtxt<'_>,
143-
render_options: &RenderOptions,
144-
) -> clean::Crate {
140+
crate fn populate(cx: &mut DocContext<'_>, mut krate: clean::Crate) -> clean::Crate {
141+
let tcx = cx.tcx;
142+
let render_options = &cx.render_options;
143+
145144
// Crawl the crate to build various caches used for the output
146-
debug!(?self.crate_version);
147-
self.traits = krate.external_traits.take();
145+
debug!(?cx.cache.crate_version);
146+
cx.cache.traits = krate.external_traits.take();
148147
let RenderOptions { extern_html_root_takes_precedence, output: dst, .. } = render_options;
149148

150149
// Cache where all our extern crates are located
@@ -154,28 +153,28 @@ impl Cache {
154153
let extern_url =
155154
render_options.extern_html_root_urls.get(&*name.as_str()).map(|u| &**u);
156155
let location = e.location(extern_url, *extern_html_root_takes_precedence, dst, tcx);
157-
self.extern_locations.insert(e.crate_num, location);
158-
self.external_paths.insert(e.def_id(), (vec![name.to_string()], ItemType::Module));
156+
cx.cache.extern_locations.insert(e.crate_num, location);
157+
cx.cache.external_paths.insert(e.def_id(), (vec![name.to_string()], ItemType::Module));
159158
}
160159

161160
// FIXME: avoid this clone (requires implementing Default manually)
162-
self.primitive_locations = PrimitiveType::primitive_locations(tcx).clone();
163-
for (prim, &def_id) in &self.primitive_locations {
161+
cx.cache.primitive_locations = PrimitiveType::primitive_locations(tcx).clone();
162+
for (prim, &def_id) in &cx.cache.primitive_locations {
164163
let crate_name = tcx.crate_name(def_id.krate);
165164
// Recall that we only allow primitive modules to be at the root-level of the crate.
166165
// If that restriction is ever lifted, this will have to include the relative paths instead.
167-
self.external_paths.insert(
166+
cx.cache.external_paths.insert(
168167
def_id,
169168
(vec![crate_name.to_string(), prim.as_sym().to_string()], ItemType::Primitive),
170169
);
171170
}
172171

173-
krate = CacheBuilder { tcx, cache: self }.fold_crate(krate);
172+
krate = CacheBuilder { tcx, cache: &mut cx.cache }.fold_crate(krate);
174173

175-
for (trait_did, dids, impl_) in self.orphan_trait_impls.drain(..) {
176-
if self.traits.contains_key(&trait_did) {
174+
for (trait_did, dids, impl_) in cx.cache.orphan_trait_impls.drain(..) {
175+
if cx.cache.traits.contains_key(&trait_did) {
177176
for did in dids {
178-
self.impls.entry(did).or_default().push(impl_.clone());
177+
cx.cache.impls.entry(did).or_default().push(impl_.clone());
179178
}
180179
}
181180
}

0 commit comments

Comments
 (0)