Skip to content

Commit f41beab

Browse files
committed
rustdoc: Remove Crate.externs and compute on-demand instead
1 parent 331465a commit f41beab

File tree

3 files changed

+16
-23
lines changed

3 files changed

+16
-23
lines changed

src/librustdoc/clean/types.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ impl From<DefId> for ItemId {
117117
#[derive(Clone, Debug)]
118118
crate struct Crate {
119119
crate module: Item,
120-
crate externs: Vec<ExternalCrate>,
121120
crate primitives: ThinVec<(DefId, PrimitiveType)>,
122121
/// Only here so that they can be filtered through the rustdoc passes.
123122
crate external_traits: Rc<RefCell<FxHashMap<DefId, TraitWithExtraInfo>>>,
@@ -126,7 +125,7 @@ crate struct Crate {
126125

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

131130
impl Crate {
132131
crate fn name(&self, tcx: TyCtxt<'_>) -> Symbol {

src/librustdoc/clean/utils.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,8 @@ use std::mem;
2424
mod tests;
2525

2626
crate fn krate(cx: &mut DocContext<'_>) -> Crate {
27-
use crate::visit_lib::LibEmbargoVisitor;
28-
2927
let module = crate::visit_ast::RustdocVisitor::new(cx).visit();
3028

31-
let mut externs = Vec::new();
32-
for &cnum in cx.tcx.crates(()) {
33-
externs.push(ExternalCrate { crate_num: cnum });
34-
// Analyze doc-reachability for extern items
35-
LibEmbargoVisitor::new(cx).visit_lib(cnum);
36-
}
37-
3829
// Clean the crate, translating the entire librustc_ast AST to one that is
3930
// understood by rustdoc.
4031
let mut module = module.clean(cx);
@@ -76,13 +67,7 @@ crate fn krate(cx: &mut DocContext<'_>) -> Crate {
7667
}));
7768
}
7869

79-
Crate {
80-
module,
81-
externs,
82-
primitives,
83-
external_traits: cx.external_traits.clone(),
84-
collapsed: false,
85-
}
70+
Crate { module, primitives, external_traits: cx.external_traits.clone(), collapsed: false }
8671
}
8772

8873
fn external_generic_args(

src/librustdoc/formats/cache.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_middle::middle::privacy::AccessLevels;
66
use rustc_middle::ty::TyCtxt;
77
use rustc_span::symbol::sym;
88

9-
use crate::clean::{self, ItemId, PrimitiveType};
9+
use crate::clean::{self, ExternalCrate, ItemId, PrimitiveType};
1010
use crate::config::RenderOptions;
1111
use crate::core::DocContext;
1212
use crate::fold::DocFolder;
@@ -15,6 +15,7 @@ use crate::formats::Impl;
1515
use crate::html::markdown::short_markdown_summary;
1616
use crate::html::render::cache::{get_index_search_type, ExternalLocation};
1717
use crate::html::render::IndexItem;
18+
use crate::visit_lib::LibEmbargoVisitor;
1819

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

144144
// Crawl the crate to build various caches used for the output
145145
debug!(?cx.cache.crate_version);
146146
cx.cache.traits = krate.external_traits.take();
147-
let RenderOptions { extern_html_root_takes_precedence, output: dst, .. } = render_options;
147+
148+
let mut externs = Vec::new();
149+
for &cnum in cx.tcx.crates(()) {
150+
externs.push(ExternalCrate { crate_num: cnum });
151+
// Analyze doc-reachability for extern items
152+
LibEmbargoVisitor::new(cx).visit_lib(cnum);
153+
}
154+
155+
let RenderOptions { extern_html_root_takes_precedence, output: dst, .. } =
156+
&cx.render_options;
148157

149158
// Cache where all our extern crates are located
150159
// FIXME: this part is specific to HTML so it'd be nice to remove it from the common code
151-
for &e in &krate.externs {
160+
for e in externs {
152161
let name = e.name(tcx);
153162
let extern_url =
154-
render_options.extern_html_root_urls.get(&*name.as_str()).map(|u| &**u);
163+
cx.render_options.extern_html_root_urls.get(&*name.as_str()).map(|u| &**u);
155164
let location = e.location(extern_url, *extern_html_root_takes_precedence, dst, tcx);
156165
cx.cache.extern_locations.insert(e.crate_num, location);
157166
cx.cache.external_paths.insert(e.def_id(), (vec![name.to_string()], ItemType::Module));

0 commit comments

Comments
 (0)