Skip to content

Commit 15d7556

Browse files
committed
Auto merge of #102928 - notriddle:notriddle/rustdoc-thin-vec, r=GuillaumeGomez
rustdoc: use ThinVec for cleaned generics Depends on Gankra/thin-vec#38
2 parents 6718ea1 + eb29696 commit 15d7556

File tree

15 files changed

+72
-75
lines changed

15 files changed

+72
-75
lines changed

Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -4887,9 +4887,9 @@ checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
48874887

48884888
[[package]]
48894889
name = "thin-vec"
4890-
version = "0.2.8"
4890+
version = "0.2.9"
48914891
source = "registry+https://github.com/rust-lang/crates.io-index"
4892-
checksum = "104c2cb3180b6fb6d5b2278768e9b88b578d32ba751ea6e8d026688a40d7ed87"
4892+
checksum = "ceb05e71730d396f960f8f3901cdb41be2d339b303e9d7d3a07c5ff0536e671b"
48934893

48944894
[[package]]
48954895
name = "thiserror"

compiler/rustc_ast/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ rustc_macros = { path = "../rustc_macros" }
1414
rustc_serialize = { path = "../rustc_serialize" }
1515
rustc_span = { path = "../rustc_span" }
1616
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
17-
thin-vec = "0.2.8"
17+
thin-vec = "0.2.9"
1818
tracing = "0.1"

compiler/rustc_ast_lowering/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ rustc_session = { path = "../rustc_session" }
2121
rustc_span = { path = "../rustc_span" }
2222
rustc_target = { path = "../rustc_target" }
2323
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
24-
thin-vec = "0.2.8"
24+
thin-vec = "0.2.9"
2525
tracing = "0.1"

compiler/rustc_builtin_macros/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ rustc_session = { path = "../rustc_session" }
2323
rustc_span = { path = "../rustc_span" }
2424
rustc_target = { path = "../rustc_target" }
2525
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
26-
thin-vec = "0.2.8"
26+
thin-vec = "0.2.9"
2727
tracing = "0.1"

