Skip to content

Commit c20657c

Browse files
authored
Rollup merge of rust-lang#79264 - jyn514:less-doctree, r=GuillaumeGomez
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 Part of rust-lang#78082 (the easiest part, I'm still debugging some other changes). r? `@GuillaumeGomez`
2 parents 96ec5d2 + e280ae8 commit c20657c

8 files changed

+182
-375
lines changed

src/librustdoc/clean/mod.rs

+64-121
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,75 @@ impl Clean<BareFunctionDecl> for hir::BareFnTy<'_> {
20171922
}
20181923
}
20191924

2020-
impl Clean<Item> for doctree::Static<'_> {
1925+
impl Clean<Item> for (&hir::Item<'_>, Option<Ident>) {
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 (item, renamed) = self;
1930+
let def_id = cx.tcx.hir().local_def_id(item.hir_id).to_def_id();
1931+
let name = match renamed {
1932+
Some(ident) => ident.name,
1933+
None => cx.tcx.hir().name(item.hir_id),
1934+
};
1935+
let kind = match item.kind {
1936+
ItemKind::Static(ty, mutability, body_id) => StaticItem(Static {
1937+
type_: ty.clean(cx),
1938+
mutability,
1939+
expr: print_const_expr(cx, body_id),
20301940
}),
2031-
cx,
2032-
)
1941+
ItemKind::Const(ty, body_id) => ConstantItem(Constant {
1942+
type_: ty.clean(cx),
1943+
expr: print_const_expr(cx, body_id),
1944+
value: print_evaluated_const(cx, def_id),
1945+
is_literal: is_literal_expr(cx, body_id.hir_id),
1946+
}),
1947+
ItemKind::OpaqueTy(ref ty) => OpaqueTyItem(OpaqueTy {
1948+
bounds: ty.bounds.clean(cx),
1949+
generics: ty.generics.clean(cx),
1950+
}),
1951+
ItemKind::TyAlias(ty, ref generics) => {
1952+
let rustdoc_ty = ty.clean(cx);
1953+
let item_type = rustdoc_ty.def_id().and_then(|did| inline::build_ty(cx, did));
1954+
TypedefItem(
1955+
Typedef { type_: rustdoc_ty, generics: generics.clean(cx), item_type },
1956+
false,
1957+
)
1958+
}
1959+
ItemKind::Enum(ref def, ref generics) => EnumItem(Enum {
1960+
variants: def.variants.iter().map(|v| v.clean(cx)).collect(),
1961+
generics: generics.clean(cx),
1962+
variants_stripped: false,
1963+
}),
1964+
ItemKind::TraitAlias(ref generics, bounds) => TraitAliasItem(TraitAlias {
1965+
generics: generics.clean(cx),
1966+
bounds: bounds.clean(cx),
1967+
}),
1968+
ItemKind::Union(ref variant_data, ref generics) => UnionItem(Union {
1969+
struct_type: doctree::struct_type_from_def(&variant_data),
1970+
generics: generics.clean(cx),
1971+
fields: variant_data.fields().clean(cx),
1972+
fields_stripped: false,
1973+
}),
1974+
ItemKind::Struct(ref variant_data, ref generics) => StructItem(Struct {
1975+
struct_type: doctree::struct_type_from_def(&variant_data),
1976+
generics: generics.clean(cx),
1977+
fields: variant_data.fields().clean(cx),
1978+
fields_stripped: false,
1979+
}),
1980+
_ => unreachable!("not yet converted"),
1981+
};
1982+
1983+
Item::from_def_id_and_parts(def_id, Some(name), kind, cx)
20331984
}
20341985
}
20351986

2036-
impl Clean<Item> for doctree::Constant<'_> {
1987+
impl Clean<Item> for hir::Variant<'_> {
20371988
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-
)
1989+
let kind = VariantItem(Variant { kind: self.data.clean(cx) });
1990+
let what_rustc_thinks =
1991+
Item::from_hir_id_and_parts(self.id, Some(self.ident.name), kind, cx);
1992+
// don't show `pub` for variants, which are always public
1993+
Item { visibility: Inherited, ..what_rustc_thinks }
20511994
}
20521995
}
20531996

src/librustdoc/doctree.rs

+4-79
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ crate use self::StructType::*;
44

55
use rustc_ast as ast;
66
use rustc_span::hygiene::MacroKind;
7-
use rustc_span::{self, Span, Symbol};
7+
use rustc_span::{self, symbol::Ident, Span, Symbol};
88

99
use rustc_hir as hir;
1010
use rustc_hir::def_id::CrateNum;
@@ -17,22 +17,16 @@ 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+
// (item, renamed)
24+
crate items: Vec<(&'hir hir::Item<'hir>, Option<Ident>)>,
3025
crate traits: Vec<Trait<'hir>>,
3126
crate impls: Vec<Impl<'hir>>,
3227
crate foreigns: Vec<ForeignItem<'hir>>,
3328
crate macros: Vec<Macro>,
3429
crate proc_macros: Vec<ProcMacro>,
35-
crate trait_aliases: Vec<TraitAlias<'hir>>,
3630
crate is_crate: bool,
3731
}
3832

@@ -46,21 +40,14 @@ impl Module<'hir> {
4640
attrs,
4741
extern_crates: Vec::new(),
4842
imports: Vec::new(),
49-
structs: Vec::new(),
50-
unions: Vec::new(),
51-
enums: Vec::new(),
5243
fns: Vec::new(),
5344
mods: Vec::new(),
54-
typedefs: Vec::new(),
55-
opaque_tys: Vec::new(),
56-
statics: Vec::new(),
57-
constants: Vec::new(),
45+
items: Vec::new(),
5846
traits: Vec::new(),
5947
impls: Vec::new(),
6048
foreigns: Vec::new(),
6149
macros: Vec::new(),
6250
proc_macros: Vec::new(),
63-
trait_aliases: Vec::new(),
6451
is_crate: false,
6552
}
6653
}
@@ -76,29 +63,6 @@ crate enum StructType {
7663
Unit,
7764
}
7865

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-
10266
crate struct Variant<'hir> {
10367
crate name: Symbol,
10468
crate id: hir::HirId,
@@ -114,38 +78,6 @@ crate struct Function<'hir> {
11478
crate body: hir::BodyId,
11579
}
11680

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-
14981
crate struct Trait<'hir> {
15082
crate is_auto: hir::IsAuto,
15183
crate unsafety: hir::Unsafety,
@@ -157,13 +89,6 @@ crate struct Trait<'hir> {
15789
crate id: hir::HirId,
15890
}
15991

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-
16792
#[derive(Debug)]
16893
crate struct Impl<'hir> {
16994
crate unsafety: hir::Unsafety,

0 commit comments

Comments
 (0)