Skip to content

Commit b51272e

Browse files
Rollup merge of rust-lang#82018 - jyn514:no-dummy-cache, r=camelid,GuillaumeGomez
Remove the dummy cache in `DocContext`; delete RenderInfo The same information is available everywhere; the only reason the dummy cache was needed is because it was previously stored in three different places. This consolidates the info a bit so the cache in `DocContext` is used throughout. As a bonus, it also completely removes `RenderInfo`. - Return a `Cache` from `run_global_ctxt`, not `RenderInfo` - Remove the unused `render_info` from `run_renderer` - Remove RenderInfo altogether Helps with rust-lang#82014. The next step is to move the `populate()` call before the `collect_intra_doc_links` pass, which currently breaks because a) lots of the cache is populated in early passes, and b) intra_doc_links itself sets some info with `register_res`. I'm working on separate PR for that to avoid making too many big changes at once. r? `@GuillaumeGomez`
2 parents 22ebb86 + be069a6 commit b51272e

19 files changed

+82
-133
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/types.rs

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ thread_local!(crate static MAX_DEF_IDX: RefCell<FxHashMap<CrateNum, DefIndex>> =
5050
#[derive(Clone, Debug)]
5151
crate struct Crate {
5252
crate name: Symbol,
53-
crate version: Option<String>,
5453
crate src: FileName,
5554
crate module: Option<Item>,
5655
crate externs: Vec<(CrateNum, ExternalCrate)>,

src/librustdoc/clean/utils.rs

+6-7
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

@@ -77,7 +77,6 @@ crate fn krate(mut cx: &mut DocContext<'_>) -> Crate {
7777

7878
Crate {
7979
name,
80-
version: None,
8180
src,
8281
module: Some(module),
8382
externs,

src/librustdoc/config.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ use std::ffi::OsStr;
44
use std::fmt;
55
use std::path::PathBuf;
66

7-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
8-
use rustc_hir::def_id::DefId;
9-
use rustc_middle::middle::privacy::AccessLevels;
7+
use rustc_data_structures::fx::FxHashMap;
108
use rustc_session::config::{self, parse_crate_types_from_list, parse_externs, CrateType};
119
use rustc_session::config::{
1210
build_codegen_options, build_debugging_options, get_cmd_lint_options, host_triple,
@@ -268,20 +266,6 @@ crate struct RenderOptions {
268266
crate unstable_features: rustc_feature::UnstableFeatures,
269267
}
270268

271-
/// Temporary storage for data obtained during `RustdocVisitor::clean()`.
272-
/// Later on moved into `cache`.
273-
#[derive(Default, Clone)]
274-
crate struct RenderInfo {
275-
crate inlined: FxHashSet<DefId>,
276-
crate external_paths: crate::core::ExternalPaths,
277-
crate exact_paths: FxHashMap<DefId, Vec<String>>,
278-
crate access_levels: AccessLevels<DefId>,
279-
crate deref_trait_did: Option<DefId>,
280-
crate deref_mut_trait_did: Option<DefId>,
281-
crate owned_box_did: Option<DefId>,
282-
crate output_format: OutputFormat,
283-
}
284-
285269
impl Options {
286270
/// Parses the given command-line for options. If an error message or other early-return has
287271
/// been printed, returns `Err` with the exit code.

src/librustdoc/core.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,12 @@ use std::{cell::RefCell, collections::hash_map::Entry};
3131
use crate::clean;
3232
use crate::clean::inline::build_external_trait;
3333
use crate::clean::{AttributesExt, TraitWithExtraInfo, MAX_DEF_IDX};
34-
use crate::config::{Options as RustdocOptions, RenderOptions};
35-
use crate::config::{OutputFormat, RenderInfo};
34+
use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions};
3635
use crate::formats::cache::Cache;
3736
use crate::passes::{self, Condition::*, ConditionalPass};
3837

3938
crate use rustc_session::config::{DebuggingOptions, Input, Options};
4039

41-
crate type ExternalPaths = FxHashMap<DefId, (Vec<String>, clean::TypeKind)>;
42-
4340
crate struct DocContext<'tcx> {
4441
crate tcx: TyCtxt<'tcx>,
4542
/// Name resolver. Used for intra-doc links.
@@ -52,8 +49,6 @@ crate struct DocContext<'tcx> {
5249
///
5350
/// Most of this logic is copied from rustc_lint::late.
5451
crate param_env: ParamEnv<'tcx>,
55-
/// Later on moved into `cache`
56-
crate renderinfo: RenderInfo,
5752
/// Later on moved through `clean::Crate` into `cache`
5853
crate external_traits: Rc<RefCell<FxHashMap<DefId, clean::TraitWithExtraInfo>>>,
5954
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
@@ -81,8 +76,12 @@ crate struct DocContext<'tcx> {
8176
/// See `collect_intra_doc_links::traits_implemented_by` for more details.
8277
/// `map<module, set<trait>>`
8378
crate module_trait_cache: RefCell<FxHashMap<DefId, FxHashSet<DefId>>>,
84-
/// Fake empty cache used when cache is required as parameter.
79+
/// This same cache is used throughout rustdoc, including in [`crate::html::render`].
8580
crate cache: Cache,
81+
/// Used by [`clean::inline`] to tell if an item has already been inlined.
82+
crate inlined: FxHashSet<DefId>,
83+
/// Used by `calculate_doc_coverage`.
84+
crate output_format: OutputFormat,
8685
}
8786

8887
impl<'tcx> DocContext<'tcx> {
@@ -465,7 +464,7 @@ crate fn run_global_ctxt(
465464
mut manual_passes: Vec<String>,
466465
render_options: RenderOptions,
467466
output_format: OutputFormat,
468-
) -> (clean::Crate, RenderInfo, RenderOptions) {
467+
) -> (clean::Crate, RenderOptions, Cache) {
469468
// Certain queries assume that some checks were run elsewhere
470469
// (see https://github.com/rust-lang/rust/pull/73566#issuecomment-656954425),
471470
// so type-check everything other than function bodies in this crate before running lints.
@@ -504,17 +503,12 @@ crate fn run_global_ctxt(
504503
.collect(),
505504
};
506505

507-
let mut renderinfo = RenderInfo::default();
508-
renderinfo.access_levels = access_levels;
509-
renderinfo.output_format = output_format;
510-
511506
let mut ctxt = DocContext {
512507
tcx,
513508
resolver,
514509
param_env: ParamEnv::empty(),
515510
external_traits: Default::default(),
516511
active_extern_traits: Default::default(),
517-
renderinfo,
518512
ty_substs: Default::default(),
519513
lt_substs: Default::default(),
520514
ct_substs: Default::default(),
@@ -527,9 +521,11 @@ crate fn run_global_ctxt(
527521
.cloned()
528522
.filter(|trait_def_id| tcx.trait_is_auto(*trait_def_id))
529523
.collect(),
530-
render_options,
531524
module_trait_cache: RefCell::new(FxHashMap::default()),
532-
cache: Cache::default(),
525+
cache: Cache::new(access_levels, render_options.document_private),
526+
inlined: FxHashSet::default(),
527+
output_format,
528+
render_options,
533529
};
534530

535531
// Small hack to force the Sized trait to be present.
@@ -647,10 +643,16 @@ crate fn run_global_ctxt(
647643

648644
ctxt.sess().abort_if_errors();
649645

646+
let render_options = ctxt.render_options;
647+
let mut cache = ctxt.cache;
648+
krate = tcx.sess.time("create_format_cache", || {
649+
cache.populate(krate, tcx, &render_options.extern_html_root_urls, &render_options.output)
650+
});
651+
650652
// The main crate doc comments are always collapsed.
651653
krate.collapsed = true;
652654

653-
(krate, ctxt.renderinfo, ctxt.render_options)
655+
(krate, render_options, cache)
654656
}
655657

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

src/librustdoc/formats/cache.rs

+24-48
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_span::symbol::sym;
1111
use rustc_span::Symbol;
1212

1313
use crate::clean::{self, GetDefId};
14-
use crate::config::RenderInfo;
1514
use crate::fold::DocFolder;
1615
use crate::formats::item_type::ItemType;
1716
use crate::formats::Impl;
@@ -131,44 +130,23 @@ struct CacheBuilder<'a, 'tcx> {
131130
}
132131

133132
impl Cache {
134-
crate fn from_krate<'tcx>(
135-
render_info: RenderInfo,
136-
document_private: bool,
133+
crate fn new(access_levels: AccessLevels<DefId>, document_private: bool) -> Self {
134+
Cache { access_levels, document_private, ..Cache::default() }
135+
}
136+
137+
/// Populates the `Cache` with more data. The returned `Crate` will be missing some data that was
138+
/// 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<'_>,
137143
extern_html_root_urls: &BTreeMap<String, String>,
138144
dst: &Path,
139-
mut krate: clean::Crate,
140-
tcx: TyCtxt<'tcx>,
141-
) -> (clean::Crate, Cache) {
145+
) -> clean::Crate {
142146
// Crawl the crate to build various caches used for the output
143-
let RenderInfo {
144-
inlined: _,
145-
external_paths,
146-
exact_paths,
147-
access_levels,
148-
deref_trait_did,
149-
deref_mut_trait_did,
150-
owned_box_did,
151-
..
152-
} = render_info;
153-
154-
let external_paths =
155-
external_paths.into_iter().map(|(k, (v, t))| (k, (v, ItemType::from(t)))).collect();
156-
157-
let mut cache = Cache {
158-
external_paths,
159-
exact_paths,
160-
parent_is_trait_impl: false,
161-
stripped_mod: false,
162-
access_levels,
163-
crate_version: krate.version.take(),
164-
document_private,
165-
traits: krate.external_traits.replace(Default::default()),
166-
deref_trait_did,
167-
deref_mut_trait_did,
168-
owned_box_did,
169-
masked_crates: mem::take(&mut krate.masked_crates),
170-
..Cache::default()
171-
};
147+
debug!(?self.crate_version);
148+
self.traits = krate.external_traits.take();
149+
self.masked_crates = mem::take(&mut krate.masked_crates);
172150

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

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

192169
// Cache where all known primitives have their documentation located.
@@ -195,27 +172,26 @@ impl Cache {
195172
// reverse topological order.
196173
for &(_, ref e) in krate.externs.iter().rev() {
197174
for &(def_id, prim) in &e.primitives {
198-
cache.primitive_locations.insert(prim, def_id);
175+
self.primitive_locations.insert(prim, def_id);
199176
}
200177
}
201178
for &(def_id, prim) in &krate.primitives {
202-
cache.primitive_locations.insert(prim, def_id);
179+
self.primitive_locations.insert(prim, def_id);
203180
}
204181

205-
cache.stack.push(krate.name.to_string());
182+
self.stack.push(krate.name.to_string());
206183

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

210-
for (trait_did, dids, impl_) in cache.orphan_trait_impls.drain(..) {
211-
if cache.traits.contains_key(&trait_did) {
186+
for (trait_did, dids, impl_) in self.orphan_trait_impls.drain(..) {
187+
if self.traits.contains_key(&trait_did) {
212188
for did in dids {
213-
cache.impls.entry(did).or_default().push(impl_.clone());
189+
self.impls.entry(did).or_default().push(impl_.clone());
214190
}
215191
}
216192
}
217193

218-
(krate, cache)
194+
krate
219195
}
220196
}
221197

0 commit comments

Comments
 (0)