@@ -111,6 +111,7 @@ pub(crate) enum CandidateKind<'tcx> {
111
111
InherentImplCandidate ( DefId ) ,
112
112
ObjectCandidate ( ty:: PolyTraitRef < ' tcx > ) ,
113
113
TraitCandidate ( ty:: PolyTraitRef < ' tcx > ) ,
114
+ TraitImplSelfCandidate ( ty:: TraitRef < ' tcx > ) ,
114
115
WhereClauseCandidate ( ty:: PolyTraitRef < ' tcx > ) ,
115
116
}
116
117
@@ -648,6 +649,24 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
648
649
for step in self . steps . iter ( ) {
649
650
self . assemble_probe ( & step. self_ty ) ;
650
651
}
652
+
653
+ // HACK:
654
+ // Assemble an "inherent" candidate for the self type of the impl we're inside of.
655
+ if let Some ( own_item) =
656
+ self . tcx . opt_associated_item ( self . tcx . typeck_root_def_id ( self . body_id . to_def_id ( ) ) )
657
+ && let ty:: AssocItemContainer :: ImplContainer = own_item. container
658
+ && let Some ( trait_ref) = self . tcx . impl_trait_ref ( own_item. container_id ( self . tcx ) )
659
+ && let trait_ref = trait_ref. instantiate_identity ( )
660
+ && matches ! ( trait_ref. self_ty( ) . kind( ) , ty:: Param ( _) )
661
+ {
662
+ for item in self . impl_or_trait_item ( trait_ref. def_id ) {
663
+ self . inherent_candidates . push ( Candidate {
664
+ item,
665
+ import_ids : smallvec ! [ ] ,
666
+ kind : TraitImplSelfCandidate ( trait_ref) ,
667
+ } )
668
+ }
669
+ }
651
670
}
652
671
653
672
#[ instrument( level = "debug" , skip( self ) ) ]
@@ -1381,7 +1400,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
1381
1400
InherentImplCandidate ( _) => {
1382
1401
CandidateSource :: Impl ( candidate. item . container_id ( self . tcx ) )
1383
1402
}
1384
- ObjectCandidate ( _) | WhereClauseCandidate ( _) => {
1403
+ ObjectCandidate ( _) | WhereClauseCandidate ( _) | TraitImplSelfCandidate ( _ ) => {
1385
1404
CandidateSource :: Trait ( candidate. item . container_id ( self . tcx ) )
1386
1405
}
1387
1406
TraitCandidate ( trait_ref) => self . probe ( |_| {
@@ -1467,6 +1486,18 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
1467
1486
impl_bounds,
1468
1487
) ) ;
1469
1488
}
1489
+ TraitImplSelfCandidate ( trait_ref) => {
1490
+ ( xform_self_ty, xform_ret_ty) =
1491
+ self . xform_self_ty ( probe. item , trait_ref. self_ty ( ) , trait_ref. args ) ;
1492
+ xform_self_ty = ocx. normalize ( cause, self . param_env , xform_self_ty) ;
1493
+ match ocx. sup ( cause, self . param_env , xform_self_ty, self_ty) {
1494
+ Ok ( ( ) ) => { }
1495
+ Err ( err) => {
1496
+ debug ! ( "--> cannot relate self-types {:?}" , err) ;
1497
+ return ProbeResult :: NoMatch ;
1498
+ }
1499
+ }
1500
+ }
1470
1501
TraitCandidate ( poly_trait_ref) => {
1471
1502
// Some trait methods are excluded for arrays before 2021.
1472
1503
// (`array.into_iter()` wants a slice iterator for compatibility.)
@@ -1946,6 +1977,10 @@ impl<'tcx> Candidate<'tcx> {
1946
1977
InherentImplCandidate ( _) => InherentImplPick ,
1947
1978
ObjectCandidate ( _) => ObjectPick ,
1948
1979
TraitCandidate ( _) => TraitPick ,
1980
+
1981
+ // HACK:
1982
+ TraitImplSelfCandidate ( w) => WhereClausePick ( ty:: Binder :: dummy ( w) ) ,
1983
+
1949
1984
WhereClauseCandidate ( trait_ref) => {
1950
1985
// Only trait derived from where-clauses should
1951
1986
// appear here, so they should not contain any
0 commit comments