compiler/rustc_data_structures/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ smallvec = { version = "1.8.1", features = ["const_generics", "union", "may_dang
2525
stable_deref_trait = "1.0.0"
2626
stacker = "0.1.14"
2727
tempfile = "3.2"
28-
thin-vec = "0.2.8"
28+
thin-vec = "0.2.9"
2929
tracing = "0.1"
3030

3131
[dependencies.parking_lot]

compiler/rustc_middle/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ rustc_span = { path = "../rustc_span" }
3232
rustc_target = { path = "../rustc_target" }
3333
rustc_type_ir = { path = "../rustc_type_ir" }
3434
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
35-
thin-vec = "0.2.8"
35+
thin-vec = "0.2.9"
3636
tracing = "0.1"
3737

3838
[features]

compiler/rustc_query_impl/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ rustc_serialize = { path = "../rustc_serialize" }
2121
rustc_session = { path = "../rustc_session" }
2222
rustc_span = { path = "../rustc_span" }
2323
rustc_target = { path = "../rustc_target" }
24-
thin-vec = "0.2.8"
24+
thin-vec = "0.2.9"
2525
tracing = "0.1"
2626

2727
[features]

compiler/rustc_query_system/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ rustc_span = { path = "../rustc_span" }
2222
rustc_target = { path = "../rustc_target" }
2323
rustc_type_ir = { path = "../rustc_type_ir" }
2424
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
25-
thin-vec = "0.2.8"
25+
thin-vec = "0.2.9"
2626
tracing = "0.1"
2727

2828
[features]

compiler/rustc_serialize/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2021"
66
[dependencies]
77
indexmap = "1.9.1"
88
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
9-
thin-vec = "0.2.8"
9+
thin-vec = "0.2.9"
1010

1111
[dev-dependencies]
1212
rustc_macros = { path = "../rustc_macros" }

src/librustdoc/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ serde_json = "1.0"
2020
serde = { version = "1.0", features = ["derive"] }
2121
smallvec = "1.8.1"
2222
tempfile = "3"
23-
thin-vec = "0.2.8"
23+
thin-vec = "0.2.9"
2424
tracing = "0.1"
2525
tracing-tree = "0.2.0"
2626

src/librustdoc/clean/auto_trait.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_hir as hir;
33
use rustc_hir::lang_items::LangItem;
44
use rustc_middle::ty::{self, Region, RegionVid, TypeFoldable, TypeSuperFoldable};
55
use rustc_trait_selection::traits::auto_trait::{self, AutoTraitResult};
6+
use thin_vec::ThinVec;
67

78
use std::fmt::Debug;
89

@@ -110,7 +111,7 @@ where
110111
);
111112
let params = raw_generics.params;
112113

113-
Generics { params, where_predicates: Vec::new() }
114+
Generics { params, where_predicates: ThinVec::new() }
114115
}
115116
AutoTraitResult::ExplicitImpl => return None,
116117
};
@@ -183,7 +184,7 @@ where
183184
fn handle_lifetimes<'cx>(
184185
regions: &RegionConstraintData<'cx>,
185186
names_map: &FxHashMap<Symbol, Lifetime>,
186-
) -> Vec<WherePredicate> {
187+
) -> ThinVec<WherePredicate> {
187188
// Our goal is to 'flatten' the list of constraints by eliminating
188189
// all intermediate RegionVids. At the end, all constraints should
189190
// be between Regions (aka region variables). This gives us the information
@@ -429,7 +430,7 @@ where
429430
&mut self,
430431
item_def_id: DefId,
431432
param_env: ty::ParamEnv<'tcx>,
432-
mut existing_predicates: Vec<WherePredicate>,
433+
mut existing_predicates: ThinVec<WherePredicate>,
433434
vid_to_region: FxHashMap<ty::RegionVid, ty::Region<'tcx>>,
434435
) -> Generics {
435436
debug!(
@@ -663,7 +664,7 @@ where
663664
/// both for visual consistency between 'rustdoc' runs, and to
664665
/// make writing tests much easier
665666
#[inline]
666-
fn sort_where_predicates(&self, predicates: &mut Vec<WherePredicate>) {
667+
fn sort_where_predicates(&self, predicates: &mut [WherePredicate]) {
667668
// We should never have identical bounds - and if we do,
668669
// they're visually identical as well. Therefore, using
669670
// an unstable sort is fine.
@@ -710,7 +711,7 @@ where
710711
/// approach is probably somewhat slower, but the small number of items
711712
/// involved (impls rarely have more than a few bounds) means that it
712713
/// shouldn't matter in practice.
713-
fn unstable_debug_sort<T: Debug>(&self, vec: &mut Vec<T>) {
714+
fn unstable_debug_sort<T: Debug>(&self, vec: &mut [T]) {
714715
vec.sort_by_cached_key(|x| format!("{:?}", x))
715716
}
716717

src/librustdoc/clean/mod.rs

+47-53
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ pub(crate) fn clean_generics<'tcx>(
601601
})
602602
.collect::<Vec<_>>();
603603

604-
let mut params = Vec::with_capacity(gens.params.len());
604+
let mut params = ThinVec::with_capacity(gens.params.len());
605605
for p in gens.params.iter().filter(|p| !is_impl_trait(p) && !is_elided_lifetime(p)) {
606606
let p = clean_generic_param(cx, Some(gens), p);
607607
params.push(p);
@@ -675,7 +675,7 @@ fn clean_ty_generics<'tcx>(
675675
}
676676
ty::GenericParamDefKind::Const { .. } => Some(clean_generic_param_def(param, cx)),
677677
})
678-
.collect::<Vec<GenericParamDef>>();
678+
.collect::<ThinVec<GenericParamDef>>();
679679

680680
// param index -> [(trait DefId, associated type name & generics, type, higher-ranked params)]
681681
let mut impl_trait_proj =
@@ -1080,7 +1080,7 @@ fn clean_trait_item<'tcx>(trait_item: &hir::TraitItem<'tcx>, cx: &mut DocContext
10801080
hir::TraitItemKind::Type(bounds, None) => {
10811081
let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx));
10821082
let bounds = bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect();
1083-
TyAssocTypeItem(Box::new(generics), bounds)
1083+
TyAssocTypeItem(generics, bounds)
10841084
}
10851085
};
10861086
Item::from_def_id_and_parts(local_did, Some(trait_item.ident.name), inner, cx)
@@ -1211,56 +1211,47 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
12111211
tcx.generics_of(assoc_item.def_id),
12121212
ty::GenericPredicates { parent: None, predicates },
12131213
);
1214-
// Move bounds that are (likely) directly attached to the associated type
1215-
// from the where clause to the associated type.
1216-
// There is no guarantee that this is what the user actually wrote but we have
1217-
// no way of knowing.
1218-
let mut bounds = generics
1219-
.where_predicates
1220-
.drain_filter(|pred| match *pred {
1221-
WherePredicate::BoundPredicate {
1222-
ty: QPath(box QPathData { ref assoc, ref self_type, ref trait_, .. }),
1223-
..
1224-
} => {
1225-
if assoc.name != my_name {
1226-
return false;
1227-
}
1228-
if trait_.def_id() != assoc_item.container_id(tcx) {
1229-
return false;
1230-
}
1231-
match *self_type {
1232-
Generic(ref s) if *s == kw::SelfUpper => {}
1233-
_ => return false,
1234-
}
1235-
match &assoc.args {
1236-
GenericArgs::AngleBracketed { args, bindings } => {
1237-
if !bindings.is_empty()
1238-
|| generics
1239-
.params
1240-
.iter()
1241-
.zip(args.iter())
1242-
.any(|(param, arg)| !param_eq_arg(param, arg))
1243-
{
1244-
return false;
1245-
}
1246-
}
1247-
GenericArgs::Parenthesized { .. } => {
1248-
// The only time this happens is if we're inside the rustdoc for Fn(),
1249-
// which only has one associated type, which is not a GAT, so whatever.
1214+
// Filter out the bounds that are (likely?) directly attached to the associated type,
1215+
// as opposed to being located in the where clause.
1216+
let mut bounds: Vec<GenericBound> = Vec::new();
1217+
generics.where_predicates.retain_mut(|pred| match *pred {
1218+
WherePredicate::BoundPredicate {
1219+
ty: QPath(box QPathData { ref assoc, ref self_type, ref trait_, .. }),
1220+
bounds: ref mut pred_bounds,
1221+
..
1222+
} => {
1223+
if assoc.name != my_name {
1224+
return true;
1225+
}
1226+
if trait_.def_id() != assoc_item.container_id(tcx) {
1227+
return true;
1228+
}
1229+
match *self_type {
1230+
Generic(ref s) if *s == kw::SelfUpper => {}
1231+
_ => return true,
1232+
}
1233+
match &assoc.args {
1234+
GenericArgs::AngleBracketed { args, bindings } => {
1235+
if !bindings.is_empty()
1236+
|| generics
1237+
.params
1238+
.iter()
1239+
.zip(args.iter())
1240+
.any(|(param, arg)| !param_eq_arg(param, arg))
1241+
{
1242+
return true;
12501243
}
12511244
}
1252-
true
1253-
}
1254-
_ => false,
1255-
})
1256-
.flat_map(|pred| {
1257-
if let WherePredicate::BoundPredicate { bounds, .. } = pred {
1258-
bounds
1259-
} else {
1260-
unreachable!()
1245+
GenericArgs::Parenthesized { .. } => {
1246+
// The only time this happens is if we're inside the rustdoc for Fn(),
1247+
// which only has one associated type, which is not a GAT, so whatever.
1248+
}
12611249
}
1262-
})
1263-
.collect::<Vec<_>>();
1250+
bounds.extend(mem::replace(pred_bounds, Vec::new()));
1251+
false
1252+
}
1253+
_ => true,
1254+
});
12641255
// Our Sized/?Sized bound didn't get handled when creating the generics
12651256
// because we didn't actually get our whole set of bounds until just now
12661257
// (some of them may have come from the trait). If we do have a sized
@@ -1276,7 +1267,7 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
12761267
// (generic) associated type from the where clause to the respective parameter.
12771268
// There is no guarantee that this is what the user actually wrote but we have
12781269
// no way of knowing.
1279-
let mut where_predicates = Vec::new();
1270+
let mut where_predicates = ThinVec::new();
12801271
for mut pred in generics.where_predicates {
12811272
if let WherePredicate::BoundPredicate { ty: Generic(arg), bounds, .. } = &mut pred
12821273
&& let Some(GenericParamDef {
@@ -1306,7 +1297,7 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
13061297
bounds,
13071298
)
13081299
} else {
1309-
TyAssocTypeItem(Box::new(generics), bounds)
1300+
TyAssocTypeItem(generics, bounds)
13101301
}
13111302
} else {
13121303
// FIXME: when could this happen? Associated items in inherent impls?
@@ -1317,7 +1308,10 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
13171308
cx,
13181309
Some(assoc_item.def_id),
13191310
),
1320-
generics: Generics { params: Vec::new(), where_predicates: Vec::new() },
1311+
generics: Generics {
1312+
params: ThinVec::new(),
1313+
where_predicates: ThinVec::new(),
1314+
},
13211315
item_type: None,
13221316
}),
13231317
Vec::new(),

src/librustdoc/clean/simplify.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
use rustc_data_structures::fx::FxIndexMap;
1515
use rustc_hir::def_id::DefId;
1616
use rustc_middle::ty;
17+
use thin_vec::ThinVec;
1718

1819
use crate::clean;
1920
use crate::clean::GenericArgs as PP;
2021
use crate::clean::WherePredicate as WP;
2122
use crate::core::DocContext;
2223

23-
pub(crate) fn where_clauses(cx: &DocContext<'_>, clauses: Vec<WP>) -> Vec<WP> {
24+
pub(crate) fn where_clauses(cx: &DocContext<'_>, clauses: Vec<WP>) -> ThinVec<WP> {
2425
// First, partition the where clause into its separate components.
2526
//
2627
// We use `FxIndexMap` so that the insertion order is preserved to prevent messing up to
@@ -59,7 +60,7 @@ pub(crate) fn where_clauses(cx: &DocContext<'_>, clauses: Vec<WP>) -> Vec<WP> {
5960
});
6061

6162
// And finally, let's reassemble everything
62-
let mut clauses = Vec::new();
63+
let mut clauses = ThinVec::with_capacity(lifetimes.len() + tybounds.len() + equalities.len());
6364
clauses.extend(
6465
lifetimes.into_iter().map(|(lt, bounds)| WP::RegionPredicate { lifetime: lt, bounds }),
6566
);

src/librustdoc/clean/types.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ pub(crate) enum ItemKind {
795795
/// A required associated type in a trait declaration.
796796
///
797797
/// The bounds may be non-empty if there is a `where` clause.
798-
TyAssocTypeItem(Box<Generics>, Vec<GenericBound>),
798+
TyAssocTypeItem(Generics, Vec<GenericBound>),
799799
/// An associated type in a trait impl or a provided one in a trait declaration.
800800
AssocTypeItem(Box<Typedef>, Vec<GenericBound>),
801801
/// An item that has been stripped by a rustdoc pass
@@ -1462,8 +1462,8 @@ impl GenericParamDef {
14621462
// maybe use a Generic enum and use Vec<Generic>?
14631463
#[derive(Clone, Debug, Default)]
14641464
pub(crate) struct Generics {
1465-
pub(crate) params: Vec<GenericParamDef>,
1466-
pub(crate) where_predicates: Vec<WherePredicate>,
1465+
pub(crate) params: ThinVec<GenericParamDef>,
1466+
pub(crate) where_predicates: ThinVec<WherePredicate>,
14671467
}
14681468

14691469
impl Generics {
@@ -2594,6 +2594,7 @@ mod size_asserts {
25942594
static_assert_size!(GenericArg, 48);
25952595
static_assert_size!(GenericArgs, 32);
25962596
static_assert_size!(GenericParamDef, 56);
2597+
static_assert_size!(Generics, 16);
25972598
static_assert_size!(Item, 56);
25982599
static_assert_size!(ItemKind, 88);
25992600
static_assert_size!(PathSegment, 40);

src/librustdoc/json/conversions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
284284
ItemEnum::AssocConst { type_: ty.into_tcx(tcx), default: Some(default.expr(tcx)) }
285285
}
286286
TyAssocTypeItem(g, b) => ItemEnum::AssocType {
287-
generics: (*g).into_tcx(tcx),
287+
generics: g.into_tcx(tcx),
288288
bounds: b.into_tcx(tcx),
289289
default: None,
290290
},

0 commit comments

Comments
 (0)