@@ -3,6 +3,7 @@ use std::cmp::max;
33use std:: ops:: Deref ;
44
55use rustc_data_structures:: fx:: FxHashSet ;
6+ use rustc_data_structures:: sso:: SsoHashSet ;
67use rustc_errors:: Applicability ;
78use rustc_hir as hir;
89use rustc_hir:: HirId ;
@@ -33,6 +34,7 @@ use rustc_trait_selection::traits::query::method_autoderef::{
3334 CandidateStep , MethodAutoderefBadTy , MethodAutoderefStepsResult ,
3435} ;
3536use rustc_trait_selection:: traits:: { self , ObligationCause , ObligationCtxt } ;
37+ use rustc_type_ir:: elaborate:: supertrait_def_ids;
3638use smallvec:: { SmallVec , smallvec} ;
3739use tracing:: { debug, instrument} ;
3840
@@ -1614,10 +1616,18 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
16141616 debug ! ( "applicable_candidates: {:?}" , applicable_candidates) ;
16151617
16161618 if applicable_candidates. len ( ) > 1 {
1617- if let Some ( pick) =
1618- self . collapse_candidates_to_trait_pick ( self_ty, & applicable_candidates)
1619- {
1620- return Some ( Ok ( pick) ) ;
1619+ if self . tcx . features ( ) . supertrait_item_shadowing ( ) {
1620+ if let Some ( pick) =
1621+ self . collapse_candidates_to_subtrait_pick ( self_ty, & applicable_candidates)
1622+ {
1623+ return Some ( Ok ( pick) ) ;
1624+ }
1625+ } else {
1626+ if let Some ( pick) =
1627+ self . collapse_candidates_to_trait_pick ( self_ty, & applicable_candidates)
1628+ {
1629+ return Some ( Ok ( pick) ) ;
1630+ }
16211631 }
16221632 }
16231633
@@ -2084,6 +2094,52 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
20842094 } )
20852095 }
20862096
2097+ /// Much like `collapse_candidates_to_trait_pick`, this method allows us to collapse
2098+ /// multiple conflicting picks if there is one pick whose trait container is a subtrait
2099+ /// of the trait containers of all of the other picks.
2100+ ///
2101+ /// This implements RFC #3624.
2102+ fn collapse_candidates_to_subtrait_pick (
2103+ & self ,
2104+ self_ty : Ty < ' tcx > ,
2105+ probes : & [ ( & Candidate < ' tcx > , ProbeResult ) ] ,
2106+ ) -> Option < Pick < ' tcx > > {
2107+ let mut child_pick = probes[ 0 ] . 0 ;
2108+ let mut supertraits: SsoHashSet < _ > =
2109+ supertrait_def_ids ( self . tcx , child_pick. item . trait_container ( self . tcx ) ?) . collect ( ) ;
2110+
2111+ // All other picks should be a supertrait of the `child_pick`.
2112+ // If it's not, then we update the `child_pick` and the `supertraits`
2113+ // list.
2114+ for ( p, _) in & probes[ 1 ..] {
2115+ let p_container = p. item . trait_container ( self . tcx ) ?;
2116+ if !supertraits. contains ( & p_container) {
2117+ // This pick is not a supertrait of the `child_pick`.
2118+ // Check if it's a subtrait of the `child_pick`, which
2119+ // is sufficient to imply that all of the previous picks
2120+ // are also supertraits of this pick.
2121+ supertraits = supertrait_def_ids ( self . tcx , p_container) . collect ( ) ;
2122+ if supertraits. contains ( & child_pick. item . trait_container ( self . tcx ) . unwrap ( ) ) {
2123+ child_pick = * p;
2124+ } else {
2125+ // `child_pick` is not a supertrait of this pick. Bail.
2126+ return None ;
2127+ }
2128+ }
2129+ }
2130+
2131+ Some ( Pick {
2132+ item : child_pick. item ,
2133+ kind : TraitPick ,
2134+ import_ids : child_pick. import_ids . clone ( ) ,
2135+ autoderefs : 0 ,
2136+ autoref_or_ptr_adjustment : None ,
2137+ self_ty,
2138+ unstable_candidates : vec ! [ ] ,
2139+ receiver_steps : None ,
2140+ } )
2141+ }
2142+
20872143 /// Similarly to `probe_for_return_type`, this method attempts to find the best matching
20882144 /// candidate method where the method name may have been misspelled. Similarly to other
20892145 /// edit distance based suggestions, we provide at most one such suggestion.
0 commit comments