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
This commit is contained in:
Joshua Nelson 2020-11-20 23:50:13 -05:00
parent 593fe977a7
commit 0459aca41a
3 changed files with 71 additions and 281 deletions

View File

@ -231,21 +231,14 @@ impl Clean<Item> for doctree::Module<'_> {
let mut items: Vec<Item> = vec![];
items.extend(self.extern_crates.iter().flat_map(|x| x.clean(cx)));
items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
items.extend(self.structs.iter().map(|x| x.clean(cx)));
items.extend(self.unions.iter().map(|x| x.clean(cx)));
items.extend(self.enums.iter().map(|x| x.clean(cx)));
items.extend(self.fns.iter().map(|x| x.clean(cx)));
items.extend(self.foreigns.iter().map(|x| x.clean(cx)));
items.extend(self.mods.iter().map(|x| x.clean(cx)));
items.extend(self.typedefs.iter().map(|x| x.clean(cx)));
items.extend(self.opaque_tys.iter().map(|x| x.clean(cx)));
items.extend(self.statics.iter().map(|x| x.clean(cx)));
items.extend(self.constants.iter().map(|x| x.clean(cx)));
items.extend(self.items.iter().map(|x| x.clean(cx)));
items.extend(self.traits.iter().map(|x| x.clean(cx)));
items.extend(self.impls.iter().flat_map(|x| x.clean(cx)));
items.extend(self.macros.iter().map(|x| x.clean(cx)));
items.extend(self.proc_macros.iter().map(|x| x.clean(cx)));
items.extend(self.trait_aliases.iter().map(|x| x.clean(cx)));
// determine if we should display the inner contents or
// the outer `mod` item for the source code.
@ -1020,20 +1013,6 @@ impl Clean<Item> for doctree::Trait<'_> {
}
}
impl Clean<Item> for doctree::TraitAlias<'_> {
fn clean(&self, cx: &DocContext<'_>) -> Item {
Item::from_hir_id_and_parts(
self.id,
Some(self.name),
TraitAliasItem(TraitAlias {
generics: self.generics.clean(cx),
bounds: self.bounds.clean(cx),
}),
cx,
)
}
}
impl Clean<bool> for hir::IsAuto {
fn clean(&self, _: &DocContext<'_>) -> bool {
match *self {
@ -1777,38 +1756,6 @@ impl Clean<Visibility> for ty::Visibility {
}
}
impl Clean<Item> for doctree::Struct<'_> {
fn clean(&self, cx: &DocContext<'_>) -> Item {
Item::from_hir_id_and_parts(
self.id,
Some(self.name),
StructItem(Struct {
struct_type: self.struct_type,
generics: self.generics.clean(cx),
fields: self.fields.clean(cx),
fields_stripped: false,
}),
cx,
)
}
}
impl Clean<Item> for doctree::Union<'_> {
fn clean(&self, cx: &DocContext<'_>) -> Item {
Item::from_hir_id_and_parts(
self.id,
Some(self.name),
UnionItem(Union {
struct_type: self.struct_type,
generics: self.generics.clean(cx),
fields: self.fields.clean(cx),
fields_stripped: false,
}),
cx,
)
}
}
impl Clean<VariantStruct> for rustc_hir::VariantData<'_> {
fn clean(&self, cx: &DocContext<'_>) -> VariantStruct {
VariantStruct {
@ -1819,21 +1766,6 @@ impl Clean<VariantStruct> for rustc_hir::VariantData<'_> {
}
}
impl Clean<Item> for doctree::Enum<'_> {
fn clean(&self, cx: &DocContext<'_>) -> Item {
Item::from_hir_id_and_parts(
self.id,
Some(self.name),
EnumItem(Enum {
variants: self.variants.iter().map(|v| v.clean(cx)).collect(),
generics: self.generics.clean(cx),
variants_stripped: false,
}),
cx,
)
}
}
impl Clean<Item> for doctree::Variant<'_> {
fn clean(&self, cx: &DocContext<'_>) -> Item {
let what_rustc_thinks = Item::from_hir_id_and_parts(
@ -1981,33 +1913,6 @@ impl Clean<String> for Symbol {
}
}
impl Clean<Item> for doctree::Typedef<'_> {
fn clean(&self, cx: &DocContext<'_>) -> Item {
let type_ = self.ty.clean(cx);
let item_type = type_.def_id().and_then(|did| inline::build_ty(cx, did));
Item::from_hir_id_and_parts(
self.id,
Some(self.name),
TypedefItem(Typedef { type_, generics: self.gen.clean(cx), item_type }, false),
cx,
)
}
}
impl Clean<Item> for doctree::OpaqueTy<'_> {
fn clean(&self, cx: &DocContext<'_>) -> Item {
Item::from_hir_id_and_parts(
self.id,
Some(self.name),
OpaqueTyItem(OpaqueTy {
bounds: self.opaque_ty.bounds.clean(cx),
generics: self.opaque_ty.generics.clean(cx),
}),
cx,
)
}
}
impl Clean<BareFunctionDecl> for hir::BareFnTy<'_> {
fn clean(&self, cx: &DocContext<'_>) -> BareFunctionDecl {
let (generic_params, decl) = enter_impl_trait(cx, || {
@ -2017,37 +1922,71 @@ impl Clean<BareFunctionDecl> for hir::BareFnTy<'_> {
}
}
impl Clean<Item> for doctree::Static<'_> {
impl Clean<Item> for hir::Item<'_> {
fn clean(&self, cx: &DocContext<'_>) -> Item {
debug!("cleaning static {}: {:?}", self.name.clean(cx), self);
Item::from_hir_id_and_parts(
self.id,
Some(self.name),
StaticItem(Static {
type_: self.type_.clean(cx),
mutability: self.mutability,
expr: print_const_expr(cx, self.expr),
use hir::ItemKind;
let def_id = cx.tcx.hir().local_def_id(self.hir_id).to_def_id();
let name = cx.tcx.hir().name(self.hir_id);
let kind = match self.kind {
ItemKind::Static(ty, mutability, body_id) => StaticItem(Static {
type_: ty.clean(cx),
mutability,
expr: print_const_expr(cx, body_id),
}),
cx,
)
ItemKind::Const(ty, body_id) => ConstantItem(Constant {
type_: ty.clean(cx),
expr: print_const_expr(cx, body_id),
value: print_evaluated_const(cx, def_id),
is_literal: is_literal_expr(cx, body_id.hir_id),
}),
ItemKind::OpaqueTy(ref ty) => OpaqueTyItem(OpaqueTy {
bounds: ty.bounds.clean(cx),
generics: ty.generics.clean(cx),
}),
ItemKind::TyAlias(ty, ref generics) => {
let rustdoc_ty = ty.clean(cx);
let item_type = rustdoc_ty.def_id().and_then(|did| inline::build_ty(cx, did));
TypedefItem(
Typedef { type_: rustdoc_ty, generics: generics.clean(cx), item_type },
false,
)
}
ItemKind::Enum(ref def, ref generics) => EnumItem(Enum {
variants: def.variants.iter().map(|v| v.clean(cx)).collect(),
generics: generics.clean(cx),
variants_stripped: false,
}),
ItemKind::TraitAlias(ref generics, bounds) => TraitAliasItem(TraitAlias {
generics: generics.clean(cx),
bounds: bounds.clean(cx),
}),
ItemKind::Union(ref variant_data, ref generics) => UnionItem(Union {
struct_type: doctree::struct_type_from_def(&variant_data),
generics: generics.clean(cx),
fields: variant_data.fields().clean(cx),
fields_stripped: false,
}),
ItemKind::Struct(ref variant_data, ref generics) => StructItem(Struct {
struct_type: doctree::struct_type_from_def(&variant_data),
generics: generics.clean(cx),
fields: variant_data.fields().clean(cx),
fields_stripped: false,
}),
_ => unreachable!("not yet converted"),
};
Item::from_def_id_and_parts(def_id, Some(name), kind, cx)
}
}
impl Clean<Item> for doctree::Constant<'_> {
impl Clean<Item> for hir::Variant<'_> {
fn clean(&self, cx: &DocContext<'_>) -> Item {
let def_id = cx.tcx.hir().local_def_id(self.id).to_def_id();
Item::from_def_id_and_parts(
def_id,
Some(self.name),
ConstantItem(Constant {
type_: self.type_.clean(cx),
expr: print_const_expr(cx, self.expr),
value: print_evaluated_const(cx, def_id),
is_literal: is_literal_expr(cx, self.expr.hir_id),
}),
cx,
)
let kind = VariantItem(Variant { kind: self.data.clean(cx) });
let what_rustc_thinks =
Item::from_hir_id_and_parts(self.id, Some(self.ident.name), kind, cx);
// don't show `pub` for variants, which are always public
Item { visibility: Inherited, ..what_rustc_thinks }
}
}

View File

@ -17,22 +17,15 @@ crate struct Module<'hir> {
crate where_inner: Span,
crate extern_crates: Vec<ExternCrate<'hir>>,
crate imports: Vec<Import<'hir>>,
crate structs: Vec<Struct<'hir>>,
crate unions: Vec<Union<'hir>>,
crate enums: Vec<Enum<'hir>>,
crate fns: Vec<Function<'hir>>,
crate mods: Vec<Module<'hir>>,
crate id: hir::HirId,
crate typedefs: Vec<Typedef<'hir>>,
crate opaque_tys: Vec<OpaqueTy<'hir>>,
crate statics: Vec<Static<'hir>>,
crate constants: Vec<Constant<'hir>>,
crate items: Vec<&'hir hir::Item<'hir>>,
crate traits: Vec<Trait<'hir>>,
crate impls: Vec<Impl<'hir>>,
crate foreigns: Vec<ForeignItem<'hir>>,
crate macros: Vec<Macro>,
crate proc_macros: Vec<ProcMacro>,
crate trait_aliases: Vec<TraitAlias<'hir>>,
crate is_crate: bool,
}
@ -46,21 +39,14 @@ impl Module<'hir> {
attrs,
extern_crates: Vec::new(),
imports: Vec::new(),
structs: Vec::new(),
unions: Vec::new(),
enums: Vec::new(),
fns: Vec::new(),
mods: Vec::new(),
typedefs: Vec::new(),
opaque_tys: Vec::new(),
statics: Vec::new(),
constants: Vec::new(),
items: Vec::new(),
traits: Vec::new(),
impls: Vec::new(),
foreigns: Vec::new(),
macros: Vec::new(),
proc_macros: Vec::new(),
trait_aliases: Vec::new(),
is_crate: false,
}
}
@ -76,29 +62,6 @@ crate enum StructType {
Unit,
}
crate struct Struct<'hir> {
crate id: hir::HirId,
crate struct_type: StructType,
crate name: Symbol,
crate generics: &'hir hir::Generics<'hir>,
crate fields: &'hir [hir::StructField<'hir>],
}
crate struct Union<'hir> {
crate id: hir::HirId,
crate struct_type: StructType,
crate name: Symbol,
crate generics: &'hir hir::Generics<'hir>,
crate fields: &'hir [hir::StructField<'hir>],
}
crate struct Enum<'hir> {
crate variants: Vec<Variant<'hir>>,
crate generics: &'hir hir::Generics<'hir>,
crate id: hir::HirId,
crate name: Symbol,
}
crate struct Variant<'hir> {
crate name: Symbol,
crate id: hir::HirId,
@ -114,38 +77,6 @@ crate struct Function<'hir> {
crate body: hir::BodyId,
}
crate struct Typedef<'hir> {
crate ty: &'hir hir::Ty<'hir>,
crate gen: &'hir hir::Generics<'hir>,
crate name: Symbol,
crate id: hir::HirId,
}
crate struct OpaqueTy<'hir> {
crate opaque_ty: &'hir hir::OpaqueTy<'hir>,
crate name: Symbol,
crate id: hir::HirId,
}
#[derive(Debug)]
crate struct Static<'hir> {
crate type_: &'hir hir::Ty<'hir>,
crate mutability: hir::Mutability,
crate expr: hir::BodyId,
crate name: Symbol,
crate attrs: &'hir [ast::Attribute],
crate vis: &'hir hir::Visibility<'hir>,
crate id: hir::HirId,
crate span: Span,
}
crate struct Constant<'hir> {
crate type_: &'hir hir::Ty<'hir>,
crate expr: hir::BodyId,
crate name: Symbol,
crate id: hir::HirId,
}
crate struct Trait<'hir> {
crate is_auto: hir::IsAuto,
crate unsafety: hir::Unsafety,
@ -157,13 +88,6 @@ crate struct Trait<'hir> {
crate id: hir::HirId,
}
crate struct TraitAlias<'hir> {
crate name: Symbol,
crate generics: &'hir hir::Generics<'hir>,
crate bounds: &'hir [hir::GenericBound<'hir>],
crate id: hir::HirId,
}
#[derive(Debug)]
crate struct Impl<'hir> {
crate unsafety: hir::Unsafety,

