Skip to content

Commit 74f9097

Browse files
committed
rustdoc: make the Type enum smaller
1 parent d7b8d77 commit 74f9097

File tree

8 files changed

+57
-40
lines changed

8 files changed

+57
-40
lines changed

src/librustdoc/clean/auto_trait.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_trait_selection::traits::auto_trait::{self, AutoTraitResult};
66

77
use std::fmt::Debug;
88

9-
use super::*;
9+
use crate::clean::{self, *};
1010

1111
#[derive(Eq, PartialEq, Hash, Copy, Clone, Debug)]
1212
enum RegionTarget<'tcx> {
@@ -485,7 +485,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
485485
// of the type.
486486
// Therefore, we make sure that we never add a ?Sized
487487
// bound for projections
488-
if let Type::QPath { .. } = ty {
488+
if let Type::QPath(_) = ty {
489489
has_sized.insert(ty.clone());
490490
}
491491

@@ -546,13 +546,18 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
546546
}
547547
WherePredicate::EqPredicate { lhs, rhs } => {
548548
match lhs {
549-
Type::QPath { ref assoc, ref self_type, ref trait_, .. } => {
549+
Type::QPath(box clean::QPathData {
550+
ref assoc,
551+
ref self_type,
552+
ref trait_,
553+
..
554+
}) => {
550555
let ty = &*self_type;
551556
let mut new_trait = trait_.clone();
552557

553558
if self.is_fn_trait(trait_) && assoc.name == sym::Output {
554559
ty_to_fn
555-
.entry(*ty.clone())
560+
.entry(ty.clone())
556561
.and_modify(|e| {
557562
*e = (e.0.clone(), Some(rhs.ty().unwrap().clone()))
558563
})
@@ -571,7 +576,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
571576
// to 'T: Iterator<Item=u8>'
572577
GenericArgs::AngleBracketed { ref mut bindings, .. } => {
573578
bindings.push(TypeBinding {
574-
assoc: *assoc.clone(),
579+
assoc: assoc.clone(),
575580
kind: TypeBindingKind::Equality { term: rhs },
576581
});
577582
}
@@ -585,7 +590,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
585590
}
586591
}
587592

588-
let bounds = ty_to_bounds.entry(*ty.clone()).or_default();
593+
let bounds = ty_to_bounds.entry(ty.clone()).or_default();
589594

590595
bounds.insert(GenericBound::TraitBound(
591596
PolyTrait { trait_: new_trait, generic_params: Vec::new() },
@@ -602,7 +607,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
602607
));
603608
// Avoid creating any new duplicate bounds later in the outer
604609
// loop
605-
ty_to_traits.entry(*ty.clone()).or_default().insert(trait_.clone());
610+
ty_to_traits.entry(ty.clone()).or_default().insert(trait_.clone());
606611
}
607612
_ => panic!("Unexpected LHS {:?} for {:?}", lhs, item_def_id),
608613
}

src/librustdoc/clean/inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean:
640640

641641
g.where_predicates.retain(|pred| match pred {
642642
clean::WherePredicate::BoundPredicate {
643-
ty: clean::QPath { self_type: box clean::Generic(ref s), trait_, .. },
643+
ty: clean::QPath(box clean::QPathData { self_type: clean::Generic(ref s), trait_, .. }),
644644
bounds,
645645
..
646646
} => !(bounds.is_empty() || *s == kw::SelfUpper && trait_.def_id() == trait_did),

src/librustdoc/clean/mod.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,12 @@ fn clean_projection<'tcx>(
395395
self_type.def_id(&cx.cache)
396396
};
397397
let should_show_cast = compute_should_show_cast(self_def_id, &trait_, &self_type);
398-
Type::QPath {
399-
assoc: Box::new(projection_to_path_segment(ty, cx)),
398+
Type::QPath(Box::new(QPathData {
399+
assoc: projection_to_path_segment(ty, cx),
400400
should_show_cast,
401-
self_type: box self_type,
401+
self_type: self_type,
402402
trait_,
403-
}
403+
}))
404404
}
405405

406406
impl<'tcx> Clean<'tcx, Type> for ty::ProjectionTy<'tcx> {
@@ -1180,7 +1180,10 @@ impl<'tcx> Clean<'tcx, Item> for ty::AssocItem {
11801180
.where_predicates
11811181
.drain_filter(|pred| match *pred {
11821182
WherePredicate::BoundPredicate {
1183-
ty: QPath { ref assoc, ref self_type, ref trait_, .. },
1183+
ty:
1184+
QPath(box QPathData {
1185+
ref assoc, ref self_type, ref trait_, ..
1186+
}),
11841187
..
11851188
} => {
11861189
if assoc.name != my_name {
@@ -1189,7 +1192,7 @@ impl<'tcx> Clean<'tcx, Item> for ty::AssocItem {
11891192
if trait_.def_id() != self.container.id() {
11901193
return false;
11911194
}
1192-
match **self_type {
1195+
match *self_type {
11931196
Generic(ref s) if *s == kw::SelfUpper => {}
11941197
_ => return false,
11951198
}
@@ -1315,12 +1318,12 @@ fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type
13151318
let self_def_id = DefId::local(qself.hir_id.owner.local_def_index);
13161319
let self_type = qself.clean(cx);
13171320
let should_show_cast = compute_should_show_cast(Some(self_def_id), &trait_, &self_type);
1318-
Type::QPath {
1319-
assoc: Box::new(p.segments.last().expect("segments were empty").clean(cx)),
1321+
Type::QPath(Box::new(QPathData {
1322+
assoc: p.segments.last().expect("segments were empty").clean(cx),
13201323
should_show_cast,
1321-
self_type: box self_type,
1324+
self_type,
13221325
trait_,
1323-
}
1326+
}))
13241327
}
13251328
hir::QPath::TypeRelative(qself, segment) => {
13261329
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
@@ -1335,12 +1338,12 @@ fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type
13351338
let self_def_id = res.opt_def_id();
13361339
let self_type = qself.clean(cx);
13371340
let should_show_cast = compute_should_show_cast(self_def_id, &trait_, &self_type);
1338-
Type::QPath {
1339-
assoc: Box::new(segment.clean(cx)),
1341+
Type::QPath(Box::new(QPathData {
1342+
assoc: segment.clean(cx),
13401343
should_show_cast,
1341-
self_type: box self_type,
1344+
self_type,
13421345
trait_,
1343-
}
1346+
}))
13441347
}
13451348
hir::QPath::LangItem(..) => bug!("clean: requiring documentation of lang item"),
13461349
}

src/librustdoc/clean/types.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,13 +1564,7 @@ pub(crate) enum Type {
15641564
BorrowedRef { lifetime: Option<Lifetime>, mutability: Mutability, type_: Box<Type> },
15651565

15661566
/// A qualified path to an associated item: `<Type as Trait>::Name`
1567-
QPath {
1568-
assoc: Box<PathSegment>,
1569-
self_type: Box<Type>,
1570-
/// FIXME: compute this field on demand.
1571-
should_show_cast: bool,
1572-
trait_: Path,
1573-
},
1567+
QPath(Box<QPathData>),
15741568

15751569
/// A type that is inferred: `_`
15761570
Infer,
@@ -1581,7 +1575,16 @@ pub(crate) enum Type {
15811575

15821576
// `Type` is used a lot. Make sure it doesn't unintentionally get bigger.
15831577
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
1584-
rustc_data_structures::static_assert_size!(Type, 72);
1578+
rustc_data_structures::static_assert_size!(Type, 56);
1579+
1580+
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
1581+
pub(crate) struct QPathData {
1582+
pub(crate) assoc: PathSegment,
1583+
pub(crate) self_type: Type,
1584+
/// FIXME: compute this field on demand.
1585+
pub(crate) should_show_cast: bool,
1586+
pub(crate) trait_: Path,
1587+
}
15851588

15861589
impl Type {
15871590
/// When comparing types for equality, it can help to ignore `&` wrapping.
@@ -1676,8 +1679,9 @@ impl Type {
16761679
}
16771680

16781681
pub(crate) fn projection(&self) -> Option<(&Type, DefId, PathSegment)> {
1679-
if let QPath { self_type, trait_, assoc, .. } = self {
1680-
Some((self_type, trait_.def_id(), *assoc.clone()))
1682+
if let QPath(qpath) = self {
1683+
let QPathData { self_type, trait_, assoc, .. } = &**qpath;
1684+
Some((self_type, trait_.def_id(), assoc.clone()))
16811685
} else {
16821686
None
16831687
}
@@ -1701,7 +1705,7 @@ impl Type {
17011705
Slice(..) => PrimitiveType::Slice,
17021706
Array(..) => PrimitiveType::Array,
17031707
RawPointer(..) => PrimitiveType::RawPointer,
1704-
QPath { ref self_type, .. } => return self_type.inner_def_id(cache),
1708+
QPath(ref qpath) => return qpath.self_type.inner_def_id(cache),
17051709
Generic(_) | Infer | ImplTrait(_) => return None,
17061710
};
17071711
cache.and_then(|c| Primitive(t).def_id(c))
@@ -2222,7 +2226,7 @@ pub(crate) enum GenericArg {
22222226
// `GenericArg` can occur many times in a single `Path`, so make sure it
22232227
// doesn't increase in size unexpectedly.
22242228
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
2225-
rustc_data_structures::static_assert_size!(GenericArg, 80);
2229+
rustc_data_structures::static_assert_size!(GenericArg, 64);
22262230

22272231
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
22282232
pub(crate) enum GenericArgs {

src/librustdoc/html/format.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,12 @@ fn fmt_type<'cx>(
980980
write!(f, "impl {}", print_generic_bounds(bounds, cx))
981981
}
982982
}
983-
clean::QPath { ref assoc, ref self_type, ref trait_, should_show_cast } => {
983+
clean::QPath(box clean::QPathData {
984+
ref assoc,
985+
ref self_type,
986+
ref trait_,
987+
should_show_cast,
988+
}) => {
984989
if f.alternate() {
985990
if should_show_cast {
986991
write!(f, "<{:#} as {:#}>::", self_type.print(cx), trait_.print(cx))?

src/librustdoc/html/render/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,8 +2629,8 @@ fn collect_paths_for_type(first_ty: clean::Type, cache: &Cache) -> Vec<String> {
26292629
clean::Type::BorrowedRef { type_, .. } => {
26302630
work.push_back(*type_);
26312631
}
2632-
clean::Type::QPath { self_type, trait_, .. } => {
2633-
work.push_back(*self_type);
2632+
clean::Type::QPath(box clean::QPathData { self_type, trait_, .. }) => {
2633+
work.push_back(self_type);
26342634
process_path(trait_.def_id());
26352635
}
26362636
_ => {}

src/librustdoc/html/render/search_index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ fn get_index_type_name(clean_type: &clean::Type) -> Option<Symbol> {
235235
| clean::Tuple(_)
236236
| clean::Slice(_)
237237
| clean::Array(_, _)
238-
| clean::QPath { .. }
238+
| clean::QPath(_)
239239
| clean::Infer => None,
240240
}
241241
}

src/librustdoc/json/conversions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,13 +473,13 @@ impl FromWithTcx<clean::Type> for Type {
473473
mutable: mutability == ast::Mutability::Mut,
474474
type_: Box::new((*type_).into_tcx(tcx)),
475475
},
476-
QPath { assoc, self_type, trait_, .. } => {
476+
QPath(box clean::QPathData { assoc, self_type, trait_, .. }) => {
477477
// FIXME: should `trait_` be a clean::Path equivalent in JSON?
478478
let trait_ = clean::Type::Path { path: trait_ }.into_tcx(tcx);
479479
Type::QualifiedPath {
480480
name: assoc.name.to_string(),
481481
args: Box::new(assoc.args.clone().into_tcx(tcx)),
482-
self_type: Box::new((*self_type).into_tcx(tcx)),
482+
self_type: Box::new(self_type.into_tcx(tcx)),
483483
trait_: Box::new(trait_),
484484
}
485485
}

0 commit comments

Comments
 (0)