mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
hir_ty: introduce visible_from_module param into method resolution
This commit is contained in:
parent
b1b456c642
commit
34bb13e293
4 changed files with 40 additions and 4 deletions
|
@ -1967,12 +1967,18 @@ impl Type {
|
||||||
let env = self.ty.environment.clone();
|
let env = self.ty.environment.clone();
|
||||||
let krate = krate.id;
|
let krate = krate.id;
|
||||||
|
|
||||||
|
let from_module = match self.as_adt() {
|
||||||
|
Some(adt) => Some(adt.module(db).id),
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
|
||||||
method_resolution::iterate_method_candidates(
|
method_resolution::iterate_method_candidates(
|
||||||
&canonical,
|
&canonical,
|
||||||
db,
|
db,
|
||||||
env,
|
env,
|
||||||
krate,
|
krate,
|
||||||
traits_in_scope,
|
traits_in_scope,
|
||||||
|
from_module,
|
||||||
name,
|
name,
|
||||||
method_resolution::LookupMode::MethodCall,
|
method_resolution::LookupMode::MethodCall,
|
||||||
|ty, it| match it {
|
|ty, it| match it {
|
||||||
|
@ -2004,6 +2010,7 @@ impl Type {
|
||||||
env,
|
env,
|
||||||
krate,
|
krate,
|
||||||
traits_in_scope,
|
traits_in_scope,
|
||||||
|
None,
|
||||||
name,
|
name,
|
||||||
method_resolution::LookupMode::Path,
|
method_resolution::LookupMode::Path,
|
||||||
|ty, it| callback(ty, it.into()),
|
|ty, it| callback(ty, it.into()),
|
||||||
|
|
|
@ -849,6 +849,7 @@ impl<'a> InferenceContext<'a> {
|
||||||
self.trait_env.clone(),
|
self.trait_env.clone(),
|
||||||
krate,
|
krate,
|
||||||
&traits_in_scope,
|
&traits_in_scope,
|
||||||
|
self.resolver.module(),
|
||||||
method_name,
|
method_name,
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
|
|
@ -230,6 +230,7 @@ impl<'a> InferenceContext<'a> {
|
||||||
self.trait_env.clone(),
|
self.trait_env.clone(),
|
||||||
krate,
|
krate,
|
||||||
&traits_in_scope,
|
&traits_in_scope,
|
||||||
|
None,
|
||||||
Some(name),
|
Some(name),
|
||||||
method_resolution::LookupMode::Path,
|
method_resolution::LookupMode::Path,
|
||||||
move |_ty, item| {
|
move |_ty, item| {
|
||||||
|
|
|
@ -295,6 +295,7 @@ pub(crate) fn lookup_method(
|
||||||
env: Arc<TraitEnvironment>,
|
env: Arc<TraitEnvironment>,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
traits_in_scope: &FxHashSet<TraitId>,
|
traits_in_scope: &FxHashSet<TraitId>,
|
||||||
|
visible_from_module: Option<ModuleId>,
|
||||||
name: &Name,
|
name: &Name,
|
||||||
) -> Option<(Ty, FunctionId)> {
|
) -> Option<(Ty, FunctionId)> {
|
||||||
iterate_method_candidates(
|
iterate_method_candidates(
|
||||||
|
@ -303,6 +304,7 @@ pub(crate) fn lookup_method(
|
||||||
env,
|
env,
|
||||||
krate,
|
krate,
|
||||||
&traits_in_scope,
|
&traits_in_scope,
|
||||||
|
visible_from_module,
|
||||||
Some(name),
|
Some(name),
|
||||||
LookupMode::MethodCall,
|
LookupMode::MethodCall,
|
||||||
|ty, f| match f {
|
|ty, f| match f {
|
||||||
|
@ -333,6 +335,7 @@ pub fn iterate_method_candidates<T>(
|
||||||
env: Arc<TraitEnvironment>,
|
env: Arc<TraitEnvironment>,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
traits_in_scope: &FxHashSet<TraitId>,
|
traits_in_scope: &FxHashSet<TraitId>,
|
||||||
|
visible_from_module: Option<ModuleId>,
|
||||||
name: Option<&Name>,
|
name: Option<&Name>,
|
||||||
mode: LookupMode,
|
mode: LookupMode,
|
||||||
mut callback: impl FnMut(&Ty, AssocItemId) -> Option<T>,
|
mut callback: impl FnMut(&Ty, AssocItemId) -> Option<T>,
|
||||||
|
@ -344,6 +347,7 @@ pub fn iterate_method_candidates<T>(
|
||||||
env,
|
env,
|
||||||
krate,
|
krate,
|
||||||
traits_in_scope,
|
traits_in_scope,
|
||||||
|
visible_from_module,
|
||||||
name,
|
name,
|
||||||
mode,
|
mode,
|
||||||
&mut |ty, item| {
|
&mut |ty, item| {
|
||||||
|
@ -361,6 +365,7 @@ fn iterate_method_candidates_impl(
|
||||||
env: Arc<TraitEnvironment>,
|
env: Arc<TraitEnvironment>,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
traits_in_scope: &FxHashSet<TraitId>,
|
traits_in_scope: &FxHashSet<TraitId>,
|
||||||
|
visible_from_module: Option<ModuleId>,
|
||||||
name: Option<&Name>,
|
name: Option<&Name>,
|
||||||
mode: LookupMode,
|
mode: LookupMode,
|
||||||
callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
|
callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
|
||||||
|
@ -398,6 +403,7 @@ fn iterate_method_candidates_impl(
|
||||||
env.clone(),
|
env.clone(),
|
||||||
krate,
|
krate,
|
||||||
traits_in_scope,
|
traits_in_scope,
|
||||||
|
visible_from_module,
|
||||||
name,
|
name,
|
||||||
callback,
|
callback,
|
||||||
) {
|
) {
|
||||||
|
@ -427,6 +433,7 @@ fn iterate_method_candidates_with_autoref(
|
||||||
env: Arc<TraitEnvironment>,
|
env: Arc<TraitEnvironment>,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
traits_in_scope: &FxHashSet<TraitId>,
|
traits_in_scope: &FxHashSet<TraitId>,
|
||||||
|
visible_from_module: Option<ModuleId>,
|
||||||
name: Option<&Name>,
|
name: Option<&Name>,
|
||||||
mut callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
|
mut callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
|
@ -437,6 +444,7 @@ fn iterate_method_candidates_with_autoref(
|
||||||
env.clone(),
|
env.clone(),
|
||||||
krate,
|
krate,
|
||||||
&traits_in_scope,
|
&traits_in_scope,
|
||||||
|
visible_from_module,
|
||||||
name,
|
name,
|
||||||
&mut callback,
|
&mut callback,
|
||||||
) {
|
) {
|
||||||
|
@ -453,6 +461,7 @@ fn iterate_method_candidates_with_autoref(
|
||||||
env.clone(),
|
env.clone(),
|
||||||
krate,
|
krate,
|
||||||
&traits_in_scope,
|
&traits_in_scope,
|
||||||
|
visible_from_module,
|
||||||
name,
|
name,
|
||||||
&mut callback,
|
&mut callback,
|
||||||
) {
|
) {
|
||||||
|
@ -469,6 +478,7 @@ fn iterate_method_candidates_with_autoref(
|
||||||
env,
|
env,
|
||||||
krate,
|
krate,
|
||||||
&traits_in_scope,
|
&traits_in_scope,
|
||||||
|
visible_from_module,
|
||||||
name,
|
name,
|
||||||
&mut callback,
|
&mut callback,
|
||||||
) {
|
) {
|
||||||
|
@ -484,6 +494,7 @@ fn iterate_method_candidates_by_receiver(
|
||||||
env: Arc<TraitEnvironment>,
|
env: Arc<TraitEnvironment>,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
traits_in_scope: &FxHashSet<TraitId>,
|
traits_in_scope: &FxHashSet<TraitId>,
|
||||||
|
visible_from_module: Option<ModuleId>,
|
||||||
name: Option<&Name>,
|
name: Option<&Name>,
|
||||||
mut callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
|
mut callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
|
@ -491,7 +502,15 @@ fn iterate_method_candidates_by_receiver(
|
||||||
// be found in any of the derefs of receiver_ty, so we have to go through
|
// be found in any of the derefs of receiver_ty, so we have to go through
|
||||||
// that.
|
// that.
|
||||||
for self_ty in std::iter::once(receiver_ty).chain(rest_of_deref_chain) {
|
for self_ty in std::iter::once(receiver_ty).chain(rest_of_deref_chain) {
|
||||||
if iterate_inherent_methods(self_ty, db, name, Some(receiver_ty), krate, &mut callback) {
|
if iterate_inherent_methods(
|
||||||
|
self_ty,
|
||||||
|
db,
|
||||||
|
name,
|
||||||
|
Some(receiver_ty),
|
||||||
|
krate,
|
||||||
|
visible_from_module,
|
||||||
|
&mut callback,
|
||||||
|
) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -521,7 +540,7 @@ fn iterate_method_candidates_for_self_ty(
|
||||||
name: Option<&Name>,
|
name: Option<&Name>,
|
||||||
mut callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
|
mut callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
if iterate_inherent_methods(self_ty, db, name, None, krate, &mut callback) {
|
if iterate_inherent_methods(self_ty, db, name, None, krate, None, &mut callback) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
iterate_trait_method_candidates(self_ty, db, env, krate, traits_in_scope, name, None, callback)
|
iterate_trait_method_candidates(self_ty, db, env, krate, traits_in_scope, name, None, callback)
|
||||||
|
@ -558,7 +577,7 @@ fn iterate_trait_method_candidates(
|
||||||
// iteration
|
// iteration
|
||||||
let mut known_implemented = false;
|
let mut known_implemented = false;
|
||||||
for (_name, item) in data.items.iter() {
|
for (_name, item) in data.items.iter() {
|
||||||
if !is_valid_candidate(db, name, receiver_ty, *item, self_ty) {
|
if !is_valid_candidate(db, name, receiver_ty, *item, self_ty, None) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if !known_implemented {
|
if !known_implemented {
|
||||||
|
@ -582,6 +601,7 @@ fn iterate_inherent_methods(
|
||||||
name: Option<&Name>,
|
name: Option<&Name>,
|
||||||
receiver_ty: Option<&Canonical<Ty>>,
|
receiver_ty: Option<&Canonical<Ty>>,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
|
visible_from_module: Option<ModuleId>,
|
||||||
callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
|
callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let def_crates = match self_ty.value.def_crates(db, krate) {
|
let def_crates = match self_ty.value.def_crates(db, krate) {
|
||||||
|
@ -593,7 +613,7 @@ fn iterate_inherent_methods(
|
||||||
|
|
||||||
for &impl_def in impls.for_self_ty(&self_ty.value) {
|
for &impl_def in impls.for_self_ty(&self_ty.value) {
|
||||||
for &item in db.impl_data(impl_def).items.iter() {
|
for &item in db.impl_data(impl_def).items.iter() {
|
||||||
if !is_valid_candidate(db, name, receiver_ty, item, self_ty) {
|
if !is_valid_candidate(db, name, receiver_ty, item, self_ty, visible_from_module) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// we have to check whether the self type unifies with the type
|
// we have to check whether the self type unifies with the type
|
||||||
|
@ -638,6 +658,7 @@ fn is_valid_candidate(
|
||||||
receiver_ty: Option<&Canonical<Ty>>,
|
receiver_ty: Option<&Canonical<Ty>>,
|
||||||
item: AssocItemId,
|
item: AssocItemId,
|
||||||
self_ty: &Canonical<Ty>,
|
self_ty: &Canonical<Ty>,
|
||||||
|
visible_from_module: Option<ModuleId>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
match item {
|
match item {
|
||||||
AssocItemId::FunctionId(m) => {
|
AssocItemId::FunctionId(m) => {
|
||||||
|
@ -659,6 +680,12 @@ fn is_valid_candidate(
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if let Some(from_module) = visible_from_module {
|
||||||
|
if !db.fn_visibility(m).is_visible_from(db.upcast(), from_module) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
AssocItemId::ConstId(c) => {
|
AssocItemId::ConstId(c) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue