Skip to content

Commit 0459aca

Browse files
committed
Get rid of some doctree items
They can be derived directly from the `hir::Item`, there's no special logic. - TypeDef - OpaqueTy - Constant - Static - TraitAlias - Enum - Union - Struct
1 parent 593fe97 commit 0459aca

File tree

3 files changed

+71
-281
lines changed

3 files changed

+71
-281
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 60 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -231,21 +231,14 @@ impl Clean<Item> for doctree::Module<'_> {
231231
let mut items: Vec<Item> = vec![];
232232
items.extend(self.extern_crates.iter().flat_map(|x| x.clean(cx)));
233233
items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
234-
items.extend(self.structs.iter().map(|x| x.clean(cx)));
235-
items.extend(self.unions.iter().map(|x| x.clean(cx)));
236-
items.extend(self.enums.iter().map(|x| x.clean(cx)));
237234
items.extend(self.fns.iter().map(|x| x.clean(cx)));
238235
items.extend(self.foreigns.iter().map(|x| x.clean(cx)));
239236
items.extend(self.mods.iter().map(|x| x.clean(cx)));
240-
items.extend(self.typedefs.iter().map(|x| x.clean(cx)));
241-
items.extend(self.opaque_tys.iter().map(|x| x.clean(cx)));
242-
items.extend(self.statics.iter().map(|x| x.clean(cx)));
243-
items.extend(self.constants.iter().map(|x| x.clean(cx)));
237+
items.extend(self.items.iter().map(|x| x.clean(cx)));
244238
items.extend(self.traits.iter().map(|x| x.clean(cx)));
245239
items.extend(self.impls.iter().flat_map(|x| x.clean(cx)));
246240
items.extend(self.macros.iter().map(|x| x.clean(cx)));
247241
items.extend(self.proc_macros.iter().map(|x| x.clean(cx)));
248-
items.extend(self.trait_aliases.iter().map(|x| x.clean(cx)));
249242

250243
// determine if we should display the inner contents or
251244
// the outer `mod` item for the source code.
@@ -1020,20 +1013,6 @@ impl Clean<Item> for doctree::Trait<'_> {
10201013
}
10211014
}
10221015

1023-
impl Clean<Item> for doctree::TraitAlias<'_> {
1024-
fn clean(&self, cx: &DocContext<'_>) -> Item {
1025-
Item::from_hir_id_and_parts(
1026-
self.id,
1027-
Some(self.name),
1028-
TraitAliasItem(TraitAlias {
1029-
generics: self.generics.clean(cx),
1030-
bounds: self.bounds.clean(cx),
1031-
}),
1032-
cx,
1033-
)
1034-
}
1035-
}
1036-
10371016
impl Clean<bool> for hir::IsAuto {
10381017
fn clean(&self, _: &DocContext<'_>) -> bool {
10391018
match *self {
@@ -1777,38 +1756,6 @@ impl Clean<Visibility> for ty::Visibility {
17771756
}
17781757
}
17791758

1780-
impl Clean<Item> for doctree::Struct<'_> {
1781-
fn clean(&self, cx: &DocContext<'_>) -> Item {
1782-
Item::from_hir_id_and_parts(
1783-
self.id,
1784-
Some(self.name),
1785-
StructItem(Struct {
1786-
struct_type: self.struct_type,
1787-
generics: self.generics.clean(cx),
1788-
fields: self.fields.clean(cx),
1789-
fields_stripped: false,
1790-
}),
1791-
cx,
1792-
)
1793-
}
1794-
}
1795-
1796-
impl Clean<Item> for doctree::Union<'_> {
1797-
fn clean(&self, cx: &DocContext<'_>) -> Item {
1798-
Item::from_hir_id_and_parts(
1799-
self.id,
1800-
Some(self.name),
1801-
UnionItem(Union {
1802-
struct_type: self.struct_type,
1803-
generics: self.generics.clean(cx),
1804-
fields: self.fields.clean(cx),
1805-
fields_stripped: false,
1806-
}),
1807-
cx,
1808-
)
1809-
}
1810-
}
1811-
18121759
impl Clean<VariantStruct> for rustc_hir::VariantData<'_> {
18131760
fn clean(&self, cx: &DocContext<'_>) -> VariantStruct {
18141761
VariantStruct {
@@ -1819,21 +1766,6 @@ impl Clean<VariantStruct> for rustc_hir::VariantData<'_> {
18191766
}
18201767
}
18211768

1822-
impl Clean<Item> for doctree::Enum<'_> {
1823-
fn clean(&self, cx: &DocContext<'_>) -> Item {
1824-
Item::from_hir_id_and_parts(
1825-
self.id,
1826-
Some(self.name),
1827-
EnumItem(Enum {
1828-
variants: self.variants.iter().map(|v| v.clean(cx)).collect(),
1829-
generics: self.generics.clean(cx),
1830-
variants_stripped: false,
1831-
}),
1832-
cx,
1833-
)
1834-
}
1835-
}
1836-
18371769
impl Clean<Item> for doctree::Variant<'_> {
18381770
fn clean(&self, cx: &DocContext<'_>) -> Item {
18391771
let what_rustc_thinks = Item::from_hir_id_and_parts(
@@ -1981,33 +1913,6 @@ impl Clean<String> for Symbol {
19811913
}
19821914
}
19831915

1984-
impl Clean<Item> for doctree::Typedef<'_> {
1985-
fn clean(&self, cx: &DocContext<'_>) -> Item {
1986-
let type_ = self.ty.clean(cx);
1987-
let item_type = type_.def_id().and_then(|did| inline::build_ty(cx, did));
1988-
Item::from_hir_id_and_parts(
1989-
self.id,
1990-
Some(self.name),
1991-
TypedefItem(Typedef { type_, generics: self.gen.clean(cx), item_type }, false),
1992-
cx,
1993-
)
1994-
}
1995-
}
1996-
1997-
impl Clean<Item> for doctree::OpaqueTy<'_> {
1998-
fn clean(&self, cx: &DocContext<'_>) -> Item {
1999-
Item::from_hir_id_and_parts(
2000-
self.id,
2001-
Some(self.name),
2002-
OpaqueTyItem(OpaqueTy {
2003-
bounds: self.opaque_ty.bounds.clean(cx),
2004-
generics: self.opaque_ty.generics.clean(cx),
2005-
}),
2006-
cx,
2007-
)
2008-
}
2009-
}
2010-
20111916
impl Clean<BareFunctionDecl> for hir::BareFnTy<'_> {
20121917
fn clean(&self, cx: &DocContext<'_>) -> BareFunctionDecl {
20131918
let (generic_params, decl) = enter_impl_trait(cx, || {
@@ -2017,37 +1922,71 @@ impl Clean<BareFunctionDecl> for hir::BareFnTy<'_> {
20171922
}
20181923
}
20191924

2020-
impl Clean<Item> for doctree::Static<'_> {
1925+
impl Clean<Item> for hir::Item<'_> {
20211926
fn clean(&self, cx: &DocContext<'_>) -> Item {
2022-
debug!("cleaning static {}: {:?}", self.name.clean(cx), self);
2023-
Item::from_hir_id_and_parts(
2024-
self.id,
2025-
Some(self.name),
2026-
StaticItem(Static {
2027-
type_: self.type_.clean(cx),
2028-
mutability: self.mutability,
2029-
expr: print_const_expr(cx, self.expr),
1927+
use hir::ItemKind;
1928+
1929+
let def_id = cx.tcx.hir().local_def_id(self.hir_id).to_def_id();
1930+
let name = cx.tcx.hir().name(self.hir_id);
1931+
let kind = match self.kind {
1932+
ItemKind::Static(ty, mutability, body_id) => StaticItem(Static {
1933+
type_: ty.clean(cx),
1934+
mutability,
1935+
expr: print_const_expr(cx, body_id),
20301936
}),
2031-
cx,
2032-
)
1937+
ItemKind::Const(ty, body_id) => ConstantItem(Constant {
1938+
type_: ty.clean(cx),
1939+
expr: print_const_expr(cx, body_id),
1940+
value: print_evaluated_const(cx, def_id),
1941+
is_literal: is_literal_expr(cx, body_id.hir_id),
1942+
}),
1943+
ItemKind::OpaqueTy(ref ty) => OpaqueTyItem(OpaqueTy {
1944+
bounds: ty.bounds.clean(cx),
1945+
generics: ty.generics.clean(cx),
1946+
}),
1947+
ItemKind::TyAlias(ty, ref generics) => {
1948+
let rustdoc_ty = ty.clean(cx);
1949+
let item_type = rustdoc_ty.def_id().and_then(|did| inline::build_ty(cx, did));
1950+
TypedefItem(
1951+
Typedef { type_: rustdoc_ty, generics: generics.clean(cx), item_type },
1952+
false,
1953+
)
1954+
}
1955+
ItemKind::Enum(ref def, ref generics) => EnumItem(Enum {
1956+
variants: def.variants.iter().map(|v| v.clean(cx)).collect(),
1957+
generics: generics.clean(cx),
1958+
variants_stripped: false,
1959+
}),
1960+
ItemKind::TraitAlias(ref generics, bounds) => TraitAliasItem(TraitAlias {
1961+
generics: generics.clean(cx),
1962+
bounds: bounds.clean(cx),
1963+
}),
1964+
ItemKind::Union(ref variant_data, ref generics) => UnionItem(Union {
1965+
struct_type: doctree::struct_type_from_def(&variant_data),
1966+
generics: generics.clean(cx),
1967+
fields: variant_data.fields().clean(cx),
1968+
fields_stripped: false,
1969+
}),
1970+
ItemKind::Struct(ref variant_data, ref generics) => StructItem(Struct {
1971+
struct_type: doctree::struct_type_from_def(&variant_data),
1972+
generics: generics.clean(cx),
1973+
fields: variant_data.fields().clean(cx),
1974+
fields_stripped: false,
1975+
}),
1976+
_ => unreachable!("not yet converted"),
1977+
};
1978+
1979+
Item::from_def_id_and_parts(def_id, Some(name), kind, cx)
20331980
}
20341981
}
20351982

2036-
impl Clean<Item> for doctree::Constant<'_> {
1983+
impl Clean<Item> for hir::Variant<'_> {
20371984
fn clean(&self, cx: &DocContext<'_>) -> Item {
2038-
let def_id = cx.tcx.hir().local_def_id(self.id).to_def_id();
2039-
2040-
Item::from_def_id_and_parts(
2041-
def_id,
2042-
Some(self.name),
2043-
ConstantItem(Constant {
2044-
type_: self.type_.clean(cx),
2045-
expr: print_const_expr(cx, self.expr),
2046-
value: print_evaluated_const(cx, def_id),
2047-
is_literal: is_literal_expr(cx, self.expr.hir_id),
2048-
}),
2049-
cx,
2050-
)
1985+
let kind = VariantItem(Variant { kind: self.data.clean(cx) });
1986+
let what_rustc_thinks =
1987+
Item::from_hir_id_and_parts(self.id, Some(self.ident.name), kind, cx);
1988+
// don't show `pub` for variants, which are always public
1989+
Item { visibility: Inherited, ..what_rustc_thinks }
20511990
}
20521991
}
20531992

src/librustdoc/doctree.rs

Lines changed: 2 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,15 @@ crate struct Module<'hir> {
1717
crate where_inner: Span,
1818
crate extern_crates: Vec<ExternCrate<'hir>>,
1919
crate imports: Vec<Import<'hir>>,
20-
crate structs: Vec<Struct<'hir>>,
21-
crate unions: Vec<Union<'hir>>,
22-
crate enums: Vec<Enum<'hir>>,
2320
crate fns: Vec<Function<'hir>>,
2421
crate mods: Vec<Module<'hir>>,
2522
crate id: hir::HirId,
26-
crate typedefs: Vec<Typedef<'hir>>,
27-
crate opaque_tys: Vec<OpaqueTy<'hir>>,
28-
crate statics: Vec<Static<'hir>>,
29-
crate constants: Vec<Constant<'hir>>,
23+
crate items: Vec<&'hir hir::Item<'hir>>,
3024
crate traits: Vec<Trait<'hir>>,
3125
crate impls: Vec<Impl<'hir>>,
3226
crate foreigns: Vec<ForeignItem<'hir>>,
3327
crate macros: Vec<Macro>,
3428
crate proc_macros: Vec<ProcMacro>,
35-
crate trait_aliases: Vec<TraitAlias<'hir>>,
3629
crate is_crate: bool,
3730
}
3831

