@@ -49,9 +49,10 @@ pub trait AstConv<'tcx> {
49
49
50
50
fn default_constness_for_trait_bounds ( & self ) -> Constness ;
51
51
52
- /// Returns predicates in scope of the form `X: Foo`, where `X` is
53
- /// a type parameter `X` with the given id `def_id`. This is a
54
- /// subset of the full set of predicates.
52
+ /// Returns predicates in scope of the form `X: Foo<T>`, where `X`
53
+ /// is a type parameter `X` with the given id `def_id` and T
54
+ /// matches `assoc_name`. This is a subset of the full set of
55
+ /// predicates.
55
56
///
56
57
/// This is used for one specific purpose: resolving "short-hand"
57
58
/// associated type references like `T::Item`. In principle, we
@@ -60,7 +61,12 @@ pub trait AstConv<'tcx> {
60
61
/// but this can lead to cycle errors. The problem is that we have
61
62
/// to do this resolution *in order to create the predicates in
62
63
/// the first place*. Hence, we have this "special pass".
63
- fn get_type_parameter_bounds ( & self , span : Span , def_id : DefId ) -> ty:: GenericPredicates < ' tcx > ;
64
+ fn get_type_parameter_bounds (
65
+ & self ,
66
+ span : Span ,
67
+ def_id : DefId ,
68
+ assoc_name : Ident ,
69
+ ) -> ty:: GenericPredicates < ' tcx > ;
64
70
65
71
/// Returns the lifetime to use when a lifetime is omitted (and not elided).
66
72
fn re_infer ( & self , param : Option < & ty:: GenericParamDef > , span : Span )
@@ -764,7 +770,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
764
770
}
765
771
766
772
// Returns `true` if a bounds list includes `?Sized`.
767
- pub fn is_unsized ( & self , ast_bounds : & [ hir:: GenericBound < ' _ > ] , span : Span ) -> bool {
773
+ pub fn is_unsized ( & self , ast_bounds : & [ & hir:: GenericBound < ' _ > ] , span : Span ) -> bool {
768
774
let tcx = self . tcx ( ) ;
769
775
770
776
// Try to find an unbound in bounds.
@@ -822,7 +828,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
822
828
fn add_bounds (
823
829
& self ,
824
830
param_ty : Ty < ' tcx > ,
825
- ast_bounds : & [ hir:: GenericBound < ' _ > ] ,
831
+ ast_bounds : & [ & hir:: GenericBound < ' _ > ] ,
826
832
bounds : & mut Bounds < ' tcx > ,
827
833
) {
828
834
let constness = self . default_constness_for_trait_bounds ( ) ;
@@ -837,7 +843,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
837
843
hir:: GenericBound :: Trait ( _, hir:: TraitBoundModifier :: Maybe ) => { }
838
844
hir:: GenericBound :: LangItemTrait ( lang_item, span, hir_id, args) => self
839
845
. instantiate_lang_item_trait_ref (
840
- lang_item, span, hir_id, args, param_ty, bounds,
846
+ * lang_item, * span, * hir_id, args, param_ty, bounds,
841
847
) ,
842
848
hir:: GenericBound :: Outlives ( ref l) => bounds
843
849
. region_bounds
@@ -868,6 +874,42 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
868
874
ast_bounds : & [ hir:: GenericBound < ' _ > ] ,
869
875
sized_by_default : SizedByDefault ,
870
876
span : Span ,
877
+ ) -> Bounds < ' tcx > {
878
+ let ast_bounds: Vec < _ > = ast_bounds. iter ( ) . collect ( ) ;
879
+ self . compute_bounds_inner ( param_ty, & ast_bounds, sized_by_default, span)
880
+ }
881
+
882
+ /// Convert the bounds in `ast_bounds` that refer to traits which define an associated type
883
+ /// named `assoc_name` into ty::Bounds. Ignore the rest.
884
+ pub fn compute_bounds_that_match_assoc_type (
885
+ & self ,
886
+ param_ty : Ty < ' tcx > ,
887
+ ast_bounds : & [ hir:: GenericBound < ' _ > ] ,
888
+ sized_by_default : SizedByDefault ,
889
+ span : Span ,
890
+ assoc_name : Ident ,
891
+ ) -> Bounds < ' tcx > {
892
+ let mut result = Vec :: new ( ) ;
893
+
894
+ for ast_bound in ast_bounds {
895
+ if let Some ( trait_ref) = ast_bound. trait_ref ( ) {
896
+ if let Some ( trait_did) = trait_ref. trait_def_id ( ) {
897
+ if self . tcx ( ) . trait_may_define_assoc_type ( trait_did, assoc_name) {
898
+ result. push ( ast_bound) ;
899
+ }
900
+ }
901
+ }
902
+ }
903
+
904
+ self . compute_bounds_inner ( param_ty, & result, sized_by_default, span)
905
+ }
906
+
907
+ fn compute_bounds_inner (
908
+ & self ,
909
+ param_ty : Ty < ' tcx > ,
910
+ ast_bounds : & [ & hir:: GenericBound < ' _ > ] ,
911
+ sized_by_default : SizedByDefault ,
912
+ span : Span ,
871
913
) -> Bounds < ' tcx > {
872
914
let mut bounds = Bounds :: default ( ) ;
873
915
@@ -1037,7 +1079,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1037
1079
// Calling `skip_binder` is okay, because `add_bounds` expects the `param_ty`
1038
1080
// parameter to have a skipped binder.
1039
1081
let param_ty = tcx. mk_projection ( assoc_ty. def_id , candidate. skip_binder ( ) . substs ) ;
1040
- self . add_bounds ( param_ty, ast_bounds, bounds) ;
1082
+ let ast_bounds: Vec < _ > = ast_bounds. iter ( ) . collect ( ) ;
1083
+ self . add_bounds ( param_ty, & ast_bounds, bounds) ;
1041
1084
}
1042
1085
}
1043
1086
Ok ( ( ) )
@@ -1354,21 +1397,24 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1354
1397
ty_param_def_id, assoc_name, span,
1355
1398
) ;
1356
1399
1357
- let predicates =
1358
- & self . get_type_parameter_bounds ( span, ty_param_def_id. to_def_id ( ) ) . predicates ;
1400
+ let predicates = & self
1401
+ . get_type_parameter_bounds ( span, ty_param_def_id. to_def_id ( ) , assoc_name)
1402
+ . predicates ;
1359
1403
1360
1404
debug ! ( "find_bound_for_assoc_item: predicates={:#?}" , predicates) ;
1361
1405
1362
1406
let param_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( ty_param_def_id) ;
1363
1407
let param_name = tcx. hir ( ) . ty_param_name ( param_hir_id) ;
1364
1408
self . one_bound_for_assoc_type (
1365
1409
|| {
1366
- traits:: transitive_bounds (
1410
+ traits:: transitive_bounds_that_define_assoc_type (
1367
1411
tcx,
1368
1412
predicates. iter ( ) . filter_map ( |( p, _) | {
1369
1413
p. to_opt_poly_trait_ref ( ) . map ( |trait_ref| trait_ref. value )
1370
1414
} ) ,
1415
+ assoc_name,
1371
1416
)
1417
+ . into_iter ( )
1372
1418
} ,
1373
1419
|| param_name. to_string ( ) ,
1374
1420
assoc_name,
0 commit comments