rustdoc: bind typedef inner type items to the folding system

This let's us handle a multitude of things for free:
 - #[doc(hidden)]
 - private fields/variants
 - --document-private-items
 - --document-hidden-items

And correct in the process the determination of "has stripped items" by
doing the same logic done by other ones.
This commit is contained in:
Urgau 2023-08-18 17:57:20 +02:00
parent 6b3bba8c3e
commit af6889c28c
6 changed files with 53 additions and 36 deletions

View File

@ -971,10 +971,8 @@ fn clean_ty_alias_inner_type<'tcx>(
.map(|variant| clean_variant_def_with_args(variant, args, cx))
.collect();
let has_stripped_variants = adt_def.variants().len() != variants.len();
TypeAliasInnerType::Enum {
variants,
has_stripped_variants,
is_non_exhaustive: adt_def.is_variant_list_non_exhaustive(),
}
} else {
@ -987,11 +985,10 @@ fn clean_ty_alias_inner_type<'tcx>(
let fields: Vec<_> =
clean_variant_def_with_args(variant, args, cx).kind.inner_items().cloned().collect();
let has_stripped_fields = variant.fields.len() != fields.len();
if adt_def.is_struct() {
TypeAliasInnerType::Struct { ctor_kind: variant.ctor_kind(), fields, has_stripped_fields }
TypeAliasInnerType::Struct { ctor_kind: variant.ctor_kind(), fields }
} else {
TypeAliasInnerType::Union { fields, has_stripped_fields }
TypeAliasInnerType::Union { fields }
}
})
}
@ -2415,7 +2412,6 @@ pub(crate) fn clean_variant_def_with_args<'tcx>(
variant
.fields
.iter()
.filter(|field| field.vis.is_public())
.map(|field| {
let ty = cx.tcx.type_of(field.did).instantiate(cx.tcx, args);
clean_field_with_def_id(
@ -2431,7 +2427,6 @@ pub(crate) fn clean_variant_def_with_args<'tcx>(
fields: variant
.fields
.iter()
.filter(|field| field.vis.is_public())
.map(|field| {
let ty = cx.tcx.type_of(field.did).instantiate(cx.tcx, args);
clean_field_with_def_id(

View File

@ -2231,20 +2231,9 @@ pub(crate) struct PathSegment {
#[derive(Clone, Debug)]
pub(crate) enum TypeAliasInnerType {
Enum {
variants: IndexVec<VariantIdx, Item>,
has_stripped_variants: bool,
is_non_exhaustive: bool,
},
Union {
fields: Vec<Item>,
has_stripped_fields: bool,
},
Struct {
ctor_kind: Option<CtorKind>,
fields: Vec<Item>,
has_stripped_fields: bool,
},
Enum { variants: IndexVec<VariantIdx, Item>, is_non_exhaustive: bool },
Union { fields: Vec<Item> },
Struct { ctor_kind: Option<CtorKind>, fields: Vec<Item> },
}
#[derive(Clone, Debug)]

View File

@ -52,10 +52,31 @@ fn fold_inner_recur(&mut self, kind: ItemKind) -> ItemKind {
VariantItem(Variant { kind, discriminant })
}
TypeAliasItem(mut typealias) => {
typealias.inner_type = typealias.inner_type.map(|inner_type| match inner_type {
TypeAliasInnerType::Enum { variants, is_non_exhaustive } => {
let variants = variants
.into_iter_enumerated()
.filter_map(|(_, x)| self.fold_item(x))
.collect();
TypeAliasInnerType::Enum { variants, is_non_exhaustive }
}
TypeAliasInnerType::Union { fields } => {
let fields = fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
TypeAliasInnerType::Union { fields }
}
TypeAliasInnerType::Struct { ctor_kind, fields } => {
let fields = fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
TypeAliasInnerType::Struct { ctor_kind, fields }
}
});
TypeAliasItem(typealias)
}
ExternCrateItem { src: _ }
| ImportItem(_)
| FunctionItem(_)
| TypeAliasItem(_)
| OpaqueTyItem(_)
| StaticItem(_)
| ConstantItem(_)

View File

@ -457,6 +457,7 @@ fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
| clean::StructItem(..)
| clean::UnionItem(..)
| clean::VariantItem(..)
| clean::TypeAliasItem(..)
| clean::ImplItem(..) => {
self.cache.parent_stack.push(ParentStackItem::new(&item));
(self.fold_item_recur(item), true)

View File

@ -1270,30 +1270,34 @@ fn toggle<W, F>(w: &mut W, f: F)
}
match &t.inner_type {
Some(clean::TypeAliasInnerType::Enum {
variants,
has_stripped_variants: has_stripped_entries,
is_non_exhaustive,
}) => {
Some(clean::TypeAliasInnerType::Enum { variants, is_non_exhaustive }) => {
toggle(w, |w| {
let variants_iter = || variants.iter().filter(|i| !i.is_stripped());
wrap_item(w, |w| {
let variants_len = variants.len();
let variants_count = variants_iter().count();
let has_stripped_entries = variants_len != variants_count;
write!(w, "enum {}{}", it.name.unwrap(), t.generics.print(cx));
render_enum_fields(
w,
cx,
None,
variants.iter(),
variants.len(),
*has_stripped_entries,
variants_iter(),
variants_count,
has_stripped_entries,
*is_non_exhaustive,
)
});
item_variants(w, cx, it, variants.iter());
item_variants(w, cx, it, variants_iter());
});
}
Some(clean::TypeAliasInnerType::Union { fields, has_stripped_fields }) => {
Some(clean::TypeAliasInnerType::Union { fields }) => {
toggle(w, |w| {
wrap_item(w, |w| {
let fields_count = fields.iter().filter(|i| !i.is_stripped()).count();
let has_stripped_fields = fields.len() != fields_count;
write!(w, "union {}{}", it.name.unwrap(), t.generics.print(cx));
render_struct_fields(
w,
@ -1302,16 +1306,19 @@ fn toggle<W, F>(w: &mut W, f: F)
fields,
"",
true,
*has_stripped_fields,
has_stripped_fields,
cx,
);
});
item_fields(w, cx, it, fields, None);
});
}
Some(clean::TypeAliasInnerType::Struct { ctor_kind, fields, has_stripped_fields }) => {
Some(clean::TypeAliasInnerType::Struct { ctor_kind, fields }) => {
toggle(w, |w| {
wrap_item(w, |w| {
let fields_count = fields.iter().filter(|i| !i.is_stripped()).count();
let has_stripped_fields = fields.len() != fields_count;
write!(w, "struct {}{}", it.name.unwrap(), t.generics.print(cx));
render_struct_fields(
w,
@ -1320,7 +1327,7 @@ fn toggle<W, F>(w: &mut W, f: F)
fields,
"",
true,
*has_stripped_fields,
has_stripped_fields,
cx,
);
});

View File

@ -22,6 +22,8 @@ pub enum IrTyKind<A, B> {
TyKind(A, B),
// no comment
StructKind { a: A, },
#[doc(hidden)]
Unspecified,
}
// @has 'inner_variants/type.NearlyTyKind.html'
@ -52,7 +54,9 @@ pub union OneOr<A: Copy> {
// @has 'inner_variants/struct.One.html'
pub struct One<T> {
pub val: T,
__hidden: T,
#[doc(hidden)]
pub __hidden: T,
__private: T,
}
// @has 'inner_variants/type.OneU64.html'