Skip to content

Commit a82e028

Browse files
committed
Cleanup util::Generics method names
1 parent f3567bb commit a82e028

File tree

11 files changed

+82
-49
lines changed

11 files changed

+82
-49
lines changed

crates/hir-def/src/generics.rs

+23-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
//! generic parameters. See also the `Generics` type and the `generics_of` query
44
//! in rustc.
55
6+
use std::ops;
7+
68
use either::Either;
79
use hir_expand::{
810
name::{AsName, Name},
911
ExpandResult,
1012
};
1113
use intern::Interned;
12-
use la_arena::{Arena, Idx};
14+
use la_arena::Arena;
1315
use once_cell::unsync::Lazy;
1416
use stdx::impl_from;
1517
use syntax::ast::{self, HasGenericParams, HasName, HasTypeBounds};
@@ -23,7 +25,7 @@ use crate::{
2325
nameres::{DefMap, MacroSubNs},
2426
type_ref::{ConstRef, LifetimeRef, TypeBound, TypeRef},
2527
AdtId, ConstParamId, GenericDefId, HasModule, ItemTreeLoc, LifetimeParamId,
26-
LocalTypeOrConstParamId, Lookup, TypeOrConstParamId, TypeParamId,
28+
LocalLifetimeParamId, LocalTypeOrConstParamId, Lookup, TypeOrConstParamId, TypeParamId,
2729
};
2830

2931
/// Data about a generic type parameter (to a function, struct, impl, ...).
@@ -158,6 +160,20 @@ pub struct GenericParams {
158160
pub where_predicates: Box<[WherePredicate]>,
159161
}
160162

