Skip to content

Commit

Permalink
Auto merge of #91066 - camelid:externs, r=jyn514,GuillaumeGomez
Browse files Browse the repository at this point in the history
rustdoc: Remove `Crate.externs` and compute on-demand instead

r? `@GuillaumeGomez`
cc `@jyn514`
  • Loading branch information
bors committed Nov 20, 2021
2 parents 3d78974 + bbc3825 commit 93542a8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 39 deletions.
3 changes: 1 addition & 2 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ impl From<DefId> for ItemId {
#[derive(Clone, Debug)]
crate struct Crate {
crate module: Item,
crate externs: Vec<ExternalCrate>,
crate primitives: ThinVec<(DefId, PrimitiveType)>,
/// Only here so that they can be filtered through the rustdoc passes.
crate external_traits: Rc<RefCell<FxHashMap<DefId, TraitWithExtraInfo>>>,
Expand All @@ -126,7 +125,7 @@ crate struct Crate {

// `Crate` is frequently moved by-value. Make sure it doesn't unintentionally get bigger.
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
rustc_data_structures::static_assert_size!(Crate, 104);
rustc_data_structures::static_assert_size!(Crate, 80);

impl Crate {
crate fn name(&self, tcx: TyCtxt<'_>) -> Symbol {
Expand Down
13 changes: 2 additions & 11 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::clean::{
};
use crate::core::DocContext;
use crate::formats::item_type::ItemType;
use crate::visit_lib::LibEmbargoVisitor;

use rustc_ast as ast;
use rustc_ast::tokenstream::TokenTree;
Expand All @@ -24,13 +25,9 @@ use std::mem;
mod tests;

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

let module = crate::visit_ast::RustdocVisitor::new(cx).visit();

let mut externs = Vec::new();
for &cnum in cx.tcx.crates(()) {
externs.push(ExternalCrate { crate_num: cnum });
// Analyze doc-reachability for extern items
LibEmbargoVisitor::new(cx).visit_lib(cnum);
}
Expand Down Expand Up @@ -76,13 +73,7 @@ crate fn krate(cx: &mut DocContext<'_>) -> Crate {
}));
}

Crate {
module,
externs,
primitives,
external_traits: cx.external_traits.clone(),
collapsed: false,
}
Crate { module, primitives, external_traits: cx.external_traits.clone(), collapsed: false }
}

fn external_generic_args(
Expand Down
6 changes: 2 additions & 4 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,14 +508,12 @@ crate fn run_global_ctxt(
rustc_errors::FatalError.raise();
}

let render_options = ctxt.render_options;
let mut cache = ctxt.cache;
krate = tcx.sess.time("create_format_cache", || cache.populate(krate, tcx, &render_options));
krate = tcx.sess.time("create_format_cache", || Cache::populate(&mut ctxt, krate));

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

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

/// Due to <https://github.com/rust-lang/rust/pull/73566>,
Expand Down
45 changes: 23 additions & 22 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use rustc_middle::middle::privacy::AccessLevels;
use rustc_middle::ty::TyCtxt;
use rustc_span::symbol::sym;

use crate::clean::{self, ItemId, PrimitiveType};
use crate::config::RenderOptions;
use crate::clean::{self, ExternalCrate, ItemId, PrimitiveType};
use crate::core::DocContext;
use crate::fold::DocFolder;
use crate::formats::item_type::ItemType;
use crate::formats::Impl;
Expand Down Expand Up @@ -136,46 +136,47 @@ impl Cache {

/// 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<'_>,
render_options: &RenderOptions,
) -> clean::Crate {
crate fn populate(cx: &mut DocContext<'_>, mut krate: clean::Crate) -> clean::Crate {
let tcx = cx.tcx;

// Crawl the crate to build various caches used for the output
debug!(?self.crate_version);
self.traits = krate.external_traits.take();
let RenderOptions { extern_html_root_takes_precedence, output: dst, .. } = render_options;
debug!(?cx.cache.crate_version);
cx.cache.traits = krate.external_traits.take();

// 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
for &e in &krate.externs {
for &crate_num in cx.tcx.crates(()) {
let e = ExternalCrate { crate_num };

let name = e.name(tcx);
let render_options = &cx.render_options;
let extern_url =
render_options.extern_html_root_urls.get(&*name.as_str()).map(|u| &**u);
let location = e.location(extern_url, *extern_html_root_takes_precedence, dst, tcx);
self.extern_locations.insert(e.crate_num, location);
self.external_paths.insert(e.def_id(), (vec![name.to_string()], ItemType::Module));
let extern_url_takes_precedence = render_options.extern_html_root_takes_precedence;
let dst = &render_options.output;
let location = e.location(extern_url, extern_url_takes_precedence, dst, tcx);
cx.cache.extern_locations.insert(e.crate_num, location);
cx.cache.external_paths.insert(e.def_id(), (vec![name.to_string()], ItemType::Module));
}

// FIXME: avoid this clone (requires implementing Default manually)
self.primitive_locations = PrimitiveType::primitive_locations(tcx).clone();
for (prim, &def_id) in &self.primitive_locations {
cx.cache.primitive_locations = PrimitiveType::primitive_locations(tcx).clone();
for (prim, &def_id) in &cx.cache.primitive_locations {
let crate_name = tcx.crate_name(def_id.krate);
// Recall that we only allow primitive modules to be at the root-level of the crate.
// If that restriction is ever lifted, this will have to include the relative paths instead.
self.external_paths.insert(
cx.cache.external_paths.insert(
def_id,
(vec![crate_name.to_string(), prim.as_sym().to_string()], ItemType::Primitive),
);
}

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

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

0 comments on commit 93542a8

Please sign in to comment.