Skip to content

Commit

Permalink
rustdoc: Remove Crate.externs and compute on-demand instead
Browse files Browse the repository at this point in the history
  • Loading branch information
camelid committed Nov 20, 2021
1 parent 331465a commit f41beab
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
3 changes: 1 addition & 2 deletions src/librustdoc/clean/types.rs
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
17 changes: 1 addition & 16 deletions src/librustdoc/clean/utils.rs
Expand Up @@ -24,17 +24,8 @@ 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);
}

// Clean the crate, translating the entire librustc_ast AST to one that is
// understood by rustdoc.
let mut module = module.clean(cx);
Expand Down Expand Up @@ -76,13 +67,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
19 changes: 14 additions & 5 deletions src/librustdoc/formats/cache.rs
Expand Up @@ -6,7 +6,7 @@ use rustc_middle::middle::privacy::AccessLevels;
use rustc_middle::ty::TyCtxt;
use rustc_span::symbol::sym;

use crate::clean::{self, ItemId, PrimitiveType};
use crate::clean::{self, ExternalCrate, ItemId, PrimitiveType};
use crate::config::RenderOptions;
use crate::core::DocContext;
use crate::fold::DocFolder;
Expand All @@ -15,6 +15,7 @@ use crate::formats::Impl;
use crate::html::markdown::short_markdown_summary;
use crate::html::render::cache::{get_index_search_type, ExternalLocation};
use crate::html::render::IndexItem;
use crate::visit_lib::LibEmbargoVisitor;

/// This cache is used to store information about the [`clean::Crate`] being
/// rendered in order to provide more useful documentation. This contains
Expand Down Expand Up @@ -139,19 +140,27 @@ impl Cache {
/// in `krate` due to the data being moved into the `Cache`.
crate fn populate(cx: &mut DocContext<'_>, mut krate: clean::Crate) -> clean::Crate {
let tcx = cx.tcx;
let render_options = &cx.render_options;

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

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);
}

let RenderOptions { extern_html_root_takes_precedence, output: dst, .. } =
&cx.render_options;

// 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 e in externs {
let name = e.name(tcx);
let extern_url =
render_options.extern_html_root_urls.get(&*name.as_str()).map(|u| &**u);
cx.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);
cx.cache.extern_locations.insert(e.crate_num, location);
cx.cache.external_paths.insert(e.def_id(), (vec![name.to_string()], ItemType::Module));
Expand Down

0 comments on commit f41beab

Please sign in to comment.