This commit is contained in:
Lukas Wirth 2023-02-27 15:51:45 +01:00
parent b38fcde3ba
commit 9e5fa74279
7 changed files with 44 additions and 52 deletions

View File

@ -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,
)
}

View File

@ -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,

View File

@ -1673,6 +1673,7 @@ impl<'a> SemanticsScope<'a> {
}
}
#[derive(Debug)]
pub struct VisibleTraits(pub FxHashSet<TraitId>);
impl ops::Deref for VisibleTraits {

View File

@ -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;

View File

@ -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)]

View File

@ -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(),

View File

@ -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,