Skip to content

Commit 4d7a648

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. - Return a `Cache` from `run_global_ctxt`, not `RenderInfo` - Remove the unused `render_info` from `run_renderer` - Remove RefCell around `inlined` - Add intra-doc links
1 parent e37a13c commit 4d7a648

16 files changed

+79
-82
lines changed

src/librustdoc/clean/blanket_impl.rs

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

src/librustdoc/clean/inline.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_span::Span;
1717

1818
use crate::clean::{self, Attributes, GetDefId, ToSource, TypeKind};
1919
use crate::core::DocContext;
20+
use crate::formats::item_type::ItemType;
2021

2122
use super::Clean;
2223

@@ -122,7 +123,7 @@ crate fn try_inline(
122123
let target_attrs = load_attrs(cx, did);
123124
let attrs = box merge_attrs(cx, Some(parent_module), target_attrs, attrs_clone);
124125

125-
cx.renderinfo.inlined.insert(did);
126+
cx.inlined.insert(did);
126127
let what_rustc_thinks = clean::Item::from_def_id_and_parts(did, Some(name), kind, cx);
127128
ret.push(clean::Item { attrs, ..what_rustc_thinks });
128129
Some(ret)
@@ -181,9 +182,9 @@ crate fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: clean::Typ
181182
};
182183

183184
if did.is_local() {
184-
cx.renderinfo.exact_paths.insert(did, fqn);
185+
cx.cache.exact_paths.insert(did, fqn);
185186
} else {
186-
cx.renderinfo.external_paths.insert(did, (fqn, kind));
187+
cx.cache.external_paths.insert(did, (fqn, ItemType::from(kind)));
187188
}
188189
}
189190

