Skip to content

rustdoc: make rustdoc less pass-aware #32678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 31 additions & 24 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
pub use self::Type::*;
pub use self::PrimitiveType::*;
pub use self::TypeKind::*;
pub use self::StructField::*;
pub use self::VariantKind::*;
pub use self::Mutability::*;
pub use self::Import::*;
Expand Down Expand Up @@ -53,6 +52,7 @@ use std::env::current_dir;
use core::DocContext;
use doctree;
use visit_ast;
use html::item_type::ItemType;

/// A stable identifier to the particular version of JSON output.
/// Increment this when the `Crate` and related structures change.
Expand Down Expand Up @@ -273,36 +273,49 @@ impl Item {
}
pub fn is_crate(&self) -> bool {
match self.inner {
ModuleItem(Module { items: _, is_crate: true }) => true,
_ => false
StrippedItem(box ModuleItem(Module { is_crate: true, ..})) |
ModuleItem(Module { is_crate: true, ..}) => true,
_ => false,
}
}
pub fn is_mod(&self) -> bool {
match self.inner { ModuleItem(..) => true, _ => false }
ItemType::from_item(self) == ItemType::Module
}
pub fn is_trait(&self) -> bool {
match self.inner { TraitItem(..) => true, _ => false }
ItemType::from_item(self) == ItemType::Trait
}
pub fn is_struct(&self) -> bool {
match self.inner { StructItem(..) => true, _ => false }
ItemType::from_item(self) == ItemType::Struct
}
pub fn is_enum(&self) -> bool {
match self.inner { EnumItem(..) => true, _ => false }
ItemType::from_item(self) == ItemType::Module
}
pub fn is_fn(&self) -> bool {
match self.inner { FunctionItem(..) => true, _ => false }
ItemType::from_item(self) == ItemType::Function
}
pub fn is_associated_type(&self) -> bool {
match self.inner { AssociatedTypeItem(..) => true, _ => false }
ItemType::from_item(self) == ItemType::AssociatedType
}
pub fn is_associated_const(&self) -> bool {
match self.inner { AssociatedConstItem(..) => true, _ => false }
ItemType::from_item(self) == ItemType::AssociatedConst
}
pub fn is_method(&self) -> bool {
match self.inner { MethodItem(..) => true, _ => false }
ItemType::from_item(self) == ItemType::Method
}
pub fn is_ty_method(&self) -> bool {
match self.inner { TyMethodItem(..) => true, _ => false }
ItemType::from_item(self) == ItemType::TyMethod
}
pub fn is_stripped(&self) -> bool {
match self.inner { StrippedItem(..) => true, _ => false }
}
pub fn has_stripped_fields(&self) -> Option<bool> {
match self.inner {
StructItem(ref _struct) => Some(_struct.fields_stripped),
VariantItem(Variant { kind: StructVariant(ref vstruct)} ) => {
Some(vstruct.fields_stripped)
},
_ => None,
}
}

pub fn stability_class(&self) -> String {
Expand Down Expand Up @@ -341,7 +354,7 @@ pub enum ItemEnum {
TyMethodItem(TyMethod),
/// A method with a body.
MethodItem(Method),
StructFieldItem(StructField),
StructFieldItem(Type),
VariantItem(Variant),
/// `fn`s from an extern block
ForeignFunctionItem(Function),
Expand All @@ -352,6 +365,8 @@ pub enum ItemEnum {
AssociatedConstItem(Type, Option<String>),
AssociatedTypeItem(Vec<TyParamBound>, Option<Type>),
DefaultImplItem(DefaultImpl),
/// An item that has been stripped by a rustdoc pass
StrippedItem(Box<ItemEnum>),
}

#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
Expand Down Expand Up @@ -1733,12 +1748,6 @@ impl<'tcx> Clean<Type> for ty::Ty<'tcx> {
}
}

#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub enum StructField {
HiddenStructField, // inserted later by strip passes
TypedStructField(Type),
}

impl Clean<Item> for hir::StructField {
fn clean(&self, cx: &DocContext) -> Item {
Item {
Expand All @@ -1749,7 +1758,7 @@ impl Clean<Item> for hir::StructField {
stability: get_stability(cx, cx.map.local_def_id(self.id)),
deprecation: get_deprecation(cx, cx.map.local_def_id(self.id)),
def_id: cx.map.local_def_id(self.id),
inner: StructFieldItem(TypedStructField(self.ty.clean(cx))),
inner: StructFieldItem(self.ty.clean(cx)),
}
}
}
Expand All @@ -1766,7 +1775,7 @@ impl<'tcx> Clean<Item> for ty::FieldDefData<'tcx, 'static> {
stability: get_stability(cx, self.did),
deprecation: get_deprecation(cx, self.did),
def_id: self.did,
inner: StructFieldItem(TypedStructField(self.unsubst_ty().clean(cx))),
inner: StructFieldItem(self.unsubst_ty().clean(cx)),
}
}
}
Expand Down Expand Up @@ -1897,9 +1906,7 @@ impl<'tcx> Clean<Item> for ty::VariantDefData<'tcx, 'static> {
def_id: field.did,
stability: get_stability(cx, field.did),
deprecation: get_deprecation(cx, field.did),
inner: StructFieldItem(
TypedStructField(field.unsubst_ty().clean(cx))
)
inner: StructFieldItem(field.unsubst_ty().clean(cx))
}
}).collect()
})
Expand Down
56 changes: 44 additions & 12 deletions src/librustdoc/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,50 @@

