@@ -104,6 +104,7 @@ pub(crate) struct Candidate<'tcx> {
104104 pub ( crate ) item : ty:: AssocItem ,
105105 pub ( crate ) kind : CandidateKind < ' tcx > ,
106106 pub ( crate ) import_ids : SmallVec < [ LocalDefId ; 1 ] > ,
107+ receiver_trait_derefs : usize ,
107108}
108109
109110#[ derive( Debug , Clone ) ]
@@ -176,6 +177,11 @@ pub struct Pick<'tcx> {
176177
177178 /// Unstable candidates alongside the stable ones.
178179 unstable_candidates : Vec < ( Candidate < ' tcx > , Symbol ) > ,
180+
181+ /// Number of jumps along the Receiver::target chain we followed
182+ /// to identify this method. Used only for deshadowing errors.
183+ #[ allow( dead_code) ]
184+ pub receiver_trait_derefs : usize ,
179185}
180186
181187#[ derive( Clone , Debug , PartialEq , Eq ) ]
@@ -497,6 +503,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
497503 item,
498504 kind : CandidateKind :: TraitCandidate ( ty:: Binder :: dummy ( trait_ref) ) ,
499505 import_ids : smallvec ! [ ] ,
506+ receiver_trait_derefs : 0usize ,
500507 } ,
501508 false ,
502509 ) ;
@@ -646,12 +653,16 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
646653
647654 fn assemble_inherent_candidates ( & mut self ) {
648655 for step in self . steps . iter ( ) {
649- self . assemble_probe ( & step. self_ty ) ;
656+ self . assemble_probe ( & step. self_ty , step . autoderefs ) ;
650657 }
651658 }
652659
653660 #[ instrument( level = "debug" , skip( self ) ) ]
654- fn assemble_probe ( & mut self , self_ty : & Canonical < ' tcx , QueryResponse < ' tcx , Ty < ' tcx > > > ) {
661+ fn assemble_probe (
662+ & mut self ,
663+ self_ty : & Canonical < ' tcx , QueryResponse < ' tcx , Ty < ' tcx > > > ,
664+ receiver_trait_derefs : usize ,
665+ ) {
655666 let raw_self_ty = self_ty. value . value ;
656667 match * raw_self_ty. kind ( ) {
657668 ty:: Dynamic ( data, ..) if let Some ( p) = data. principal ( ) => {
@@ -675,27 +686,39 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
675686 let ( QueryResponse { value : generalized_self_ty, .. } , _ignored_var_values) =
676687 self . fcx . instantiate_canonical ( self . span , self_ty) ;
677688
678- self . assemble_inherent_candidates_from_object ( generalized_self_ty) ;
679- self . assemble_inherent_impl_candidates_for_type ( p. def_id ( ) ) ;
689+ self . assemble_inherent_candidates_from_object (
690+ generalized_self_ty,
691+ receiver_trait_derefs,
692+ ) ;
693+ self . assemble_inherent_impl_candidates_for_type ( p. def_id ( ) , receiver_trait_derefs) ;
680694 if self . tcx . has_attr ( p. def_id ( ) , sym:: rustc_has_incoherent_inherent_impls) {
681- self . assemble_inherent_candidates_for_incoherent_ty ( raw_self_ty) ;
695+ self . assemble_inherent_candidates_for_incoherent_ty (
696+ raw_self_ty,
697+ receiver_trait_derefs,
698+ ) ;
682699 }
683700 }
684701 ty:: Adt ( def, _) => {
685702 let def_id = def. did ( ) ;
686- self . assemble_inherent_impl_candidates_for_type ( def_id) ;
703+ self . assemble_inherent_impl_candidates_for_type ( def_id, receiver_trait_derefs ) ;
687704 if self . tcx . has_attr ( def_id, sym:: rustc_has_incoherent_inherent_impls) {
688- self . assemble_inherent_candidates_for_incoherent_ty ( raw_self_ty) ;
705+ self . assemble_inherent_candidates_for_incoherent_ty (
706+ raw_self_ty,
707+ receiver_trait_derefs,
708+ ) ;
689709 }
690710 }
691711 ty:: Foreign ( did) => {
692- self . assemble_inherent_impl_candidates_for_type ( did) ;
712+ self . assemble_inherent_impl_candidates_for_type ( did, receiver_trait_derefs ) ;
693713 if self . tcx . has_attr ( did, sym:: rustc_has_incoherent_inherent_impls) {
694- self . assemble_inherent_candidates_for_incoherent_ty ( raw_self_ty) ;
714+ self . assemble_inherent_candidates_for_incoherent_ty (
715+ raw_self_ty,
716+ receiver_trait_derefs,
717+ ) ;
695718 }
696719 }
697720 ty:: Param ( p) => {
698- self . assemble_inherent_candidates_from_param ( p) ;
721+ self . assemble_inherent_candidates_from_param ( p, receiver_trait_derefs ) ;
699722 }
700723 ty:: Bool
701724 | ty:: Char
@@ -708,29 +731,38 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
708731 | ty:: RawPtr ( _, _)
709732 | ty:: Ref ( ..)
710733 | ty:: Never
711- | ty:: Tuple ( ..) => self . assemble_inherent_candidates_for_incoherent_ty ( raw_self_ty) ,
734+ | ty:: Tuple ( ..) => self
735+ . assemble_inherent_candidates_for_incoherent_ty ( raw_self_ty, receiver_trait_derefs) ,
712736 _ => { }
713737 }
714738 }
715739
716- fn assemble_inherent_candidates_for_incoherent_ty ( & mut self , self_ty : Ty < ' tcx > ) {
740+ fn assemble_inherent_candidates_for_incoherent_ty (
741+ & mut self ,
742+ self_ty : Ty < ' tcx > ,
743+ receiver_trait_derefs : usize ,
744+ ) {
717745 let Some ( simp) = simplify_type ( self . tcx , self_ty, TreatParams :: AsCandidateKey ) else {
718746 bug ! ( "unexpected incoherent type: {:?}" , self_ty)
719747 } ;
720748 for & impl_def_id in self . tcx . incoherent_impls ( simp) . into_iter ( ) . flatten ( ) {
721- self . assemble_inherent_impl_probe ( impl_def_id) ;
749+ self . assemble_inherent_impl_probe ( impl_def_id, receiver_trait_derefs ) ;
722750 }
723751 }
724752
725- fn assemble_inherent_impl_candidates_for_type ( & mut self , def_id : DefId ) {
753+ fn assemble_inherent_impl_candidates_for_type (
754+ & mut self ,
755+ def_id : DefId ,
756+ receiver_trait_derefs : usize ,
757+ ) {
726758 let impl_def_ids = self . tcx . at ( self . span ) . inherent_impls ( def_id) . into_iter ( ) . flatten ( ) ;
727759 for & impl_def_id in impl_def_ids {
728- self . assemble_inherent_impl_probe ( impl_def_id) ;
760+ self . assemble_inherent_impl_probe ( impl_def_id, receiver_trait_derefs ) ;
729761 }
730762 }
731763
732764 #[ instrument( level = "debug" , skip( self ) ) ]
733- fn assemble_inherent_impl_probe ( & mut self , impl_def_id : DefId ) {
765+ fn assemble_inherent_impl_probe ( & mut self , impl_def_id : DefId , receiver_trait_derefs : usize ) {
734766 if !self . impl_dups . insert ( impl_def_id) {
735767 return ; // already visited
736768 }
@@ -746,14 +778,19 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
746778 item,
747779 kind : InherentImplCandidate ( impl_def_id) ,
748780 import_ids : smallvec ! [ ] ,
781+ receiver_trait_derefs,
749782 } ,
750783 true ,
751784 ) ;
752785 }
753786 }
754787
755788 #[ instrument( level = "debug" , skip( self ) ) ]
756- fn assemble_inherent_candidates_from_object ( & mut self , self_ty : Ty < ' tcx > ) {
789+ fn assemble_inherent_candidates_from_object (
790+ & mut self ,
791+ self_ty : Ty < ' tcx > ,
792+ receiver_trait_derefs : usize ,
793+ ) {
757794 let principal = match self_ty. kind ( ) {
758795 ty:: Dynamic ( ref data, ..) => Some ( data) ,
759796 _ => None ,
@@ -776,14 +813,23 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
776813 let trait_ref = principal. with_self_ty ( self . tcx , self_ty) ;
777814 self . elaborate_bounds ( iter:: once ( trait_ref) , |this, new_trait_ref, item| {
778815 this. push_candidate (
779- Candidate { item, kind : ObjectCandidate ( new_trait_ref) , import_ids : smallvec ! [ ] } ,
816+ Candidate {
817+ item,
818+ kind : ObjectCandidate ( new_trait_ref) ,
819+ import_ids : smallvec ! [ ] ,
820+ receiver_trait_derefs,
821+ } ,
780822 true ,
781823 ) ;
782824 } ) ;
783825 }
784826
785827 #[ instrument( level = "debug" , skip( self ) ) ]
786- fn assemble_inherent_candidates_from_param ( & mut self , param_ty : ty:: ParamTy ) {
828+ fn assemble_inherent_candidates_from_param (
829+ & mut self ,
830+ param_ty : ty:: ParamTy ,
831+ receiver_trait_derefs : usize ,
832+ ) {
787833 // FIXME: do we want to commit to this behavior for param bounds?
788834
789835 let bounds = self . param_env . caller_bounds ( ) . iter ( ) . filter_map ( |predicate| {
@@ -812,6 +858,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
812858 item,
813859 kind : WhereClauseCandidate ( poly_trait_ref) ,
814860 import_ids : smallvec ! [ ] ,
861+ receiver_trait_derefs,
815862 } ,
816863 true ,
817864 ) ;
@@ -906,6 +953,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
906953 item,
907954 import_ids : import_ids. clone ( ) ,
908955 kind : TraitCandidate ( bound_trait_ref) ,
956+ receiver_trait_derefs : 0usize ,
909957 } ,
910958 false ,
911959 ) ;
@@ -929,6 +977,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
929977 item,
930978 import_ids : import_ids. clone ( ) ,
931979 kind : TraitCandidate ( ty:: Binder :: dummy ( trait_ref) ) ,
980+ receiver_trait_derefs : 0usize ,
932981 } ,
933982 false ,
934983 ) ;
@@ -1400,6 +1449,7 @@ impl<'tcx> Pick<'tcx> {
14001449 autoref_or_ptr_adjustment : _,
14011450 self_ty,
14021451 unstable_candidates : _,
1452+ receiver_trait_derefs : _,
14031453 } = * self ;
14041454 self_ty != other. self_ty || def_id != other. item . def_id
14051455 }
@@ -1769,6 +1819,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
17691819 autoref_or_ptr_adjustment : None ,
17701820 self_ty,
17711821 unstable_candidates : vec ! [ ] ,
1822+ receiver_trait_derefs : 0 ,
17721823 } )
17731824 }
17741825
@@ -2062,6 +2113,7 @@ impl<'tcx> Candidate<'tcx> {
20622113 autoref_or_ptr_adjustment : None ,
20632114 self_ty,
20642115 unstable_candidates,
2116+ receiver_trait_derefs : self . receiver_trait_derefs ,
20652117 }
20662118 }
20672119}
0 commit comments