]> git.lizzy.rs Git - rust.git/commitdiff
hir_ty: introduce visible_from_module param into method resolution
authorcynecx <me@cynecx.net>
Sat, 20 Mar 2021 18:28:26 +0000 (19:28 +0100)
committercynecx <me@cynecx.net>
Sat, 20 Mar 2021 18:28:26 +0000 (19:28 +0100)
crates/hir/src/lib.rs
crates/hir_ty/src/infer/expr.rs
crates/hir_ty/src/infer/path.rs
crates/hir_ty/src/method_resolution.rs

index 30e5776712b7011653a8a3c476382560fc394fc7..1a65a5cad317d519f0e8c1ed6b95c7bd5cab56f7 100644 (file)
@@ -1967,12 +1967,18 @@ pub fn iterate_method_candidates<T>(
         let env = self.ty.environment.clone();
         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(
             &canonical,
             db,
             env,
             krate,
             traits_in_scope,
+            from_module,
             name,
             method_resolution::LookupMode::MethodCall,
             |ty, it| match it {
@@ -2004,6 +2010,7 @@ pub fn iterate_path_candidates<T>(
             env,
             krate,
             traits_in_scope,
+            None,
             name,
             method_resolution::LookupMode::Path,
             |ty, it| callback(ty, it.into()),
index 9bf9f87e4bf7f78b462791627ff8ea174d21c55f..b08880cdf07851155e4f6a9b770c3439eae67688 100644 (file)
@@ -849,6 +849,7 @@ fn infer_method_call(
                 self.trait_env.clone(),
                 krate,
                 &traits_in_scope,
+                self.resolver.module(),
                 method_name,
             )
         });
index 58cce56abb0d59cb757e7168630abf06675d53d2..cefa385094865db16c239d3257c1e821db7b751d 100644 (file)
@@ -230,6 +230,7 @@ fn resolve_ty_assoc_item(
             self.trait_env.clone(),
             krate,
             &traits_in_scope,
+            None,
             Some(name),
             method_resolution::LookupMode::Path,
             move |_ty, item| {
index da6bc2a4a174ad038ce34b0fdde96a6a21efc2f0..6c34982a1892d70b55fcb53452e0dcec108f5b9d 100644 (file)
@@ -295,6 +295,7 @@ pub(crate) fn lookup_method(
     env: Arc<TraitEnvironment>,
     krate: CrateId,
     traits_in_scope: &FxHashSet<TraitId>,
+    visible_from_module: Option<ModuleId>,
     name: &Name,
 ) -> Option<(Ty, FunctionId)> {
     iterate_method_candidates(
@@ -303,6 +304,7 @@ pub(crate) fn lookup_method(
         env,
         krate,
         &traits_in_scope,
+        visible_from_module,
         Some(name),
         LookupMode::MethodCall,
         |ty, f| match f {
@@ -333,6 +335,7 @@ pub fn iterate_method_candidates<T>(
     env: Arc<TraitEnvironment>,
     krate: CrateId,
     traits_in_scope: &FxHashSet<TraitId>,
+    visible_from_module: Option<ModuleId>,
     name: Option<&Name>,
     mode: LookupMode,
     mut callback: impl FnMut(&Ty, AssocItemId) -> Option<T>,
@@ -344,6 +347,7 @@ pub fn iterate_method_candidates<T>(
         env,
         krate,
         traits_in_scope,
+        visible_from_module,
         name,
         mode,
         &mut |ty, item| {
@@ -361,6 +365,7 @@ fn iterate_method_candidates_impl(
     env: Arc<TraitEnvironment>,
     krate: CrateId,
     traits_in_scope: &FxHashSet<TraitId>,
+    visible_from_module: Option<ModuleId>,
     name: Option<&Name>,
     mode: LookupMode,
     callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
@@ -398,6 +403,7 @@ fn iterate_method_candidates_impl(
                     env.clone(),
                     krate,
                     traits_in_scope,
+                    visible_from_module,
                     name,
                     callback,
                 ) {
@@ -427,6 +433,7 @@ fn iterate_method_candidates_with_autoref(
     env: Arc<TraitEnvironment>,
     krate: CrateId,
     traits_in_scope: &FxHashSet<TraitId>,
+    visible_from_module: Option<ModuleId>,
     name: Option<&Name>,
     mut callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
 ) -> bool {
@@ -437,6 +444,7 @@ fn iterate_method_candidates_with_autoref(
         env.clone(),
         krate,
         &traits_in_scope,
+        visible_from_module,
         name,
         &mut callback,
     ) {
@@ -453,6 +461,7 @@ fn iterate_method_candidates_with_autoref(
         env.clone(),
         krate,
         &traits_in_scope,
+        visible_from_module,
         name,
         &mut callback,
     ) {
@@ -469,6 +478,7 @@ fn iterate_method_candidates_with_autoref(
         env,
         krate,
         &traits_in_scope,
+        visible_from_module,
         name,
         &mut callback,
     ) {
@@ -484,6 +494,7 @@ fn iterate_method_candidates_by_receiver(
     env: Arc<TraitEnvironment>,
     krate: CrateId,
     traits_in_scope: &FxHashSet<TraitId>,
+    visible_from_module: Option<ModuleId>,
     name: Option<&Name>,
     mut callback: &mut dyn FnMut(&Ty, AssocItemId) -> 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
     // that.
     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;
         }
     }
@@ -521,7 +540,7 @@ fn iterate_method_candidates_for_self_ty(
     name: Option<&Name>,
     mut callback: &mut dyn FnMut(&Ty, AssocItemId) -> 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;
     }
     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
         let mut known_implemented = false;
         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;
             }
             if !known_implemented {
@@ -582,6 +601,7 @@ fn iterate_inherent_methods(
     name: Option<&Name>,
     receiver_ty: Option<&Canonical<Ty>>,
     krate: CrateId,
+    visible_from_module: Option<ModuleId>,
     callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
 ) -> bool {
     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 &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;
                 }
                 // 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>>,
     item: AssocItemId,
     self_ty: &Canonical<Ty>,
+    visible_from_module: Option<ModuleId>,
 ) -> bool {
     match item {
         AssocItemId::FunctionId(m) => {
@@ -659,6 +680,12 @@ fn is_valid_candidate(
                     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
         }
         AssocItemId::ConstId(c) => {