Unnamed repository; edit this file 'description' to name the repository.
Simplify
Lukas Wirth 2023-02-27
parent b38fcde · commit 9e5fa74
-rw-r--r--crates/hir-ty/src/method_resolution.rs41
-rw-r--r--crates/hir/src/lib.rs22
-rw-r--r--crates/hir/src/semantics.rs1
-rw-r--r--crates/ide-assists/src/handlers/convert_iter_for_each_to_for.rs19
-rw-r--r--crates/ide-assists/src/handlers/generate_is_empty_from_len.rs9
-rw-r--r--crates/ide-completion/src/completions/dot.rs2
-rw-r--r--crates/ide-db/src/imports/import_assets.rs2
7 files changed, 44 insertions, 52 deletions
diff --git a/crates/hir-ty/src/method_resolution.rs b/crates/hir-ty/src/method_resolution.rs
index 8c7714b9a6..6bdfca0892 100644
--- a/crates/hir-ty/src/method_resolution.rs
+++ b/crates/hir-ty/src/method_resolution.rs
@@ -867,16 +867,20 @@ fn iterate_method_candidates_with_autoref(
return ControlFlow::Continue(());
}
- iterate_method_candidates_by_receiver(
- receiver_ty,
- first_adjustment.clone(),
- db,
- env.clone(),
- traits_in_scope,
- visible_from_module,
- name,
- &mut callback,
- )?;
+ let iterate_method_candidates_by_receiver = |receiver_ty, first_adjustment| {
+ iterate_method_candidates_by_receiver(
+ receiver_ty,
+ first_adjustment,
+ db,
+ env.clone(),
+ traits_in_scope,
+ visible_from_module,
+ name,
+ &mut callback,
+ )
+ };
+
+ iterate_method_candidates_by_receiver(receiver_ty, first_adjustment.clone())?;
let refed = Canonical {
value: TyKind::Ref(Mutability::Not, static_lifetime(), receiver_ty.value.clone())
@@ -884,16 +888,7 @@ fn iterate_method_candidates_with_autoref(
binders: receiver_ty.binders.clone(),
};
- iterate_method_candidates_by_receiver(
- &refed,
- first_adjustment.with_autoref(Mutability::Not),
- db,
- env.clone(),
- traits_in_scope,
- visible_from_module,
- name,
- &mut callback,
- )?;
+ iterate_method_candidates_by_receiver(&refed, first_adjustment.with_autoref(Mutability::Not))?;
let ref_muted = Canonical {
value: TyKind::Ref(Mutability::Mut, static_lifetime(), receiver_ty.value.clone())
@@ -904,12 +899,6 @@ fn iterate_method_candidates_with_autoref(
iterate_method_candidates_by_receiver(
&ref_muted,
first_adjustment.with_autoref(Mutability::Mut),
- db,
- env,
- traits_in_scope,
- visible_from_module,
- name,
- &mut callback,
)
}
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 163a319443..1f4f3091df 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -3339,12 +3339,10 @@ impl Type {
.map(move |ty| self.derived(ty))
}
- pub fn iterate_method_candidates<T>(
+ pub fn iterate_method_candidates_with_traits<T>(
&self,
db: &dyn HirDatabase,
scope: &SemanticsScope<'_>,
- // FIXME this can be retrieved from `scope`, except autoimport uses this
- // to specify a different set, so the method needs to be split
traits_in_scope: &FxHashSet<TraitId>,
with_local_impls: Option<Module>,
name: Option<&Name>,
@@ -3372,6 +3370,24 @@ impl Type {
slot
}
+ pub fn iterate_method_candidates<T>(
+ &self,
+ db: &dyn HirDatabase,
+ scope: &SemanticsScope<'_>,
+ with_local_impls: Option<Module>,
+ name: Option<&Name>,
+ mut callback: impl FnMut(Function) -> Option<T>,
+ ) -> Option<T> {
+ self.iterate_method_candidates_with_traits(
+ db,
+ scope,
+ &scope.visible_traits().0,
+ with_local_impls,
+ name,
+ callback,
+ )
+ }
+
fn iterate_method_candidates_dyn(
&self,
db: &dyn HirDatabase,
diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs
index 486b7ee62e..b3ba1c7d34 100644
--- a/crates/hir/src/semantics.rs
+++ b/crates/hir/src/semantics.rs
@@ -1673,6 +1673,7 @@ impl<'a> SemanticsScope<'a> {
}
}
+#[derive(Debug)]
pub struct VisibleTraits(pub FxHashSet<TraitId>);
impl ops::Deref for VisibleTraits {
diff --git a/crates/ide-assists/src/handlers/convert_iter_for_each_to_for.rs b/crates/ide-assists/src/handlers/convert_iter_for_each_to_for.rs
index f32ef2d59d..9e1d9a702a 100644
--- a/crates/ide-assists/src/handlers/convert_iter_for_each_to_for.rs
+++ b/crates/ide-assists/src/handlers/convert_iter_for_each_to_for.rs
@@ -157,19 +157,12 @@ fn is_ref_and_impls_iter_method(
let iter_trait = FamousDefs(sema, krate).core_iter_Iterator()?;
let has_wanted_method = ty
- .iterate_method_candidates(
- sema.db,
- &scope,
- &scope.visible_traits().0,
- None,
- Some(&wanted_method),
- |func| {
- if func.ret_type(sema.db).impls_trait(sema.db, iter_trait, &[]) {
- return Some(());
- }
- None
- },
- )
+ .iterate_method_candidates(sema.db, &scope, None, Some(&wanted_method), |func| {
+ if func.ret_type(sema.db).impls_trait(sema.db, iter_trait, &[]) {
+ return Some(());
+ }
+ None
+ })
.is_some();
if !has_wanted_method {
return None;
diff --git a/crates/ide-assists/src/handlers/generate_is_empty_from_len.rs b/crates/ide-assists/src/handlers/generate_is_empty_from_len.rs
index 9ce525ca37..4429186196 100644
--- a/crates/ide-assists/src/handlers/generate_is_empty_from_len.rs
+++ b/crates/ide-assists/src/handlers/generate_is_empty_from_len.rs
@@ -95,14 +95,7 @@ fn get_impl_method(
let scope = ctx.sema.scope(impl_.syntax())?;
let ty = impl_def.self_ty(db);
- ty.iterate_method_candidates(
- db,
- &scope,
- &scope.visible_traits().0,
- None,
- Some(fn_name),
- |func| Some(func),
- )
+ ty.iterate_method_candidates(db, &scope, None, Some(fn_name), |func| Some(func))
}
#[cfg(test)]
diff --git a/crates/ide-completion/src/completions/dot.rs b/crates/ide-completion/src/completions/dot.rs
index 7c6e5e100f..09ac57153a 100644
--- a/crates/ide-completion/src/completions/dot.rs
+++ b/crates/ide-completion/src/completions/dot.rs
@@ -122,7 +122,7 @@ fn complete_methods(
mut f: impl FnMut(hir::Function),
) {
let mut seen_methods = FxHashSet::default();
- receiver.iterate_method_candidates(
+ receiver.iterate_method_candidates_with_traits(
ctx.db,
&ctx.scope,
&ctx.traits_in_scope(),
diff --git a/crates/ide-db/src/imports/import_assets.rs b/crates/ide-db/src/imports/import_assets.rs
index 994d48385a..b26b0a9087 100644
--- a/crates/ide-db/src/imports/import_assets.rs
+++ b/crates/ide-db/src/imports/import_assets.rs
@@ -528,7 +528,7 @@ fn trait_applicable_items(
},
)
} else {
- trait_candidate.receiver_ty.iterate_method_candidates(
+ trait_candidate.receiver_ty.iterate_method_candidates_with_traits(
db,
scope,
&trait_candidates,