@@ -315,7 +316,7 @@ crate fn build_impl(
315316
attrs: Option<Attrs<'_>>,
316317
ret: &mut Vec<clean::Item>,
317318
) {
318-
if !cx.renderinfo.inlined.insert(did) {
319+
if !cx.inlined.insert(did) {
319320
return;
320321
}
321322

@@ -327,7 +328,7 @@ crate fn build_impl(
327328
if !did.is_local() {
328329
if let Some(traitref) = associated_trait {
329330
let did = traitref.def_id;
330-
if !cx.renderinfo.access_levels.is_public(did) {
331+
if !cx.cache.access_levels.is_public(did) {
331332
return;
332333
}
333334

@@ -359,7 +360,7 @@ crate fn build_impl(
359360
// reachable in rustdoc generated documentation
360361
if !did.is_local() {
361362
if let Some(did) = for_.def_id() {
362-
if !cx.renderinfo.access_levels.is_public(did) {
363+
if !cx.cache.access_levels.is_public(did) {
363364
return;
364365
}
365366

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
13041304
// Substitute private type aliases
13051305
if let Some(def_id) = def_id.as_local() {
13061306
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
1307-
if !cx.renderinfo.access_levels.is_exported(def_id.to_def_id()) {
1307+
if !cx.cache.access_levels.is_exported(def_id.to_def_id()) {
13081308
alias = Some(&cx.tcx.hir().expect_item(hir_id).kind);
13091309
}
13101310
}

src/librustdoc/clean/utils.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ use rustc_middle::ty::{self, DefIdTree, TyCtxt};
1717
use rustc_span::symbol::{kw, sym, Symbol};
1818
use std::mem;
1919

20-
crate fn krate(mut cx: &mut DocContext<'_>) -> Crate {
20+
crate fn krate(cx: &mut DocContext<'_>) -> Crate {
2121
use crate::visit_lib::LibEmbargoVisitor;
2222

2323
let krate = cx.tcx.hir().krate();
24-
let module = crate::visit_ast::RustdocVisitor::new(&mut cx).visit(krate);
24+
let module = crate::visit_ast::RustdocVisitor::new(cx).visit(krate);
2525

26-
cx.renderinfo.deref_trait_did = cx.tcx.lang_items().deref_trait();
27-
cx.renderinfo.deref_mut_trait_did = cx.tcx.lang_items().deref_mut_trait();
28-
cx.renderinfo.owned_box_did = cx.tcx.lang_items().owned_box();
26+
cx.cache.deref_trait_did = cx.tcx.lang_items().deref_trait();
27+
cx.cache.deref_mut_trait_did = cx.tcx.lang_items().deref_mut_trait();
28+
cx.cache.owned_box_did = cx.tcx.lang_items().owned_box();
2929

3030
let mut externs = Vec::new();
3131
for &cnum in cx.tcx.crates().iter() {
3232
externs.push((cnum, cnum.clean(cx)));
3333
// Analyze doc-reachability for extern items
34-
LibEmbargoVisitor::new(&mut cx).visit_lib(cnum);
34+
LibEmbargoVisitor::new(cx).visit_lib(cnum);
3535
}
3636
externs.sort_by(|&(a, _), &(b, _)| a.cmp(&b));
3737

src/librustdoc/core.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ crate struct DocContext<'tcx> {
5252
///
5353
/// Most of this logic is copied from rustc_lint::late.
5454
crate param_env: ParamEnv<'tcx>,
55-
/// Later on moved into `cache`
56-
crate renderinfo: RenderInfo,
5755
/// Later on moved through `clean::Crate` into `cache`
5856
crate external_traits: Rc<RefCell<FxHashMap<DefId, clean::TraitWithExtraInfo>>>,
5957
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
@@ -81,8 +79,12 @@ crate struct DocContext<'tcx> {
8179
/// See `collect_intra_doc_links::traits_implemented_by` for more details.
8280
/// `map<module, set<trait>>`
8381
crate module_trait_cache: RefCell<FxHashMap<DefId, FxHashSet<DefId>>>,
84-
/// Fake empty cache used when cache is required as parameter.
82+
/// This same cache is used throughout rustdoc, including in [`crate::html::render`].
8583
crate cache: Cache,
84+
/// Used by [`clean::inline`] to tell if an item has already been inlined.
85+
crate inlined: FxHashSet<DefId>,
86+
/// Used by `calculate_doc_coverage`.
87+
crate output_format: OutputFormat,
8688
}
8789

8890
impl<'tcx> DocContext<'tcx> {
@@ -465,7 +467,7 @@ crate fn run_global_ctxt(
465467
mut manual_passes: Vec<String>,
466468
render_options: RenderOptions,
467469
output_format: OutputFormat,
468-
) -> (clean::Crate, RenderInfo, RenderOptions) {
470+
) -> (clean::Crate, RenderOptions, Cache) {
469471
// Certain queries assume that some checks were run elsewhere
470472
// (see https://github.com/rust-lang/rust/pull/73566#issuecomment-656954425),
471473
// 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: ParamEnv::empty(),
515517
external_traits: Default::default(),
516518
active_extern_traits: Default::default(),
517-
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: Cache::new(renderinfo, render_options.document_private),
533+
inlined: FxHashSet::default(),
534+
output_format,
535+
render_options,
533536
};
534537

535538
// Small hack to force the Sized trait to be present.
@@ -647,10 +650,16 @@ crate fn run_global_ctxt(
647650

648651
ctxt.sess().abort_if_errors();
649652

653+
let render_options = ctxt.render_options;
654+
let mut cache = ctxt.cache;
655+
krate = tcx.sess.time("create_format_cache", || {
656+
cache.populate(krate, tcx, &render_options.extern_html_root_urls, &render_options.output)
657+
});
658+
650659
// The main crate doc comments are always collapsed.
651660
krate.collapsed = true;
652661

653-
(krate, ctxt.renderinfo, ctxt.render_options)
662+
(krate, render_options, cache)
654663
}
655664

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

src/librustdoc/formats/cache.rs

+28-27
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,7 @@ struct CacheBuilder<'a, 'tcx> {
131131
}
132132

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

157-
let mut cache = Cache {
150+
Cache {
158151
external_paths,
159152
exact_paths,
160-
parent_is_trait_impl: false,
161-
stripped_mod: false,
162153
access_levels,
163-
crate_version: krate.version.take(),
164154
document_private,
165-
traits: krate.external_traits.replace(Default::default()),
166155
deref_trait_did,
167156
deref_mut_trait_did,
168157
owned_box_did,
169-
masked_crates: mem::take(&mut krate.masked_crates),
170158
..Cache::default()
171-
};
159+
}
160+
}
161+
162+
/// Populates the `Cache` with more data. The returned `Crate` will be missing some data that was
163+
/// in `krate` due to the data being moved into the `Cache`.
164+
crate fn populate(
165+
&mut self,
166+
mut krate: clean::Crate,
167+
tcx: TyCtxt<'_>,
168+
extern_html_root_urls: &BTreeMap<String, String>,
169+
dst: &Path,
170+
) -> clean::Crate {
171+
self.crate_version = krate.version.take();
172+
debug!(?self.crate_version);
173+
self.traits = krate.external_traits.take();
174+
self.masked_crates = mem::take(&mut krate.masked_crates);
172175

173176
// Cache where all our extern crates are located
174177
// FIXME: this part is specific to HTML so it'd be nice to remove it from the common code
@@ -181,12 +184,11 @@ impl Cache {
181184
_ => PathBuf::new(),
182185
};
183186
let extern_url = extern_html_root_urls.get(&*e.name.as_str()).map(|u| &**u);
184-
cache
185-
.extern_locations
187+
self.extern_locations
186188
.insert(n, (e.name, src_root, extern_location(e, extern_url, &dst)));
187189

188190
let did = DefId { krate: n, index: CRATE_DEF_INDEX };
189-
cache.external_paths.insert(did, (vec![e.name.to_string()], ItemType::Module));
191+
self.external_paths.insert(did, (vec![e.name.to_string()], ItemType::Module));
190192
}
191193

192194
// Cache where all known primitives have their documentation located.
@@ -195,27 +197,26 @@ impl Cache {
195197
// reverse topological order.
196198
for &(_, ref e) in krate.externs.iter().rev() {
197199
for &(def_id, prim) in &e.primitives {
198-
cache.primitive_locations.insert(prim, def_id);
200+
self.primitive_locations.insert(prim, def_id);
199201
}
200202
}
201203
for &(def_id, prim) in &krate.primitives {
202-
cache.primitive_locations.insert(prim, def_id);
204+
self.primitive_locations.insert(prim, def_id);
203205
}
204206

205-
cache.stack.push(krate.name.to_string());
207+
self.stack.push(krate.name.to_string());
206208

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

210-
for (trait_did, dids, impl_) in cache.orphan_trait_impls.drain(..) {
211-
if cache.traits.contains_key(&trait_did) {
211+
for (trait_did, dids, impl_) in self.orphan_trait_impls.drain(..) {
212+
if self.traits.contains_key(&trait_did) {
212213
for did in dids {
213-
cache.impls.entry(did).or_default().push(impl_.clone());
214+
self.impls.entry(did).or_default().push(impl_.clone());
214215
}
215216
}
216217
}
217218

218-
(krate, cache)
219+
krate
219220
}
220221
}
221222

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,

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>,

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;
@@ -133,7 +133,6 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
133133
fn init(
134134
krate: clean::Crate,
135135
options: RenderOptions,
136-
_render_info: RenderInfo,
137136
_edition: Edition,
138137
cache: Cache,
139138
tcx: TyCtxt<'tcx>,

0 commit comments

Comments
 (0)