use clean::*;

pub enum FoldItem {
Retain(Item),
Strip(Item),
Erase,
}

impl FoldItem {
pub fn fold(self) -> Option<Item> {
match self {
FoldItem::Erase => None,
FoldItem::Retain(i) => Some(i),
FoldItem::Strip(item@ Item { inner: StrippedItem(..), .. } ) => Some(item),
FoldItem::Strip(mut i) => {
i.inner = StrippedItem(box i.inner);
Some(i)
}
}
}
}

pub trait DocFolder : Sized {
fn fold_item(&mut self, item: Item) -> Option<Item> {
self.fold_item_recur(item)
}

/// don't override!
fn fold_item_recur(&mut self, item: Item) -> Option<Item> {
let Item { attrs, name, source, visibility, def_id, inner, stability, deprecation } = item;
let inner = match inner {
fn fold_inner_recur(&mut self, inner: ItemEnum) -> ItemEnum {
match inner {
StrippedItem(..) => unreachable!(),
ModuleItem(i) => {
ModuleItem(self.fold_mod(i))
},
StructItem(mut i) => {
let num_fields = i.fields.len();
i.fields = i.fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
i.fields_stripped |= num_fields != i.fields.len();
i.fields_stripped |= num_fields != i.fields.len() ||
i.fields.iter().any(|f| f.is_stripped());
StructItem(i)
},
ModuleItem(i) => {
ModuleItem(self.fold_mod(i))
},
EnumItem(mut i) => {
let num_variants = i.variants.len();
i.variants = i.variants.into_iter().filter_map(|x| self.fold_item(x)).collect();
i.variants_stripped |= num_variants != i.variants.len();
i.variants_stripped |= num_variants != i.variants.len() ||
i.variants.iter().any(|f| f.is_stripped());
EnumItem(i)
},
TraitItem(mut i) => {
Expand All @@ -48,13 +70,24 @@ pub trait DocFolder : Sized {
StructVariant(mut j) => {
let num_fields = j.fields.len();
j.fields = j.fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
j.fields_stripped |= num_fields != j.fields.len();
j.fields_stripped |= num_fields != j.fields.len() ||
j.fields.iter().any(|f| f.is_stripped());
VariantItem(Variant {kind: StructVariant(j), ..i2})
},
_ => VariantItem(i2)
}
},
x => x
}
}

/// don't override!
fn fold_item_recur(&mut self, item: Item) -> Option<Item> {
let Item { attrs, name, source, visibility, def_id, inner, stability, deprecation } = item;

let inner = match inner {
StrippedItem(box i) => StrippedItem(box self.fold_inner_recur(i)),
_ => self.fold_inner_recur(inner),
};

Some(Item { attrs: attrs, name: name, source: source, inner: inner,
Expand All @@ -70,9 +103,8 @@ pub trait DocFolder : Sized {
}

fn fold_crate(&mut self, mut c: Crate) -> Crate {
c.module = c.module.and_then(|module| {
self.fold_item(module)
});
c.module = c.module.and_then(|module| self.fold_item(module));

c.external_traits = c.external_traits.into_iter().map(|(k, mut v)| {
v.items = v.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
(k, v)
Expand Down
8 changes: 7 additions & 1 deletion src/librustdoc/html/item_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ pub enum ItemType {

impl ItemType {
pub fn from_item(item: &clean::Item) -> ItemType {
match item.inner {
let inner = match item.inner {
clean::StrippedItem(box ref item) => item,
ref inner@_ => inner,
};

match *inner {
clean::ModuleItem(..) => ItemType::Module,
clean::ExternCrateItem(..) => ItemType::ExternCrate,
clean::ImportItem(..) => ItemType::Import,
Expand All @@ -67,6 +72,7 @@ impl ItemType {
clean::AssociatedConstItem(..) => ItemType::AssociatedConst,
clean::AssociatedTypeItem(..) => ItemType::AssociatedType,
clean::DefaultImplItem(..) => ItemType::Impl,
clean::StrippedItem(..) => unreachable!(),
}
}

Expand Down
Loading