Skip to content

Commit 39fbcdf

Browse files
committed
rustdoc: use strategic ThinVec/Box to shrink clean::ItemKind
1 parent a526d7c commit 39fbcdf

File tree

10 files changed

+111
-68
lines changed

10 files changed

+111
-68
lines changed

src/librustdoc/clean/inline.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ pub(crate) fn try_inline(
131131
Res::Def(DefKind::Const, did) => {
132132
record_extern_fqn(cx, did, ItemType::Constant);
133133
cx.with_param_env(did, |cx| {
134-
let (generics, ty, ct) = build_const_item(cx, did);
135-
clean::ConstantItem(generics, Box::new(ty), ct)
134+
let ct = build_const_item(cx, did);
135+
clean::ConstantItem(Box::new(ct))
136136
})
137137
}
138138
Res::Def(DefKind::Macro(kind), did) => {
@@ -723,7 +723,7 @@ pub(crate) fn print_inlined_const(tcx: TyCtxt<'_>, did: DefId) -> String {
723723
fn build_const_item(
724724
cx: &mut DocContext<'_>,
725725
def_id: DefId,
726-
) -> (clean::Generics, clean::Type, clean::Constant) {
726+
) -> clean::Constant {
727727
let mut generics =
728728
clean_ty_generics(cx, cx.tcx.generics_of(def_id), cx.tcx.explicit_predicates_of(def_id));
729729
clean::simplify::move_bounds_to_generic_parameters(&mut generics);
@@ -733,17 +733,17 @@ fn build_const_item(
733733
None,
734734
None,
735735
);
736-
(generics, ty, clean::Constant { kind: clean::ConstantKind::Extern { def_id } })
736+
clean::Constant { generics, type_: ty, kind: clean::ConstantKind::Extern { def_id } }
737737
}
738738

739739
fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::Static {
740740
clean::Static {
741-
type_: clean_middle_ty(
741+
type_: Box::new(clean_middle_ty(
742742
ty::Binder::dummy(cx.tcx.type_of(did).instantiate_identity()),
743743
cx,
744744
Some(did),
745745
None,
746-
),
746+
)),
747747
mutability: if mutable { Mutability::Mut } else { Mutability::Not },
748748
expr: None,
749749
}

src/librustdoc/clean/mod.rs

+31-26
Original file line numberDiff line numberDiff line change
@@ -287,23 +287,23 @@ fn clean_lifetime<'tcx>(lifetime: &hir::Lifetime, cx: &mut DocContext<'tcx>) ->
287287
pub(crate) fn clean_const<'tcx>(
288288
constant: &hir::ConstArg<'tcx>,
289289
_cx: &mut DocContext<'tcx>,
290-
) -> Constant {
290+
) -> ConstantKind {
291291
match &constant.kind {
292292
hir::ConstArgKind::Path(qpath) => {
293-
Constant { kind: ConstantKind::Path { path: qpath_to_string(&qpath).into() } }
293+
ConstantKind::Path { path: qpath_to_string(&qpath).into() }
294294
}
295295
hir::ConstArgKind::Anon(anon) => {
296-
Constant { kind: ConstantKind::Anonymous { body: anon.body } }
296+
ConstantKind::Anonymous { body: anon.body }
297297
}
298298
}
299299
}
300300

301301
pub(crate) fn clean_middle_const<'tcx>(
302302
constant: ty::Binder<'tcx, ty::Const<'tcx>>,
303303
_cx: &mut DocContext<'tcx>,
304-
) -> Constant {
304+
) -> ConstantKind {
305305
// FIXME: instead of storing the stringified expression, store `self` directly instead.
306-
Constant { kind: ConstantKind::TyConst { expr: constant.skip_binder().to_string().into() } }
306+
ConstantKind::TyConst { expr: constant.skip_binder().to_string().into() }
307307
}
308308

309309
pub(crate) fn clean_middle_region<'tcx>(region: ty::Region<'tcx>) -> Option<Lifetime> {
@@ -1231,12 +1231,11 @@ fn clean_trait_item<'tcx>(trait_item: &hir::TraitItem<'tcx>, cx: &mut DocContext
12311231
cx.with_param_env(local_did, |cx| {
12321232
let inner = match trait_item.kind {
12331233
hir::TraitItemKind::Const(ty, Some(default)) => {
1234-
let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx));
1235-
AssocConstItem(
1236-
generics,
1237-
Box::new(clean_ty(ty, cx)),
1238-
ConstantKind::Local { def_id: local_did, body: default },
1239-
)
1234+
AssocConstItem(Box::new(Constant {
1235+
generics: enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx)),
1236+
kind: ConstantKind::Local { def_id: local_did, body: default },
1237+
type_: clean_ty(ty, cx),
1238+
}))
12401239
}
12411240
hir::TraitItemKind::Const(ty, None) => {
12421241
let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx));
@@ -1283,9 +1282,11 @@ pub(crate) fn clean_impl_item<'tcx>(
12831282
cx.with_param_env(local_did, |cx| {
12841283
let inner = match impl_.kind {
12851284
hir::ImplItemKind::Const(ty, expr) => {
1286-
let generics = clean_generics(impl_.generics, cx);
1287-
let default = ConstantKind::Local { def_id: local_did, body: expr };
1288-
AssocConstItem(generics, Box::new(clean_ty(ty, cx)), default)
1285+
AssocConstItem(Box::new(Constant {
1286+
generics: clean_generics(impl_.generics, cx),
1287+
kind: ConstantKind::Local { def_id: local_did, body: expr },
1288+
type_: clean_ty(ty, cx),
1289+
}))
12891290
}
12901291
hir::ImplItemKind::Fn(ref sig, body) => {
12911292
let m = clean_function(cx, sig, impl_.generics, FunctionArgs::Body(body));
@@ -1320,12 +1321,12 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
13201321
let tcx = cx.tcx;
13211322
let kind = match assoc_item.kind {
13221323
ty::AssocKind::Const => {
1323-
let ty = Box::new(clean_middle_ty(
1324+
let ty = clean_middle_ty(
13241325
ty::Binder::dummy(tcx.type_of(assoc_item.def_id).instantiate_identity()),
13251326
cx,
13261327
Some(assoc_item.def_id),
13271328
None,
1328-
));
1329+
);
13291330

13301331
let mut generics = clean_ty_generics(
13311332
cx,
@@ -1339,9 +1340,13 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
13391340
ty::TraitContainer => tcx.defaultness(assoc_item.def_id).has_value(),
13401341
};
13411342
if provided {
1342-
AssocConstItem(generics, ty, ConstantKind::Extern { def_id: assoc_item.def_id })
1343+
AssocConstItem(Box::new(Constant {
1344+
generics,
1345+
kind: ConstantKind::Extern { def_id: assoc_item.def_id },
1346+
type_: ty,
1347+
}))
13431348
} else {
1344-
TyAssocConstItem(generics, ty)
1349+
TyAssocConstItem(generics, Box::new(ty))
13451350
}
13461351
}
13471352
ty::AssocKind::Fn => {
@@ -1397,7 +1402,7 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
13971402
{
13981403
true
13991404
}
1400-
(GenericParamDefKind::Const { .. }, GenericArg::Const(c)) => match &c.kind {
1405+
(GenericParamDefKind::Const { .. }, GenericArg::Const(c)) => match &**c {
14011406
ConstantKind::TyConst { expr } => **expr == *param.name.as_str(),
14021407
_ => false,
14031408
},
@@ -2745,13 +2750,13 @@ fn clean_maybe_renamed_item<'tcx>(
27452750
cx.with_param_env(def_id, |cx| {
27462751
let kind = match item.kind {
27472752
ItemKind::Static(ty, mutability, body_id) => {
2748-
StaticItem(Static { type_: clean_ty(ty, cx), mutability, expr: Some(body_id) })
2753+
StaticItem(Static { type_: Box::new(clean_ty(ty, cx)), mutability, expr: Some(body_id) })
27492754
}
2750-
ItemKind::Const(ty, generics, body_id) => ConstantItem(
2751-
clean_generics(generics, cx),
2752-
Box::new(clean_ty(ty, cx)),
2753-
Constant { kind: ConstantKind::Local { body: body_id, def_id } },
2754-
),
2755+
ItemKind::Const(ty, generics, body_id) => ConstantItem(Box::new(Constant {
2756+
generics: clean_generics(generics, cx),
2757+
type_: clean_ty(ty, cx),
2758+
kind: ConstantKind::Local { body: body_id, def_id }
2759+
})),
27552760
ItemKind::OpaqueTy(ref ty) => OpaqueTyItem(OpaqueTy {
27562761
bounds: ty.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
27572762
generics: clean_generics(ty.generics, cx),
@@ -3109,7 +3114,7 @@ fn clean_maybe_renamed_foreign_item<'tcx>(
31093114
ForeignFunctionItem(Box::new(Function { decl, generics }), safety)
31103115
}
31113116
hir::ForeignItemKind::Static(ty, mutability, safety) => ForeignStaticItem(
3112-
Static { type_: clean_ty(ty, cx), mutability, expr: None },
3117+
Static { type_: Box::new(clean_ty(ty, cx)), mutability, expr: None },
31133118
safety,
31143119
),
31153120
hir::ForeignItemKind::Type => ForeignTypeItem,

src/librustdoc/clean/types.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -852,9 +852,9 @@ pub(crate) enum ItemKind {
852852
PrimitiveItem(PrimitiveType),
853853
/// A required associated constant in a trait declaration.
854854
TyAssocConstItem(Generics, Box<Type>),
855-
ConstantItem(Generics, Box<Type>, Constant),
855+
ConstantItem(Box<Constant>),
856856
/// An associated constant in a trait impl or a provided one in a trait declaration.
857-
AssocConstItem(Generics, Box<Type>, ConstantKind),
857+
AssocConstItem(Box<Constant>),
858858
/// A required associated type in a trait declaration.
859859
///
860860
/// The bounds may be non-empty if there is a `where` clause.
@@ -888,7 +888,7 @@ impl ItemKind {
888888
| TypeAliasItem(_)
889889
| OpaqueTyItem(_)
890890
| StaticItem(_)
891-
| ConstantItem(_, _, _)
891+
| ConstantItem(_)
892892
| TraitAliasItem(_)
893893
| TyMethodItem(_)
894894
| MethodItem(_, _)
@@ -922,7 +922,7 @@ impl ItemKind {
922922
| TypeAliasItem(_)
923923
| OpaqueTyItem(_)
924924
| StaticItem(_)
925-
| ConstantItem(_, _, _)
925+
| ConstantItem(_)
926926
| TraitAliasItem(_)
927927
| ForeignFunctionItem(_, _)
928928
| ForeignStaticItem(_, _)
@@ -2050,7 +2050,7 @@ impl From<hir::PrimTy> for PrimitiveType {
20502050
pub(crate) struct Struct {
20512051
pub(crate) ctor_kind: Option<CtorKind>,
20522052
pub(crate) generics: Generics,
2053-
pub(crate) fields: Vec<Item>,
2053+
pub(crate) fields: ThinVec<Item>,
20542054
}
20552055

20562056
impl Struct {
@@ -2076,7 +2076,7 @@ impl Union {
20762076
/// only as a variant in an enum.
20772077
#[derive(Clone, Debug)]
20782078
pub(crate) struct VariantStruct {
2079-
pub(crate) fields: Vec<Item>,
2079+
pub(crate) fields: ThinVec<Item>,
20802080
}
20812081

20822082
impl VariantStruct {
@@ -2110,7 +2110,7 @@ pub(crate) struct Variant {
21102110
#[derive(Clone, Debug)]
21112111
pub(crate) enum VariantKind {
21122112
CLike,
2113-
Tuple(Vec<Item>),
2113+
Tuple(ThinVec<Item>),
21142114
Struct(VariantStruct),
21152115
}
21162116

@@ -2246,7 +2246,7 @@ impl Path {
22462246
pub(crate) enum GenericArg {
22472247
Lifetime(Lifetime),
22482248
Type(Type),
2249-
Const(Box<Constant>),
2249+
Const(Box<ConstantKind>),
22502250
Infer,
22512251
}
22522252

@@ -2359,20 +2359,22 @@ pub(crate) struct BareFunctionDecl {
23592359

23602360
#[derive(Clone, Debug)]
23612361
pub(crate) struct Static {
2362-
pub(crate) type_: Type,
2362+
pub(crate) type_: Box<Type>,
23632363
pub(crate) mutability: Mutability,
23642364
pub(crate) expr: Option<BodyId>,
23652365
}
23662366

23672367
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
23682368
pub(crate) struct Constant {
2369+
pub(crate) generics: Generics,
23692370
pub(crate) kind: ConstantKind,
2371+
pub(crate) type_: Type,
23702372
}
23712373

23722374
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
23732375
pub(crate) enum Term {
23742376
Type(Type),
2375-
Constant(Constant),
2377+
Constant(ConstantKind),
23762378
}
23772379

23782380
impl Term {
@@ -2594,7 +2596,7 @@ mod size_asserts {
25942596
static_assert_size!(GenericParamDef, 40);
25952597
static_assert_size!(Generics, 16);
25962598
static_assert_size!(Item, 56);
2597-
static_assert_size!(ItemKind, 56);
2599+
static_assert_size!(ItemKind, 48);
25982600
static_assert_size!(PathSegment, 40);
25992601
static_assert_size!(Type, 32);
26002602
// tidy-alphabetical-end

src/librustdoc/fold.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub(crate) trait DocFolder: Sized {
7979
| FunctionItem(_)
8080
| OpaqueTyItem(_)
8181
| StaticItem(_)
82-
| ConstantItem(_, _, _)
82+
| ConstantItem(..)
8383
| TraitAliasItem(_)
8484
| TyMethodItem(_)
8585
| MethodItem(_, _)

src/librustdoc/html/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ impl clean::Lifetime {
375375
}
376376
}
377377

378-
impl clean::Constant {
378+
impl clean::ConstantKind {
379379
pub(crate) fn print(&self, tcx: TyCtxt<'_>) -> impl Display + '_ {
380380
let expr = self.expr(tcx);
381381
display_fn(

src/librustdoc/html/render/mod.rs

+38-13
Original file line numberDiff line numberDiff line change
@@ -1090,17 +1090,25 @@ fn render_assoc_item(
10901090
clean::MethodItem(m, _) => {
10911091
assoc_method(w, item, &m.generics, &m.decl, link, parent, cx, render_mode)
10921092
}
1093-
kind @ (clean::TyAssocConstItem(generics, ty) | clean::AssocConstItem(generics, ty, _)) => {
1093+
clean::TyAssocConstItem(generics, ty) => {
10941094
assoc_const(
10951095
w,
10961096
item,
10971097
generics,
10981098
ty,
1099-
match kind {
1100-
clean::TyAssocConstItem(..) => None,
1101-
clean::AssocConstItem(.., default) => Some(default),
1102-
_ => unreachable!(),
1103-
},
1099+
None,
1100+
link,
1101+
if parent == ItemType::Trait { 4 } else { 0 },
1102+
cx,
1103+
)
1104+
}
1105+
clean::AssocConstItem(ci) => {
1106+
assoc_const(
1107+
w,
1108+
item,
1109+
&ci.generics,
1110+
&ci.type_,
1111+
Some(&ci.kind),
11041112
link,
11051113
if parent == ItemType::Trait { 4 } else { 0 },
11061114
cx,
@@ -1690,8 +1698,7 @@ fn render_impl(
16901698
w.write_str("</h4></section>");
16911699
}
16921700
}
1693-
kind @ (clean::TyAssocConstItem(generics, ty)
1694-
| clean::AssocConstItem(generics, ty, _)) => {
1701+
clean::TyAssocConstItem(generics, ty) => {
16951702
let source_id = format!("{item_type}.{name}");
16961703
let id = cx.derive_id(&source_id);
16971704
write!(w, "<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">");
@@ -1706,11 +1713,29 @@ fn render_impl(
17061713
item,
17071714
generics,
17081715
ty,
1709-
match kind {
1710-
clean::TyAssocConstItem(..) => None,
1711-
clean::AssocConstItem(.., default) => Some(default),
1712-
_ => unreachable!(),
1713-
},
1716+
None,
1717+
link.anchor(if trait_.is_some() { &source_id } else { &id }),
1718+
0,
1719+
cx,
1720+
);
1721+
w.write_str("</h4></section>");
1722+
}
1723+
clean::AssocConstItem(ci) => {
1724+
let source_id = format!("{item_type}.{name}");
1725+
let id = cx.derive_id(&source_id);
1726+
write!(w, "<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">");
1727+
render_rightside(w, cx, item, render_mode);
1728+
if trait_.is_some() {
1729+
// Anchors are only used on trait impls.
1730+
write!(w, "<a href=\"#{id}\" class=\"anchor\">§</a>");
1731+
}
1732+
w.write_str("<h4 class=\"code-header\">");
1733+
assoc_const(
1734+
w,
1735+
item,
1736+
&ci.generics,
1737+
&ci.type_,
1738+
Some(&ci.kind),
17141739
link.anchor(if trait_.is_some() { &source_id } else { &id }),
17151740
0,
17161741
cx,

src/librustdoc/html/render/print_item.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ pub(super) fn print_item(cx: &mut Context<'_>, item: &clean::Item, buf: &mut Buf
267267
clean::PrimitiveItem(_) => item_primitive(buf, cx, item),
268268
clean::StaticItem(ref i) => item_static(buf, cx, item, i, None),
269269
clean::ForeignStaticItem(ref i, safety) => item_static(buf, cx, item, i, Some(*safety)),
270-
clean::ConstantItem(generics, ty, c) => item_constant(buf, cx, item, generics, ty, c),
270+
clean::ConstantItem(ci) => item_constant(buf, cx, item, &ci.generics, &ci.type_, &ci.kind),
271271
clean::ForeignTypeItem => item_foreign_type(buf, cx, item),
272272
clean::KeywordItem => item_keyword(buf, cx, item),
273273
clean::OpaqueTyItem(ref e) => item_opaque_ty(buf, cx, item, e),
@@ -1841,7 +1841,7 @@ fn item_constant(
18411841
it: &clean::Item,
18421842
generics: &clean::Generics,
18431843
ty: &clean::Type,
1844-
c: &clean::Constant,
1844+
c: &clean::ConstantKind,
18451845
) {
18461846
wrap_item(w, |w| {
18471847
let tcx = cx.tcx();
@@ -1911,7 +1911,7 @@ fn item_fields(
19111911
w: &mut Buffer,
19121912
cx: &mut Context<'_>,
19131913
it: &clean::Item,
1914-
fields: &Vec<clean::Item>,
1914+
fields: &[clean::Item],
19151915
ctor_kind: Option<CtorKind>,
19161916
) {
19171917
let mut fields = fields

0 commit comments

Comments
 (0)