163+
impl ops::Index<LocalTypeOrConstParamId> for GenericParams {
164+
type Output = TypeOrConstParamData;
165+
fn index(&self, index: LocalTypeOrConstParamId) -> &TypeOrConstParamData {
166+
&self.type_or_consts[index]
167+
}
168+
}
169+
170+
impl ops::Index<LocalLifetimeParamId> for GenericParams {
171+
type Output = LifetimeParamData;
172+
fn index(&self, index: LocalLifetimeParamId) -> &LifetimeParamData {
173+
&self.lifetimes[index]
174+
}
175+
}
176+
161177
/// A single predicate from a where clause, i.e. `where Type: Trait`. Combined
162178
/// where clauses like `where T: Foo + Bar` are turned into multiple of these.
163179
/// It might still result in multiple actual predicates though, because of
@@ -199,7 +215,7 @@ impl GenericParamsCollector {
199215
lower_ctx: &LowerCtx<'_>,
200216
node: &dyn HasGenericParams,
201217
add_param_attrs: impl FnMut(
202-
Either<Idx<TypeOrConstParamData>, Idx<LifetimeParamData>>,
218+
Either<LocalTypeOrConstParamId, LocalLifetimeParamId>,
203219
ast::GenericParam,
204220
),
205221
) {
@@ -227,7 +243,7 @@ impl GenericParamsCollector {
227243
lower_ctx: &LowerCtx<'_>,
228244
params: ast::GenericParamList,
229245
mut add_param_attrs: impl FnMut(
230-
Either<Idx<TypeOrConstParamData>, Idx<LifetimeParamData>>,
246+
Either<LocalTypeOrConstParamId, LocalLifetimeParamId>,
231247
ast::GenericParam,
232248
),
233249
) {
@@ -416,16 +432,16 @@ impl GenericParams {
416432
}
417433

418434
/// Iterator of type_or_consts field
419-
pub fn iter(
435+
pub fn iter_type_or_consts(
420436
&self,
421-
) -> impl DoubleEndedIterator<Item = (Idx<TypeOrConstParamData>, &TypeOrConstParamData)> {
437+
) -> impl DoubleEndedIterator<Item = (LocalTypeOrConstParamId, &TypeOrConstParamData)> {
422438
self.type_or_consts.iter()
423439
}
424440

425441
/// Iterator of lifetimes field
426442
pub fn iter_lt(
427443
&self,
428-
) -> impl DoubleEndedIterator<Item = (Idx<LifetimeParamData>, &LifetimeParamData)> {
444+
) -> impl DoubleEndedIterator<Item = (LocalLifetimeParamId, &LifetimeParamData)> {
429445
self.lifetimes.iter()
430446
}
431447

crates/hir-ty/src/consteval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub(crate) fn path_to_const(
9090
}
9191
ParamLoweringMode::Variable => {
9292
let args = args();
93-
match args.as_ref().and_then(|args| args.param_idx(p.into())) {
93+
match args.as_ref().and_then(|args| args.type_or_const_param_idx(p.into())) {
9494
Some(it) => ConstValue::BoundVar(BoundVar::new(debruijn, it)),
9595
None => {
9696
never!(

crates/hir-ty/src/display.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ impl HirDisplay for Const {
453453
ConstValue::Placeholder(idx) => {
454454
let id = from_placeholder_idx(f.db, *idx);
455455
let generics = generics(f.db.upcast(), id.parent);
456-
let param_data = &generics.params.type_or_consts[id.local_id];
456+
let param_data = &generics.params[id.local_id];
457457
write!(f, "{}", param_data.name().unwrap().display(f.db.upcast()))?;
458458
Ok(())
459459
}
@@ -1176,7 +1176,7 @@ impl HirDisplay for Ty {
11761176
TyKind::Placeholder(idx) => {
11771177
let id = from_placeholder_idx(db, *idx);
11781178
let generics = generics(db.upcast(), id.parent);
1179-
let param_data = &generics.params.type_or_consts[id.local_id];
1179+
let param_data = &generics.params[id.local_id];
11801180
match param_data {
11811181
TypeOrConstParamData::TypeParamData(p) => match p.provenance {
11821182
TypeParamProvenance::TypeParamList | TypeParamProvenance::TraitSelf => {
@@ -1724,7 +1724,7 @@ impl HirDisplay for LifetimeData {
17241724
LifetimeData::Placeholder(idx) => {
17251725
let id = lt_from_placeholder_idx(f.db, *idx);
17261726
let generics = generics(f.db.upcast(), id.parent);
1727-
let param_data = &generics.params.lifetimes[id.local_id];
1727+
let param_data = &generics.params[id.local_id];
17281728
write!(f, "{}", param_data.name.display(f.db.upcast()))?;
17291729
Ok(())
17301730
}

crates/hir-ty/src/infer/closure.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ impl CapturedItemWithoutTy {
357357
outer_binder: DebruijnIndex,
358358
) -> Result<chalk_ir::Const<Interner>, Self::Error> {
359359
let x = from_placeholder_idx(self.db, idx);
360-
let Some(idx) = self.generics.param_idx(x) else {
360+
let Some(idx) = self.generics.type_or_const_param_idx(x) else {
361361
return Err(());
362362
};
363363
Ok(BoundVar::new(outer_binder, idx).to_const(Interner, ty))
@@ -369,7 +369,7 @@ impl CapturedItemWithoutTy {
369369
outer_binder: DebruijnIndex,
370370
) -> std::result::Result<Ty, Self::Error> {
371371
let x = from_placeholder_idx(self.db, idx);
372-
let Some(idx) = self.generics.param_idx(x) else {
372+
let Some(idx) = self.generics.type_or_const_param_idx(x) else {
373373
return Err(());
374374
};
375375
Ok(BoundVar::new(outer_binder, idx).to_ty(Interner))

crates/hir-ty/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ impl Hash for ConstScalar {
288288

289289
/// Return an index of a parameter in the generic type parameter list by it's id.
290290
pub fn param_idx(db: &dyn HirDatabase, id: TypeOrConstParamId) -> Option<usize> {
291-
generics(db.upcast(), id.parent).param_idx(id)
291+
generics(db.upcast(), id.parent).type_or_const_param_idx(id)
292292
}
293293

294294
pub(crate) fn wrap_empty_binders<T>(value: T) -> Binders<T>

crates/hir-ty/src/lower.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ impl<'a> TyLoweringContext<'a> {
578578
let idx = match self
579579
.generics()
580580
.expect("generics in scope")
581-
.param_idx(param_id.into())
581+
.type_or_const_param_idx(param_id.into())
582582
{
583583
None => {
584584
never!("no matching generics");
@@ -1026,7 +1026,7 @@ impl<'a> TyLoweringContext<'a> {
10261026
}
10271027
ParamLoweringMode::Variable => {
10281028
let idx = generics(self.db.upcast(), def)
1029-
.param_idx(param_id)
1029+
.type_or_const_param_idx(param_id)
10301030
.expect("matching generics");
10311031
TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, idx))
10321032
}
@@ -1505,7 +1505,7 @@ fn named_associated_type_shorthand_candidates<R>(
15051505
// Handle `Self::Type` referring to own associated type in trait definitions
15061506
if let GenericDefId::TraitId(trait_id) = param_id.parent() {
15071507
let trait_generics = generics(db.upcast(), trait_id.into());
1508-
if trait_generics.params.type_or_consts[param_id.local_id()].is_trait_self() {
1508+
if trait_generics.params[param_id.local_id()].is_trait_self() {
15091509
let def_generics = generics(db.upcast(), def);
15101510
let starting_idx = match def {
15111511
GenericDefId::TraitId(_) => 0,

crates/hir-ty/src/mir/lower.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -495,9 +495,11 @@ impl<'ctx> MirLowerCtx<'ctx> {
495495
ty,
496496
value: chalk_ir::ConstValue::BoundVar(BoundVar::new(
497497
DebruijnIndex::INNERMOST,
498-
gen.param_idx(p.into()).ok_or(MirLowerError::TypeError(
499-
"fail to lower const generic param",
500-
))?,
498+
gen.type_or_const_param_idx(p.into()).ok_or(
499+
MirLowerError::TypeError(
500+
"fail to lower const generic param",
501+
),
502+
)?,
501503
)),
502504
}
503505
.intern(Interner),

crates/hir-ty/src/mir/monomorphization.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl FallibleTypeFolder<Interner> for Filler<'_> {
101101
_outer_binder: DebruijnIndex,
102102
) -> std::result::Result<chalk_ir::Const<Interner>, Self::Error> {
103103
let it = from_placeholder_idx(self.db, idx);
104-
let Some(idx) = self.generics.as_ref().and_then(|g| g.param_idx(it)) else {
104+
let Some(idx) = self.generics.as_ref().and_then(|g| g.type_or_const_param_idx(it)) else {
105105
not_supported!("missing idx in generics");
106106
};
107107
Ok(self
@@ -119,7 +119,7 @@ impl FallibleTypeFolder<Interner> for Filler<'_> {
119119
_outer_binder: DebruijnIndex,
120120
) -> std::result::Result<Ty, Self::Error> {
121121
let it = from_placeholder_idx(self.db, idx);
122-
let Some(idx) = self.generics.as_ref().and_then(|g| g.param_idx(it)) else {
122+
let Some(idx) = self.generics.as_ref().and_then(|g| g.type_or_const_param_idx(it)) else {
123123
not_supported!("missing idx in generics");
124124
};
125125
Ok(self

crates/hir-ty/src/utils.rs

+38-25
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,11 @@ impl Generics {
308308
};
309309

310310
let lt_iter = self.params.iter_lt().map(from_lt_id(self));
311-
self.params.iter().map(from_toc_id(self)).chain(lt_iter).chain(self.iter_parent())
311+
self.params
312+
.iter_type_or_consts()
313+
.map(from_toc_id(self))
314+
.chain(lt_iter)
315+
.chain(self.iter_parent())
312316
}
313317

314318
/// Iterate over types and const params without parent params.
@@ -340,16 +344,19 @@ impl Generics {
340344
}
341345
};
342346

343-
self.params.iter().map(from_toc_id(self)).chain(self.params.iter_lt().map(from_lt_id(self)))
347+
self.params
348+
.iter_type_or_consts()
349+
.map(from_toc_id(self))
350+
.chain(self.params.iter_lt().map(from_lt_id(self)))
344351
}
345352

346353
/// Iterator over types and const params of parent.
347-
#[allow(clippy::needless_lifetimes)]
348-
pub(crate) fn iter_parent<'a>(
349-
&'a self,
350-
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamDataRef<'a>)> + 'a {
354+
pub(crate) fn iter_parent(
355+
&self,
356+
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamDataRef<'_>)> + '_ {
351357
self.parent_generics().into_iter().flat_map(|it| {
352-
let from_toc_id = move |(local_id, p): (_, &'a TypeOrConstParamData)| {
358+
let from_toc_id = move |(local_id, p)| {
359+
let p: &_ = p;
353360
let id = TypeOrConstParamId { parent: it.def, local_id };
354361
match p {
355362
TypeOrConstParamData::TypeParamData(p) => (
@@ -363,14 +370,14 @@ impl Generics {
363370
}
364371
};
365372

366-
let from_lt_id = move |(local_id, p): (_, &'a LifetimeParamData)| {
373+
let from_lt_id = move |(local_id, p): (_, _)| {
367374
(
368375
GenericParamId::LifetimeParamId(LifetimeParamId { parent: it.def, local_id }),
369376
GenericParamDataRef::LifetimeParamData(p),
370377
)
371378
};
372379
let lt_iter = it.params.iter_lt().map(from_lt_id);
373-
it.params.iter().map(from_toc_id).chain(lt_iter)
380+
it.params.iter_type_or_consts().map(from_toc_id).chain(lt_iter)
374381
})
375382
}
376383

@@ -387,7 +394,7 @@ impl Generics {
387394
}
388395

389396
/// Returns number of generic parameter excluding those from parent
390-
fn len_params(&self) -> usize {
397+
fn len_type_and_const_params(&self) -> usize {
391398
self.params.type_or_consts.len()
392399
}
393400

@@ -398,7 +405,7 @@ impl Generics {
398405
let mut impl_trait_params = 0;
399406
let mut const_params = 0;
400407
let mut lifetime_params = 0;
401-
self.params.iter().for_each(|(_, data)| match data {
408+
self.params.iter_type_or_consts().for_each(|(_, data)| match data {
402409
TypeOrConstParamData::TypeParamData(p) => match p.provenance {
403410
TypeParamProvenance::TypeParamList => type_params += 1,
404411
TypeParamProvenance::TraitSelf => self_params += 1,
@@ -413,18 +420,23 @@ impl Generics {
413420
(parent_len, self_params, type_params, const_params, impl_trait_params, lifetime_params)
414421
}
415422

416-
pub(crate) fn param_idx(&self, param: TypeOrConstParamId) -> Option<usize> {
417-
Some(self.find_param(param)?.0)
423+
pub(crate) fn type_or_const_param_idx(&self, param: TypeOrConstParamId) -> Option<usize> {
424+
Some(self.find_type_or_const_param(param)?.0)
418425
}
419426

420-
fn find_param(&self, param: TypeOrConstParamId) -> Option<(usize, &TypeOrConstParamData)> {
427+
fn find_type_or_const_param(
428+
&self,
429+
param: TypeOrConstParamId,
430+
) -> Option<(usize, &TypeOrConstParamData)> {
421431
if param.parent == self.def {
422-
let (idx, (_local_id, data)) =
423-
self.params.iter().enumerate().find(|(_, (idx, _))| *idx == param.local_id)?;
424-
Some((idx, data))
432+
let idx = param.local_id.into_raw().into_u32() as usize;
433+
if idx >= self.params.type_or_consts.len() {
434+
return None;
435+
}
436+
Some((idx, &self.params.type_or_consts[param.local_id]))
425437
} else {
426438
self.parent_generics()
427-
.and_then(|g| g.find_param(param))
439+
.and_then(|g| g.find_type_or_const_param(param))
428440
// Remember that parent parameters come after parameters for self.
429441
.map(|(idx, data)| (self.len_self() + idx, data))
430442
}
@@ -436,13 +448,14 @@ impl Generics {
436448

437449
fn find_lifetime(&self, lifetime: LifetimeParamId) -> Option<(usize, &LifetimeParamData)> {
438450
if lifetime.parent == self.def {
439-
let (idx, (_local_id, data)) = self
440-
.params
441-
.iter_lt()
442-
.enumerate()
443-
.find(|(_, (idx, _))| *idx == lifetime.local_id)?;
444-
445-
Some((self.len_params() + idx, data))
451+
let idx = lifetime.local_id.into_raw().into_u32() as usize;
452+
if idx >= self.params.lifetimes.len() {
453+
return None;
454+
}
455+
Some((
456+
self.len_type_and_const_params() + idx,
457+
&self.params.lifetimes[lifetime.local_id],
458+
))
446459
} else {
447460
self.parent_generics()
448461
.and_then(|g| g.find_lifetime(lifetime))

crates/hir/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ impl Module {
653653
GenericParamId::LifetimeParamId(LifetimeParamId { parent, local_id })
654654
});
655655
let type_params = generic_params
656-
.iter()
656+
.iter_type_or_consts()
657657
.filter(|(_, it)| it.type_param().is_some())
658658
.map(|(local_id, _)| {
659659
GenericParamId::TypeParamId(TypeParamId::from_unchecked(

crates/rust-analyzer/src/cli/analysis_stats.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,9 @@ impl flags::AnalysisStats {
280280
let mut fail = 0;
281281
for &a in adts {
282282
let generic_params = db.generic_params(a.into());
283-
if generic_params.iter().next().is_some() || generic_params.iter_lt().next().is_some() {
283+
if generic_params.iter_type_or_consts().next().is_some()
284+
|| generic_params.iter_lt().next().is_some()
285+
{
284286
// Data types with generics don't have layout.
285287
continue;
286288
}

0 commit comments

Comments
 (0)