Skip to content

Commit 2aa4aa7

Browse files
committed
rustdoc: factor Type::QPath out into its own box
This reduces the size of Type.
1 parent a39bdb1 commit 2aa4aa7

File tree

7 files changed

+47
-40
lines changed

7 files changed

+47
-40
lines changed

src/librustdoc/clean/auto_trait.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -551,13 +551,15 @@ where
551551
}
552552
WherePredicate::EqPredicate { lhs, rhs } => {
553553
match lhs {
554-
Type::QPath { ref assoc, ref self_type, ref trait_, .. } => {
554+
Type::QPath(box QPathData {
555+
ref assoc, ref self_type, ref trait_, ..
556+
}) => {
555557
let ty = &*self_type;
556558
let mut new_trait = trait_.clone();
557559

558560
if self.is_fn_trait(trait_) && assoc.name == sym::Output {
559561
ty_to_fn
560-
.entry(*ty.clone())
562+
.entry(ty.clone())
561563
.and_modify(|e| {
562564
*e = (e.0.clone(), Some(rhs.ty().unwrap().clone()))
563565
})
@@ -582,7 +584,7 @@ where
582584
// to 'T: Iterator<Item=u8>'
583585
GenericArgs::AngleBracketed { ref mut bindings, .. } => {
584586
bindings.push(TypeBinding {
585-
assoc: *assoc.clone(),
587+
assoc: assoc.clone(),
586588
kind: TypeBindingKind::Equality { term: rhs },
587589
});
588590
}
@@ -596,7 +598,7 @@ where
596598
}
597599
}
598600

599-
let bounds = ty_to_bounds.entry(*ty.clone()).or_default();
601+
let bounds = ty_to_bounds.entry(ty.clone()).or_default();
600602

601603
bounds.insert(GenericBound::TraitBound(
602604
PolyTrait { trait_: new_trait, generic_params: Vec::new() },
@@ -613,7 +615,7 @@ where
613615
));
614616
// Avoid creating any new duplicate bounds later in the outer
615617
// loop
616-
ty_to_traits.entry(*ty.clone()).or_default().insert(trait_.clone());
618+
ty_to_traits.entry(ty.clone()).or_default().insert(trait_.clone());
617619
}
618620
_ => panic!("Unexpected LHS {:?} for {:?}", lhs, item_def_id),
619621
}

src/librustdoc/clean/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean:
672672

673673
g.where_predicates.retain(|pred| match pred {
674674
clean::WherePredicate::BoundPredicate {
675-
ty: clean::QPath { self_type: box clean::Generic(ref s), trait_, .. },
675+
ty: clean::QPath(box clean::QPathData { self_type: clean::Generic(ref s), trait_, .. }),
676676
bounds,
677677
..
678678
} => !(bounds.is_empty() || *s == kw::SelfUpper && trait_.def_id() == trait_did),

src/librustdoc/clean/mod.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -410,12 +410,12 @@ fn clean_projection<'tcx>(
410410
self_type.def_id(&cx.cache)
411411
};
412412
let should_show_cast = compute_should_show_cast(self_def_id, &trait_, &self_type);
413-
Type::QPath {
414-
assoc: Box::new(projection_to_path_segment(ty, cx)),
413+
Type::QPath(Box::new(QPathData {
414+
assoc: projection_to_path_segment(ty, cx),
415415
should_show_cast,
416-
self_type: Box::new(self_type),
416+
self_type,
417417
trait_,
418-
}
418+
}))
419419
}
420420

421421
fn compute_should_show_cast(self_def_id: Option<DefId>, trait_: &Path, self_type: &Type) -> bool {
@@ -1182,7 +1182,7 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
11821182
.where_predicates
11831183
.drain_filter(|pred| match *pred {
11841184
WherePredicate::BoundPredicate {
1185-
ty: QPath { ref assoc, ref self_type, ref trait_, .. },
1185+
ty: QPath(box QPathData { ref assoc, ref self_type, ref trait_, .. }),
11861186
..
11871187
} => {
11881188
if assoc.name != my_name {
@@ -1191,7 +1191,7 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
11911191
if trait_.def_id() != assoc_item.container_id(tcx) {
11921192
return false;
11931193
}
1194-
match **self_type {
1194+
match *self_type {
11951195
Generic(ref s) if *s == kw::SelfUpper => {}
11961196
_ => return false,
11971197
}
@@ -1324,15 +1324,12 @@ fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type
13241324
let self_def_id = DefId::local(qself.hir_id.owner.local_def_index);
13251325
let self_type = clean_ty(qself, cx);
13261326
let should_show_cast = compute_should_show_cast(Some(self_def_id), &trait_, &self_type);
1327-
Type::QPath {
1328-
assoc: Box::new(clean_path_segment(
1329-
p.segments.last().expect("segments were empty"),
1330-
cx,
1331-
)),
1327+
Type::QPath(Box::new(QPathData {
1328+
assoc: clean_path_segment(p.segments.last().expect("segments were empty"), cx),
13321329
should_show_cast,
1333-
self_type: Box::new(self_type),
1330+
self_type,
13341331
trait_,
1335-
}
1332+
}))
13361333
}
13371334
hir::QPath::TypeRelative(qself, segment) => {
13381335
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
@@ -1347,12 +1344,12 @@ fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type
13471344
let self_def_id = res.opt_def_id();
13481345
let self_type = clean_ty(qself, cx);
13491346
let should_show_cast = compute_should_show_cast(self_def_id, &trait_, &self_type);
1350-
Type::QPath {
1351-
assoc: Box::new(clean_path_segment(segment, cx)),
1347+
Type::QPath(Box::new(QPathData {
1348+
assoc: clean_path_segment(segment, cx),
13521349
should_show_cast,
1353-
self_type: Box::new(self_type),
1350+
self_type,
13541351
trait_,
1355-
}
1352+
}))
13561353
}
13571354
hir::QPath::LangItem(..) => bug!("clean: requiring documentation of lang item"),
13581355
}

src/librustdoc/clean/types.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -1556,13 +1556,7 @@ pub(crate) enum Type {
15561556
BorrowedRef { lifetime: Option<Lifetime>, mutability: Mutability, type_: Box<Type> },
15571557

15581558
/// A qualified path to an associated item: `<Type as Trait>::Name`
1559-
QPath {
1560-
assoc: Box<PathSegment>,
1561-
self_type: Box<Type>,
1562-
/// FIXME: compute this field on demand.
1563-
should_show_cast: bool,
1564-
trait_: Path,
1565-
},
1559+
QPath(Box<QPathData>),
15661560

15671561
/// A type that is inferred: `_`
15681562
Infer,
@@ -1660,8 +1654,8 @@ impl Type {
16601654
}
16611655

16621656
pub(crate) fn projection(&self) -> Option<(&Type, DefId, PathSegment)> {
1663-
if let QPath { self_type, trait_, assoc, .. } = self {
1664-
Some((self_type, trait_.def_id(), *assoc.clone()))
1657+
if let QPath(box QPathData { self_type, trait_, assoc, .. }) = self {
1658+
Some((self_type, trait_.def_id(), assoc.clone()))
16651659
} else {
16661660
None
16671661
}
@@ -1685,7 +1679,7 @@ impl Type {
16851679
Slice(..) => PrimitiveType::Slice,
16861680
Array(..) => PrimitiveType::Array,
16871681
RawPointer(..) => PrimitiveType::RawPointer,
1688-
QPath { ref self_type, .. } => return self_type.inner_def_id(cache),
1682+
QPath(box QPathData { ref self_type, .. }) => return self_type.inner_def_id(cache),
16891683
Generic(_) | Infer | ImplTrait(_) => return None,
16901684
};
16911685
cache.and_then(|c| Primitive(t).def_id(c))
@@ -1699,6 +1693,15 @@ impl Type {
16991693
}
17001694
}
17011695

1696+
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
1697+
pub(crate) struct QPathData {
1698+
pub assoc: PathSegment,
1699+
pub self_type: Type,
1700+
/// FIXME: compute this field on demand.
1701+
pub should_show_cast: bool,
1702+
pub trait_: Path,
1703+
}
1704+
17021705
/// A primitive (aka, builtin) type.
17031706
///
17041707
/// This represents things like `i32`, `str`, etc.
@@ -2490,11 +2493,11 @@ mod size_asserts {
24902493
// These are in alphabetical order, which is easy to maintain.
24912494
static_assert_size!(Crate, 72); // frequently moved by-value
24922495
static_assert_size!(DocFragment, 32);
2493-
static_assert_size!(GenericArg, 80);
2496+
static_assert_size!(GenericArg, 64);
24942497
static_assert_size!(GenericArgs, 32);
24952498
static_assert_size!(GenericParamDef, 56);
24962499
static_assert_size!(Item, 56);
24972500
static_assert_size!(ItemKind, 112);
24982501
static_assert_size!(PathSegment, 40);
2499-
static_assert_size!(Type, 72);
2502+
static_assert_size!(Type, 56);
25002503
}

src/librustdoc/html/format.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,12 @@ fn fmt_type<'cx>(
10791079
write!(f, "impl {}", print_generic_bounds(bounds, cx))
10801080
}
10811081
}
1082-
clean::QPath { ref assoc, ref self_type, ref trait_, should_show_cast } => {
1082+
clean::QPath(box clean::QPathData {
1083+
ref assoc,
1084+
ref self_type,
1085+
ref trait_,
1086+
should_show_cast,
1087+
}) => {
10831088
if f.alternate() {
10841089
if should_show_cast {
10851090
write!(f, "<{:#} as {:#}>::", self_type.print(cx), trait_.print(cx))?

src/librustdoc/html/render/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2623,8 +2623,8 @@ fn collect_paths_for_type(first_ty: clean::Type, cache: &Cache) -> Vec<String> {
26232623
clean::Type::BorrowedRef { type_, .. } => {
26242624
work.push_back(*type_);
26252625
}
2626-
clean::Type::QPath { self_type, trait_, .. } => {
2627-
work.push_back(*self_type);
2626+
clean::Type::QPath(box clean::QPathData { self_type, trait_, .. }) => {
2627+
work.push_back(self_type);
26282628
process_path(trait_.def_id());
26292629
}
26302630
_ => {}

src/librustdoc/json/conversions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,10 @@ impl FromWithTcx<clean::Type> for Type {
480480
mutable: mutability == ast::Mutability::Mut,
481481
type_: Box::new((*type_).into_tcx(tcx)),
482482
},
483-
QPath { assoc, self_type, trait_, .. } => Type::QualifiedPath {
483+
QPath(box clean::QPathData { assoc, self_type, trait_, .. }) => Type::QualifiedPath {
484484
name: assoc.name.to_string(),
485485
args: Box::new(assoc.args.clone().into_tcx(tcx)),
486-
self_type: Box::new((*self_type).into_tcx(tcx)),
486+
self_type: Box::new(self_type.into_tcx(tcx)),
487487
trait_: trait_.into_tcx(tcx),
488488
},
489489
}

0 commit comments

Comments
 (0)