Skip to content

Commit 4c70372

Browse files
Make ItemKind::ExternCrate looks like hir::ItemKind::ExternCrate to make transition over hir::ItemKind simpler
1 parent 15598a8 commit 4c70372

File tree

10 files changed

+53
-43
lines changed

10 files changed

+53
-43
lines changed

src/librustdoc/clean/mod.rs

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

src/librustdoc/clean/types.rs

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

302302
#[derive(Clone, Debug)]
303303
crate enum ItemKind {
304-
ExternCrateItem(Symbol, Option<Symbol>),
304+
ExternCrateItem {
305+
/// The crate's name, *not* the name it's imported as.
306+
src: Option<Symbol>,
307+
},
305308
ImportItem(Import),
306309
StructItem(Struct),
307310
UnionItem(Union),
@@ -354,7 +357,7 @@ impl ItemKind {
354357
TraitItem(t) => t.items.iter(),
355358
ImplItem(i) => i.items.iter(),
356359
ModuleItem(m) => m.items.iter(),
357-
ExternCrateItem(_, _)
360+
ExternCrateItem { .. }
358361
| ImportItem(_)
359362
| FunctionItem(_)
360363
| 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
@@ -102,7 +102,9 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
102102
}
103103

104104
cx.mod_item_out(&name)?;
105-
} else if item.name.is_some() {
105+
// FIXME: checking `item.name.is_some()` is very implicit and leads to lots of special
106+
// cases. Use an explicit match instead.
107+
} else if item.name.is_some() && !item.is_extern_crate() {
106108
prof.generic_activity_with_arg("render_item", &*item.name.unwrap_or(unknown).as_str())
107109
.run(|| cx.item(item))?;
108110
}

src/librustdoc/html/render/mod.rs

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

21532153
match *myitem.kind {
2154-
clean::ExternCrateItem(ref name, ref src) => {
2154+
clean::ExternCrateItem { ref src } => {
21552155
use crate::html::format::anchor;
21562156

21572157
match *src {
@@ -2160,13 +2160,13 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
21602160
"<tr><td><code>{}extern crate {} as {};",
21612161
myitem.visibility.print_with_space(cx.tcx(), myitem.def_id, cx.cache()),
21622162
anchor(myitem.def_id, &*src.as_str(), cx.cache()),
2163-
name
2163+
myitem.name.as_ref().unwrap(),
21642164
),
21652165
None => write!(
21662166
w,
21672167
"<tr><td><code>{}extern crate {};",
21682168
myitem.visibility.print_with_space(cx.tcx(), myitem.def_id, cx.cache()),
2169-
anchor(myitem.def_id, &*name.as_str(), cx.cache())
2169+
anchor(myitem.def_id, &*myitem.name.as_ref().unwrap().as_str(), cx.cache()),
21702170
),
21712171
}
21722172
w.write_str("</code></td></tr>");

src/librustdoc/json/conversions.rs

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

5661
fn convert_span(&self, span: clean::Span) -> Option<Span> {
@@ -153,9 +158,6 @@ fn from_clean_item_kind(item: clean::ItemKind, tcx: TyCtxt<'_>) -> ItemEnum {
153158
use clean::ItemKind::*;
154159
match item {
155160
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-
}
159161
ImportItem(i) => ItemEnum::ImportItem(i.into()),
160162
StructItem(s) => ItemEnum::StructItem(s.into()),
161163
UnionItem(u) => ItemEnum::UnionItem(u.into()),
@@ -186,6 +188,7 @@ fn from_clean_item_kind(item: clean::ItemKind, tcx: TyCtxt<'_>) -> ItemEnum {
186188
PrimitiveItem(_) | KeywordItem(_) => {
187189
panic!("{:?} is not supported for JSON output", item)
188190
}
191+
ExternCrateItem { .. } => unreachable!(),
189192
}
190193
}
191194

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
@@ -60,7 +60,7 @@ crate fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> boo
6060
| clean::TypedefItem(_, _)
6161
| clean::StaticItem(_)
6262
| clean::ConstantItem(_)
63-
| clean::ExternCrateItem(_, _)
63+
| clean::ExternCrateItem { .. }
6464
| clean::ImportItem(_)
6565
| clean::PrimitiveItem(_)
6666
| 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)