Skip to content
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

rustdoc: use strategic ThinVec/Box to shrink clean::ItemKind #128263

Merged
merged 1 commit into from
Jul 28, 2024
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
15 changes: 6 additions & 9 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
Expand All @@ -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,
}
Expand Down
69 changes: 35 additions & 34 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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 => {
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 13 additions & 11 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -888,7 +888,7 @@ impl ItemKind {
| TypeAliasItem(_)
| OpaqueTyItem(_)
| StaticItem(_)
| ConstantItem(_, _, _)
| ConstantItem(_)
| TraitAliasItem(_)
| TyMethodItem(_)
| MethodItem(_, _)
Expand Down Expand Up @@ -922,7 +922,7 @@ impl ItemKind {
| TypeAliasItem(_)
| OpaqueTyItem(_)
| StaticItem(_)
| ConstantItem(_, _, _)
| ConstantItem(_)
| TraitAliasItem(_)
| ForeignFunctionItem(_, _)
| ForeignStaticItem(_, _)
Expand Down Expand Up @@ -2050,7 +2050,7 @@ impl From<hir::PrimTy> for 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 {
Expand All @@ -2076,7 +2076,7 @@ impl Union {
/// 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 {
Expand Down Expand Up @@ -2110,7 +2110,7 @@ pub(crate) struct Variant {
#[derive(Clone, Debug)]
pub(crate) enum VariantKind {
CLike,
Tuple(Vec<Item>),
Tuple(ThinVec<Item>),
Struct(VariantStruct),
}

Expand Down Expand Up @@ -2246,7 +2246,7 @@ impl Path {
pub(crate) enum GenericArg {
Lifetime(Lifetime),
Type(Type),
Const(Box<Constant>),
Const(Box<ConstantKind>),
Infer,
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub(crate) trait DocFolder: Sized {
| FunctionItem(_)
| OpaqueTyItem(_)
| StaticItem(_)
| ConstantItem(_, _, _)
| ConstantItem(..)
| TraitAliasItem(_)
| TyMethodItem(_)
| MethodItem(_, _)
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
67 changes: 44 additions & 23 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1690,8 +1694,7 @@ fn render_impl(
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}\">");
Expand All @@ -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("</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,
Expand Down
Loading
Loading