implement len and is_empty method in generic_params

This commit is contained in:
dfireBird 2024-03-29 18:26:46 +05:30
parent a8b7acf22f
commit 69c4ac6304
No known key found for this signature in database
GPG key ID: 26D522CA5FC2B93D
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;