use references in Generics iter methods
This commit is contained in:
parent
b357bcab2b
commit
0e54e2b55a
@ -142,6 +142,12 @@ impl GenericParamData {
|
|||||||
|
|
||||||
impl_from!(TypeParamData, ConstParamData, LifetimeParamData for 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.
|
/// Data about the generic parameters of a function, struct, impl, etc.
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
||||||
pub struct GenericParams {
|
pub struct GenericParams {
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
//! purposes.
|
//! purposes.
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
cmp::Ordering,
|
|
||||||
fmt::{self, Debug},
|
fmt::{self, Debug},
|
||||||
mem::size_of,
|
mem::size_of,
|
||||||
};
|
};
|
||||||
@ -1324,6 +1323,7 @@ fn hir_fmt_generics(
|
|||||||
) -> Result<(), HirDisplayError> {
|
) -> Result<(), HirDisplayError> {
|
||||||
let db = f.db;
|
let db = f.db;
|
||||||
if parameters.len(Interner) > 0 {
|
if parameters.len(Interner) > 0 {
|
||||||
|
use std::cmp::Ordering;
|
||||||
let param_compare =
|
let param_compare =
|
||||||
|a: &GenericArg, b: &GenericArg| match (a.data(Interner), b.data(Interner)) {
|
|a: &GenericArg, b: &GenericArg| match (a.data(Interner), b.data(Interner)) {
|
||||||
(crate::GenericArgData::Lifetime(_), crate::GenericArgData::Lifetime(_)) => {
|
(crate::GenericArgData::Lifetime(_), crate::GenericArgData::Lifetime(_)) => {
|
||||||
|
@ -24,7 +24,7 @@ use hir_def::{
|
|||||||
data::adt::StructKind,
|
data::adt::StructKind,
|
||||||
expander::Expander,
|
expander::Expander,
|
||||||
generics::{
|
generics::{
|
||||||
GenericParamData, TypeOrConstParamData, TypeParamProvenance, WherePredicate,
|
GenericParamDataRef, TypeOrConstParamData, TypeParamProvenance, WherePredicate,
|
||||||
WherePredicateTypeTarget,
|
WherePredicateTypeTarget,
|
||||||
},
|
},
|
||||||
lang_item::LangItem,
|
lang_item::LangItem,
|
||||||
@ -356,7 +356,7 @@ impl<'a> TyLoweringContext<'a> {
|
|||||||
.filter(|(_, data)| {
|
.filter(|(_, data)| {
|
||||||
matches!(
|
matches!(
|
||||||
data,
|
data,
|
||||||
GenericParamData::TypeParamData(data)
|
GenericParamDataRef::TypeParamData(data)
|
||||||
if data.provenance == TypeParamProvenance::ArgumentImplTrait
|
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))| {
|
let defaults = Arc::from_iter(generic_params.iter().enumerate().map(|(idx, (id, p))| {
|
||||||
match p {
|
match p {
|
||||||
GenericParamData::TypeParamData(p) => {
|
GenericParamDataRef::TypeParamData(p) => {
|
||||||
let mut ty =
|
let mut ty =
|
||||||
p.default.as_ref().map_or(TyKind::Error.intern(Interner), |t| ctx.lower_ty(t));
|
p.default.as_ref().map_or(TyKind::Error.intern(Interner), |t| ctx.lower_ty(t));
|
||||||
// Each default can only refer to previous parameters.
|
// 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);
|
ty = fallback_bound_vars(ty, idx, parent_start_idx);
|
||||||
crate::make_binders(db, &generic_params, ty.cast(Interner))
|
crate::make_binders(db, &generic_params, ty.cast(Interner))
|
||||||
}
|
}
|
||||||
GenericParamData::ConstParamData(p) => {
|
GenericParamDataRef::ConstParamData(p) => {
|
||||||
let GenericParamId::ConstParamId(id) = id else {
|
let GenericParamId::ConstParamId(id) = id else {
|
||||||
unreachable!("Unexpected lifetime or type argument")
|
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);
|
val = fallback_bound_vars(val, idx, parent_start_idx);
|
||||||
make_binders(db, &generic_params, val)
|
make_binders(db, &generic_params, val)
|
||||||
}
|
}
|
||||||
GenericParamData::LifetimeParamData(_) => {
|
GenericParamDataRef::LifetimeParamData(_) => {
|
||||||
// using static because it requires defaults
|
// using static because it requires defaults
|
||||||
make_binders(db, &generic_params, static_lifetime().cast(Interner))
|
make_binders(db, &generic_params, static_lifetime().cast(Interner))
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ use chalk_ir::{
|
|||||||
use hir_def::{
|
use hir_def::{
|
||||||
db::DefDatabase,
|
db::DefDatabase,
|
||||||
generics::{
|
generics::{
|
||||||
GenericParamData, GenericParams, LifetimeParamData, TypeOrConstParamData,
|
GenericParamDataRef, GenericParams, LifetimeParamData, TypeOrConstParamData,
|
||||||
TypeParamProvenance, WherePredicate, WherePredicateTypeTarget,
|
TypeParamProvenance, WherePredicate, WherePredicateTypeTarget,
|
||||||
},
|
},
|
||||||
lang_item::LangItem,
|
lang_item::LangItem,
|
||||||
@ -277,28 +277,28 @@ impl Generics {
|
|||||||
/// Iterator over types and const params of self, then parent.
|
/// Iterator over types and const params of self, then parent.
|
||||||
pub(crate) fn iter<'a>(
|
pub(crate) fn iter<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamData)> + 'a {
|
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamDataRef<'a>)> + 'a {
|
||||||
let from_toc_id = |it: &'a Generics| {
|
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 };
|
let id = TypeOrConstParamId { parent: it.def, local_id };
|
||||||
match p {
|
match p {
|
||||||
TypeOrConstParamData::TypeParamData(p) => (
|
TypeOrConstParamData::TypeParamData(p) => (
|
||||||
GenericParamId::TypeParamId(TypeParamId::from_unchecked(id)),
|
GenericParamId::TypeParamId(TypeParamId::from_unchecked(id)),
|
||||||
GenericParamData::TypeParamData(p.clone()),
|
GenericParamDataRef::TypeParamData(p),
|
||||||
),
|
),
|
||||||
TypeOrConstParamData::ConstParamData(p) => (
|
TypeOrConstParamData::ConstParamData(p) => (
|
||||||
GenericParamId::ConstParamId(ConstParamId::from_unchecked(id)),
|
GenericParamId::ConstParamId(ConstParamId::from_unchecked(id)),
|
||||||
GenericParamData::ConstParamData(p.clone()),
|
GenericParamDataRef::ConstParamData(p),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let from_lt_id = |it: &'a Generics| {
|
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 }),
|
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.
|
/// Iterate over types and const params without parent params.
|
||||||
pub(crate) fn iter_self<'a>(
|
pub(crate) fn iter_self<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamData)> + 'a {
|
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamDataRef<'a>)> + 'a {
|
||||||
let from_toc_id = |it: &'a Generics| {
|
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 };
|
let id = TypeOrConstParamId { parent: it.def, local_id };
|
||||||
match p {
|
match p {
|
||||||
TypeOrConstParamData::TypeParamData(p) => (
|
TypeOrConstParamData::TypeParamData(p) => (
|
||||||
GenericParamId::TypeParamId(TypeParamId::from_unchecked(id)),
|
GenericParamId::TypeParamId(TypeParamId::from_unchecked(id)),
|
||||||
GenericParamData::TypeParamData(p.clone()),
|
GenericParamDataRef::TypeParamData(p),
|
||||||
),
|
),
|
||||||
TypeOrConstParamData::ConstParamData(p) => (
|
TypeOrConstParamData::ConstParamData(p) => (
|
||||||
GenericParamId::ConstParamId(ConstParamId::from_unchecked(id)),
|
GenericParamId::ConstParamId(ConstParamId::from_unchecked(id)),
|
||||||
GenericParamData::ConstParamData(p.clone()),
|
GenericParamDataRef::ConstParamData(p),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let from_lt_id = |it: &'a Generics| {
|
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 }),
|
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.
|
/// Iterator over types and const params of parent.
|
||||||
pub(crate) fn iter_parent(
|
#[allow(clippy::needless_lifetimes)]
|
||||||
&self,
|
pub(crate) fn iter_parent<'a>(
|
||||||
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamData)> + '_ {
|
&'a self,
|
||||||
|
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamDataRef<'a>)> + 'a {
|
||||||
self.parent_generics().into_iter().flat_map(|it| {
|
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 };
|
let id = TypeOrConstParamId { parent: it.def, local_id };
|
||||||
match p {
|
match p {
|
||||||
TypeOrConstParamData::TypeParamData(p) => (
|
TypeOrConstParamData::TypeParamData(p) => (
|
||||||
GenericParamId::TypeParamId(TypeParamId::from_unchecked(id)),
|
GenericParamId::TypeParamId(TypeParamId::from_unchecked(id)),
|
||||||
GenericParamData::TypeParamData(p.clone()),
|
GenericParamDataRef::TypeParamData(p),
|
||||||
),
|
),
|
||||||
TypeOrConstParamData::ConstParamData(p) => (
|
TypeOrConstParamData::ConstParamData(p) => (
|
||||||
GenericParamId::ConstParamId(ConstParamId::from_unchecked(id)),
|
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 }),
|
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);
|
let lt_iter = it.params.iter_lt().map(from_lt_id);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user