@@ -41,6 +41,7 @@ use rustc_trait_selection::traits::query::method_autoderef::{
4141use rustc_trait_selection:: traits:: query:: CanonicalTyGoal ;
4242use rustc_trait_selection:: traits:: ObligationCtxt ;
4343use rustc_trait_selection:: traits:: { self , ObligationCause } ;
44+ use std:: cell:: Cell ;
4445use std:: cell:: RefCell ;
4546use std:: cmp:: max;
4647use std:: iter;
@@ -76,8 +77,12 @@ pub(crate) struct ProbeContext<'a, 'tcx> {
7677 /// requested name (by edit distance)
7778 allow_similar_names : bool ,
7879
80+ /// List of potential private candidates. Will be trimmed to ones that
81+ /// actually apply and then the result inserted into `private_candidate`
82+ private_candidates : Vec < Candidate < ' tcx > > ,
83+
7984 /// Some(candidate) if there is a private candidate
80- private_candidate : Option < ( DefKind , DefId ) > ,
85+ private_candidate : Cell < Option < ( DefKind , DefId ) > > ,
8186
8287 /// Collects near misses when the candidate functions are missing a `self` keyword and is only
8388 /// used for error reporting
@@ -581,7 +586,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
581586 orig_steps_var_values,
582587 steps,
583588 allow_similar_names : false ,
584- private_candidate : None ,
589+ private_candidates : Vec :: new ( ) ,
590+ private_candidate : Cell :: new ( None ) ,
585591 static_candidates : RefCell :: new ( Vec :: new ( ) ) ,
586592 unsatisfied_predicates : RefCell :: new ( Vec :: new ( ) ) ,
587593 scope_expr_id,
@@ -593,7 +599,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
593599 self . inherent_candidates . clear ( ) ;
594600 self . extension_candidates . clear ( ) ;
595601 self . impl_dups . clear ( ) ;
596- self . private_candidate = None ;
602+ self . private_candidates . clear ( ) ;
603+ self . private_candidate . set ( None ) ;
597604 self . static_candidates . borrow_mut ( ) . clear ( ) ;
598605 self . unsatisfied_predicates . borrow_mut ( ) . clear ( ) ;
599606 }
@@ -617,9 +624,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
617624 } else {
618625 self . extension_candidates . push ( candidate) ;
619626 }
620- } else if self . private_candidate . is_none ( ) {
621- self . private_candidate =
622- Some ( ( candidate. item . kind . as_def_kind ( ) , candidate. item . def_id ) ) ;
627+ } else {
628+ self . private_candidates . push ( candidate) ;
623629 }
624630 }
625631
@@ -1171,7 +1177,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11711177 let mut possibly_unsatisfied_predicates = Vec :: new ( ) ;
11721178
11731179 for ( kind, candidates) in
1174- & [ ( "inherent" , & self . inherent_candidates ) , ( "extension" , & self . extension_candidates ) ]
1180+ [ ( "inherent" , & self . inherent_candidates ) , ( "extension" , & self . extension_candidates ) ]
11751181 {
11761182 debug ! ( "searching {} candidates" , kind) ;
11771183 let res = self . consider_candidates (
@@ -1185,6 +1191,14 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11851191 }
11861192 }
11871193
1194+ if self . private_candidate . get ( ) . is_none ( ) {
1195+ if let Some ( Ok ( pick) ) =
1196+ self . consider_candidates ( self_ty, & self . private_candidates , & mut vec ! [ ] , None )
1197+ {
1198+ self . private_candidate . set ( Some ( ( pick. item . kind . as_def_kind ( ) , pick. item . def_id ) ) ) ;
1199+ }
1200+ }
1201+
11881202 // `pick_method` may be called twice for the same self_ty if no stable methods
11891203 // match. Only extend once.
11901204 if unstable_candidates. is_some ( ) {
0 commit comments