about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-03-29 13:15:08 +0000
committerbors <bors@rust-lang.org>2024-03-29 13:15:08 +0000
commit9d84142ef22103b76262974301f2cb22c186f0fe (patch)
tree7fb0c164dbf5748c3c6c92b8d715fd6ee5120f9b
parenta8b7acf22ff5f4e9d075dfa45ddaacf7b608e35e (diff)
parent69c4ac6304ca2b56436064cf0b933637ab778eed (diff)
downloadrust-9d84142ef22103b76262974301f2cb22c186f0fe.tar.gz
rust-9d84142ef22103b76262974301f2cb22c186f0fe.zip
Auto merge of #16974 - dfireBird:generic_params_refactor, r=lnicola
refactor: Implement len and is_empty method in generic_params
-rw-r--r--crates/hir-def/src/generics.rs9
-rw-r--r--crates/hir-def/src/item_tree/pretty.rs2
-rw-r--r--crates/hir-ty/src/method_resolution.rs6
-rw-r--r--crates/hir-ty/src/utils.rs4
-rw-r--r--crates/hir/src/lib.rs3
5 files changed, 15 insertions, 9 deletions
diff --git a/crates/hir-def/src/generics.rs b/crates/hir-def/src/generics.rs
index 4bc86623dfb..4638b377197 100644
--- a/crates/hir-def/src/generics.rs
+++ b/crates/hir-def/src/generics.rs
@@ -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,
diff --git a/crates/hir-def/src/item_tree/pretty.rs b/crates/hir-def/src/item_tree/pretty.rs
index 953bf6b85d6..0c84057950b 100644
--- a/crates/hir-def/src/item_tree/pretty.rs
+++ b/crates/hir-def/src/item_tree/pretty.rs
@@ -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;
         }
 
diff --git a/crates/hir-ty/src/method_resolution.rs b/crates/hir-ty/src/method_resolution.rs
index 6f52a60e925..73b07df56f7 100644
--- a/crates/hir-ty/src/method_resolution.rs
+++ b/crates/hir-ty/src/method_resolution.rs
@@ -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),
diff --git a/crates/hir-ty/src/utils.rs b/crates/hir-ty/src/utils.rs
index 6a96ca804c9..afd4d1f271d 100644
--- a/crates/hir-ty/src/utils.rs
+++ b/crates/hir-ty/src/utils.rs
@@ -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
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index cf6297c0370..106056c2fc3 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -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;