Skip to content

Commit

Permalink
Rollup merge of #82018 - jyn514:no-dummy-cache, r=camelid,GuillaumeGomez
Browse files Browse the repository at this point in the history
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 #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`
  • Loading branch information
GuillaumeGomez committed Mar 1, 2021
2 parents 22ebb86 + be069a6 commit b51272e
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 133 deletions.
2 changes: 1 addition & 1 deletion src/librustdoc/clean/blanket_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
debug!("get_blanket_impls({:?})", ty);
let mut impls = Vec::new();
for &trait_def_id in self.cx.tcx.all_traits(LOCAL_CRATE).iter() {
if !self.cx.renderinfo.access_levels.is_public(trait_def_id)
if !self.cx.cache.access_levels.is_public(trait_def_id)
|| self.cx.generated_synthetics.get(&(ty, trait_def_id)).is_some()
{
continue;
Expand Down
13 changes: 7 additions & 6 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use rustc_span::Span;

use crate::clean::{self, Attributes, GetDefId, ToSource, TypeKind};
use crate::core::DocContext;
use crate::formats::item_type::ItemType;

use super::Clean;

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

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

if did.is_local() {
cx.renderinfo.exact_paths.insert(did, fqn);
cx.cache.exact_paths.insert(did, fqn);
} else {
cx.renderinfo.external_paths.insert(did, (fqn, kind));
cx.cache.external_paths.insert(did, (fqn, ItemType::from(kind)));
}
}

Expand Down Expand Up @@ -315,7 +316,7 @@ crate fn build_impl(
attrs: Option<Attrs<'_>>,
ret: &mut Vec<clean::Item>,
) {
if !cx.renderinfo.inlined.insert(did) {
if !cx.inlined.insert(did) {
return;
}

Expand All @@ -327,7 +328,7 @@ crate fn build_impl(
if !did.is_local() {
if let Some(traitref) = associated_trait {
let did = traitref.def_id;
if !cx.renderinfo.access_levels.is_public(did) {
if !cx.cache.access_levels.is_public(did) {
return;
}

Expand Down Expand Up @@ -359,7 +360,7 @@ crate fn build_impl(
// reachable in rustdoc generated documentation
if !did.is_local() {
if let Some(did) = for_.def_id() {
if !cx.renderinfo.access_levels.is_public(did) {
if !cx.cache.access_levels.is_public(did) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
// Substitute private type aliases
if let Some(def_id) = def_id.as_local() {
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
if !cx.renderinfo.access_levels.is_exported(def_id.to_def_id()) {
if !cx.cache.access_levels.is_exported(def_id.to_def_id()) {
alias = Some(&cx.tcx.hir().expect_item(hir_id).kind);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ thread_local!(crate static MAX_DEF_IDX: RefCell<FxHashMap<CrateNum, DefIndex>> =
#[derive(Clone, Debug)]
crate struct Crate {
crate name: Symbol,
crate version: Option<String>,
crate src: FileName,
crate module: Option<Item>,
crate externs: Vec<(CrateNum, ExternalCrate)>,
Expand Down
13 changes: 6 additions & 7 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ use rustc_middle::ty::{self, DefIdTree, TyCtxt};
use rustc_span::symbol::{kw, sym, Symbol};
use std::mem;

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

let krate = cx.tcx.hir().krate();
let module = crate::visit_ast::RustdocVisitor::new(&mut cx).visit(krate);
let module = crate::visit_ast::RustdocVisitor::new(cx).visit(krate);

cx.renderinfo.deref_trait_did = cx.tcx.lang_items().deref_trait();
cx.renderinfo.deref_mut_trait_did = cx.tcx.lang_items().deref_mut_trait();
cx.renderinfo.owned_box_did = cx.tcx.lang_items().owned_box();
cx.cache.deref_trait_did = cx.tcx.lang_items().deref_trait();
cx.cache.deref_mut_trait_did = cx.tcx.lang_items().deref_mut_trait();
cx.cache.owned_box_did = cx.tcx.lang_items().owned_box();

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

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

Crate {
name,
version: None,
src,
module: Some(module),
externs,
Expand Down
18 changes: 1 addition & 17 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use std::ffi::OsStr;
use std::fmt;
use std::path::PathBuf;

use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def_id::DefId;
use rustc_middle::middle::privacy::AccessLevels;
use rustc_data_structures::fx::FxHashMap;
use rustc_session::config::{self, parse_crate_types_from_list, parse_externs, CrateType};
use rustc_session::config::{
build_codegen_options, build_debugging_options, get_cmd_lint_options, host_triple,
Expand Down Expand Up @@ -268,20 +266,6 @@ crate struct RenderOptions {
crate unstable_features: rustc_feature::UnstableFeatures,
}

/// Temporary storage for data obtained during `RustdocVisitor::clean()`.
/// Later on moved into `cache`.
#[derive(Default, Clone)]
crate struct RenderInfo {
crate inlined: FxHashSet<DefId>,
crate external_paths: crate::core::ExternalPaths,
crate exact_paths: FxHashMap<DefId, Vec<String>>,
crate access_levels: AccessLevels<DefId>,
crate deref_trait_did: Option<DefId>,
crate deref_mut_trait_did: Option<DefId>,
crate owned_box_did: Option<DefId>,
crate output_format: OutputFormat,
}

impl Options {
/// Parses the given command-line for options. If an error message or other early-return has
/// been printed, returns `Err` with the exit code.
Expand Down
34 changes: 18 additions & 16 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@ use std::{cell::RefCell, collections::hash_map::Entry};
use crate::clean;
use crate::clean::inline::build_external_trait;
use crate::clean::{AttributesExt, TraitWithExtraInfo, MAX_DEF_IDX};
use crate::config::{Options as RustdocOptions, RenderOptions};
use crate::config::{OutputFormat, RenderInfo};
use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions};
use crate::formats::cache::Cache;
use crate::passes::{self, Condition::*, ConditionalPass};

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

crate type ExternalPaths = FxHashMap<DefId, (Vec<String>, clean::TypeKind)>;

crate struct DocContext<'tcx> {
crate tcx: TyCtxt<'tcx>,
/// Name resolver. Used for intra-doc links.
Expand All @@ -52,8 +49,6 @@ crate struct DocContext<'tcx> {
///
/// Most of this logic is copied from rustc_lint::late.
crate param_env: ParamEnv<'tcx>,
/// Later on moved into `cache`
crate renderinfo: RenderInfo,
/// Later on moved through `clean::Crate` into `cache`
crate external_traits: Rc<RefCell<FxHashMap<DefId, clean::TraitWithExtraInfo>>>,
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
Expand Down Expand Up @@ -81,8 +76,12 @@ crate struct DocContext<'tcx> {
/// See `collect_intra_doc_links::traits_implemented_by` for more details.
/// `map<module, set<trait>>`
crate module_trait_cache: RefCell<FxHashMap<DefId, FxHashSet<DefId>>>,
/// Fake empty cache used when cache is required as parameter.
/// This same cache is used throughout rustdoc, including in [`crate::html::render`].
crate cache: Cache,
/// Used by [`clean::inline`] to tell if an item has already been inlined.
crate inlined: FxHashSet<DefId>,
/// Used by `calculate_doc_coverage`.
crate output_format: OutputFormat,
}

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

let mut renderinfo = RenderInfo::default();
renderinfo.access_levels = access_levels;
renderinfo.output_format = output_format;

let mut ctxt = DocContext {
tcx,
resolver,
param_env: ParamEnv::empty(),
external_traits: Default::default(),
active_extern_traits: Default::default(),
renderinfo,
ty_substs: Default::default(),
lt_substs: Default::default(),
ct_substs: Default::default(),
Expand All @@ -527,9 +521,11 @@ crate fn run_global_ctxt(
.cloned()
.filter(|trait_def_id| tcx.trait_is_auto(*trait_def_id))
.collect(),
render_options,
module_trait_cache: RefCell::new(FxHashMap::default()),
cache: Cache::default(),
cache: Cache::new(access_levels, render_options.document_private),
inlined: FxHashSet::default(),
output_format,
render_options,
};

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

ctxt.sess().abort_if_errors();

let render_options = ctxt.render_options;
let mut cache = ctxt.cache;
krate = tcx.sess.time("create_format_cache", || {
cache.populate(krate, tcx, &render_options.extern_html_root_urls, &render_options.output)
});

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

(krate, ctxt.renderinfo, ctxt.render_options)
(krate, render_options, cache)
}

/// Due to <https://github.com/rust-lang/rust/pull/73566>,
Expand Down
72 changes: 24 additions & 48 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use rustc_span::symbol::sym;
use rustc_span::Symbol;

use crate::clean::{self, GetDefId};
use crate::config::RenderInfo;
use crate::fold::DocFolder;
use crate::formats::item_type::ItemType;
use crate::formats::Impl;
Expand Down Expand Up @@ -131,44 +130,23 @@ struct CacheBuilder<'a, 'tcx> {
}

impl Cache {
crate fn from_krate<'tcx>(
render_info: RenderInfo,
document_private: bool,
crate fn new(access_levels: AccessLevels<DefId>, document_private: bool) -> Self {
Cache { access_levels, document_private, ..Cache::default() }
}

/// Populates the `Cache` with more data. The returned `Crate` will be missing some data that was
/// in `krate` due to the data being moved into the `Cache`.
crate fn populate(
&mut self,
mut krate: clean::Crate,
tcx: TyCtxt<'_>,
extern_html_root_urls: &BTreeMap<String, String>,
dst: &Path,
mut krate: clean::Crate,
tcx: TyCtxt<'tcx>,
) -> (clean::Crate, Cache) {
) -> clean::Crate {
// Crawl the crate to build various caches used for the output
let RenderInfo {
inlined: _,
external_paths,
exact_paths,
access_levels,
deref_trait_did,
deref_mut_trait_did,
owned_box_did,
..
} = render_info;

let external_paths =
external_paths.into_iter().map(|(k, (v, t))| (k, (v, ItemType::from(t)))).collect();

let mut cache = Cache {
external_paths,
exact_paths,
parent_is_trait_impl: false,
stripped_mod: false,
access_levels,
crate_version: krate.version.take(),
document_private,
traits: krate.external_traits.replace(Default::default()),
deref_trait_did,
deref_mut_trait_did,
owned_box_did,
masked_crates: mem::take(&mut krate.masked_crates),
..Cache::default()
};
debug!(?self.crate_version);
self.traits = krate.external_traits.take();
self.masked_crates = mem::take(&mut krate.masked_crates);

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

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

// Cache where all known primitives have their documentation located.
Expand All @@ -195,27 +172,26 @@ impl Cache {
// reverse topological order.
for &(_, ref e) in krate.externs.iter().rev() {
for &(def_id, prim) in &e.primitives {
cache.primitive_locations.insert(prim, def_id);
self.primitive_locations.insert(prim, def_id);
}
}
for &(def_id, prim) in &krate.primitives {
cache.primitive_locations.insert(prim, def_id);
self.primitive_locations.insert(prim, def_id);
}

cache.stack.push(krate.name.to_string());
self.stack.push(krate.name.to_string());

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

for (trait_did, dids, impl_) in cache.orphan_trait_impls.drain(..) {
if cache.traits.contains_key(&trait_did) {
for (trait_did, dids, impl_) in self.orphan_trait_impls.drain(..) {
if self.traits.contains_key(&trait_did) {
for did in dids {
cache.impls.entry(did).or_default().push(impl_.clone());
self.impls.entry(did).or_default().push(impl_.clone());
}
}
}

(krate, cache)
krate
}
}

Expand Down
Loading

0 comments on commit b51272e

Please sign in to comment.