Skip to content

Commit

Permalink
Get rid of doctree::ExternCrate
Browse files Browse the repository at this point in the history
  • Loading branch information
jyn514 committed Nov 24, 2020
1 parent 2a58fa0 commit 66e30ec
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 63 deletions.
83 changes: 47 additions & 36 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};
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_index::vec::{Idx, IndexVec};
use rustc_infer::infer::region_constraints::{Constraint, RegionConstraintData};
use rustc_middle::bug;
Expand Down Expand Up @@ -229,7 +229,6 @@ impl Clean<Item> for doctree::Module<'_> {
let attrs = self.attrs.clean(cx);

let mut items: Vec<Item> = vec![];
items.extend(self.extern_crates.iter().flat_map(|x| x.clean(cx)));
items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
items.extend(self.foreigns.iter().map(|x| x.clean(cx)));
items.extend(self.mods.iter().map(|x| x.clean(cx)));
Expand Down Expand Up @@ -2004,6 +2003,9 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {
is_auto: is_auto.clean(cx),
})
}
ItemKind::ExternCrate(orig_name) => {
return clean_extern_crate(item, name, orig_name, cx);
}
_ => unreachable!("not yet converted"),
};

Expand Down Expand Up @@ -2081,45 +2083,54 @@ fn clean_impl(impl_: &hir::Item<'_>, cx: &DocContext<'_>) -> Vec<Item> {
ret
}

impl Clean<Vec<Item>> for doctree::ExternCrate<'_> {
fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
let please_inline = self.vis.node.is_pub()
&& self.attrs.iter().any(|a| {
a.has_name(sym::doc)
&& match a.meta_item_list() {
Some(l) => attr::list_contains_name(&l, sym::inline),
None => false,
}
});
fn clean_extern_crate(
krate: &hir::Item<'_>,
name: Symbol,
orig_name: Option<Symbol>,
cx: &DocContext<'_>,
) -> Vec<Item> {
// this is the ID of the `extern crate` statement
let def_id = cx.tcx.hir().local_def_id(krate.hir_id);
let cnum = cx.tcx.extern_mod_stmt_cnum(def_id).unwrap_or(LOCAL_CRATE);
// this is the ID of the crate itself
let crate_def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX };
let please_inline = krate.vis.node.is_pub()
&& krate.attrs.iter().any(|a| {
a.has_name(sym::doc)
&& match a.meta_item_list() {
Some(l) => attr::list_contains_name(&l, sym::inline),
None => false,
}
});

if please_inline {
let mut visited = FxHashSet::default();
if please_inline {
let mut visited = FxHashSet::default();

let res = Res::Def(DefKind::Mod, DefId { krate: self.cnum, index: CRATE_DEF_INDEX });
let res = Res::Def(DefKind::Mod, crate_def_id);

if let Some(items) = inline::try_inline(
cx,
cx.tcx.parent_module(self.hir_id).to_def_id(),
res,
self.name,
Some(self.attrs),
&mut visited,
) {
return items;
}
if let Some(items) = inline::try_inline(
cx,
cx.tcx.parent_module(krate.hir_id).to_def_id(),
res,
name,
Some(krate.attrs),
&mut visited,
) {
return items;
}

vec![Item {
name: None,
attrs: self.attrs.clean(cx),
source: self.span.clean(cx),
def_id: DefId { krate: self.cnum, index: CRATE_DEF_INDEX },
visibility: self.vis.clean(cx),
stability: None,
deprecation: None,
kind: ExternCrateItem(self.name.clean(cx), self.path.clone()),
}]
}
let path = orig_name.map(|x| x.to_string());
// FIXME: using `from_def_id_and_kind` breaks `rustdoc/masked` for some reason
vec![Item {
name: None,
attrs: krate.attrs.clean(cx),
source: krate.span.clean(cx),
def_id: crate_def_id,
visibility: krate.vis.clean(cx),
stability: None,
deprecation: None,
kind: ExternCrateItem(name.clean(cx), path),
}]
}

impl Clean<Vec<Item>> for doctree::Import<'_> {
Expand Down
14 changes: 0 additions & 14 deletions src/librustdoc/doctree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ use rustc_ast as ast;
use rustc_span::{self, symbol::Ident, Span, Symbol};

use rustc_hir as hir;
use rustc_hir::def_id::CrateNum;
use rustc_hir::HirId;

crate struct Module<'hir> {
crate name: Option<Symbol>,
crate attrs: &'hir [ast::Attribute],
crate where_outer: Span,
crate where_inner: Span,
crate extern_crates: Vec<ExternCrate<'hir>>,
crate imports: Vec<Import<'hir>>,
crate mods: Vec<Module<'hir>>,
crate id: hir::HirId,
Expand All @@ -33,7 +30,6 @@ impl Module<'hir> {
where_outer: rustc_span::DUMMY_SP,
where_inner: rustc_span::DUMMY_SP,
attrs,
extern_crates: Vec::new(),
imports: Vec::new(),
mods: Vec::new(),
items: Vec::new(),
Expand Down Expand Up @@ -69,16 +65,6 @@ crate struct Macro {
crate imported_from: Option<Symbol>,
}

crate struct ExternCrate<'hir> {
crate name: Symbol,
crate hir_id: HirId,
crate cnum: CrateNum,
crate path: Option<String>,
crate vis: &'hir hir::Visibility<'hir>,
crate attrs: &'hir [ast::Attribute],
crate span: Span,
}

#[derive(Debug)]
crate struct Import<'hir> {
crate name: Symbol,
Expand Down
15 changes: 2 additions & 13 deletions src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_ast as ast;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_hir::def_id::DefId;
use rustc_hir::Node;
use rustc_middle::middle::privacy::AccessLevel;
use rustc_middle::ty::TyCtxt;
Expand Down Expand Up @@ -248,18 +248,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
// If we're inlining, skip private items.
_ if self.inlining && !item.vis.node.is_pub() => {}
hir::ItemKind::GlobalAsm(..) => {}
hir::ItemKind::ExternCrate(orig_name) => {
let def_id = self.cx.tcx.hir().local_def_id(item.hir_id);
om.extern_crates.push(ExternCrate {
cnum: self.cx.tcx.extern_mod_stmt_cnum(def_id).unwrap_or(LOCAL_CRATE),
name: ident.name,
hir_id: item.hir_id,
path: orig_name.map(|x| x.to_string()),
vis: &item.vis,
attrs: &item.attrs,
span: item.span,
})
}
hir::ItemKind::Use(_, hir::UseKind::ListStem) => {}
hir::ItemKind::Use(ref path, kind) => {
let is_glob = kind == hir::UseKind::Glob;
Expand Down Expand Up @@ -313,6 +301,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
));
}
hir::ItemKind::Fn(..)
| hir::ItemKind::ExternCrate(..)
| hir::ItemKind::Enum(..)
| hir::ItemKind::Struct(..)
| hir::ItemKind::Union(..)
Expand Down

0 comments on commit 66e30ec

Please sign in to comment.