@@ -4,6 +4,7 @@ use std::iter;
4
4
use std:: ops:: Deref ;
5
5
6
6
use rustc_data_structures:: fx:: FxHashSet ;
7
+ use rustc_data_structures:: sso:: SsoHashSet ;
7
8
use rustc_errors:: Applicability ;
8
9
use rustc_hir as hir;
9
10
use rustc_hir:: def:: DefKind ;
@@ -35,6 +36,7 @@ use rustc_trait_selection::traits::query::method_autoderef::{
35
36
} ;
36
37
use rustc_trait_selection:: traits:: query:: CanonicalTyGoal ;
37
38
use rustc_trait_selection:: traits:: { self , ObligationCause , ObligationCtxt } ;
39
+ use rustc_type_ir:: elaborate:: supertrait_def_ids;
38
40
use smallvec:: { smallvec, SmallVec } ;
39
41
use tracing:: { debug, instrument} ;
40
42
@@ -1260,10 +1262,18 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
1260
1262
debug ! ( "applicable_candidates: {:?}" , applicable_candidates) ;
1261
1263
1262
1264
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
+ }
1267
1277
}
1268
1278
}
1269
1279
@@ -1697,6 +1707,51 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
1697
1707
} )
1698
1708
}
1699
1709
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
+
1700
1755
/// Similarly to `probe_for_return_type`, this method attempts to find the best matching
1701
1756
/// candidate method where the method name may have been misspelled. Similarly to other
1702
1757
/// edit distance based suggestions, we provide at most one such suggestion.
0 commit comments