Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: Cleanup ExternalCrate #86889

Merged
merged 2 commits into from
Jul 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_attr as attr;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir as hir;
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_index::vec::{Idx, IndexVec};
use rustc_infer::infer::region_constraints::{Constraint, RegionConstraintData};
use rustc_middle::middle::resolve_lifetime as rl;
Expand Down Expand Up @@ -85,12 +85,6 @@ impl<T: Clean<U>, U> Clean<Option<U>> for Option<T> {
}
}

impl Clean<ExternalCrate> for CrateNum {
fn clean(&self, _cx: &mut DocContext<'_>) -> ExternalCrate {
ExternalCrate { crate_num: *self }
}
}

impl Clean<Item> for doctree::Module<'_> {
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
let mut items: Vec<Item> = vec![];
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ crate struct Crate {
crate name: Symbol,
crate src: FileName,
crate module: Item,
crate externs: Vec<(CrateNum, ExternalCrate)>,
crate externs: Vec<ExternalCrate>,
crate primitives: ThinVec<(DefId, PrimitiveType)>,
// These are later on moved into `CACHEKEY`, leaving the map empty.
// Only here so that they can be filtered through the rustdoc passes.
Expand All @@ -133,14 +133,14 @@ crate struct TraitWithExtraInfo {
crate is_notable: bool,
}

#[derive(Clone, Debug)]
#[derive(Copy, Clone, Debug)]
crate struct ExternalCrate {
crate crate_num: CrateNum,
}

impl ExternalCrate {
#[inline]
fn def_id(&self) -> DefId {
crate fn def_id(&self) -> DefId {
DefId { krate: self.crate_num, index: CRATE_DEF_INDEX }
}

Expand Down
12 changes: 6 additions & 6 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::clean::auto_trait::AutoTraitFinder;
use crate::clean::blanket_impl::BlanketImplFinder;
use crate::clean::{
inline, Clean, Crate, Generic, GenericArg, GenericArgs, ImportSource, Item, ItemKind, Lifetime,
Path, PathSegment, PolyTrait, Primitive, PrimitiveType, ResolvedPath, Type, TypeBinding,
Visibility,
inline, Clean, Crate, ExternalCrate, Generic, GenericArg, GenericArgs, ImportSource, Item,
ItemKind, Lifetime, Path, PathSegment, PolyTrait, Primitive, PrimitiveType, ResolvedPath, Type,
TypeBinding, Visibility,
};
use crate::core::DocContext;
use crate::formats::item_type::ItemType;
Expand Down Expand Up @@ -35,11 +35,11 @@ crate fn krate(cx: &mut DocContext<'_>) -> Crate {

let mut externs = Vec::new();
for &cnum in cx.tcx.crates(()).iter() {
externs.push((cnum, cnum.clean(cx)));
externs.push(ExternalCrate { crate_num: cnum });
// Analyze doc-reachability for extern items
LibEmbargoVisitor::new(cx).visit_lib(cnum);
}
externs.sort_by(|&(a, _), &(b, _)| a.cmp(&b));
externs.sort_unstable_by_key(|e| e.crate_num);

// Clean the crate, translating the entire librustc_ast AST to one that is
// understood by rustdoc.
Expand All @@ -61,7 +61,7 @@ crate fn krate(cx: &mut DocContext<'_>) -> Crate {
_ => unreachable!(),
}

let local_crate = LOCAL_CRATE.clean(cx);
let local_crate = ExternalCrate { crate_num: LOCAL_CRATE };
let src = local_crate.src(cx.tcx);
let name = local_crate.name(cx.tcx);
let primitives = local_crate.primitives(cx.tcx);
Expand Down
9 changes: 4 additions & 5 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,18 @@ impl Cache {

// 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 &(n, ref e) in &krate.externs {
for &e in &krate.externs {
let name = e.name(tcx);
let extern_url = extern_html_root_urls.get(&*name.as_str()).map(|u| &**u);
let did = DefId { krate: n, index: CRATE_DEF_INDEX };
self.extern_locations.insert(n, e.location(extern_url, &dst, tcx));
self.external_paths.insert(did, (vec![name.to_string()], ItemType::Module));
self.extern_locations.insert(e.crate_num, e.location(extern_url, &dst, tcx));
self.external_paths.insert(e.def_id(), (vec![name.to_string()], ItemType::Module));
}

// Cache where all known primitives have their documentation located.
//
// Favor linking to as local extern as possible, so iterate all crates in
// reverse topological order.
for &(_, ref e) in krate.externs.iter().rev() {
for &e in krate.externs.iter().rev() {
for &(def_id, prim) in &e.primitives(tcx) {
self.primitive_locations.insert(prim, def_id);
}
Expand Down