Skip to content

Commit 42b2043

Browse files
committed
[WIP] fill out ExternCrate
1 parent 341ae70 commit 42b2043

File tree

2 files changed

+63
-66
lines changed

2 files changed

+63
-66
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -232,25 +232,31 @@ impl Clean<ExternalCrate> for CrateNum {
232232
impl Clean<ItemEnum> for hir::ItemKind<'_> {
233233
fn clean(&self, _cx: &DocContext<'_>) -> ItemEnum {
234234
match self {
235-
ExternalCrate(name) =>
235+
ExternalCrate(name) =>
236236
}
237237
}
238238
}
239239
*/
240240

241-
impl Clean<Item> for hir::Item<'_> {
242-
fn clean(&self, cx: &DocContext<'_>) -> Item {
241+
enum CleanedModule {
242+
Module(ItemKind),
243+
Inlined(Vec<Item>),
244+
}
245+
246+
impl Clean<Vec<Item>> for hir::Item<'_> {
247+
fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
243248
use hir::ItemKind;
244249

245250
let def_id = cx.tcx.hir().local_def_id(self.hir_id).to_def_id();
246251
let name = cx.tcx.item_name(def_id).clean(cx);
247-
let inner = match self.kind {
252+
let kind = match self.kind {
248253
// TODO: should store Symbol, not String
249-
ItemKind::ExternCrate(renamed) => ExternCrateItem(name.clone(), renamed.clean(cx)),
250-
ItemKind::Use(path, kind) => {
251-
unimplemented!()
252-
}
253-
/*
254+
ItemKind::ExternCrate(renamed) => match clean_extern_crate(self, renamed, cx) {
255+
CleanedModule::Module(inner) => inner,
256+
CleanedModule::Inlined(items) => return items,
257+
},
258+
ItemKind::Use(path, kind) => unimplemented!(),
259+
/*
254260
ImportItem(Import {
255261
kind: kind.clean(cx),
256262
source: path.clean(cx),
@@ -259,16 +265,16 @@ impl Clean<Item> for hir::Item<'_> {
259265
_ => unimplemented!(),
260266
};
261267

262-
Item {
268+
vec![Item {
263269
def_id,
264-
inner,
270+
kind,
265271
name: Some(name),
266272
source: cx.tcx.def_span(def_id).clean(cx),
267273
attrs: self.attrs.clean(cx), // should this use tcx.attrs instead?
268274
visibility: self.vis.clean(cx), // TODO: use tcx.visibility once #78077 lands
269275
stability: cx.tcx.lookup_stability(def_id).copied(),
270276
deprecation: cx.tcx.lookup_deprecation(def_id).clean(cx),
271-
}
277+
}]
272278
}
273279
}
274280

@@ -279,17 +285,22 @@ impl Clean<Item> for hir::Crate<'_> {
279285
let attrs = self.item.attrs.clean(cx);
280286

281287
// Get _all_ the items!
282-
let mut items = self.items.clean(cx);
283-
items.extend(self.exported_macros.clean(cx));
284-
items.extend(self.trait_items.clean(cx));
285-
items.extend(self.impl_items.clean(cx));
288+
let items = self.items.clean(cx).into_iter().flatten();
289+
let items = items.chain(self.exported_macros.clean(cx));
290+
let items = items.chain(self.trait_items.clean(cx));
291+
let items = items.chain(self.impl_items.clean(cx));
286292
// NOTE: bodies intentionally skipped
287293

288-
items.extend(self.trait_impls.iter().flat_map(|(_trait, impls)| {
289-
impls.into_iter().map(|&impl_| cx.tcx.hir().item(impl_).clean(cx))
290-
}));
291-
items.extend(self.modules.clean(cx).into_iter().flatten());
292-
items.extend(self.proc_macros.iter().map(|hir_id| {
294+
let items = items.chain(
295+
self.trait_impls
296+
.iter()
297+
.flat_map(|(_trait, impls)| {
298+
impls.into_iter().map(|&impl_| cx.tcx.hir().item(impl_).clean(cx))
299+
})
300+
.flatten(),
301+
);
302+
let items = items.chain(self.modules.clean(cx).into_iter().flatten());
303+
let items = items.chain(self.proc_macros.iter().map(|hir_id| {
293304
let _def_id = hir_id.owner.local_def_index;
294305
// TODO: look how `rustc_metadata::rmeta::encoder` does this
295306
unimplemented!()
@@ -320,7 +331,7 @@ impl Clean<Item> for hir::Crate<'_> {
320331
stability: cx.stability(id),
321332
deprecation: cx.deprecation(id).clean(cx),
322333
def_id: cx.tcx.hir().local_def_id(id).to_def_id(),
323-
kind: ModuleItem(Module { is_crate: true, items }),
334+
kind: ModuleItem(Module { is_crate: true, items: items.collect() }),
324335
}
325336
}
326337
}
@@ -2226,45 +2237,40 @@ impl Clean<Vec<Item>> for doctree::Impl<'_> {
22262237
}
22272238
}
22282239

2229-
impl Clean<Vec<Item>> for doctree::ExternCrate<'_> {
2230-
fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
2231-
let please_inline = self.vis.node.is_pub()
2232-
&& self.attrs.iter().any(|a| {
2233-
a.has_name(sym::doc)
2234-
&& match a.meta_item_list() {
2235-
Some(l) => attr::list_contains_name(&l, sym::inline),
2236-
None => false,
2237-
}
2238-
});
2240+
fn clean_extern_crate(
2241+
item: &hir::Item<'_>,
2242+
renamed: Option<Symbol>,
2243+
cx: &DocContext<'_>,
2244+
) -> CleanedModule {
2245+
let please_inline = item.vis.node.is_pub()
2246+
&& item.attrs.iter().any(|a| {
2247+
a.has_name(sym::doc)
2248+
&& match a.meta_item_list() {
2249+
Some(l) => attr::list_contains_name(&l, sym::inline),
2250+
None => false,
2251+
}
2252+
});
2253+
let def_id = cx.tcx.hir().local_def_id(item.hir_id).to_def_id();
2254+
let name = cx.tcx.item_name(def_id);
22392255

2240-
if please_inline {
2241-
let mut visited = FxHashSet::default();
2256+
if please_inline {
2257+
let mut visited = FxHashSet::default();
22422258

2243-
let res = Res::Def(DefKind::Mod, DefId { krate: self.cnum, index: CRATE_DEF_INDEX });
2259+
let res = Res::Def(DefKind::Mod, def_id);
22442260

2245-
if let Some(items) = inline::try_inline(
2246-
cx,
2247-
cx.tcx.parent_module(self.hir_id).to_def_id(),
2248-
res,
2249-
self.name,
2250-
Some(self.attrs),
2251-
&mut visited,
2252-
) {
2253-
return items;
2254-
}
2261+
if let Some(items) = inline::try_inline(
2262+
cx,
2263+
cx.tcx.parent_module(item.hir_id).to_def_id(),
2264+
res,
2265+
name,
2266+
Some(item.attrs),
2267+
&mut visited,
2268+
) {
2269+
return CleanedModule::Inlined(items);
22552270
}
2256-
2257-
vec![Item {
2258-
name: None,
2259-
attrs: self.attrs.clean(cx),
2260-
source: self.span.clean(cx),
2261-
def_id: DefId { krate: self.cnum, index: CRATE_DEF_INDEX },
2262-
visibility: self.vis.clean(cx),
2263-
stability: None,
2264-
deprecation: None,
2265-
kind: ExternCrateItem(self.name.clean(cx), self.path.clone()),
2266-
}]
22672271
}
2272+
2273+
CleanedModule::Module(ExternCrateItem(name.clean(cx), renamed.clean(cx)))
22682274
}
22692275

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

src/librustdoc/doctree.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use rustc_span::hygiene::MacroKind;
77
use rustc_span::{self, Span, Symbol};
88

99
use rustc_hir as hir;
10-
use rustc_hir::def_id::CrateNum;
11-
use rustc_hir::HirId;
1210

1311
#[derive(Debug, Clone, Copy)]
1412
crate enum StructType {
@@ -125,17 +123,10 @@ crate struct Impl<'hir> {
125123
}
126124

127125
crate struct ForeignItem<'hir> {
126+
crate vis: &'hir hir::Visibility<'hir>,
128127
crate id: hir::HirId,
129128
crate name: Symbol,
130129
crate kind: &'hir hir::ForeignItemKind<'hir>,
131-
}
132-
133-
crate struct ExternCrate<'hir> {
134-
crate name: Symbol,
135-
crate hir_id: HirId,
136-
crate cnum: CrateNum,
137-
crate path: Option<String>,
138-
crate vis: &'hir hir::Visibility<'hir>,
139130
crate attrs: &'hir [ast::Attribute],
140131
crate span: Span,
141132
}

0 commit comments

Comments
 (0)