Skip to content

Commit 66e30ec

Browse files
committedNov 24, 2020
Get rid of doctree::ExternCrate
1 parent 2a58fa0 commit 66e30ec

File tree

3 files changed

+49
-63
lines changed

3 files changed

+49
-63
lines changed
 

‎src/librustdoc/clean/mod.rs

+47-36
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_attr as attr;
1414
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1515
use rustc_hir as hir;
1616
use rustc_hir::def::{CtorKind, DefKind, Res};
17-
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX};
17+
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
1818
use rustc_index::vec::{Idx, IndexVec};
1919
use rustc_infer::infer::region_constraints::{Constraint, RegionConstraintData};
2020
use rustc_middle::bug;
@@ -229,7 +229,6 @@ impl Clean<Item> for doctree::Module<'_> {
229229
let attrs = self.attrs.clean(cx);
230230

231231
let mut items: Vec<Item> = vec![];
232-
items.extend(self.extern_crates.iter().flat_map(|x| x.clean(cx)));
233232
items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
234233
items.extend(self.foreigns.iter().map(|x| x.clean(cx)));
235234
items.extend(self.mods.iter().map(|x| x.clean(cx)));
@@ -2004,6 +2003,9 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {
20042003
is_auto: is_auto.clean(cx),
20052004
})
20062005
}
2006+
ItemKind::ExternCrate(orig_name) => {
2007+
return clean_extern_crate(item, name, orig_name, cx);
2008+
}
20072009
_ => unreachable!("not yet converted"),
20082010
};
20092011

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

2084-
impl Clean<Vec<Item>> for doctree::ExternCrate<'_> {
2085-
fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
2086-
let please_inline = self.vis.node.is_pub()
2087-
&& self.attrs.iter().any(|a| {
2088-
a.has_name(sym::doc)
2089-
&& match a.meta_item_list() {
2090-
Some(l) => attr::list_contains_name(&l, sym::inline),
2091-
None => false,
2092-
}
2093-
});
2086+
fn clean_extern_crate(
2087+
krate: &hir::Item<'_>,
2088+
name: Symbol,
2089+
orig_name: Option<Symbol>,
2090+
cx: &DocContext<'_>,
2091+
) -> Vec<Item> {
2092+
// this is the ID of the `extern crate` statement
2093+
let def_id = cx.tcx.hir().local_def_id(krate.hir_id);
2094+
let cnum = cx.tcx.extern_mod_stmt_cnum(def_id).unwrap_or(LOCAL_CRATE);
2095+
// this is the ID of the crate itself
2096+
let crate_def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX };
2097+
let please_inline = krate.vis.node.is_pub()
2098+
&& krate.attrs.iter().any(|a| {
2099+
a.has_name(sym::doc)
2100+
&& match a.meta_item_list() {
2101+
Some(l) => attr::list_contains_name(&l, sym::inline),
2102+
None => false,
2103+
}
2104+
});
20942105

2095-
if please_inline {
2096-
let mut visited = FxHashSet::default();
2106+
if please_inline {
2107+
let mut visited = FxHashSet::default();
20972108

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

2100-
if let Some(items) = inline::try_inline(
2101-
cx,
2102-
cx.tcx.parent_module(self.hir_id).to_def_id(),
2103-
res,
2104-
self.name,
2105-
Some(self.attrs),
2106-
&mut visited,
2107-
) {
2108-
return items;
2109-
}
2111+
if let Some(items) = inline::try_inline(
2112+
cx,
2113+
cx.tcx.parent_module(krate.hir_id).to_def_id(),
2114+
res,
2115+
name,
2116+
Some(krate.attrs),
2117+
&mut visited,
2118+
) {
2119+
return items;
21102120
}
2111-
2112-
vec![Item {
2113-
name: None,
2114-
attrs: self.attrs.clean(cx),
2115-
source: self.span.clean(cx),
2116-
def_id: DefId { krate: self.cnum, index: CRATE_DEF_INDEX },
2117-
visibility: self.vis.clean(cx),
2118-
stability: None,
2119-
deprecation: None,
2120-
kind: ExternCrateItem(self.name.clean(cx), self.path.clone()),
2121-
}]
21222121
}
2122+
let path = orig_name.map(|x| x.to_string());
2123+
// FIXME: using `from_def_id_and_kind` breaks `rustdoc/masked` for some reason
2124+
vec![Item {
2125+
name: None,
2126+
attrs: krate.attrs.clean(cx),
2127+
source: krate.span.clean(cx),
2128+
def_id: crate_def_id,
2129+
visibility: krate.vis.clean(cx),
2130+
stability: None,
2131+
deprecation: None,
2132+
kind: ExternCrateItem(name.clean(cx), path),
2133+
}]
21232134
}
21242135

