@@ -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 : Symbol ,
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 )
@@ -783,7 +789,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
783
789
}
784
790
785
791
// Returns `true` if a bounds list includes `?Sized`.
786
- pub fn is_unsized ( & self , ast_bounds : & [ hir:: GenericBound < ' _ > ] , span : Span ) -> bool {
792
+ pub fn is_unsized ( & self , ast_bounds : & [ & hir:: GenericBound < ' _ > ] , span : Span ) -> bool {
787
793
let tcx = self . tcx ( ) ;
788
794
789
795
// Try to find an unbound in bounds.
@@ -841,7 +847,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
841
847
fn add_bounds (
842
848
& self ,
843
849
param_ty : Ty < ' tcx > ,
844
- ast_bounds : & [ hir:: GenericBound < ' _ > ] ,
850
+ ast_bounds : & [ & hir:: GenericBound < ' _ > ] ,
845
851
bounds : & mut Bounds < ' tcx > ,
846
852
) {
847
853
let constness = self . default_constness_for_trait_bounds ( ) ;
@@ -856,7 +862,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
856
862
hir:: GenericBound :: Trait ( _, hir:: TraitBoundModifier :: Maybe ) => { }
857
863
hir:: GenericBound :: LangItemTrait ( lang_item, span, hir_id, args) => self
858
864
. instantiate_lang_item_trait_ref (
859
- lang_item, span, hir_id, args, param_ty, bounds,
865
+ * lang_item, * span, * hir_id, args, param_ty, bounds,
860
866
) ,
861
867
hir:: GenericBound :: Outlives ( ref l) => bounds
862
868
. region_bounds
@@ -887,6 +893,42 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
887
893
ast_bounds : & [ hir:: GenericBound < ' _ > ] ,
888
894
sized_by_default : SizedByDefault ,
889
895
span : Span ,
896
+ ) -> Bounds < ' tcx > {
897
+ let ast_bounds: Vec < _ > = ast_bounds. iter ( ) . collect ( ) ;
898
+ self . compute_bounds_inner ( param_ty, & ast_bounds, sized_by_default, span)
899
+ }
900
+
901
+ /// Convert the bounds in `ast_bounds` that refer to traits which define an associated type
902
+ /// named `assoc_name` into ty::Bounds. Ignore the rest.
903
+ pub fn compute_bounds_that_match_assoc_type (
904
+ & self ,
905
+ param_ty : Ty < ' tcx > ,
906
+ ast_bounds : & [ hir:: GenericBound < ' _ > ] ,
907
+ sized_by_default : SizedByDefault ,
908
+ span : Span ,
909
+ assoc_name : Symbol ,
910
+ ) -> Bounds < ' tcx > {
911
+ let mut result = Vec :: new ( ) ;
912
+
913
+ for ast_bound in ast_bounds {
914
+ if let Some ( trait_ref) = ast_bound. trait_ref ( ) {
915
+ if let Some ( trait_did) = trait_ref. trait_def_id ( ) {
916
+ if self . tcx ( ) . trait_may_define_assoc_type ( trait_did, assoc_name) {
917
+ result. push ( ast_bound) ;
918
+ }
919
+ }
920
+ }
921
+ }
922
+
923
+ self . compute_bounds_inner ( param_ty, & result, sized_by_default, span)
924
+ }
925
+
926
+ fn compute_bounds_inner (
927
+ & self ,
928
+ param_ty : Ty < ' tcx > ,
929
+ ast_bounds : & [ & hir:: GenericBound < ' _ > ] ,
930
+ sized_by_default : SizedByDefault ,
931
+ span : Span ,
890
932
) -> Bounds < ' tcx > {
891
933
let mut bounds = Bounds :: default ( ) ;
892
934
@@ -1056,7 +1098,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1056
1098
// Calling `skip_binder` is okay, because `add_bounds` expects the `param_ty`
1057
1099
// parameter to have a skipped binder.
1058
1100
let param_ty = tcx. mk_projection ( assoc_ty. def_id , candidate. skip_binder ( ) . substs ) ;
1059
- self . add_bounds ( param_ty, ast_bounds, bounds) ;
1101
+ let ast_bounds: Vec < _ > = ast_bounds. iter ( ) . collect ( ) ;
1102
+ self . add_bounds ( param_ty, & ast_bounds, bounds) ;
1060
1103
}
1061
1104
}
1062
1105
Ok ( ( ) )
@@ -1371,21 +1414,24 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1371
1414
ty_param_def_id, assoc_name, span,
1372
1415
) ;
1373
1416
1374
- let predicates =
1375
- & self . get_type_parameter_bounds ( span, ty_param_def_id. to_def_id ( ) ) . predicates ;
1417
+ let predicates = & self
1418
+ . get_type_parameter_bounds ( span, ty_param_def_id. to_def_id ( ) , assoc_name. name )
1419
+ . predicates ;
1376
1420
1377
1421
debug ! ( "find_bound_for_assoc_item: predicates={:#?}" , predicates) ;
1378
1422
1379
1423
let param_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( ty_param_def_id) ;
1380
1424
let param_name = tcx. hir ( ) . ty_param_name ( param_hir_id) ;
1381
1425
self . one_bound_for_assoc_type (
1382
1426
|| {
1383
- traits:: transitive_bounds (
1427
+ traits:: transitive_bounds_that_define_assoc_type (
1384
1428
tcx,
1385
1429
predicates. iter ( ) . filter_map ( |( p, _) | {
1386
1430
p. to_opt_poly_trait_ref ( ) . map ( |trait_ref| trait_ref. value )
1387
1431
} ) ,
1432
+ assoc_name. name ,
1388
1433
)
1434
+ . into_iter ( )
1389
1435
} ,
1390
1436
|| param_name. to_string ( ) ,
1391
1437
assoc_name,
0 commit comments