Skip to content

Commit 3b97b75

Browse files
committed
Simplify
1 parent 3a66230 commit 3b97b75

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

src/tools/rust-analyzer/crates/hir-def/src/generics.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -444,22 +444,26 @@ impl GenericParamsCollector {
444444

445445
impl GenericParams {
446446
/// Number of Generic parameters (type_or_consts + lifetimes)
447+
#[inline]
447448
pub fn len(&self) -> usize {
448449
self.type_or_consts.len() + self.lifetimes.len()
449450
}
450451

452+
#[inline]
451453
pub fn is_empty(&self) -> bool {
452454
self.len() == 0
453455
}
454456

455457
/// Iterator of type_or_consts field
458+
#[inline]
456459
pub fn iter_type_or_consts(
457460
&self,
458461
) -> impl DoubleEndedIterator<Item = (LocalTypeOrConstParamId, &TypeOrConstParamData)> {
459462
self.type_or_consts.iter()
460463
}
461464

462465
/// Iterator of lifetimes field
466+
#[inline]
463467
pub fn iter_lt(
464468
&self,
465469
) -> impl DoubleEndedIterator<Item = (LocalLifetimeParamId, &LifetimeParamData)> {
@@ -608,7 +612,7 @@ impl GenericParams {
608612
})
609613
}
610614

611-
pub fn find_trait_self_param(&self) -> Option<LocalTypeOrConstParamId> {
615+
pub fn trait_self_param(&self) -> Option<LocalTypeOrConstParamId> {
612616
if self.type_or_consts.is_empty() {
613617
return None;
614618
}

src/tools/rust-analyzer/crates/hir-def/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,7 @@ pub enum GenericDefId {
923923
ImplId(ImplId),
924924
// enum variants cannot have generics themselves, but their parent enums
925925
// can, and this makes some code easier to write
926+
// FIXME: Try to remove this as that will reduce the amount of query slots generated per enum?
926927
EnumVariantId(EnumVariantId),
927928
// consts can have type parameters from their parents (i.e. associated consts of traits)
928929
ConstId(ConstId),

src/tools/rust-analyzer/crates/hir-ty/src/generics.rs

+19-24
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,17 @@ impl Generics {
5050
}
5151

5252
pub(crate) fn iter_id(&self) -> impl Iterator<Item = GenericParamId> + '_ {
53-
self.iter().map(|(id, _)| id)
53+
self.iter_self_id().chain(self.iter_parent_id())
5454
}
5555

5656
pub(crate) fn iter_self_id(&self) -> impl Iterator<Item = GenericParamId> + '_ {
5757
self.iter_self().map(|(id, _)| id)
5858
}
5959

60+
fn iter_parent_id(&self) -> impl Iterator<Item = GenericParamId> + '_ {
61+
self.iter_parent().map(|(id, _)| id)
62+
}
63+
6064
pub(crate) fn iter_self_type_or_consts(
6165
&self,
6266
) -> impl DoubleEndedIterator<Item = (LocalTypeOrConstParamId, &TypeOrConstParamData)> {
@@ -81,7 +85,7 @@ impl Generics {
8185
}
8286

8387
/// Iterator over types and const params of parent.
84-
pub(crate) fn iter_parent(
88+
fn iter_parent(
8589
&self,
8690
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamDataRef<'_>)> + '_ {
8791
self.parent_generics().into_iter().flat_map(|it| {
@@ -108,7 +112,6 @@ impl Generics {
108112
let mut type_params = 0;
109113
let mut impl_trait_params = 0;
110114
let mut const_params = 0;
111-
let mut lifetime_params = 0;
112115
self.params.iter_type_or_consts().for_each(|(_, data)| match data {
113116
TypeOrConstParamData::TypeParamData(p) => match p.provenance {
114117
TypeParamProvenance::TypeParamList => type_params += 1,
@@ -118,52 +121,44 @@ impl Generics {
118121
TypeOrConstParamData::ConstParamData(_) => const_params += 1,
119122
});
120123

121-
self.params.iter_lt().for_each(|(_, _)| lifetime_params += 1);
124+
let lifetime_params = self.params.iter_lt().count();
122125

123126
let parent_len = self.parent_generics().map_or(0, Generics::len);
124127
(parent_len, self_param, type_params, const_params, impl_trait_params, lifetime_params)
125128
}
126129

127130
pub(crate) fn type_or_const_param_idx(&self, param: TypeOrConstParamId) -> Option<usize> {
128-
Some(self.find_type_or_const_param(param)?.0)
131+
self.find_type_or_const_param(param)
129132
}
130133

131-
fn find_type_or_const_param(
132-
&self,
133-
param: TypeOrConstParamId,
134-
) -> Option<(usize, &TypeOrConstParamData)> {
134+
fn find_type_or_const_param(&self, param: TypeOrConstParamId) -> Option<usize> {
135135
if param.parent == self.def {
136136
let idx = param.local_id.into_raw().into_u32() as usize;
137-
if idx >= self.params.type_or_consts.len() {
138-
return None;
139-
}
140-
Some((idx, &self.params.type_or_consts[param.local_id]))
137+
debug_assert!(idx <= self.params.type_or_consts.len());
138+
Some(idx)
141139
} else {
140+
debug_assert_eq!(self.parent_generics().map(|it| it.def), Some(param.parent));
142141
self.parent_generics()
143142
.and_then(|g| g.find_type_or_const_param(param))
144143
// Remember that parent parameters come after parameters for self.
145-
.map(|(idx, data)| (self.len_self() + idx, data))
144+
.map(|idx| self.len_self() + idx)
146145
}
147146
}
148147

149148
pub(crate) fn lifetime_idx(&self, lifetime: LifetimeParamId) -> Option<usize> {
150-
Some(self.find_lifetime(lifetime)?.0)
149+
self.find_lifetime(lifetime)
151150
}
152151

153-
fn find_lifetime(&self, lifetime: LifetimeParamId) -> Option<(usize, &LifetimeParamData)> {
152+
fn find_lifetime(&self, lifetime: LifetimeParamId) -> Option<usize> {
154153
if lifetime.parent == self.def {
155154
let idx = lifetime.local_id.into_raw().into_u32() as usize;
156-
if idx >= self.params.lifetimes.len() {
157-
return None;
158-
}
159-
Some((
160-
self.params.type_or_consts.len() + idx,
161-
&self.params.lifetimes[lifetime.local_id],
162-
))
155+
debug_assert!(idx <= self.params.lifetimes.len());
156+
Some(self.params.type_or_consts.len() + idx)
163157
} else {
158+
debug_assert_eq!(self.parent_generics().map(|it| it.def), Some(lifetime.parent));
164159
self.parent_generics()
165160
.and_then(|g| g.find_lifetime(lifetime))
166-
.map(|(idx, data)| (self.len_self() + idx, data))
161+
.map(|idx| self.len_self() + idx)
167162
}
168163
}
169164

src/tools/rust-analyzer/crates/hir-ty/src/utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl Iterator for ClauseElaborator<'_> {
155155
fn direct_super_traits(db: &dyn DefDatabase, trait_: TraitId, cb: impl FnMut(TraitId)) {
156156
let resolver = trait_.resolver(db);
157157
let generic_params = db.generic_params(trait_.into());
158-
let trait_self = generic_params.find_trait_self_param();
158+
let trait_self = generic_params.trait_self_param();
159159
generic_params
160160
.where_predicates
161161
.iter()
@@ -188,7 +188,7 @@ fn direct_super_traits(db: &dyn DefDatabase, trait_: TraitId, cb: impl FnMut(Tra
188188

189189
fn direct_super_trait_refs(db: &dyn HirDatabase, trait_ref: &TraitRef, cb: impl FnMut(TraitRef)) {
190190
let generic_params = db.generic_params(trait_ref.hir_trait_id().into());
191-
let trait_self = match generic_params.find_trait_self_param() {
191+
let trait_self = match generic_params.trait_self_param() {
192192
Some(p) => TypeOrConstParamId { parent: trait_ref.hir_trait_id().into(), local_id: p },
193193
None => return,
194194
};

0 commit comments

Comments
 (0)