21252136
impl Clean<Vec<Item>> for doctree::Import<'_> {

‎src/librustdoc/doctree.rs

-14
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ use rustc_ast as ast;
66
use rustc_span::{self, symbol::Ident, Span, Symbol};
77

88
use rustc_hir as hir;
9-
use rustc_hir::def_id::CrateNum;
10-
use rustc_hir::HirId;
119

1210
crate struct Module<'hir> {
1311
crate name: Option<Symbol>,
1412
crate attrs: &'hir [ast::Attribute],
1513
crate where_outer: Span,
1614
crate where_inner: Span,
17-
crate extern_crates: Vec<ExternCrate<'hir>>,
1815
crate imports: Vec<Import<'hir>>,
1916
crate mods: Vec<Module<'hir>>,
2017
crate id: hir::HirId,
@@ -33,7 +30,6 @@ impl Module<'hir> {
3330
where_outer: rustc_span::DUMMY_SP,
3431
where_inner: rustc_span::DUMMY_SP,
3532
attrs,
36-
extern_crates: Vec::new(),
3733
imports: Vec::new(),
3834
mods: Vec::new(),
3935
items: Vec::new(),
@@ -69,16 +65,6 @@ crate struct Macro {
6965
crate imported_from: Option<Symbol>,
7066
}
7167

72-
crate struct ExternCrate<'hir> {
73-
crate name: Symbol,
74-
crate hir_id: HirId,
75-
crate cnum: CrateNum,
76-
crate path: Option<String>,
77-
crate vis: &'hir hir::Visibility<'hir>,
78-
crate attrs: &'hir [ast::Attribute],
79-
crate span: Span,
80-
}
81-
8268
#[derive(Debug)]
8369
crate struct Import<'hir> {
8470
crate name: Symbol,

‎src/librustdoc/visit_ast.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_ast as ast;
55
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
66
use rustc_hir as hir;
77
use rustc_hir::def::{DefKind, Res};
8-
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
8+
use rustc_hir::def_id::DefId;
99
use rustc_hir::Node;
1010
use rustc_middle::middle::privacy::AccessLevel;
1111
use rustc_middle::ty::TyCtxt;
@@ -248,18 +248,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
248248
// If we're inlining, skip private items.
249249
_ if self.inlining && !item.vis.node.is_pub() => {}
250250
hir::ItemKind::GlobalAsm(..) => {}
251-
hir::ItemKind::ExternCrate(orig_name) => {
252-
let def_id = self.cx.tcx.hir().local_def_id(item.hir_id);
253-
om.extern_crates.push(ExternCrate {
254-
cnum: self.cx.tcx.extern_mod_stmt_cnum(def_id).unwrap_or(LOCAL_CRATE),
255-
name: ident.name,
256-
hir_id: item.hir_id,
257-
path: orig_name.map(|x| x.to_string()),
258-
vis: &item.vis,
259-
attrs: &item.attrs,
260-
span: item.span,
261-
})
262-
}
263251
hir::ItemKind::Use(_, hir::UseKind::ListStem) => {}
264252
hir::ItemKind::Use(ref path, kind) => {
265253
let is_glob = kind == hir::UseKind::Glob;
@@ -313,6 +301,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
313301
));
314302
}
315303
hir::ItemKind::Fn(..)
304+
| hir::ItemKind::ExternCrate(..)
316305
| hir::ItemKind::Enum(..)
317306
| hir::ItemKind::Struct(..)
318307
| hir::ItemKind::Union(..)

0 commit comments

Comments
 (0)
Please sign in to comment.