Rollup merge of #128263 - notriddle:notriddle/clean-up-again, r=GuillaumeGomez

rustdoc: use strategic ThinVec/Box to shrink `clean::ItemKind`
This commit is contained in:
Matthias Krüger 2024-07-28 13:42:19 +02:00 committed by GitHub
commit 3a4051cf3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 120 additions and 89 deletions

View File

@ -131,8 +131,8 @@ pub(crate) fn try_inline(
Res::Def(DefKind::Const, did) => {
record_extern_fqn(cx, did, ItemType::Constant);
cx.with_param_env(did, |cx| {
let (generics, ty, ct) = build_const_item(cx, did);
clean::ConstantItem(generics, Box::new(ty), ct)
let ct = build_const_item(cx, did);
clean::ConstantItem(Box::new(ct))
})
}
Res::Def(DefKind::Macro(kind), did) => {
@ -720,10 +720,7 @@ pub(crate) fn print_inlined_const(tcx: TyCtxt<'_>, did: DefId) -> String {
}
}
fn build_const_item(
cx: &mut DocContext<'_>,
def_id: DefId,
) -> (clean::Generics, clean::Type, clean::Constant) {
fn build_const_item(cx: &mut DocContext<'_>, def_id: DefId) -> clean::Constant {
let mut generics =
clean_ty_generics(cx, cx.tcx.generics_of(def_id), cx.tcx.explicit_predicates_of(def_id));
clean::simplify::move_bounds_to_generic_parameters(&mut generics);
@ -733,17 +730,17 @@ fn build_const_item(
None,
None,
);
(generics, ty, clean::Constant { kind: clean::ConstantKind::Extern { def_id } })
clean::Constant { generics, type_: ty, kind: clean::ConstantKind::Extern { def_id } }
}
fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::Static {
clean::Static {
type_: clean_middle_ty(
type_: Box::new(clean_middle_ty(
ty::Binder::dummy(cx.tcx.type_of(did).instantiate_identity()),
cx,
Some(did),
None,
),
)),
mutability: if mutable { Mutability::Mut } else { Mutability::Not },
expr: None,
}

View File

@ -287,23 +287,21 @@ fn clean_lifetime<'tcx>(lifetime: &hir::Lifetime, cx: &mut DocContext<'tcx>) ->
pub(crate) fn clean_const<'tcx>(
constant: &hir::ConstArg<'tcx>,
_cx: &mut DocContext<'tcx>,
) -> Constant {
) -> ConstantKind {
match &constant.kind {
hir::ConstArgKind::Path(qpath) => {
Constant { kind: ConstantKind::Path { path: qpath_to_string(&qpath).into() } }
}
hir::ConstArgKind::Anon(anon) => {
Constant { kind: ConstantKind::Anonymous { body: anon.body } }
ConstantKind::Path { path: qpath_to_string(&qpath).into() }
}
hir::ConstArgKind::Anon(anon) => ConstantKind::Anonymous { body: anon.body },
}
}
pub(crate) fn clean_middle_const<'tcx>(
constant: ty::Binder<'tcx, ty::Const<'tcx>>,
_cx: &mut DocContext<'tcx>,
) -> Constant {
) -> ConstantKind {
// FIXME: instead of storing the stringified expression, store `self` directly instead.
Constant { kind: ConstantKind::TyConst { expr: constant.skip_binder().to_string().into() } }
ConstantKind::TyConst { expr: constant.skip_binder().to_string().into() }
}
pub(crate) fn clean_middle_region<'tcx>(region: ty::Region<'tcx>) -> Option<Lifetime> {
@ -1230,14 +1228,11 @@ fn clean_trait_item<'tcx>(trait_item: &hir::TraitItem<'tcx>, cx: &mut DocContext
let local_did = trait_item.owner_id.to_def_id();
cx.with_param_env(local_did, |cx| {
let inner = match trait_item.kind {
hir::TraitItemKind::Const(ty, Some(default)) => {
let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx));
AssocConstItem(
generics,
Box::new(clean_ty(ty, cx)),
ConstantKind::Local { def_id: local_did, body: default },
)
}
hir::TraitItemKind::Const(ty, Some(default)) => AssocConstItem(Box::new(Constant {
generics: enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx)),
kind: ConstantKind::Local { def_id: local_did, body: default },
type_: clean_ty(ty, cx),
})),
hir::TraitItemKind::Const(ty, None) => {
let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx));
TyAssocConstItem(generics, Box::new(clean_ty(ty, cx)))
@ -1282,11 +1277,11 @@ pub(crate) fn clean_impl_item<'tcx>(
let local_did = impl_.owner_id.to_def_id();
cx.with_param_env(local_did, |cx| {
let inner = match impl_.kind {
hir::ImplItemKind::Const(ty, expr) => {
let generics = clean_generics(impl_.generics, cx);
let default = ConstantKind::Local { def_id: local_did, body: expr };
AssocConstItem(generics, Box::new(clean_ty(ty, cx)), default)
}
hir::ImplItemKind::Const(ty, expr) => AssocConstItem(Box::new(Constant {
generics: clean_generics(impl_.generics, cx),
kind: ConstantKind::Local { def_id: local_did, body: expr },
type_: clean_ty(ty, cx),
})),
hir::ImplItemKind::Fn(ref sig, body) => {
let m = clean_function(cx, sig, impl_.generics, FunctionArgs::Body(body));
let defaultness = cx.tcx.defaultness(impl_.owner_id);
@ -1320,12 +1315,12 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
let tcx = cx.tcx;
let kind = match assoc_item.kind {
ty::AssocKind::Const => {
let ty = Box::new(clean_middle_ty(
let ty = clean_middle_ty(
ty::Binder::dummy(tcx.type_of(assoc_item.def_id).instantiate_identity()),
cx,
Some(assoc_item.def_id),
None,
));
);
let mut generics = clean_ty_generics(
cx,
@ -1339,9 +1334,13 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
ty::TraitContainer => tcx.defaultness(assoc_item.def_id).has_value(),
};
if provided {
AssocConstItem(generics, ty, ConstantKind::Extern { def_id: assoc_item.def_id })
AssocConstItem(Box::new(Constant {
generics,
kind: ConstantKind::Extern { def_id: assoc_item.def_id },
type_: ty,
}))
} else {
TyAssocConstItem(generics, ty)
TyAssocConstItem(generics, Box::new(ty))
}
}
ty::AssocKind::Fn => {
@ -1397,7 +1396,7 @@ fn param_eq_arg(param: &GenericParamDef, arg: &GenericArg) -> bool {
{
true
}
(GenericParamDefKind::Const { .. }, GenericArg::Const(c)) => match &c.kind {
(GenericParamDefKind::Const { .. }, GenericArg::Const(c)) => match &**c {
ConstantKind::TyConst { expr } => **expr == *param.name.as_str(),
_ => false,
},
@ -2744,14 +2743,16 @@ fn clean_maybe_renamed_item<'tcx>(
let mut name = renamed.unwrap_or_else(|| cx.tcx.hir().name(item.hir_id()));
cx.with_param_env(def_id, |cx| {
let kind = match item.kind {
ItemKind::Static(ty, mutability, body_id) => {
StaticItem(Static { type_: clean_ty(ty, cx), mutability, expr: Some(body_id) })
}
ItemKind::Const(ty, generics, body_id) => ConstantItem(
clean_generics(generics, cx),
Box::new(clean_ty(ty, cx)),
Constant { kind: ConstantKind::Local { body: body_id, def_id } },
),
ItemKind::Static(ty, mutability, body_id) => StaticItem(Static {
type_: Box::new(clean_ty(ty, cx)),
mutability,
expr: Some(body_id),
}),
ItemKind::Const(ty, generics, body_id) => ConstantItem(Box::new(Constant {
generics: clean_generics(generics, cx),
type_: clean_ty(ty, cx),
kind: ConstantKind::Local { body: body_id, def_id },
})),
ItemKind::OpaqueTy(ref ty) => OpaqueTyItem(OpaqueTy {
bounds: ty.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
generics: clean_generics(ty.generics, cx),
@ -3109,7 +3110,7 @@ fn clean_maybe_renamed_foreign_item<'tcx>(
ForeignFunctionItem(Box::new(Function { decl, generics }), safety)
}
hir::ForeignItemKind::Static(ty, mutability, safety) => ForeignStaticItem(
Static { type_: clean_ty(ty, cx), mutability, expr: None },
Static { type_: Box::new(clean_ty(ty, cx)), mutability, expr: None },
safety,
),
hir::ForeignItemKind::Type => ForeignTypeItem,

View File

@ -852,9 +852,9 @@ pub(crate) enum ItemKind {
PrimitiveItem(PrimitiveType),
/// A required associated constant in a trait declaration.
TyAssocConstItem(Generics, Box<Type>),
ConstantItem(Generics, Box<Type>, Constant),
ConstantItem(Box<Constant>),
/// An associated constant in a trait impl or a provided one in a trait declaration.
AssocConstItem(Generics, Box<Type>, ConstantKind),
AssocConstItem(Box<Constant>),
/// A required associated type in a trait declaration.
///
/// The bounds may be non-empty if there is a `where` clause.
@ -888,7 +888,7 @@ pub(crate) fn inner_items(&self) -> impl Iterator<Item = &Item> {
| TypeAliasItem(_)
| OpaqueTyItem(_)
| StaticItem(_)
| ConstantItem(_, _, _)
| ConstantItem(_)
| TraitAliasItem(_)
| TyMethodItem(_)
| MethodItem(_, _)
@ -922,7 +922,7 @@ pub(crate) fn is_non_assoc(&self) -> bool {
| TypeAliasItem(_)
| OpaqueTyItem(_)
| StaticItem(_)
| ConstantItem(_, _, _)
| ConstantItem(_)
| TraitAliasItem(_)
| ForeignFunctionItem(_, _)
| ForeignStaticItem(_, _)
@ -2050,7 +2050,7 @@ fn from(prim_ty: hir::PrimTy) -> PrimitiveType {
pub(crate) struct Struct {
pub(crate) ctor_kind: Option<CtorKind>,
pub(crate) generics: Generics,
pub(crate) fields: Vec<Item>,
pub(crate) fields: ThinVec<Item>,
}
impl Struct {
@ -2076,7 +2076,7 @@ pub(crate) fn has_stripped_entries(&self) -> bool {
/// only as a variant in an enum.
#[derive(Clone, Debug)]
pub(crate) struct VariantStruct {
pub(crate) fields: Vec<Item>,
pub(crate) fields: ThinVec<Item>,
}
impl VariantStruct {
@ -2110,7 +2110,7 @@ pub(crate) struct Variant {
#[derive(Clone, Debug)]
pub(crate) enum VariantKind {
CLike,
Tuple(Vec<Item>),
Tuple(ThinVec<Item>),
Struct(VariantStruct),
}
@ -2246,7 +2246,7 @@ pub(crate) fn generics(&self) -> Option<Vec<&Type>> {
pub(crate) enum GenericArg {
Lifetime(Lifetime),
Type(Type),
Const(Box<Constant>),
Const(Box<ConstantKind>),
Infer,
}
@ -2359,20 +2359,22 @@ pub(crate) struct BareFunctionDecl {
#[derive(Clone, Debug)]
pub(crate) struct Static {
pub(crate) type_: Type,
pub(crate) type_: Box<Type>,
pub(crate) mutability: Mutability,
pub(crate) expr: Option<BodyId>,
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub(crate) struct Constant {
pub(crate) generics: Generics,
pub(crate) kind: ConstantKind,
pub(crate) type_: Type,
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub(crate) enum Term {
Type(Type),
Constant(Constant),
Constant(ConstantKind),
}
impl Term {
@ -2594,7 +2596,7 @@ mod size_asserts {
static_assert_size!(GenericParamDef, 40);
static_assert_size!(Generics, 16);
static_assert_size!(Item, 56);
static_assert_size!(ItemKind, 56);
static_assert_size!(ItemKind, 48);
static_assert_size!(PathSegment, 40);
static_assert_size!(Type, 32);
// tidy-alphabetical-end

View File

@ -79,7 +79,7 @@ fn fold_inner_recur(&mut self, kind: ItemKind) -> ItemKind {
| FunctionItem(_)
| OpaqueTyItem(_)
| StaticItem(_)
| ConstantItem(_, _, _)
| ConstantItem(..)
| TraitAliasItem(_)
| TyMethodItem(_)
| MethodItem(_, _)

View File

@ -375,7 +375,7 @@ pub(crate) fn print(&self) -> impl Display + '_ {
}
}
impl clean::Constant {
impl clean::ConstantKind {
pub(crate) fn print(&self, tcx: TyCtxt<'_>) -> impl Display + '_ {
let expr = self.expr(tcx);
display_fn(

View File

@ -1090,22 +1090,26 @@ fn render_assoc_item(
clean::MethodItem(m, _) => {
assoc_method(w, item, &m.generics, &m.decl, link, parent, cx, render_mode)
}
kind @ (clean::TyAssocConstItem(generics, ty) | clean::AssocConstItem(generics, ty, _)) => {
assoc_const(
w,
item,
generics,
ty,
match kind {
clean::TyAssocConstItem(..) => None,
clean::AssocConstItem(.., default) => Some(default),
_ => unreachable!(),
},
link,
if parent == ItemType::Trait { 4 } else { 0 },
cx,
)
}
clean::TyAssocConstItem(generics, ty) => assoc_const(
w,
item,
generics,
ty,
None,
link,
if parent == ItemType::Trait { 4 } else { 0 },
cx,
),
clean::AssocConstItem(ci) => assoc_const(
w,
item,
&ci.generics,
&ci.type_,
Some(&ci.kind),
link,
if parent == ItemType::Trait { 4 } else { 0 },
cx,
),
clean::TyAssocTypeItem(ref generics, ref bounds) => assoc_type(
w,
item,
@ -1690,8 +1694,7 @@ fn doc_impl_item(
w.write_str("</h4></section>");
}
}
kind @ (clean::TyAssocConstItem(generics, ty)
| clean::AssocConstItem(generics, ty, _)) => {
clean::TyAssocConstItem(generics, ty) => {
let source_id = format!("{item_type}.{name}");
let id = cx.derive_id(&source_id);
write!(w, "<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">");
@ -1706,11 +1709,29 @@ fn doc_impl_item(
item,
generics,
ty,
match kind {
clean::TyAssocConstItem(..) => None,
clean::AssocConstItem(.., default) => Some(default),
_ => unreachable!(),
},
None,
link.anchor(if trait_.is_some() { &source_id } else { &id }),
0,
cx,
);
w.write_str("</h4></section>");
}
clean::AssocConstItem(ci) => {
let source_id = format!("{item_type}.{name}");
let id = cx.derive_id(&source_id);
write!(w, "<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">");
render_rightside(w, cx, item, render_mode);
if trait_.is_some() {
// Anchors are only used on trait impls.
write!(w, "<a href=\"#{id}\" class=\"anchor\">§</a>");
}
w.write_str("<h4 class=\"code-header\">");
assoc_const(
w,
item,
&ci.generics,
&ci.type_,
Some(&ci.kind),
link.anchor(if trait_.is_some() { &source_id } else { &id }),
0,
cx,

View File

@ -267,7 +267,7 @@ pub(super) fn print_item(cx: &mut Context<'_>, item: &clean::Item, buf: &mut Buf
clean::PrimitiveItem(_) => item_primitive(buf, cx, item),
clean::StaticItem(ref i) => item_static(buf, cx, item, i, None),
clean::ForeignStaticItem(ref i, safety) => item_static(buf, cx, item, i, Some(*safety)),
clean::ConstantItem(generics, ty, c) => item_constant(buf, cx, item, generics, ty, c),
clean::ConstantItem(ci) => item_constant(buf, cx, item, &ci.generics, &ci.type_, &ci.kind),
clean::ForeignTypeItem => item_foreign_type(buf, cx, item),
clean::KeywordItem => item_keyword(buf, cx, item),
clean::OpaqueTyItem(ref e) => item_opaque_ty(buf, cx, item, e),
@ -1841,7 +1841,7 @@ fn item_constant(
it: &clean::Item,
generics: &clean::Generics,
ty: &clean::Type,
c: &clean::Constant,
c: &clean::ConstantKind,
) {
wrap_item(w, |w| {
let tcx = cx.tcx();
@ -1911,7 +1911,7 @@ fn item_fields(
w: &mut Buffer,
cx: &mut Context<'_>,
it: &clean::Item,
fields: &Vec<clean::Item>,
fields: &[clean::Item],
ctor_kind: Option<CtorKind>,
) {
let mut fields = fields

View File

@ -188,6 +188,16 @@ fn from_tcx(constant: clean::Constant, tcx: TyCtxt<'_>) -> Self {
}
}
impl FromWithTcx<clean::ConstantKind> for Constant {
// FIXME(generic_const_items): Add support for generic const items.
fn from_tcx(constant: clean::ConstantKind, tcx: TyCtxt<'_>) -> Self {
let expr = constant.expr(tcx);
let value = constant.value(tcx);
let is_literal = constant.is_literal(tcx);
Constant { expr, value, is_literal }
}
}
impl FromWithTcx<clean::AssocItemConstraint> for TypeBinding {
fn from_tcx(constraint: clean::AssocItemConstraint, tcx: TyCtxt<'_>) -> Self {
TypeBinding {
@ -325,8 +335,8 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
TypeAliasItem(t) => ItemEnum::TypeAlias(t.into_tcx(tcx)),
OpaqueTyItem(t) => ItemEnum::OpaqueTy(t.into_tcx(tcx)),
// FIXME(generic_const_items): Add support for generic free consts
ConstantItem(_generics, t, c) => {
ItemEnum::Constant { type_: (*t).into_tcx(tcx), const_: c.into_tcx(tcx) }
ConstantItem(ci) => {
ItemEnum::Constant { type_: ci.type_.into_tcx(tcx), const_: ci.kind.into_tcx(tcx) }
}
MacroItem(m) => ItemEnum::Macro(m.source),
ProcMacroItem(m) => ItemEnum::ProcMacro(m.into_tcx(tcx)),
@ -341,8 +351,8 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
ItemEnum::AssocConst { type_: (*ty).into_tcx(tcx), default: None }
}
// FIXME(generic_const_items): Add support for generic associated consts.
AssocConstItem(_generics, ty, default) => {
ItemEnum::AssocConst { type_: (*ty).into_tcx(tcx), default: Some(default.expr(tcx)) }
AssocConstItem(ci) => {
ItemEnum::AssocConst { type_: ci.type_.into_tcx(tcx), default: Some(ci.kind.expr(tcx)) }
}
TyAssocTypeItem(g, b) => ItemEnum::AssocType {
generics: g.into_tcx(tcx),
@ -829,7 +839,7 @@ fn from_tcx(opaque: clean::OpaqueTy, tcx: TyCtxt<'_>) -> Self {
impl FromWithTcx<clean::Static> for Static {
fn from_tcx(stat: clean::Static, tcx: TyCtxt<'_>) -> Self {
Static {
type_: stat.type_.into_tcx(tcx),
type_: (*stat.type_).into_tcx(tcx),
mutable: stat.mutability == ast::Mutability::Mut,
expr: stat
.expr

View File

@ -62,7 +62,7 @@ pub(crate) fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -
| clean::AssocTypeItem(..)
| clean::TypeAliasItem(_)
| clean::StaticItem(_)
| clean::ConstantItem(_, _, _)
| clean::ConstantItem(..)
| clean::ExternCrateItem { .. }
| clean::ImportItem(_)
| clean::PrimitiveItem(_)

View File

@ -28,7 +28,7 @@ fn visit_inner_recur(&mut self, kind: &ItemKind) {
| TypeAliasItem(_)
| OpaqueTyItem(_)
| StaticItem(_)
| ConstantItem(_, _, _)
| ConstantItem(..)
| TraitAliasItem(_)
| TyMethodItem(_)
| MethodItem(_, _)