Skip to content

Commit f879ecc

Browse files
Make ItemKind::ExternCrate looks like hir::ItemKind::ExternCrate to make transition over hir::ItemKind simpler
1 parent 8ccc89b commit f879ecc

File tree

10 files changed

+54
-43
lines changed

10 files changed

+54
-43
lines changed

src/librustdoc/clean/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2129,12 +2129,12 @@ fn clean_extern_crate(
21292129
}
21302130
// FIXME: using `from_def_id_and_kind` breaks `rustdoc/masked` for some reason
21312131
vec![Item {
2132-
name: None,
2132+
name: Some(name),
21332133
attrs: box krate.attrs.clean(cx),
21342134
source: krate.span.clean(cx),
21352135
def_id: crate_def_id,
21362136
visibility: krate.vis.clean(cx),
2137-
kind: box ExternCrateItem(name, orig_name),
2137+
kind: box ExternCrateItem { src: orig_name },
21382138
}]
21392139
}
21402140

src/librustdoc/clean/types.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,10 @@ impl Item {
323323

324324
#[derive(Clone, Debug)]
325325
crate enum ItemKind {
326-
ExternCrateItem(Symbol, Option<Symbol>),
326+
ExternCrateItem {
327+
/// The crate's name, *not* the name it's imported as.
328+
src: Option<Symbol>,
329+
},
327330
ImportItem(Import),
328331
StructItem(Struct),
329332
UnionItem(Union),
@@ -376,7 +379,7 @@ impl ItemKind {
376379
TraitItem(t) => t.items.iter(),
377380
ImplItem(i) => i.items.iter(),
378381
ModuleItem(m) => m.items.iter(),
379-
ExternCrateItem(_, _)
382+
ExternCrateItem { .. }
380383
| ImportItem(_)
381384
| FunctionItem(_)
382385
| TypedefItem(_, _)

src/librustdoc/formats/item_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'a> From<&'a clean::Item> for ItemType {
6767

6868
match *kind {
6969
clean::ModuleItem(..) => ItemType::Module,
70-
clean::ExternCrateItem(..) => ItemType::ExternCrate,
70+
clean::ExternCrateItem { .. } => ItemType::ExternCrate,
7171
clean::ImportItem(..) => ItemType::Import,
7272
clean::StructItem(..) => ItemType::Struct,
7373
clean::UnionItem(..) => ItemType::Union,

src/librustdoc/formats/renderer.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
9191
}
9292

9393
cx.mod_item_out(&name)?;
94-
} else if item.name.is_some() {
94+
// FIXME: checking `item.name.is_some()` is very implicit and leads to lots of special
95+
// cases. Use an explicit match instead.
96+
} else if item.name.is_some() && !item.is_extern_crate() {
9597
prof.generic_activity_with_arg("render_item", &*item.name.unwrap_or(unknown).as_str())
9698
.run(|| cx.item(item))?;
9799
}

src/librustdoc/html/render/print_item.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
240240
}
241241

242242
match *myitem.kind {
243-
clean::ExternCrateItem(ref name, ref src) => {
243+
clean::ExternCrateItem { ref src } => {
244244
use crate::html::format::anchor;
245245

246246
match *src {
@@ -249,13 +249,13 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
249249
"<tr><td><code>{}extern crate {} as {};",
250250
myitem.visibility.print_with_space(cx.tcx(), myitem.def_id, cx.cache()),
251251
anchor(myitem.def_id, &*src.as_str(), cx.cache()),
252-
name
252+
myitem.name.as_ref().unwrap(),
253253
),
254254
None => write!(
255255
w,
256256
"<tr><td><code>{}extern crate {};",
257257
myitem.visibility.print_with_space(cx.tcx(), myitem.def_id, cx.cache()),
258-
anchor(myitem.def_id, &*name.as_str(), cx.cache())
258+
anchor(myitem.def_id, &*myitem.name.as_ref().unwrap().as_str(), cx.cache()),
259259
),
260260
}
261261
w.write_str("</code></td></tr>");

src/librustdoc/json/conversions.rs

+33-29
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_ast::ast;
1010
use rustc_hir::def::CtorKind;
1111
use rustc_middle::ty::TyCtxt;
1212
use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
13+
use rustc_span::symbol::Symbol;
1314
use rustc_span::Pos;
1415

1516
use rustdoc_json_types::*;
@@ -25,32 +26,37 @@ impl JsonRenderer<'_> {
2526
let item_type = ItemType::from(&item);
2627
let deprecation = item.deprecation(self.tcx);
2728
let clean::Item { source, name, attrs, kind, visibility, def_id } = item;
28-
match *kind {
29-
clean::StrippedItem(_) => None,
30-
kind => Some(Item {
31-
id: from_def_id(def_id),
32-
crate_id: def_id.krate.as_u32(),
33-
name: name.map(|sym| sym.to_string()),
34-
source: self.convert_span(source),
35-
visibility: self.convert_visibility(visibility),
36-
docs: attrs.collapsed_doc_value(),
37-
links: attrs
38-
.links
39-
.into_iter()
40-
.filter_map(|clean::ItemLink { link, did, .. }| {
41-
did.map(|did| (link, from_def_id(did)))
42-
})
43-
.collect(),
44-
attrs: attrs
45-
.other_attrs
46-
.iter()
47-
.map(rustc_ast_pretty::pprust::attribute_to_string)
48-
.collect(),
49-
deprecation: deprecation.map(from_deprecation),
50-
kind: item_type.into(),
51-
inner: from_clean_item_kind(kind, self.tcx),
52-
}),
53-
}
29+
let inner = match *kind {
30+
clean::ItemKind::ExternCrateItem { ref src } => ItemEnum::ExternCrateItem {
31+
name: name.as_ref().unwrap().to_string(),
32+
rename: src.map(|x| x.to_string()),
33+
},
34+
clean::StrippedItem(_) => return None,
35+
x => from_clean_item_kind(x, self.tcx),
36+
};
37+
Some(Item {
38+
id: from_def_id(def_id),
39+
crate_id: def_id.krate.as_u32(),
40+
name: name.map(|sym| sym.to_string()),
41+
source: self.convert_span(source),
42+
visibility: self.convert_visibility(visibility),
43+
docs: attrs.collapsed_doc_value(),
44+
links: attrs
45+
.links
46+
.into_iter()
47+
.filter_map(|clean::ItemLink { link, did, .. }| {
48+
did.map(|did| (link, from_def_id(did)))
49+
})
50+
.collect(),
51+
attrs: attrs
52+
.other_attrs
53+
.iter()
54+
.map(rustc_ast_pretty::pprust::attribute_to_string)
55+
.collect(),
56+
deprecation: deprecation.map(from_deprecation),
57+
kind: item_type.into(),
58+
inner,
59+
})
5460
}
5561

5662
fn convert_span(&self, span: clean::Span) -> Option<Span> {
@@ -153,9 +159,6 @@ fn from_clean_item_kind(item: clean::ItemKind, tcx: TyCtxt<'_>) -> ItemEnum {
153159
use clean::ItemKind::*;
154160
match item {
155161
ModuleItem(m) => ItemEnum::ModuleItem(m.into()),
156-
ExternCrateItem(c, a) => {
157-
ItemEnum::ExternCrateItem { name: c.to_string(), rename: a.map(|x| x.to_string()) }
158-
}
159162
ImportItem(i) => ItemEnum::ImportItem(i.into()),
160163
StructItem(s) => ItemEnum::StructItem(s.into()),
161164
UnionItem(u) => ItemEnum::UnionItem(u.into()),
@@ -186,6 +189,7 @@ fn from_clean_item_kind(item: clean::ItemKind, tcx: TyCtxt<'_>) -> ItemEnum {
186189
PrimitiveItem(_) | KeywordItem(_) => {
187190
panic!("{:?} is not supported for JSON output", item)
188191
}
192+
ExternCrateItem { .. } => unreachable!(),
189193
}
190194
}
191195

src/librustdoc/json/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
183183
match &*item.kind {
184184
// These don't have names so they don't get added to the output by default
185185
ImportItem(_) => self.item(item.clone()).unwrap(),
186-
ExternCrateItem(_, _) => self.item(item.clone()).unwrap(),
186+
ExternCrateItem { .. } => self.item(item.clone()).unwrap(),
187187
ImplItem(i) => i.items.iter().for_each(|i| self.item(i.clone()).unwrap()),
188188
_ => {}
189189
}

src/librustdoc/passes/calculate_doc_coverage.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> {
193193
// don't count items in stripped modules
194194
return Some(i);
195195
}
196-
clean::ImportItem(..) | clean::ExternCrateItem(..) => {
196+
clean::ImportItem(..) | clean::ExternCrateItem { .. } => {
197197
// docs on `use` and `extern crate` statements are not displayed, so they're not
198198
// worth counting
199199
return Some(i);

src/librustdoc/passes/doc_test_lints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ crate fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> boo
6363
| clean::TypedefItem(_, _)
6464
| clean::StaticItem(_)
6565
| clean::ConstantItem(_)
66-
| clean::ExternCrateItem(_, _)
66+
| clean::ExternCrateItem { .. }
6767
| clean::ImportItem(_)
6868
| clean::PrimitiveItem(_)
6969
| clean::KeywordItem(_)

src/librustdoc/passes/stripper.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl<'a> DocFolder for Stripper<'a> {
6666
}
6767

6868
// handled in the `strip-priv-imports` pass
69-
clean::ExternCrateItem(..) | clean::ImportItem(..) => {}
69+
clean::ExternCrateItem { .. } | clean::ImportItem(..) => {}
7070

7171
clean::ImplItem(..) => {}
7272

@@ -161,7 +161,9 @@ crate struct ImportStripper;
161161
impl DocFolder for ImportStripper {
162162
fn fold_item(&mut self, i: Item) -> Option<Item> {
163163
match *i.kind {
164-
clean::ExternCrateItem(..) | clean::ImportItem(..) if !i.visibility.is_public() => None,
164+
clean::ExternCrateItem { .. } | clean::ImportItem(..) if !i.visibility.is_public() => {
165+
None
166+
}
165167
_ => Some(self.fold_item_recur(i)),
166168
}
167169
}

0 commit comments

Comments
 (0)