Skip to content

Commit 92861c7

Browse files
Rollup merge of #80845 - GuillaumeGomez:item-kind-transition, r=jyn514
Make ItemKind::ExternCrate looks like hir::ItemKind::ExternCrate to make transition over hir::ItemKind simpler It was surprisingly difficult to make this change, mostly because of two issues: * We now store the `ExternCrate` name in the parent struct (`clean::Item`), which forced me to modify the json conversion code a bit more than expected. * The second problem was that, since we now have a `Some(name)`, it was trying to render it, ending up in a panic because we ended up in a `unreachable` statement. The solution was simply to add `!item.is_extern_crate()` in `formats::renderer` before calling `cx.item(item, &cache)?;`. I'll continue to replace all the `clean::ItemKind` variants one by one until it looks exactly like `hir::ItemKind`. Then we'll simply discard the rustdoc type. Once this done, we'll be able to discard `clean::Item` too to use `hir::Item`. r? ``@jyn514``
2 parents 8fd946c + 286a357 commit 92861c7

File tree

10 files changed

+55
-45
lines changed

10 files changed

+55
-45
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

+34-31
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,33 @@ 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::StrippedItem(_) => return None,
31+
x => from_clean_item_kind(x, self.tcx, &name),
32+
};
33+
Some(Item {
34+
id: from_def_id(def_id),
35+
crate_id: def_id.krate.as_u32(),
36+
name: name.map(|sym| sym.to_string()),
37+
source: self.convert_span(source),
38+
visibility: self.convert_visibility(visibility),
39+
docs: attrs.collapsed_doc_value(),
40+
links: attrs
41+
.links
42+
.into_iter()
43+
.filter_map(|clean::ItemLink { link, did, .. }| {
44+
did.map(|did| (link, from_def_id(did)))
45+
})
46+
.collect(),
47+
attrs: attrs
48+
.other_attrs
49+
.iter()
50+
.map(rustc_ast_pretty::pprust::attribute_to_string)
51+
.collect(),
52+
deprecation: deprecation.map(from_deprecation),
53+
kind: item_type.into(),
54+
inner,
55+
})
5456
}
5557

5658
fn convert_span(&self, span: clean::Span) -> Option<Span> {
@@ -149,13 +151,10 @@ crate fn from_def_id(did: DefId) -> Id {
149151
Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index)))
150152
}
151153

152-
fn from_clean_item_kind(item: clean::ItemKind, tcx: TyCtxt<'_>) -> ItemEnum {
154+
fn from_clean_item_kind(item: clean::ItemKind, tcx: TyCtxt<'_>, name: &Option<Symbol>) -> ItemEnum {
153155
use clean::ItemKind::*;
154156
match item {
155157
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-
}
159158
ImportItem(i) => ItemEnum::ImportItem(i.into()),
160159
StructItem(s) => ItemEnum::StructItem(s.into()),
161160
UnionItem(u) => ItemEnum::UnionItem(u.into()),
@@ -182,10 +181,14 @@ fn from_clean_item_kind(item: clean::ItemKind, tcx: TyCtxt<'_>) -> ItemEnum {
182181
bounds: g.into_iter().map(Into::into).collect(),
183182
default: t.map(Into::into),
184183
},
185-
StrippedItem(inner) => from_clean_item_kind(*inner, tcx),
184+
StrippedItem(inner) => from_clean_item_kind(*inner, tcx, name),
186185
PrimitiveItem(_) | KeywordItem(_) => {
187186
panic!("{:?} is not supported for JSON output", item)
188187
}
188+
ExternCrateItem { ref src } => ItemEnum::ExternCrateItem {
189+
name: name.as_ref().unwrap().to_string(),
190+
rename: src.map(|x| x.to_string()),
191+
},
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
@@ -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)