Skip to content

Commit

Permalink
Rollup merge of #79264 - jyn514:less-doctree, r=GuillaumeGomez
Browse files Browse the repository at this point in the history
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 #78082 (the easiest part, I'm still debugging some other changes).
r? `@GuillaumeGomez`
  • Loading branch information
Dylan-DPC committed Nov 21, 2020
2 parents 96ec5d2 + e280ae8 commit c20657c
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 375 deletions.
185 changes: 64 additions & 121 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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(
Expand Down Expand Up @@ -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, || {
Expand All @@ -2017,37 +1922,75 @@ impl Clean<BareFunctionDecl> for hir::BareFnTy<'_> {
}
}

impl Clean<Item> for doctree::Static<'_> {
impl Clean<Item> for (&hir::Item<'_>, Option<Ident>) {
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 (item, renamed) = self;
let def_id = cx.tcx.hir().local_def_id(item.hir_id).to_def_id();
let name = match renamed {
Some(ident) => ident.name,
None => cx.tcx.hir().name(item.hir_id),
};
let kind = match item.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 }
}
}

Expand Down
83 changes: 4 additions & 79 deletions src/librustdoc/doctree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ crate use self::StructType::*;

use rustc_ast as ast;
use rustc_span::hygiene::MacroKind;
use rustc_span::{self, Span, Symbol};
use rustc_span::{self, symbol::Ident, Span, Symbol};

use rustc_hir as hir;
use rustc_hir::def_id::CrateNum;
Expand All @@ -17,22 +17,16 @@ 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>>,
// (item, renamed)
crate items: Vec<(&'hir hir::Item<'hir>, Option<Ident>)>,
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,
}

Expand All @@ -46,21 +40,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,
}
}
Expand All @@ -76,29 +63,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,
Expand All @@ -114,38 +78,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,
Expand All @@ -157,13 +89,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,
Expand Down
Loading

0 comments on commit c20657c

Please sign in to comment.