Skip to content

Commit 991ef9a

Browse files
committed
Remove the dummy cache in DocContext
The same information is available everywhere; the only reason the dummy cache was needed is because it waas previously stored in three different places. This consolidates the info a bit so the cache in `DocContext` is used throughout. As a bonus, it means `renderinfo` is used much much less. - Make `cache` a `RefCell` so it can be modified with a shared reference - Return a `Cache` from `run_global_ctxt`, not `RenderInfo` - Remove the unused `render_info` from `run_renderer`
1 parent 178108b commit 991ef9a

16 files changed

+84
-93
lines changed

Diff for: src/librustdoc/clean/blanket_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
2626
debug!("get_blanket_impls({:?})", ty);
2727
let mut impls = Vec::new();
2828
for &trait_def_id in self.cx.tcx.all_traits(LOCAL_CRATE).iter() {
29-
if !self.cx.renderinfo.borrow().access_levels.is_public(trait_def_id)
29+
if !self.cx.cache.borrow().access_levels.is_public(trait_def_id)
3030
|| self.cx.generated_synthetics.borrow_mut().get(&(ty, trait_def_id)).is_some()
3131
{
3232
continue;

Diff for: src/librustdoc/clean/inline.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ crate fn try_inline(
122122
let target_attrs = load_attrs(cx, did);
123123
let attrs = box merge_attrs(cx, Some(parent_module), target_attrs, attrs_clone);
124124

125-
cx.renderinfo.borrow_mut().inlined.insert(did);
125+
cx.inlined.borrow_mut().insert(did);
126126
let what_rustc_thinks = clean::Item::from_def_id_and_parts(did, Some(name), kind, cx);
127127
ret.push(clean::Item { attrs, ..what_rustc_thinks });
128128
Some(ret)
@@ -181,9 +181,10 @@ crate fn record_extern_fqn(cx: &DocContext<'_>, did: DefId, kind: clean::TypeKin
181181
};
182182

183183
if did.is_local() {
184-
cx.renderinfo.borrow_mut().exact_paths.insert(did, fqn);
184+
cx.cache.borrow_mut().exact_paths.insert(did, fqn);
185185
} else {
186-
cx.renderinfo.borrow_mut().external_paths.insert(did, (fqn, kind));
186+
use crate::formats::item_type::ItemType;
187+
cx.cache.borrow_mut().external_paths.insert(did, (fqn, ItemType::from(kind)));
187188
}
188189
}
189190

@@ -317,7 +318,7 @@ crate fn build_impl(
317318
attrs: Option<Attrs<'_>>,
318319
ret: &mut Vec<clean::Item>,
319320
) {
320-
if !cx.renderinfo.borrow_mut().inlined.insert(did) {
321+
if !cx.inlined.borrow_mut().insert(did) {
321322
return;
322323
}
323324

@@ -329,7 +330,7 @@ crate fn build_impl(
329330
if !did.is_local() {
330331
if let Some(traitref) = associated_trait {
331332
let did = traitref.def_id;
332-
if !cx.renderinfo.borrow().access_levels.is_public(did) {
333+
if !cx.cache.borrow().access_levels.is_public(did) {
333334
return;
334335
}
335336

@@ -361,7 +362,7 @@ crate fn build_impl(
361362
// reachable in rustdoc generated documentation
362363
if !did.is_local() {
363364
if let Some(did) = for_.def_id() {
364-
if !cx.renderinfo.borrow().access_levels.is_public(did) {
365+
if !cx.cache.borrow().access_levels.is_public(did) {
365366
return;
366367
}
367368

Diff for: src/librustdoc/clean/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &DocContext<'_>) -> Type {
13111311
// Substitute private type aliases
13121312
if let Some(def_id) = def_id.as_local() {
13131313
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
1314-
if !cx.renderinfo.borrow().access_levels.is_exported(def_id.to_def_id()) {
1314+
if !cx.cache.borrow().access_levels.is_exported(def_id.to_def_id()) {
13151315
alias = Some(&cx.tcx.hir().expect_item(hir_id).kind);
13161316
}
13171317
}
@@ -2304,14 +2304,14 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
23042304
if matchers.len() <= 1 {
23052305
format!(
23062306
"{}macro {}{} {{\n ...\n}}",
2307-
vis.print_with_space(cx.tcx, def_id, &cx.cache),
2307+
vis.print_with_space(cx.tcx, def_id, &cx.cache.borrow()),
23082308
name,
23092309
matchers.iter().map(|span| span.to_src(cx)).collect::<String>(),
23102310
)
23112311
} else {
23122312
format!(
23132313
"{}macro {} {{\n{}}}",
2314-
vis.print_with_space(cx.tcx, def_id, &cx.cache),
2314+
vis.print_with_space(cx.tcx, def_id, &cx.cache.borrow()),
23152315
name,
23162316
matchers
23172317
.iter()

Diff for: src/librustdoc/clean/utils.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ crate fn krate(mut cx: &mut DocContext<'_>) -> Crate {
2323
let krate = cx.tcx.hir().krate();
2424
let module = crate::visit_ast::RustdocVisitor::new(&mut cx).visit(krate);
2525

26-
let mut r = cx.renderinfo.get_mut();
27-
r.deref_trait_did = cx.tcx.lang_items().deref_trait();
28-
r.deref_mut_trait_did = cx.tcx.lang_items().deref_mut_trait();
29-
r.owned_box_did = cx.tcx.lang_items().owned_box();
26+
let mut cache = cx.cache.get_mut();
27+
cache.deref_trait_did = cx.tcx.lang_items().deref_trait();
28+
cache.deref_mut_trait_did = cx.tcx.lang_items().deref_mut_trait();
29+
cache.owned_box_did = cx.tcx.lang_items().owned_box();
3030

3131
let mut externs = Vec::new();
3232
for &cnum in cx.tcx.crates().iter() {
@@ -348,7 +348,7 @@ crate fn resolve_type(cx: &DocContext<'_>, path: Path, id: hir::HirId) -> Type {
348348
return Generic(kw::SelfUpper);
349349
}
350350
Res::Def(DefKind::TyParam, _) if path.segments.len() == 1 => {
351-
return Generic(Symbol::intern(&format!("{:#}", path.print(&cx.cache))));
351+
return Generic(Symbol::intern(&format!("{:#}", path.print(&cx.cache.borrow()))));
352352
}
353353
Res::SelfTy(..) | Res::Def(DefKind::TyParam | DefKind::AssocTy, _) => true,
354354
_ => false,

Diff for: src/librustdoc/core.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ crate struct DocContext<'tcx> {
4949
///
5050
/// Most of this logic is copied from rustc_lint::late.
5151
crate param_env: Cell<ParamEnv<'tcx>>,
52-
/// Later on moved into `cache`
53-
crate renderinfo: RefCell<RenderInfo>,
5452
/// Later on moved through `clean::Crate` into `cache`
5553
crate external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>,
5654
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
@@ -78,8 +76,12 @@ crate struct DocContext<'tcx> {
7876
/// See `collect_intra_doc_links::traits_implemented_by` for more details.
7977
/// `map<module, set<trait>>`
8078
crate module_trait_cache: RefCell<FxHashMap<DefId, FxHashSet<DefId>>>,
81-
/// Fake empty cache used when cache is required as parameter.
82-
crate cache: Cache,
79+
/// This same cache is used throughout rustdoc, including in `render`.
80+
crate cache: RefCell<Cache>,
81+
/// Used by `clean::inline` to tell if an item has already been inlined.
82+
crate inlined: RefCell<FxHashSet<DefId>>,
83+
/// Used by `calculate_doc_coverage`.
84+
crate output_format: OutputFormat,
8385
}
8486

8587
impl<'tcx> DocContext<'tcx> {
@@ -464,7 +466,7 @@ crate fn run_global_ctxt(
464466
mut manual_passes: Vec<String>,
465467
render_options: RenderOptions,
466468
output_format: OutputFormat,
467-
) -> (clean::Crate, RenderInfo, RenderOptions) {
469+
) -> (clean::Crate, RenderOptions, Cache) {
468470
// Certain queries assume that some checks were run elsewhere
469471
// (see https://github.com/rust-lang/rust/pull/73566#issuecomment-656954425),
470472
// so type-check everything other than function bodies in this crate before running lints.
@@ -514,7 +516,6 @@ crate fn run_global_ctxt(
514516
param_env: Cell::new(ParamEnv::empty()),
515517
external_traits: Default::default(),
516518
active_extern_traits: Default::default(),
517-
renderinfo: RefCell::new(renderinfo),
518519
ty_substs: Default::default(),
519520
lt_substs: Default::default(),
520521
ct_substs: Default::default(),
@@ -527,9 +528,11 @@ crate fn run_global_ctxt(
527528
.cloned()
528529
.filter(|trait_def_id| tcx.trait_is_auto(*trait_def_id))
529530
.collect(),
530-
render_options,
531531
module_trait_cache: RefCell::new(FxHashMap::default()),
532-
cache: Cache::default(),
532+
cache: RefCell::new(Cache::new(renderinfo, render_options.document_private)),
533+
inlined: RefCell::new(FxHashSet::default()),
534+
output_format,
535+
render_options,
533536
};
534537
debug!("crate: {:?}", tcx.hir().krate());
535538

@@ -634,10 +637,17 @@ crate fn run_global_ctxt(
634637

635638
ctxt.sess().abort_if_errors();
636639

640+
let render_options = ctxt.render_options;
641+
let mut cache = ctxt.cache.into_inner();
642+
643+
krate = tcx.sess.time("create_format_cache", || {
644+
cache.populate(krate, tcx, &render_options.extern_html_root_urls, &render_options.output)
645+
});
646+
637647
// The main crate doc comments are always collapsed.
638648
krate.collapsed = true;
639649

640-
(krate, ctxt.renderinfo.into_inner(), ctxt.render_options)
650+
(krate, render_options, cache)
641651
}
642652

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

Diff for: src/librustdoc/formats/cache.rs

+26-27
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,7 @@ struct CacheBuilder<'a, 'tcx> {
130130
}
131131

132132
impl Cache {
133-
crate fn from_krate<'tcx>(
134-
render_info: RenderInfo,
135-
document_private: bool,
136-
extern_html_root_urls: &BTreeMap<String, String>,
137-
dst: &Path,
138-
mut krate: clean::Crate,
139-
tcx: TyCtxt<'tcx>,
140-
) -> (clean::Crate, Cache) {
133+
crate fn new(render_info: RenderInfo, document_private: bool) -> Self {
141134
// Crawl the crate to build various caches used for the output
142135
let RenderInfo {
143136
inlined: _,
@@ -153,21 +146,29 @@ impl Cache {
153146
let external_paths =
154147
external_paths.into_iter().map(|(k, (v, t))| (k, (v, ItemType::from(t)))).collect();
155148

156-
let mut cache = Cache {
149+
Cache {
157150
external_paths,
158151
exact_paths,
159-
parent_is_trait_impl: false,
160-
stripped_mod: false,
161152
access_levels,
162-
crate_version: krate.version.take(),
163153
document_private,
164-
traits: krate.external_traits.replace(Default::default()),
165154
deref_trait_did,
166155
deref_mut_trait_did,
167156
owned_box_did,
168-
masked_crates: mem::take(&mut krate.masked_crates),
169157
..Cache::default()
170-
};
158+
}
159+
}
160+
161+
crate fn populate(
162+
&mut self,
163+
mut krate: clean::Crate,
164+
tcx: TyCtxt<'_>,
165+
extern_html_root_urls: &BTreeMap<String, String>,
166+
dst: &Path,
167+
) -> clean::Crate {
168+
self.crate_version = krate.version.take();
169+
debug!(?self.crate_version);
170+
self.traits = krate.external_traits.take();
171+
self.masked_crates = mem::take(&mut krate.masked_crates);
171172

172173
// Cache where all our extern crates are located
173174
// FIXME: this part is specific to HTML so it'd be nice to remove it from the common code
@@ -180,12 +181,11 @@ impl Cache {
180181
_ => PathBuf::new(),
181182
};
182183
let extern_url = extern_html_root_urls.get(&*e.name.as_str()).map(|u| &**u);
183-
cache
184-
.extern_locations
184+
self.extern_locations
185185
.insert(n, (e.name, src_root, extern_location(e, extern_url, &dst)));
186186

187187
let did = DefId { krate: n, index: CRATE_DEF_INDEX };
188-
cache.external_paths.insert(did, (vec![e.name.to_string()], ItemType::Module));
188+
self.external_paths.insert(did, (vec![e.name.to_string()], ItemType::Module));
189189
}
190190

191191
// Cache where all known primitives have their documentation located.
@@ -194,27 +194,26 @@ impl Cache {
194194
// reverse topological order.
195195
for &(_, ref e) in krate.externs.iter().rev() {
196196
for &(def_id, prim) in &e.primitives {
197-
cache.primitive_locations.insert(prim, def_id);
197+
self.primitive_locations.insert(prim, def_id);
198198
}
199199
}
200200
for &(def_id, prim) in &krate.primitives {
201-
cache.primitive_locations.insert(prim, def_id);
201+
self.primitive_locations.insert(prim, def_id);
202202
}
203203

204-
cache.stack.push(krate.name.to_string());
204+
self.stack.push(krate.name.to_string());
205205

206-
krate = CacheBuilder { tcx, cache: &mut cache, empty_cache: Cache::default() }
207-
.fold_crate(krate);
206+
krate = CacheBuilder { tcx, cache: self, empty_cache: Cache::default() }.fold_crate(krate);
208207

209-
for (trait_did, dids, impl_) in cache.orphan_trait_impls.drain(..) {
210-
if cache.traits.contains_key(&trait_did) {
208+
for (trait_did, dids, impl_) in self.orphan_trait_impls.drain(..) {
209+
if self.traits.contains_key(&trait_did) {
211210
for did in dids {
212-
cache.impls.entry(did).or_default().push(impl_.clone());
211+
self.impls.entry(did).or_default().push(impl_.clone());
213212
}
214213
}
215214
}
216215

217-
(krate, cache)
216+
krate
218217
}
219218
}
220219

Diff for: src/librustdoc/formats/renderer.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_middle::ty::TyCtxt;
22
use rustc_span::edition::Edition;
33

44
use crate::clean;
5-
use crate::config::{RenderInfo, RenderOptions};
5+
use crate::config::RenderOptions;
66
use crate::error::Error;
77
use crate::formats::cache::Cache;
88

@@ -18,7 +18,6 @@ crate trait FormatRenderer<'tcx>: Clone {
1818
fn init(
1919
krate: clean::Crate,
2020
options: RenderOptions,
21-
render_info: RenderInfo,
2221
edition: Edition,
2322
cache: Cache,
2423
tcx: TyCtxt<'tcx>,
@@ -49,26 +48,16 @@ crate trait FormatRenderer<'tcx>: Clone {
4948
crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
5049
krate: clean::Crate,
5150
options: RenderOptions,
52-
render_info: RenderInfo,
51+
cache: Cache,
5352
diag: &rustc_errors::Handler,
5453
edition: Edition,
5554
tcx: TyCtxt<'tcx>,
5655
) -> Result<(), Error> {
57-
let (krate, cache) = tcx.sess.time("create_format_cache", || {
58-
Cache::from_krate(
59-
render_info.clone(),
60-
options.document_private,
61-
&options.extern_html_root_urls,
62-
&options.output,
63-
krate,
64-
tcx,
65-
)
66-
});
6756
let prof = &tcx.sess.prof;
6857

6958
let (mut format_renderer, mut krate) = prof
7059
.extra_verbose_generic_activity("create_renderer", T::descr())
71-
.run(|| T::init(krate, options, render_info, edition, cache, tcx))?;
60+
.run(|| T::init(krate, options, edition, cache, tcx))?;
7261

7362
let mut item = match krate.module.take() {
7463
Some(i) => i,

Diff for: src/librustdoc/html/render/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use serde::ser::SerializeSeq;
6666
use serde::{Serialize, Serializer};
6767

6868
use crate::clean::{self, AttributesExt, GetDefId, RenderedLink, SelfTy, TypeKind};
69-
use crate::config::{RenderInfo, RenderOptions};
69+
use crate::config::RenderOptions;
7070
use crate::docfs::{DocFS, PathError};
7171
use crate::error::Error;
7272
use crate::formats::cache::Cache;
@@ -385,7 +385,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
385385
fn init(
386386
mut krate: clean::Crate,
387387
options: RenderOptions,
388-
_render_info: RenderInfo,
389388
edition: Edition,
390389
mut cache: Cache,
391390
tcx: TyCtxt<'tcx>,

Diff for: src/librustdoc/json/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_span::edition::Edition;
1919
use rustdoc_json_types as types;
2020

2121
use crate::clean;
22-
use crate::config::{RenderInfo, RenderOptions};
22+
use crate::config::RenderOptions;
2323
use crate::error::Error;
2424
use crate::formats::cache::Cache;
2525
use crate::formats::FormatRenderer;
@@ -132,7 +132,6 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
132132
fn init(
133133
krate: clean::Crate,
134134
options: RenderOptions,
135-
_render_info: RenderInfo,
136135
_edition: Edition,
137136
cache: Cache,
138137
tcx: TyCtxt<'tcx>,

0 commit comments

Comments
 (0)