@@ -296,6 +296,7 @@ pub(crate) fn lookup_method(
296
296
env : Arc < TraitEnvironment > ,
297
297
krate : CrateId ,
298
298
traits_in_scope : & FxHashSet < TraitId > ,
299
+ visible_from_module : Option < ModuleId > ,
299
300
name : & Name ,
300
301
) -> Option < ( Ty , FunctionId ) > {
301
302
iterate_method_candidates (
@@ -304,6 +305,7 @@ pub(crate) fn lookup_method(
304
305
env,
305
306
krate,
306
307
& traits_in_scope,
308
+ visible_from_module,
307
309
Some ( name) ,
308
310
LookupMode :: MethodCall ,
309
311
|ty, f| match f {
@@ -334,6 +336,7 @@ pub fn iterate_method_candidates<T>(
334
336
env : Arc < TraitEnvironment > ,
335
337
krate : CrateId ,
336
338
traits_in_scope : & FxHashSet < TraitId > ,
339
+ visible_from_module : Option < ModuleId > ,
337
340
name : Option < & Name > ,
338
341
mode : LookupMode ,
339
342
mut callback : impl FnMut ( & Ty , AssocItemId ) -> Option < T > ,
@@ -345,6 +348,7 @@ pub fn iterate_method_candidates<T>(
345
348
env,
346
349
krate,
347
350
traits_in_scope,
351
+ visible_from_module,
348
352
name,
349
353
mode,
350
354
& mut |ty, item| {
@@ -362,6 +366,7 @@ fn iterate_method_candidates_impl(
362
366
env : Arc < TraitEnvironment > ,
363
367
krate : CrateId ,
364
368
traits_in_scope : & FxHashSet < TraitId > ,
369
+ visible_from_module : Option < ModuleId > ,
365
370
name : Option < & Name > ,
366
371
mode : LookupMode ,
367
372
callback : & mut dyn FnMut ( & Ty , AssocItemId ) -> bool ,
@@ -399,6 +404,7 @@ fn iterate_method_candidates_impl(
399
404
env. clone ( ) ,
400
405
krate,
401
406
traits_in_scope,
407
+ visible_from_module,
402
408
name,
403
409
callback,
404
410
) {
@@ -415,6 +421,7 @@ fn iterate_method_candidates_impl(
415
421
env,
416
422
krate,
417
423
traits_in_scope,
424
+ visible_from_module,
418
425
name,
419
426
callback,
420
427
)
@@ -428,6 +435,7 @@ fn iterate_method_candidates_with_autoref(
428
435
env : Arc < TraitEnvironment > ,
429
436
krate : CrateId ,
430
437
traits_in_scope : & FxHashSet < TraitId > ,
438
+ visible_from_module : Option < ModuleId > ,
431
439
name : Option < & Name > ,
432
440
mut callback : & mut dyn FnMut ( & Ty , AssocItemId ) -> bool ,
433
441
) -> bool {
@@ -438,6 +446,7 @@ fn iterate_method_candidates_with_autoref(
438
446
env. clone ( ) ,
439
447
krate,
440
448
& traits_in_scope,
449
+ visible_from_module,
441
450
name,
442
451
& mut callback,
443
452
) {
@@ -454,6 +463,7 @@ fn iterate_method_candidates_with_autoref(
454
463
env. clone ( ) ,
455
464
krate,
456
465
& traits_in_scope,
466
+ visible_from_module,
457
467
name,
458
468
& mut callback,
459
469
) {
@@ -470,6 +480,7 @@ fn iterate_method_candidates_with_autoref(
470
480
env,
471
481
krate,
472
482
& traits_in_scope,
483
+ visible_from_module,
473
484
name,
474
485
& mut callback,
475
486
) {
@@ -485,14 +496,23 @@ fn iterate_method_candidates_by_receiver(
485
496
env : Arc < TraitEnvironment > ,
486
497
krate : CrateId ,
487
498
traits_in_scope : & FxHashSet < TraitId > ,
499
+ visible_from_module : Option < ModuleId > ,
488
500
name : Option < & Name > ,
489
501
mut callback : & mut dyn FnMut ( & Ty , AssocItemId ) -> bool ,
490
502
) -> bool {
491
503
// We're looking for methods with *receiver* type receiver_ty. These could
492
504
// be found in any of the derefs of receiver_ty, so we have to go through
493
505
// that.
494
506
for self_ty in std:: iter:: once ( receiver_ty) . chain ( rest_of_deref_chain) {
495
- if iterate_inherent_methods ( self_ty, db, name, Some ( receiver_ty) , krate, & mut callback) {
507
+ if iterate_inherent_methods (
508
+ self_ty,
509
+ db,
510
+ name,
511
+ Some ( receiver_ty) ,
512
+ krate,
513
+ visible_from_module,
514
+ & mut callback,
515
+ ) {
496
516
return true ;
497
517
}
498
518
}
@@ -519,10 +539,12 @@ fn iterate_method_candidates_for_self_ty(
519
539
env : Arc < TraitEnvironment > ,
520
540
krate : CrateId ,
521
541
traits_in_scope : & FxHashSet < TraitId > ,
542
+ visible_from_module : Option < ModuleId > ,
522
543
name : Option < & Name > ,
523
544
mut callback : & mut dyn FnMut ( & Ty , AssocItemId ) -> bool ,
524
545
) -> bool {
525
- if iterate_inherent_methods ( self_ty, db, name, None , krate, & mut callback) {
546
+ if iterate_inherent_methods ( self_ty, db, name, None , krate, visible_from_module, & mut callback)
547
+ {
526
548
return true ;
527
549
}
528
550
iterate_trait_method_candidates ( self_ty, db, env, krate, traits_in_scope, name, None , callback)
@@ -559,7 +581,9 @@ fn iterate_trait_method_candidates(
559
581
// iteration
560
582
let mut known_implemented = false ;
561
583
for ( _name, item) in data. items . iter ( ) {
562
- if !is_valid_candidate ( db, name, receiver_ty, * item, self_ty) {
584
+ // Don't pass a `visible_from_module` down to `is_valid_candidate`,
585
+ // since only inherent methods should be included into visibility checking.
586
+ if !is_valid_candidate ( db, name, receiver_ty, * item, self_ty, None ) {
563
587
continue ;
564
588
}
565
589
if !known_implemented {
@@ -583,6 +607,7 @@ fn iterate_inherent_methods(
583
607
name : Option < & Name > ,
584
608
receiver_ty : Option < & Canonical < Ty > > ,
585
609
krate : CrateId ,
610
+ visible_from_module : Option < ModuleId > ,
586
611
callback : & mut dyn FnMut ( & Ty , AssocItemId ) -> bool ,
587
612
) -> bool {
588
613
let def_crates = match self_ty. value . def_crates ( db, krate) {
@@ -594,7 +619,7 @@ fn iterate_inherent_methods(
594
619
595
620
for & impl_def in impls. for_self_ty ( & self_ty. value ) {
596
621
for & item in db. impl_data ( impl_def) . items . iter ( ) {
597
- if !is_valid_candidate ( db, name, receiver_ty, item, self_ty) {
622
+ if !is_valid_candidate ( db, name, receiver_ty, item, self_ty, visible_from_module ) {
598
623
continue ;
599
624
}
600
625
// we have to check whether the self type unifies with the type
@@ -639,6 +664,7 @@ fn is_valid_candidate(
639
664
receiver_ty : Option < & Canonical < Ty > > ,
640
665
item : AssocItemId ,
641
666
self_ty : & Canonical < Ty > ,
667
+ visible_from_module : Option < ModuleId > ,
642
668
) -> bool {
643
669
match item {
644
670
AssocItemId :: FunctionId ( m) => {
@@ -660,6 +686,13 @@ fn is_valid_candidate(
660
686
return false ;
661
687
}
662
688
}
689
+ if let Some ( from_module) = visible_from_module {
690
+ if !db. function_visibility ( m) . is_visible_from ( db. upcast ( ) , from_module) {
691
+ cov_mark:: hit!( autoderef_candidate_not_visible) ;
692
+ return false ;
693
+ }
694
+ }
695
+
663
696
true
664
697
}
665
698
AssocItemId :: ConstId ( c) => {
0 commit comments