use references in Generics iter methods

This commit is contained in:
dfireBird 2024-03-19 12:57:18 +05:30
parent b357bcab2b
commit 0e54e2b55a
No known key found for this signature in database
GPG Key ID: 26D522CA5FC2B93D
4 changed files with 34 additions and 27 deletions

View File

@ -142,6 +142,12 @@ impl GenericParamData {
impl_from!(TypeParamData, ConstParamData, LifetimeParamData for GenericParamData);
pub enum GenericParamDataRef<'a> {
TypeParamData(&'a TypeParamData),
ConstParamData(&'a ConstParamData),
LifetimeParamData(&'a LifetimeParamData),
}
/// Data about the generic parameters of a function, struct, impl, etc.
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct GenericParams {

View File

@ -3,7 +3,6 @@
//! purposes.
use std::{
cmp::Ordering,
fmt::{self, Debug},
mem::size_of,
};
@ -1324,6 +1323,7 @@ fn hir_fmt_generics(
) -> Result<(), HirDisplayError> {
let db = f.db;
if parameters.len(Interner) > 0 {
use std::cmp::Ordering;
let param_compare =
|a: &GenericArg, b: &GenericArg| match (a.data(Interner), b.data(Interner)) {
(crate::GenericArgData::Lifetime(_), crate::GenericArgData::Lifetime(_)) => {

View File

@ -24,7 +24,7 @@ use hir_def::{
data::adt::StructKind,
expander::Expander,
generics::{
GenericParamData, TypeOrConstParamData, TypeParamProvenance, WherePredicate,
GenericParamDataRef, TypeOrConstParamData, TypeParamProvenance, WherePredicate,
WherePredicateTypeTarget,
},
lang_item::LangItem,
@ -356,7 +356,7 @@ impl<'a> TyLoweringContext<'a> {
.filter(|(_, data)| {
matches!(
data,
GenericParamData::TypeParamData(data)
GenericParamDataRef::TypeParamData(data)
if data.provenance == TypeParamProvenance::ArgumentImplTrait
)
})
@ -1770,7 +1770,7 @@ pub(crate) fn generic_defaults_query(
let defaults = Arc::from_iter(generic_params.iter().enumerate().map(|(idx, (id, p))| {
match p {
GenericParamData::TypeParamData(p) => {
GenericParamDataRef::TypeParamData(p) => {
let mut ty =
p.default.as_ref().map_or(TyKind::Error.intern(Interner), |t| ctx.lower_ty(t));
// Each default can only refer to previous parameters.
@ -1779,7 +1779,7 @@ pub(crate) fn generic_defaults_query(
ty = fallback_bound_vars(ty, idx, parent_start_idx);
crate::make_binders(db, &generic_params, ty.cast(Interner))
}
GenericParamData::ConstParamData(p) => {
GenericParamDataRef::ConstParamData(p) => {
let GenericParamId::ConstParamId(id) = id else {
unreachable!("Unexpected lifetime or type argument")
};
@ -1795,7 +1795,7 @@ pub(crate) fn generic_defaults_query(
val = fallback_bound_vars(val, idx, parent_start_idx);
make_binders(db, &generic_params, val)
}
GenericParamData::LifetimeParamData(_) => {
GenericParamDataRef::LifetimeParamData(_) => {
// using static because it requires defaults
make_binders(db, &generic_params, static_lifetime().cast(Interner))
}

View File

@ -12,7 +12,7 @@ use chalk_ir::{
use hir_def::{
db::DefDatabase,
generics::{
GenericParamData, GenericParams, LifetimeParamData, TypeOrConstParamData,
GenericParamDataRef, GenericParams, LifetimeParamData, TypeOrConstParamData,
TypeParamProvenance, WherePredicate, WherePredicateTypeTarget,
},
lang_item::LangItem,
@ -277,28 +277,28 @@ impl Generics {
/// Iterator over types and const params of self, then parent.
pub(crate) fn iter<'a>(
&'a self,
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamData)> + 'a {
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamDataRef<'a>)> + 'a {
let from_toc_id = |it: &'a Generics| {
move |(local_id, p): (_, &TypeOrConstParamData)| {
move |(local_id, p): (_, &'a TypeOrConstParamData)| {
let id = TypeOrConstParamId { parent: it.def, local_id };
match p {
TypeOrConstParamData::TypeParamData(p) => (
GenericParamId::TypeParamId(TypeParamId::from_unchecked(id)),
GenericParamData::TypeParamData(p.clone()),
GenericParamDataRef::TypeParamData(p),
),
TypeOrConstParamData::ConstParamData(p) => (
GenericParamId::ConstParamId(ConstParamId::from_unchecked(id)),
GenericParamData::ConstParamData(p.clone()),
GenericParamDataRef::ConstParamData(p),
),
}
}
};
let from_lt_id = |it: &'a Generics| {
move |(local_id, p): (_, &LifetimeParamData)| {
move |(local_id, p): (_, &'a LifetimeParamData)| {
(
GenericParamId::LifetimeParamId(LifetimeParamId { parent: it.def, local_id }),
GenericParamData::LifetimeParamData(p.clone()),
GenericParamDataRef::LifetimeParamData(p),
)
}
};
@ -310,28 +310,28 @@ impl Generics {
/// Iterate over types and const params without parent params.
pub(crate) fn iter_self<'a>(
&'a self,
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamData)> + 'a {
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamDataRef<'a>)> + 'a {
let from_toc_id = |it: &'a Generics| {
move |(local_id, p): (_, &TypeOrConstParamData)| {
move |(local_id, p): (_, &'a TypeOrConstParamData)| {
let id = TypeOrConstParamId { parent: it.def, local_id };
match p {
TypeOrConstParamData::TypeParamData(p) => (
GenericParamId::TypeParamId(TypeParamId::from_unchecked(id)),
GenericParamData::TypeParamData(p.clone()),
GenericParamDataRef::TypeParamData(p),
),
TypeOrConstParamData::ConstParamData(p) => (
GenericParamId::ConstParamId(ConstParamId::from_unchecked(id)),
GenericParamData::ConstParamData(p.clone()),
GenericParamDataRef::ConstParamData(p),
),
}
}
};
let from_lt_id = |it: &'a Generics| {
move |(local_id, p): (_, &LifetimeParamData)| {
move |(local_id, p): (_, &'a LifetimeParamData)| {
(
GenericParamId::LifetimeParamId(LifetimeParamId { parent: it.def, local_id }),
GenericParamData::LifetimeParamData(p.clone()),
GenericParamDataRef::LifetimeParamData(p),
)
}
};
@ -340,28 +340,29 @@ impl Generics {
}
/// Iterator over types and const params of parent.
pub(crate) fn iter_parent(
&self,
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamData)> + '_ {
#[allow(clippy::needless_lifetimes)]
pub(crate) fn iter_parent<'a>(
&'a self,
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamDataRef<'a>)> + 'a {
self.parent_generics().into_iter().flat_map(|it| {
let from_toc_id = move |(local_id, p): (_, &TypeOrConstParamData)| {
let from_toc_id = move |(local_id, p): (_, &'a TypeOrConstParamData)| {
let id = TypeOrConstParamId { parent: it.def, local_id };
match p {
TypeOrConstParamData::TypeParamData(p) => (
GenericParamId::TypeParamId(TypeParamId::from_unchecked(id)),
GenericParamData::TypeParamData(p.clone()),
GenericParamDataRef::TypeParamData(p),
),
TypeOrConstParamData::ConstParamData(p) => (
GenericParamId::ConstParamId(ConstParamId::from_unchecked(id)),
GenericParamData::ConstParamData(p.clone()),
GenericParamDataRef::ConstParamData(p),
),
}
};
let from_lt_id = move |(local_id, p): (_, &LifetimeParamData)| {
let from_lt_id = move |(local_id, p): (_, &'a LifetimeParamData)| {
(
GenericParamId::LifetimeParamId(LifetimeParamId { parent: it.def, local_id }),
GenericParamData::LifetimeParamData(p.clone()),
GenericParamDataRef::LifetimeParamData(p),
)
};
let lt_iter = it.params.iter_lt().map(from_lt_id);