@@ -104,6 +104,7 @@ pub(crate) struct Candidate<'tcx> {
104
104
pub ( crate ) item : ty:: AssocItem ,
105
105
pub ( crate ) kind : CandidateKind < ' tcx > ,
106
106
pub ( crate ) import_ids : SmallVec < [ LocalDefId ; 1 ] > ,
107
+ receiver_trait_derefs : usize ,
107
108
}
108
109
109
110
#[ derive( Debug , Clone ) ]
@@ -176,6 +177,11 @@ pub struct Pick<'tcx> {
176
177
177
178
/// Unstable candidates alongside the stable ones.
178
179
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 ,
179
185
}
180
186
181
187
#[ derive( Clone , Debug , PartialEq , Eq ) ]
@@ -497,6 +503,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
497
503
item,
498
504
kind : CandidateKind :: TraitCandidate ( ty:: Binder :: dummy ( trait_ref) ) ,
499
505
import_ids : smallvec ! [ ] ,
506
+ receiver_trait_derefs : 0usize ,
500
507
} ,
501
508
false ,
502
509
) ;
@@ -646,12 +653,16 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
646
653
647
654
fn assemble_inherent_candidates ( & mut self ) {
648
655
for step in self . steps . iter ( ) {
649
- self . assemble_probe ( & step. self_ty ) ;
656
+ self . assemble_probe ( & step. self_ty , step . autoderefs ) ;
650
657
}
651
658
}
652
659
653
660
#[ 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
+ ) {
655
666
let raw_self_ty = self_ty. value . value ;
656
667
match * raw_self_ty. kind ( ) {
657
668
ty:: Dynamic ( data, ..) if let Some ( p) = data. principal ( ) => {
@@ -675,27 +686,39 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
675
686
let ( QueryResponse { value : generalized_self_ty, .. } , _ignored_var_values) =
676
687
self . fcx . instantiate_canonical ( self . span , self_ty) ;
677
688
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) ;
680
694
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
+ ) ;
682
699
}
683
700
}
684
701
ty:: Adt ( def, _) => {
685
702
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 ) ;
687
704
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
+ ) ;
689
709
}
690
710
}
691
711
ty:: Foreign ( did) => {
692
- self . assemble_inherent_impl_candidates_for_type ( did) ;
712
+ self . assemble_inherent_impl_candidates_for_type ( did, receiver_trait_derefs ) ;
693
713
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
+ ) ;
695
718
}
696
719
}
697
720
ty:: Param ( p) => {
698
- self . assemble_inherent_candidates_from_param ( p) ;
721
+ self . assemble_inherent_candidates_from_param ( p, receiver_trait_derefs ) ;
699
722
}
700
723
ty:: Bool
701
724
| ty:: Char
@@ -708,29 +731,38 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
708
731
| ty:: RawPtr ( _, _)
709
732
| ty:: Ref ( ..)
710
733
| 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) ,
712
736
_ => { }
713
737
}
714
738
}
715
739
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
+ ) {
717
745
let Some ( simp) = simplify_type ( self . tcx , self_ty, TreatParams :: AsCandidateKey ) else {
718
746
bug ! ( "unexpected incoherent type: {:?}" , self_ty)
719
747
} ;
720
748
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 ) ;
722
750
}
723
751
}
724
752
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
+ ) {
726
758
let impl_def_ids = self . tcx . at ( self . span ) . inherent_impls ( def_id) . into_iter ( ) . flatten ( ) ;
727
759
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 ) ;
729
761
}
730
762
}
731
763
732
764
#[ 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 ) {
734
766
if !self . impl_dups . insert ( impl_def_id) {
735
767
return ; // already visited
736
768
}
@@ -746,14 +778,19 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
746
778
item,
747
779
kind : InherentImplCandidate ( impl_def_id) ,
748
780
import_ids : smallvec ! [ ] ,
781
+ receiver_trait_derefs,
749
782
} ,
750
783
true ,
751
784
) ;
752
785
}
753
786
}
754
787
755
788
#[ 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
+ ) {
757
794
let principal = match self_ty. kind ( ) {
758
795
ty:: Dynamic ( ref data, ..) => Some ( data) ,
759
796
_ => None ,
@@ -776,14 +813,23 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
776
813
let trait_ref = principal. with_self_ty ( self . tcx , self_ty) ;
777
814
self . elaborate_bounds ( iter:: once ( trait_ref) , |this, new_trait_ref, item| {
778
815
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
+ } ,
780
822
true ,
781
823
) ;
782
824
} ) ;
783
825
}
784
826
785
827
#[ 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
+ ) {
787
833
// FIXME: do we want to commit to this behavior for param bounds?
788
834
789
835
let bounds = self . param_env . caller_bounds ( ) . iter ( ) . filter_map ( |predicate| {
@@ -812,6 +858,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
812
858
item,
813
859
kind : WhereClauseCandidate ( poly_trait_ref) ,
814
860
import_ids : smallvec ! [ ] ,
861
+ receiver_trait_derefs,
815
862
} ,
816
863
true ,
817
864
) ;
@@ -906,6 +953,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
906
953
item,
907
954
import_ids : import_ids. clone ( ) ,
908
955
kind : TraitCandidate ( bound_trait_ref) ,
956
+ receiver_trait_derefs : 0usize ,
909
957
} ,
910
958
false ,
911
959
) ;
@@ -929,6 +977,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
929
977
item,
930
978
import_ids : import_ids. clone ( ) ,
931
979
kind : TraitCandidate ( ty:: Binder :: dummy ( trait_ref) ) ,
980
+ receiver_trait_derefs : 0usize ,
932
981
} ,
933
982
false ,
934
983
) ;
@@ -1404,6 +1453,7 @@ impl<'tcx> Pick<'tcx> {
1404
1453
autoref_or_ptr_adjustment : _,
1405
1454
self_ty,
1406
1455
unstable_candidates : _,
1456
+ receiver_trait_derefs : _,
1407
1457
} = * self ;
1408
1458
self_ty != other. self_ty || def_id != other. item . def_id
1409
1459
}
@@ -1773,6 +1823,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
1773
1823
autoref_or_ptr_adjustment : None ,
1774
1824
self_ty,
1775
1825
unstable_candidates : vec ! [ ] ,
1826
+ receiver_trait_derefs : 0 ,
1776
1827
} )
1777
1828
}
1778
1829
@@ -2066,6 +2117,7 @@ impl<'tcx> Candidate<'tcx> {
2066
2117
autoref_or_ptr_adjustment : None ,
2067
2118
self_ty,
2068
2119
unstable_candidates,
2120
+ receiver_trait_derefs : self . receiver_trait_derefs ,
2069
2121
}
2070
2122
}
2071
2123
}
0 commit comments