Skip to content

Commit

Permalink
Remove the dummy cache in DocContext
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
jyn514 committed Feb 12, 2021
1 parent 178108b commit 991ef9a
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 93 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 @@ -26,7 +26,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.borrow().access_levels.is_public(trait_def_id)
if !self.cx.cache.borrow().access_levels.is_public(trait_def_id)
|| self.cx.generated_synthetics.borrow_mut().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 @@ -122,7 +122,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.borrow_mut().inlined.insert(did);
cx.inlined.borrow_mut().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 +181,10 @@ crate fn record_extern_fqn(cx: &DocContext<'_>, did: DefId, kind: clean::TypeKin
};

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

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

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

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

Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,7 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &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.borrow().access_levels.is_exported(def_id.to_def_id()) {
if !cx.cache.borrow().access_levels.is_exported(def_id.to_def_id()) {
alias = Some(&cx.tcx.hir().expect_item(hir_id).kind);
}
}
Expand Down Expand Up @@ -2304,14 +2304,14 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
if matchers.len() <= 1 {
format!(
"{}macro {}{} {{\n ...\n}}",
vis.print_with_space(cx.tcx, def_id, &cx.cache),
vis.print_with_space(cx.tcx, def_id, &cx.cache.borrow()),
name,
matchers.iter().map(|span| span.to_src(cx)).collect::<String>(),
)
} else {
format!(
"{}macro {} {{\n{}}}",
vis.print_with_space(cx.tcx, def_id, &cx.cache),
vis.print_with_space(cx.tcx, def_id, &cx.cache.borrow()),
name,
matchers
.iter()
Expand Down
10 changes: 5 additions & 5 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ crate fn krate(mut cx: &mut DocContext<'_>) -> Crate {
let krate = cx.tcx.hir().krate();
let module = crate::visit_ast::RustdocVisitor::new(&mut cx).visit(krate);

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

let mut externs = Vec::new();
for &cnum in cx.tcx.crates().iter() {
Expand Down Expand Up @@ -348,7 +348,7 @@ crate fn resolve_type(cx: &DocContext<'_>, path: Path, id: hir::HirId) -> Type {
return Generic(kw::SelfUpper);
}
Res::Def(DefKind::TyParam, _) if path.segments.len() == 1 => {
return Generic(Symbol::intern(&format!("{:#}", path.print(&cx.cache))));
return Generic(Symbol::intern(&format!("{:#}", path.print(&cx.cache.borrow()))));
}
Res::SelfTy(..) | Res::Def(DefKind::TyParam | DefKind::AssocTy, _) => true,
_ => false,
Expand Down
28 changes: 19 additions & 9 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ crate struct DocContext<'tcx> {
///
/// Most of this logic is copied from rustc_lint::late.
crate param_env: Cell<ParamEnv<'tcx>>,
/// Later on moved into `cache`
crate renderinfo: RefCell<RenderInfo>,
/// Later on moved through `clean::Crate` into `cache`
crate external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>,
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
Expand Down Expand Up @@ -78,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.
crate cache: Cache,
/// This same cache is used throughout rustdoc, including in `render`.
crate cache: RefCell<Cache>,
/// Used by `clean::inline` to tell if an item has already been inlined.
crate inlined: RefCell<FxHashSet<DefId>>,
/// Used by `calculate_doc_coverage`.
crate output_format: OutputFormat,
}

impl<'tcx> DocContext<'tcx> {
Expand Down Expand Up @@ -464,7 +466,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 @@ -514,7 +516,6 @@ crate fn run_global_ctxt(
param_env: Cell::new(ParamEnv::empty()),
external_traits: Default::default(),
active_extern_traits: Default::default(),
renderinfo: RefCell::new(renderinfo),
ty_substs: Default::default(),
lt_substs: Default::default(),
ct_substs: Default::default(),
Expand All @@ -527,9 +528,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: RefCell::new(Cache::new(renderinfo, render_options.document_private)),
inlined: RefCell::new(FxHashSet::default()),
output_format,
render_options,
};
debug!("crate: {:?}", tcx.hir().krate());

Expand Down Expand Up @@ -634,10 +637,17 @@ crate fn run_global_ctxt(

ctxt.sess().abort_if_errors();

let render_options = ctxt.render_options;
let mut cache = ctxt.cache.into_inner();

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.into_inner(), ctxt.render_options)
(krate, render_options, cache)
}

/// Due to <https://github.com/rust-lang/rust/pull/73566>,
Expand Down
53 changes: 26 additions & 27 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,7 @@ struct CacheBuilder<'a, 'tcx> {
}

impl Cache {
crate fn from_krate<'tcx>(
render_info: RenderInfo,
document_private: bool,
extern_html_root_urls: &BTreeMap<String, String>,
dst: &Path,
mut krate: clean::Crate,
tcx: TyCtxt<'tcx>,
) -> (clean::Crate, Cache) {
crate fn new(render_info: RenderInfo, document_private: bool) -> Self {
// Crawl the crate to build various caches used for the output
let RenderInfo {
inlined: _,
Expand All @@ -153,21 +146,29 @@ impl Cache {
let external_paths =
external_paths.into_iter().map(|(k, (v, t))| (k, (v, ItemType::from(t)))).collect();

let mut cache = 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()
};
}
}

crate fn populate(
&mut self,
mut krate: clean::Crate,
tcx: TyCtxt<'_>,
extern_html_root_urls: &BTreeMap<String, String>,
dst: &Path,
) -> clean::Crate {
self.crate_version = krate.version.take();
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 @@ -180,12 +181,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 @@ -194,27 +194,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
17 changes: 3 additions & 14 deletions src/librustdoc/formats/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustc_middle::ty::TyCtxt;
use rustc_span::edition::Edition;

use crate::clean;
use crate::config::{RenderInfo, RenderOptions};
use crate::config::RenderOptions;
use crate::error::Error;
use crate::formats::cache::Cache;

Expand All @@ -18,7 +18,6 @@ crate trait FormatRenderer<'tcx>: Clone {
fn init(
krate: clean::Crate,
options: RenderOptions,
render_info: RenderInfo,
edition: Edition,
cache: Cache,
tcx: TyCtxt<'tcx>,
Expand Down Expand Up @@ -49,26 +48,16 @@ crate trait FormatRenderer<'tcx>: Clone {
crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
krate: clean::Crate,
options: RenderOptions,
render_info: RenderInfo,
cache: Cache,
diag: &rustc_errors::Handler,
edition: Edition,
tcx: TyCtxt<'tcx>,
) -> Result<(), Error> {
let (krate, cache) = tcx.sess.time("create_format_cache", || {
Cache::from_krate(
render_info.clone(),
options.document_private,
&options.extern_html_root_urls,
&options.output,
krate,
tcx,
)
});
let prof = &tcx.sess.prof;

let (mut format_renderer, mut krate) = prof
.extra_verbose_generic_activity("create_renderer", T::descr())
.run(|| T::init(krate, options, render_info, edition, cache, tcx))?;
.run(|| T::init(krate, options, edition, cache, tcx))?;

let mut item = match krate.module.take() {
Some(i) => i,
Expand Down
3 changes: 1 addition & 2 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ use serde::ser::SerializeSeq;
use serde::{Serialize, Serializer};

use crate::clean::{self, AttributesExt, GetDefId, RenderedLink, SelfTy, TypeKind};
use crate::config::{RenderInfo, RenderOptions};
use crate::config::RenderOptions;
use crate::docfs::{DocFS, PathError};
use crate::error::Error;
use crate::formats::cache::Cache;
Expand Down Expand Up @@ -385,7 +385,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
fn init(
mut krate: clean::Crate,
options: RenderOptions,
_render_info: RenderInfo,
edition: Edition,
mut cache: Cache,
tcx: TyCtxt<'tcx>,
Expand Down
3 changes: 1 addition & 2 deletions src/librustdoc/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rustc_span::edition::Edition;
use rustdoc_json_types as types;

use crate::clean;
use crate::config::{RenderInfo, RenderOptions};
use crate::config::RenderOptions;
use crate::error::Error;
use crate::formats::cache::Cache;
use crate::formats::FormatRenderer;
Expand Down Expand Up @@ -132,7 +132,6 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
fn init(
krate: clean::Crate,
options: RenderOptions,
_render_info: RenderInfo,
_edition: Edition,
cache: Cache,
tcx: TyCtxt<'tcx>,
Expand Down
Loading

0 comments on commit 991ef9a

Please sign in to comment.