Skip to content

Commit 4054c0f

Browse files
Reduce clean::Type size by replacing a DefId (only used to check for display) with a boolean
1 parent e1ec326 commit 4054c0f

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

src/librustdoc/clean/mod.rs

+26-7
Original file line numberDiff line numberDiff line change
@@ -384,15 +384,24 @@ impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
384384
let lifted = self.lift_to_tcx(cx.tcx).unwrap();
385385
let trait_ = lifted.trait_ref(cx.tcx).clean(cx);
386386
let self_type = self.self_ty().clean(cx);
387+
let self_def_id = self_type.def_id(&cx.cache);
388+
let should_show_cast = compute_should_show_cast(self_def_id, &trait_, &self_type);
387389
Type::QPath {
388390
assoc: Box::new(projection_to_path_segment(*self, cx)),
389-
self_def_id: self_type.def_id(&cx.cache),
391+
should_show_cast,
390392
self_type: box self_type,
391393
trait_,
392394
}
393395
}
394396
}
395397

398+
fn compute_should_show_cast(self_def_id: Option<DefId>, trait_: &Path, self_type: &Type) -> bool {
399+
!trait_.segments.is_empty()
400+
&& self_def_id
401+
.zip(Some(trait_.def_id()))
402+
.map_or(!self_type.is_self_type(), |(id, trait_)| id != trait_)
403+
}
404+
396405
fn projection_to_path_segment(ty: ty::ProjectionTy<'_>, cx: &mut DocContext<'_>) -> PathSegment {
397406
let item = cx.tcx.associated_item(ty.item_def_id);
398407
let generics = cx.tcx.generics_of(ty.item_def_id);
@@ -421,8 +430,12 @@ impl Clean<GenericParamDef> for ty::GenericParamDef {
421430
// the cleaning process of the type itself. To resolve this and have the
422431
// `self_def_id` set, we override it here.
423432
// See https://github.com/rust-lang/rust/issues/85454
424-
if let QPath { ref mut self_def_id, .. } = default {
425-
*self_def_id = Some(cx.tcx.parent(self.def_id));
433+
if let QPath { ref mut should_show_cast, ref trait_, ref self_type, .. } =
434+
default
435+
{
436+
let self_def_id = cx.tcx.parent(self.def_id);
437+
*should_show_cast =
438+
compute_should_show_cast(self_def_id, trait_, self_type);
426439
}
427440

428441
Some(default)
@@ -1309,10 +1322,13 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
13091322
segments: trait_segments.iter().map(|x| x.clean(cx)).collect(),
13101323
};
13111324
register_res(cx, trait_.res);
1325+
let self_def_id = DefId::local(qself.hir_id.owner.local_def_index);
1326+
let self_type = qself.clean(cx);
1327+
let should_show_cast = compute_should_show_cast(Some(self_def_id), &trait_, &self_type);
13121328
Type::QPath {
13131329
assoc: Box::new(p.segments.last().expect("segments were empty").clean(cx)),
1314-
self_def_id: Some(DefId::local(qself.hir_id.owner.local_def_index)),
1315-
self_type: box qself.clean(cx),
1330+
should_show_cast,
1331+
self_type: box self_type,
13161332
trait_,
13171333
}
13181334
}
@@ -1326,10 +1342,13 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
13261342
};
13271343
let trait_ = hir::Path { span, res, segments: &[] }.clean(cx);
13281344
register_res(cx, trait_.res);
1345+
let self_def_id = res.opt_def_id();
1346+
let self_type = qself.clean(cx);
1347+
let should_show_cast = compute_should_show_cast(self_def_id, &trait_, &self_type);
13291348
Type::QPath {
13301349
assoc: Box::new(segment.clean(cx)),
1331-
self_def_id: res.opt_def_id(),
1332-
self_type: box qself.clean(cx),
1350+
should_show_cast,
1351+
self_type: box self_type,
13331352
trait_,
13341353
}
13351354
}

src/librustdoc/clean/types.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1560,10 +1560,8 @@ crate enum Type {
15601560
QPath {
15611561
assoc: Box<PathSegment>,
15621562
self_type: Box<Type>,
1563-
/// FIXME: This is a hack that should be removed; see [this discussion][1].
1564-
///
1565-
/// [1]: https://github.com/rust-lang/rust/pull/85479#discussion_r635729093
1566-
self_def_id: Option<DefId>,
1563+
/// FIXME: compute this field on demand.
1564+
should_show_cast: bool,
15671565
trait_: Path,
15681566
},
15691567

@@ -1576,7 +1574,7 @@ crate enum Type {
15761574

15771575
// `Type` is used a lot. Make sure it doesn't unintentionally get bigger.
15781576
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
1579-
rustc_data_structures::static_assert_size!(Type, 80);
1577+
rustc_data_structures::static_assert_size!(Type, 72);
15801578

15811579
impl Type {
15821580
/// When comparing types for equality, it can help to ignore `&` wrapping.
@@ -2180,7 +2178,7 @@ crate enum GenericArg {
21802178
// `GenericArg` can occur many times in a single `Path`, so make sure it
21812179
// doesn't increase in size unexpectedly.
21822180
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
2183-
rustc_data_structures::static_assert_size!(GenericArg, 88);
2181+
rustc_data_structures::static_assert_size!(GenericArg, 80);
21842182

21852183
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
21862184
crate enum GenericArgs {

src/librustdoc/html/format.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -982,11 +982,7 @@ fn fmt_type<'cx>(
982982
write!(f, "impl {}", print_generic_bounds(bounds, cx))
983983
}
984984
}
985-
clean::QPath { ref assoc, ref self_type, ref trait_, ref self_def_id } => {
986-
let should_show_cast = !trait_.segments.is_empty()
987-
&& self_def_id
988-
.zip(Some(trait_.def_id()))
989-
.map_or(!self_type.is_self_type(), |(id, trait_)| id != trait_);
985+
clean::QPath { ref assoc, ref self_type, ref trait_, should_show_cast } => {
990986
if f.alternate() {
991987
if should_show_cast {
992988
write!(f, "<{:#} as {:#}>::", self_type.print(cx), trait_.print(cx))?

0 commit comments

Comments
 (0)