Auto merge of #16974 - dfireBird:generic_params_refactor, r=lnicola

refactor: Implement len and is_empty method in generic_params
This commit is contained in:
bors 2024-03-29 13:15:08 +00:00
commit 9d84142ef2
5 changed files with 15 additions and 9 deletions

View File

@ -404,6 +404,15 @@ impl GenericParamsCollector {
}
impl GenericParams {
/// Number of Generic parameters (type_or_consts + lifetimes)
pub fn len(&self) -> usize {
self.type_or_consts.len() + self.lifetimes.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Iterator of type_or_consts field
pub fn iter(
&self,

View File

@ -526,7 +526,7 @@ impl Printer<'_> {
}
fn print_generic_params(&mut self, params: &GenericParams) {
if params.type_or_consts.is_empty() && params.lifetimes.is_empty() {
if params.is_empty() {
return;
}

View File

@ -644,8 +644,7 @@ pub fn is_dyn_method(
let ItemContainerId::TraitId(trait_id) = func.lookup(db.upcast()).container else {
return None;
};
let generic_params = db.generic_params(trait_id.into());
let trait_params = generic_params.type_or_consts.len() + generic_params.lifetimes.len();
let trait_params = db.generic_params(trait_id.into()).len();
let fn_params = fn_subst.len(Interner) - trait_params;
let trait_ref = TraitRef {
trait_id: to_chalk_trait_id(trait_id),
@ -687,8 +686,7 @@ pub(crate) fn lookup_impl_method_query(
let ItemContainerId::TraitId(trait_id) = func.lookup(db.upcast()).container else {
return (func, fn_subst);
};
let generic_params = db.generic_params(trait_id.into());
let trait_params = generic_params.type_or_consts.len() + generic_params.lifetimes.len();
let trait_params = db.generic_params(trait_id.into()).len();
let fn_params = fn_subst.len(Interner) - trait_params;
let trait_ref = TraitRef {
trait_id: to_chalk_trait_id(trait_id),

View File

@ -373,13 +373,13 @@ impl Generics {
/// Returns total number of generic parameters in scope, including those from parent.
pub(crate) fn len(&self) -> usize {
let parent = self.parent_generics().map_or(0, Generics::len);
let child = self.params.type_or_consts.len() + self.params.lifetimes.len();
let child = self.params.len();
parent + child
}
/// Returns numbers of generic parameters and lifetimes excluding those from parent.
pub(crate) fn len_self(&self) -> usize {
self.params.type_or_consts.len() + self.params.lifetimes.len()
self.params.len()
}
/// Returns number of generic parameter excluding those from parent

View File

@ -1418,8 +1418,7 @@ impl Adt {
}
pub fn layout(self, db: &dyn HirDatabase) -> Result<Layout, LayoutError> {
let generic_params = &db.generic_params(self.into());
if generic_params.iter().next().is_some() || generic_params.iter_lt().next().is_some() {
if !db.generic_params(self.into()).is_empty() {
return Err(LayoutError::HasPlaceholder);
}
let krate = self.krate(db).id;