@@ -46,21 +39,14 @@ impl Module<'hir> {
4639
attrs,
4740
extern_crates: Vec::new(),
4841
imports: Vec::new(),
49-
structs: Vec::new(),
50-
unions: Vec::new(),
51-
enums: Vec::new(),
5242
fns: Vec::new(),
5343
mods: Vec::new(),
54-
typedefs: Vec::new(),
55-
opaque_tys: Vec::new(),
56-
statics: Vec::new(),
57-
constants: Vec::new(),
44+
items: Vec::new(),
5845
traits: Vec::new(),
5946
impls: Vec::new(),
6047
foreigns: Vec::new(),
6148
macros: Vec::new(),
6249
proc_macros: Vec::new(),
63-
trait_aliases: Vec::new(),
6450
is_crate: false,
6551
}
6652
}
@@ -76,29 +62,6 @@ crate enum StructType {
7662
Unit,
7763
}
7864

79-
crate struct Struct<'hir> {
80-
crate id: hir::HirId,
81-
crate struct_type: StructType,
82-
crate name: Symbol,
83-
crate generics: &'hir hir::Generics<'hir>,
84-
crate fields: &'hir [hir::StructField<'hir>],
85-
}
86-
87-
crate struct Union<'hir> {
88-
crate id: hir::HirId,
89-
crate struct_type: StructType,
90-
crate name: Symbol,
91-
crate generics: &'hir hir::Generics<'hir>,
92-
crate fields: &'hir [hir::StructField<'hir>],
93-
}
94-
95-
crate struct Enum<'hir> {
96-
crate variants: Vec<Variant<'hir>>,
97-
crate generics: &'hir hir::Generics<'hir>,
98-
crate id: hir::HirId,
99-
crate name: Symbol,
100-
}
101-
10265
crate struct Variant<'hir> {
10366
crate name: Symbol,
10467
crate id: hir::HirId,
@@ -114,38 +77,6 @@ crate struct Function<'hir> {
11477
crate body: hir::BodyId,
11578
}
11679

117-
crate struct Typedef<'hir> {
118-
crate ty: &'hir hir::Ty<'hir>,
119-
crate gen: &'hir hir::Generics<'hir>,
120-
crate name: Symbol,
121-
crate id: hir::HirId,
122-
}
123-
124-
crate struct OpaqueTy<'hir> {
125-
crate opaque_ty: &'hir hir::OpaqueTy<'hir>,
126-
crate name: Symbol,
127-
crate id: hir::HirId,
128-
}
129-
130-
#[derive(Debug)]
131-
crate struct Static<'hir> {
132-
crate type_: &'hir hir::Ty<'hir>,
133-
crate mutability: hir::Mutability,
134-
crate expr: hir::BodyId,
135-
crate name: Symbol,
136-
crate attrs: &'hir [ast::Attribute],
137-
crate vis: &'hir hir::Visibility<'hir>,
138-
crate id: hir::HirId,
139-
crate span: Span,
140-
}
141-
142-
crate struct Constant<'hir> {
143-
crate type_: &'hir hir::Ty<'hir>,
144-
crate expr: hir::BodyId,
145-
crate name: Symbol,
146-
crate id: hir::HirId,
147-
}
148-
14980
crate struct Trait<'hir> {
15081
crate is_auto: hir::IsAuto,
15182
crate unsafety: hir::Unsafety,
@@ -157,13 +88,6 @@ crate struct Trait<'hir> {
15788
crate id: hir::HirId,
15889
}
15990

160-
crate struct TraitAlias<'hir> {
161-
crate name: Symbol,
162-
crate generics: &'hir hir::Generics<'hir>,
163-
crate bounds: &'hir [hir::GenericBound<'hir>],
164-
crate id: hir::HirId,
165-
}
166-
16791
#[derive(Debug)]
16892
crate struct Impl<'hir> {
16993
crate unsafety: hir::Unsafety,

0 commit comments

Comments
 (0)