Skip to content

Commit 93542a8

Browse files
committed
Auto merge of #91066 - camelid:externs, r=jyn514,GuillaumeGomez
rustdoc: Remove `Crate.externs` and compute on-demand instead r? `@GuillaumeGomez` cc `@jyn514`
2 parents 3d78974 + bbc3825 commit 93542a8

File tree

4 files changed

+28
-39
lines changed

4 files changed

+28
-39
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

+2-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::clean::{
77
};
88
use crate::core::DocContext;
99
use crate::formats::item_type::ItemType;
10+
use crate::visit_lib::LibEmbargoVisitor;
1011

1112
use rustc_ast as ast;
1213
use rustc_ast::tokenstream::TokenTree;
@@ -24,13 +25,9 @@ use std::mem;
2425
mod tests;
2526

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

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

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

8879
fn external_generic_args(

src/librustdoc/core.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -508,14 +508,12 @@ crate fn run_global_ctxt(
508508
rustc_errors::FatalError.raise();
509509
}
510510

511-
let render_options = ctxt.render_options;
512-
let mut cache = ctxt.cache;
513-
krate = tcx.sess.time("create_format_cache", || cache.populate(krate, tcx, &render_options));
511+
krate = tcx.sess.time("create_format_cache", || Cache::populate(&mut ctxt, krate));
514512

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

518-
(krate, render_options, cache)
516+
(krate, ctxt.render_options, ctxt.cache)
519517
}
520518

521519
/// Due to <https://github.com/rust-lang/rust/pull/73566>,

src/librustdoc/formats/cache.rs

+23-22
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ 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};
10-
use crate::config::RenderOptions;
9+
use crate::clean::{self, ExternalCrate, ItemId, PrimitiveType};
10+
use crate::core::DocContext;
1111
use crate::fold::DocFolder;
1212
use crate::formats::item_type::ItemType;
1313
use crate::formats::Impl;
@@ -136,46 +136,47 @@ impl Cache {
136136

137137
/// Populates the `Cache` with more data. The returned `Crate` will be missing some data that was
138138
/// in `krate` due to the data being moved into the `Cache`.
139-
crate fn populate(
140-
&mut self,
141-
mut krate: clean::Crate,
142-
tcx: TyCtxt<'_>,
143-
render_options: &RenderOptions,
144-
) -> clean::Crate {
139+
crate fn populate(cx: &mut DocContext<'_>, mut krate: clean::Crate) -> clean::Crate {
140+
let tcx = cx.tcx;
141+
145142
// Crawl the crate to build various caches used for the output
146-
debug!(?self.crate_version);
147-
self.traits = krate.external_traits.take();
148-
let RenderOptions { extern_html_root_takes_precedence, output: dst, .. } = render_options;
143+
debug!(?cx.cache.crate_version);
144+
cx.cache.traits = krate.external_traits.take();
149145

150146
// Cache where all our extern crates are located
151147
// FIXME: this part is specific to HTML so it'd be nice to remove it from the common code
152-
for &e in &krate.externs {
148+
for &crate_num in cx.tcx.crates(()) {
149+
let e = ExternalCrate { crate_num };
150+
153151
let name = e.name(tcx);
152+
let render_options = &cx.render_options;
154153
let extern_url =
155154
render_options.extern_html_root_urls.get(&*name.as_str()).map(|u| &**u);
156-
let location = e.location(extern_url, *extern_html_root_takes_precedence, dst, tcx);
157-
self.extern_locations.insert(e.crate_num, location);
158-
self.external_paths.insert(e.def_id(), (vec![name.to_string()], ItemType::Module));
155+
let extern_url_takes_precedence = render_options.extern_html_root_takes_precedence;
156+
let dst = &render_options.output;
157+
let location = e.location(extern_url, extern_url_takes_precedence, dst, tcx);
158+
cx.cache.extern_locations.insert(e.crate_num, location);
159+
cx.cache.external_paths.insert(e.def_id(), (vec![name.to_string()], ItemType::Module));
159160
}
160161

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

173-
krate = CacheBuilder { tcx, cache: self }.fold_crate(krate);
174+
krate = CacheBuilder { tcx, cache: &mut cx.cache }.fold_crate(krate);
174175

175-
for (trait_did, dids, impl_) in self.orphan_trait_impls.drain(..) {
176-
if self.traits.contains_key(&trait_did) {
176+
for (trait_did, dids, impl_) in cx.cache.orphan_trait_impls.drain(..) {
177+
if cx.cache.traits.contains_key(&trait_did) {
177178
for did in dids {
178-
self.impls.entry(did).or_default().push(impl_.clone());
179+
cx.cache.impls.entry(did).or_default().push(impl_.clone());
179180
}
180181
}
181182
}

0 commit comments

Comments
 (0)