From 3abf0ba4fca59894b2b7fcafde347a3497c22e85 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Fri, 26 Jul 2024 23:47:09 -0700 Subject: [PATCH] rustdoc: use strategic ThinVec/Box to shrink `clean::ItemKind` --- src/librustdoc/clean/inline.rs | 15 ++-- src/librustdoc/clean/mod.rs | 69 ++++++++++--------- src/librustdoc/clean/types.rs | 24 ++++--- src/librustdoc/fold.rs | 2 +- src/librustdoc/html/format.rs | 2 +- src/librustdoc/html/render/mod.rs | 67 +++++++++++------- src/librustdoc/html/render/print_item.rs | 6 +- src/librustdoc/json/conversions.rs | 20 ++++-- .../passes/check_doc_test_visibility.rs | 2 +- src/librustdoc/visit.rs | 2 +- 10 files changed, 120 insertions(+), 89 deletions(-) diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 0024e246ef007..dbdc4a5ae7188 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -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, } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 26011926cddda..aaefac32711f1 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -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 { @@ -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 @@ pub(crate) fn clean_middle_assoc_item<'tcx>( { 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, diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 3709953159686..92fe98130e859 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -852,9 +852,9 @@ pub(crate) enum ItemKind { PrimitiveItem(PrimitiveType), /// A required associated constant in a trait declaration. TyAssocConstItem(Generics, Box), - ConstantItem(Generics, Box, Constant), + ConstantItem(Box), /// An associated constant in a trait impl or a provided one in a trait declaration. - AssocConstItem(Generics, Box, ConstantKind), + AssocConstItem(Box), /// A required associated type in a trait declaration. /// /// The bounds may be non-empty if there is a `where` clause. @@ -888,7 +888,7 @@ impl ItemKind { | TypeAliasItem(_) | OpaqueTyItem(_) | StaticItem(_) - | ConstantItem(_, _, _) + | ConstantItem(_) | TraitAliasItem(_) | TyMethodItem(_) | MethodItem(_, _) @@ -922,7 +922,7 @@ impl ItemKind { | TypeAliasItem(_) | OpaqueTyItem(_) | StaticItem(_) - | ConstantItem(_, _, _) + | ConstantItem(_) | TraitAliasItem(_) | ForeignFunctionItem(_, _) | ForeignStaticItem(_, _) @@ -2050,7 +2050,7 @@ impl From for PrimitiveType { pub(crate) struct Struct { pub(crate) ctor_kind: Option, pub(crate) generics: Generics, - pub(crate) fields: Vec, + pub(crate) fields: ThinVec, } impl Struct { @@ -2076,7 +2076,7 @@ impl Union { /// only as a variant in an enum. #[derive(Clone, Debug)] pub(crate) struct VariantStruct { - pub(crate) fields: Vec, + pub(crate) fields: ThinVec, } impl VariantStruct { @@ -2110,7 +2110,7 @@ pub(crate) struct Variant { #[derive(Clone, Debug)] pub(crate) enum VariantKind { CLike, - Tuple(Vec), + Tuple(ThinVec), Struct(VariantStruct), } @@ -2246,7 +2246,7 @@ impl Path { pub(crate) enum GenericArg { Lifetime(Lifetime), Type(Type), - Const(Box), + Const(Box), Infer, } @@ -2359,20 +2359,22 @@ pub(crate) struct BareFunctionDecl { #[derive(Clone, Debug)] pub(crate) struct Static { - pub(crate) type_: Type, + pub(crate) type_: Box, pub(crate) mutability: Mutability, pub(crate) expr: Option, } #[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 diff --git a/src/librustdoc/fold.rs b/src/librustdoc/fold.rs index 346e9a4e113a5..beb7686e29c56 100644 --- a/src/librustdoc/fold.rs +++ b/src/librustdoc/fold.rs @@ -79,7 +79,7 @@ pub(crate) trait DocFolder: Sized { | FunctionItem(_) | OpaqueTyItem(_) | StaticItem(_) - | ConstantItem(_, _, _) + | ConstantItem(..) | TraitAliasItem(_) | TyMethodItem(_) | MethodItem(_, _) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 055781f7fed72..18ee04a2f4d5f 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -375,7 +375,7 @@ impl clean::Lifetime { } } -impl clean::Constant { +impl clean::ConstantKind { pub(crate) fn print(&self, tcx: TyCtxt<'_>) -> impl Display + '_ { let expr = self.expr(tcx); display_fn( diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 5b9ef67109cf2..47712264e972d 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -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 render_impl( w.write_str(""); } } - 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, "
"); @@ -1706,11 +1709,29 @@ fn render_impl( 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("
"); + } + clean::AssocConstItem(ci) => { + let source_id = format!("{item_type}.{name}"); + let id = cx.derive_id(&source_id); + write!(w, "
"); + render_rightside(w, cx, item, render_mode); + if trait_.is_some() { + // Anchors are only used on trait impls. + write!(w, "ยง"); + } + w.write_str("

"); + assoc_const( + w, + item, + &ci.generics, + &ci.type_, + Some(&ci.kind), link.anchor(if trait_.is_some() { &source_id } else { &id }), 0, cx, diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index cf78a1d223c89..0cf62ccf6e7ff 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -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, + fields: &[clean::Item], ctor_kind: Option, ) { let mut fields = fields diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index 4ab0df3670859..a4731feb8de00 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -188,6 +188,16 @@ impl FromWithTcx for Constant { } } +impl FromWithTcx 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 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 @@ impl FromWithTcx for OpaqueTy { impl FromWithTcx 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 diff --git a/src/librustdoc/passes/check_doc_test_visibility.rs b/src/librustdoc/passes/check_doc_test_visibility.rs index 0437f5e5fd818..f97c417847359 100644 --- a/src/librustdoc/passes/check_doc_test_visibility.rs +++ b/src/librustdoc/passes/check_doc_test_visibility.rs @@ -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(_) diff --git a/src/librustdoc/visit.rs b/src/librustdoc/visit.rs index b335dc5bd16b8..de836439be957 100644 --- a/src/librustdoc/visit.rs +++ b/src/librustdoc/visit.rs @@ -28,7 +28,7 @@ pub(crate) trait DocVisitor: Sized { | TypeAliasItem(_) | OpaqueTyItem(_) | StaticItem(_) - | ConstantItem(_, _, _) + | ConstantItem(..) | TraitAliasItem(_) | TyMethodItem(_) | MethodItem(_, _)