@@ -4,6 +4,7 @@ use std::iter;
44use std:: ops:: Deref ;
55
66use rustc_data_structures:: fx:: FxHashSet ;
7+ use rustc_data_structures:: sso:: SsoHashSet ;
78use rustc_errors:: Applicability ;
89use rustc_hir as hir;
910use rustc_hir:: def:: DefKind ;
@@ -35,6 +36,7 @@ use rustc_trait_selection::traits::query::method_autoderef::{
3536} ;
3637use rustc_trait_selection:: traits:: query:: CanonicalTyGoal ;
3738use rustc_trait_selection:: traits:: { self , ObligationCause , ObligationCtxt } ;
39+ use rustc_type_ir:: elaborate:: supertrait_def_ids;
3840use smallvec:: { smallvec, SmallVec } ;
3941use tracing:: { debug, instrument} ;
4042
@@ -1260,10 +1262,18 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
12601262 debug ! ( "applicable_candidates: {:?}" , applicable_candidates) ;
12611263
12621264 if applicable_candidates. len ( ) > 1 {
1263- if let Some ( pick) =
1264- self . collapse_candidates_to_trait_pick ( self_ty, & applicable_candidates)
1265- {
1266- return Some ( Ok ( pick) ) ;
1265+ if self . tcx . features ( ) . supertrait_item_shadowing {
1266+ if let Some ( pick) =
1267+ self . collapse_candidates_to_subtrait_pick ( self_ty, & applicable_candidates)
1268+ {
1269+ return Some ( Ok ( pick) ) ;
1270+ }
1271+ } else {
1272+ if let Some ( pick) =
1273+ self . collapse_candidates_to_trait_pick ( self_ty, & applicable_candidates)
1274+ {
1275+ return Some ( Ok ( pick) ) ;
1276+ }
12671277 }
12681278 }
12691279
@@ -1697,6 +1707,51 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
16971707 } )
16981708 }
16991709
1710+ /// Much like `collapse_candidates_to_trait_pick`, this method allows us to collapse
1711+ /// multiple conflicting picks if there is one pick whose trait container is a subtrait
1712+ /// of the trait containers of all of the other picks.
1713+ ///
1714+ /// This implements RFC #3624.
1715+ fn collapse_candidates_to_subtrait_pick (
1716+ & self ,
1717+ self_ty : Ty < ' tcx > ,
1718+ probes : & [ ( & Candidate < ' tcx > , ProbeResult ) ] ,
1719+ ) -> Option < Pick < ' tcx > > {
1720+ let mut child_pick = probes[ 0 ] . 0 ;
1721+ let mut supertraits: SsoHashSet < _ > =
1722+ supertrait_def_ids ( self . tcx , child_pick. item . trait_container ( self . tcx ) ?) . collect ( ) ;
1723+
1724+ // All other picks should be a supertrait of the `child_pick`.
1725+ // If it's not, then we update the `child_pick` and the `supertraits`
1726+ // list.
1727+ for ( p, _) in & probes[ 1 ..] {
1728+ let p_container = p. item . trait_container ( self . tcx ) ?;
1729+ if !supertraits. contains ( & p_container) {
1730+ // This pick is not a supertrait of the `child_pick`.
1731+ // Check if it's a subtrait of the `child_pick`, which
1732+ // is sufficient to imply that all of the previous picks
1733+ // are also supertraits of this pick.
1734+ supertraits = supertrait_def_ids ( self . tcx , p_container) . collect ( ) ;
1735+ if supertraits. contains ( & child_pick. item . trait_container ( self . tcx ) . unwrap ( ) ) {
1736+ child_pick = * p;
1737+ } else {
1738+ // `child_pick` is not a supertrait of this pick. Bail.
1739+ return None ;
1740+ }
1741+ }
1742+ }
1743+
1744+ Some ( Pick {
1745+ item : child_pick. item ,
1746+ kind : TraitPick ,
1747+ import_ids : child_pick. import_ids . clone ( ) ,
1748+ autoderefs : 0 ,
1749+ autoref_or_ptr_adjustment : None ,
1750+ self_ty,
1751+ unstable_candidates : vec ! [ ] ,
1752+ } )
1753+ }
1754+
17001755 /// Similarly to `probe_for_return_type`, this method attempts to find the best matching
17011756 /// candidate method where the method name may have been misspelled. Similarly to other
17021757 /// edit distance based suggestions, we provide at most one such suggestion.
0 commit comments