aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/method_resolution.rs
diff options
context:
space:
mode:
authorcynecx <[email protected]>2021-03-20 18:28:26 +0000
committercynecx <[email protected]>2021-03-20 18:28:26 +0000
commit34bb13e293e757bb7267eb76884caacb4b94b48b (patch)
treef60bad4d993844e02fc154f2860f17be35b5c737 /crates/hir_ty/src/method_resolution.rs
parentb1b456c64254e82c4edf065c9cc526bf8e734d68 (diff)
hir_ty: introduce visible_from_module param into method resolution
Diffstat (limited to 'crates/hir_ty/src/method_resolution.rs')
-rw-r--r--crates/hir_ty/src/method_resolution.rs35
1 files changed, 31 insertions, 4 deletions
diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs
index da6bc2a4a..6c34982a1 100644
--- a/crates/hir_ty/src/method_resolution.rs
+++ b/crates/hir_ty/src/method_resolution.rs
@@ -295,6 +295,7 @@ pub(crate) fn lookup_method(
295 env: Arc<TraitEnvironment>, 295 env: Arc<TraitEnvironment>,
296 krate: CrateId, 296 krate: CrateId,
297 traits_in_scope: &FxHashSet<TraitId>, 297 traits_in_scope: &FxHashSet<TraitId>,
298 visible_from_module: Option<ModuleId>,
298 name: &Name, 299 name: &Name,
299) -> Option<(Ty, FunctionId)> { 300) -> Option<(Ty, FunctionId)> {
300 iterate_method_candidates( 301 iterate_method_candidates(
@@ -303,6 +304,7 @@ pub(crate) fn lookup_method(
303 env, 304 env,
304 krate, 305 krate,
305 &traits_in_scope, 306 &traits_in_scope,
307 visible_from_module,
306 Some(name), 308 Some(name),
307 LookupMode::MethodCall, 309 LookupMode::MethodCall,
308 |ty, f| match f { 310 |ty, f| match f {
@@ -333,6 +335,7 @@ pub fn iterate_method_candidates<T>(
333 env: Arc<TraitEnvironment>, 335 env: Arc<TraitEnvironment>,
334 krate: CrateId, 336 krate: CrateId,
335 traits_in_scope: &FxHashSet<TraitId>, 337 traits_in_scope: &FxHashSet<TraitId>,
338 visible_from_module: Option<ModuleId>,
336 name: Option<&Name>, 339 name: Option<&Name>,
337 mode: LookupMode, 340 mode: LookupMode,
338 mut callback: impl FnMut(&Ty, AssocItemId) -> Option<T>, 341 mut callback: impl FnMut(&Ty, AssocItemId) -> Option<T>,
@@ -344,6 +347,7 @@ pub fn iterate_method_candidates<T>(
344 env, 347 env,
345 krate, 348 krate,
346 traits_in_scope, 349 traits_in_scope,
350 visible_from_module,
347 name, 351 name,
348 mode, 352 mode,
349 &mut |ty, item| { 353 &mut |ty, item| {
@@ -361,6 +365,7 @@ fn iterate_method_candidates_impl(
361 env: Arc<TraitEnvironment>, 365 env: Arc<TraitEnvironment>,
362 krate: CrateId, 366 krate: CrateId,
363 traits_in_scope: &FxHashSet<TraitId>, 367 traits_in_scope: &FxHashSet<TraitId>,
368 visible_from_module: Option<ModuleId>,
364 name: Option<&Name>, 369 name: Option<&Name>,
365 mode: LookupMode, 370 mode: LookupMode,
366 callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool, 371 callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
@@ -398,6 +403,7 @@ fn iterate_method_candidates_impl(
398 env.clone(), 403 env.clone(),
399 krate, 404 krate,
400 traits_in_scope, 405 traits_in_scope,
406 visible_from_module,
401 name, 407 name,
402 callback, 408 callback,
403 ) { 409 ) {
@@ -427,6 +433,7 @@ fn iterate_method_candidates_with_autoref(
427 env: Arc<TraitEnvironment>, 433 env: Arc<TraitEnvironment>,
428 krate: CrateId, 434 krate: CrateId,
429 traits_in_scope: &FxHashSet<TraitId>, 435 traits_in_scope: &FxHashSet<TraitId>,
436 visible_from_module: Option<ModuleId>,
430 name: Option<&Name>, 437 name: Option<&Name>,
431 mut callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool, 438 mut callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
432) -> bool { 439) -> bool {
@@ -437,6 +444,7 @@ fn iterate_method_candidates_with_autoref(
437 env.clone(), 444 env.clone(),
438 krate, 445 krate,
439 &traits_in_scope, 446 &traits_in_scope,
447 visible_from_module,
440 name, 448 name,
441 &mut callback, 449 &mut callback,
442 ) { 450 ) {
@@ -453,6 +461,7 @@ fn iterate_method_candidates_with_autoref(
453 env.clone(), 461 env.clone(),
454 krate, 462 krate,
455 &traits_in_scope, 463 &traits_in_scope,
464 visible_from_module,
456 name, 465 name,
457 &mut callback, 466 &mut callback,
458 ) { 467 ) {
@@ -469,6 +478,7 @@ fn iterate_method_candidates_with_autoref(
469 env, 478 env,
470 krate, 479 krate,
471 &traits_in_scope, 480 &traits_in_scope,
481 visible_from_module,
472 name, 482 name,
473 &mut callback, 483 &mut callback,
474 ) { 484 ) {
@@ -484,6 +494,7 @@ fn iterate_method_candidates_by_receiver(
484 env: Arc<TraitEnvironment>, 494 env: Arc<TraitEnvironment>,
485 krate: CrateId, 495 krate: CrateId,
486 traits_in_scope: &FxHashSet<TraitId>, 496 traits_in_scope: &FxHashSet<TraitId>,
497 visible_from_module: Option<ModuleId>,
487 name: Option<&Name>, 498 name: Option<&Name>,
488 mut callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool, 499 mut callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
489) -> bool { 500) -> bool {
@@ -491,7 +502,15 @@ fn iterate_method_candidates_by_receiver(
491 // be found in any of the derefs of receiver_ty, so we have to go through 502 // be found in any of the derefs of receiver_ty, so we have to go through
492 // that. 503 // that.
493 for self_ty in std::iter::once(receiver_ty).chain(rest_of_deref_chain) { 504 for self_ty in std::iter::once(receiver_ty).chain(rest_of_deref_chain) {
494 if iterate_inherent_methods(self_ty, db, name, Some(receiver_ty), krate, &mut callback) { 505 if iterate_inherent_methods(
506 self_ty,
507 db,
508 name,
509 Some(receiver_ty),
510 krate,
511 visible_from_module,
512 &mut callback,
513 ) {
495 return true; 514 return true;
496 } 515 }
497 } 516 }
@@ -521,7 +540,7 @@ fn iterate_method_candidates_for_self_ty(
521 name: Option<&Name>, 540 name: Option<&Name>,
522 mut callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool, 541 mut callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
523) -> bool { 542) -> bool {
524 if iterate_inherent_methods(self_ty, db, name, None, krate, &mut callback) { 543 if iterate_inherent_methods(self_ty, db, name, None, krate, None, &mut callback) {
525 return true; 544 return true;
526 } 545 }
527 iterate_trait_method_candidates(self_ty, db, env, krate, traits_in_scope, name, None, callback) 546 iterate_trait_method_candidates(self_ty, db, env, krate, traits_in_scope, name, None, callback)
@@ -558,7 +577,7 @@ fn iterate_trait_method_candidates(
558 // iteration 577 // iteration
559 let mut known_implemented = false; 578 let mut known_implemented = false;
560 for (_name, item) in data.items.iter() { 579 for (_name, item) in data.items.iter() {
561 if !is_valid_candidate(db, name, receiver_ty, *item, self_ty) { 580 if !is_valid_candidate(db, name, receiver_ty, *item, self_ty, None) {
562 continue; 581 continue;
563 } 582 }
564 if !known_implemented { 583 if !known_implemented {
@@ -582,6 +601,7 @@ fn iterate_inherent_methods(
582 name: Option<&Name>, 601 name: Option<&Name>,
583 receiver_ty: Option<&Canonical<Ty>>, 602 receiver_ty: Option<&Canonical<Ty>>,
584 krate: CrateId, 603 krate: CrateId,
604 visible_from_module: Option<ModuleId>,
585 callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool, 605 callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
586) -> bool { 606) -> bool {
587 let def_crates = match self_ty.value.def_crates(db, krate) { 607 let def_crates = match self_ty.value.def_crates(db, krate) {
@@ -593,7 +613,7 @@ fn iterate_inherent_methods(
593 613
594 for &impl_def in impls.for_self_ty(&self_ty.value) { 614 for &impl_def in impls.for_self_ty(&self_ty.value) {
595 for &item in db.impl_data(impl_def).items.iter() { 615 for &item in db.impl_data(impl_def).items.iter() {
596 if !is_valid_candidate(db, name, receiver_ty, item, self_ty) { 616 if !is_valid_candidate(db, name, receiver_ty, item, self_ty, visible_from_module) {
597 continue; 617 continue;
598 } 618 }
599 // we have to check whether the self type unifies with the type 619 // we have to check whether the self type unifies with the type
@@ -638,6 +658,7 @@ fn is_valid_candidate(
638 receiver_ty: Option<&Canonical<Ty>>, 658 receiver_ty: Option<&Canonical<Ty>>,
639 item: AssocItemId, 659 item: AssocItemId,
640 self_ty: &Canonical<Ty>, 660 self_ty: &Canonical<Ty>,
661 visible_from_module: Option<ModuleId>,
641) -> bool { 662) -> bool {
642 match item { 663 match item {
643 AssocItemId::FunctionId(m) => { 664 AssocItemId::FunctionId(m) => {
@@ -659,6 +680,12 @@ fn is_valid_candidate(
659 return false; 680 return false;
660 } 681 }
661 } 682 }
683 if let Some(from_module) = visible_from_module {
684 if !db.fn_visibility(m).is_visible_from(db.upcast(), from_module) {
685 return false;
686 }
687 }
688
662 true 689 true
663 } 690 }
664 AssocItemId::ConstId(c) => { 691 AssocItemId::ConstId(c) => {