View File

@ -82,50 +82,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
module
}
fn visit_variant_data(
&mut self,
item: &'tcx hir::Item<'_>,
name: Symbol,
sd: &'tcx hir::VariantData<'_>,
generics: &'tcx hir::Generics<'_>,
) -> Struct<'tcx> {
debug!("visiting struct");
let struct_type = struct_type_from_def(&*sd);
Struct { id: item.hir_id, struct_type, name, generics, fields: sd.fields() }
}
fn visit_union_data(
&mut self,
item: &'tcx hir::Item<'_>,
name: Symbol,
sd: &'tcx hir::VariantData<'_>,
generics: &'tcx hir::Generics<'_>,
) -> Union<'tcx> {
debug!("visiting union");
let struct_type = struct_type_from_def(&*sd);
Union { id: item.hir_id, struct_type, name, generics, fields: sd.fields() }
}
fn visit_enum_def(
&mut self,
it: &'tcx hir::Item<'_>,
name: Symbol,
def: &'tcx hir::EnumDef<'_>,
generics: &'tcx hir::Generics<'_>,
) -> Enum<'tcx> {
debug!("visiting enum");
Enum {
name,
variants: def
.variants
.iter()
.map(|v| Variant { name: v.ident.name, id: v.id, def: &v.data })
.collect(),
generics,
id: it.hir_id,
}
}
fn visit_fn(
&mut self,
om: &mut Module<'tcx>,
@ -414,45 +370,21 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
Some(ident.name),
));
}
hir::ItemKind::Enum(ref ed, ref gen) => {
om.enums.push(self.visit_enum_def(item, ident.name, ed, gen))
}
hir::ItemKind::Struct(ref sd, ref gen) => {
om.structs.push(self.visit_variant_data(item, ident.name, sd, gen))
}
hir::ItemKind::Union(ref sd, ref gen) => {
om.unions.push(self.visit_union_data(item, ident.name, sd, gen))
}
hir::ItemKind::Enum(..) => om.items.push(item),
hir::ItemKind::Struct(..) => om.items.push(item),
hir::ItemKind::Union(..) => om.items.push(item),
hir::ItemKind::Fn(ref sig, ref gen, body) => {
self.visit_fn(om, item, ident.name, &sig.decl, sig.header, gen, body)
}
hir::ItemKind::TyAlias(ty, ref gen) => {
let t = Typedef { ty, gen, name: ident.name, id: item.hir_id };
om.typedefs.push(t);
}
hir::ItemKind::OpaqueTy(ref opaque_ty) => {
let t = OpaqueTy { opaque_ty, name: ident.name, id: item.hir_id };
om.opaque_tys.push(t);
}
hir::ItemKind::Static(type_, mutability, expr) => {
let s = Static {
type_,
mutability,
expr,
id: item.hir_id,
name: ident.name,
attrs: &item.attrs,
span: item.span,
vis: &item.vis,
};
om.statics.push(s);
}
hir::ItemKind::Const(type_, expr) => {
hir::ItemKind::TyAlias(..)
| hir::ItemKind::OpaqueTy(..)
| hir::ItemKind::Static(..)
| hir::ItemKind::TraitAlias(..) => om.items.push(item),
hir::ItemKind::Const(..) => {
// Underscore constants do not correspond to a nameable item and
// so are never useful in documentation.
if ident.name != kw::Underscore {
let s = Constant { type_, expr, id: item.hir_id, name: ident.name };
om.constants.push(s);
om.items.push(item);
}
}
hir::ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref item_ids) => {
@ -469,11 +401,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
};
om.traits.push(t);
}
hir::ItemKind::TraitAlias(ref generics, ref bounds) => {
let t = TraitAlias { name: ident.name, generics, bounds, id: item.hir_id };
om.trait_aliases.push(t);
}
hir::ItemKind::Impl {
unsafety,
polarity,