@@ -56,6 +56,9 @@ use std::slice;
56
56
#[ derive( Debug ) ]
57
57
pub struct PathSeg ( pub DefId , pub usize ) ;
58
58
59
+ #[ derive( Copy , Clone , Debug ) ]
60
+ pub struct OnlySelfBounds ( pub bool ) ;
61
+
59
62
pub trait AstConv < ' tcx > {
60
63
fn tcx ( & self ) -> TyCtxt < ' tcx > ;
61
64
@@ -670,6 +673,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
670
673
args : & GenericArgs < ' _ > ,
671
674
infer_args : bool ,
672
675
self_ty : Ty < ' tcx > ,
676
+ only_self_bounds : OnlySelfBounds ,
673
677
) -> GenericArgCountResult {
674
678
let ( substs, arg_count) = self . create_substs_for_ast_path (
675
679
trait_ref_span,
@@ -706,6 +710,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
706
710
& mut dup_bindings,
707
711
binding_span. unwrap_or ( binding. span ) ,
708
712
constness,
713
+ only_self_bounds,
709
714
) ;
710
715
// Okay to ignore `Err` because of `ErrorGuaranteed` (see above).
711
716
}
@@ -741,6 +746,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
741
746
self_ty : Ty < ' tcx > ,
742
747
bounds : & mut Bounds < ' tcx > ,
743
748
speculative : bool ,
749
+ only_self_bounds : OnlySelfBounds ,
744
750
) -> GenericArgCountResult {
745
751
let hir_id = trait_ref. hir_ref_id ;
746
752
let binding_span = None ;
@@ -766,6 +772,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
766
772
args,
767
773
infer_args,
768
774
self_ty,
775
+ only_self_bounds,
769
776
)
770
777
}
771
778
@@ -777,6 +784,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
777
784
args : & GenericArgs < ' _ > ,
778
785
self_ty : Ty < ' tcx > ,
779
786
bounds : & mut Bounds < ' tcx > ,
787
+ only_self_bounds : OnlySelfBounds ,
780
788
) {
781
789
let binding_span = Some ( span) ;
782
790
let constness = ty:: BoundConstness :: NotConst ;
@@ -799,6 +807,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
799
807
args,
800
808
infer_args,
801
809
self_ty,
810
+ only_self_bounds,
802
811
) ;
803
812
}
804
813
@@ -947,6 +956,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
947
956
ast_bounds : I ,
948
957
bounds : & mut Bounds < ' tcx > ,
949
958
bound_vars : & ' tcx ty:: List < ty:: BoundVariableKind > ,
959
+ only_self_bounds : OnlySelfBounds ,
950
960
) {
951
961
for ast_bound in ast_bounds {
952
962
match ast_bound {
@@ -964,11 +974,18 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
964
974
param_ty,
965
975
bounds,
966
976
false ,
977
+ only_self_bounds,
967
978
) ;
968
979
}
969
980
& hir:: GenericBound :: LangItemTrait ( lang_item, span, hir_id, args) => {
970
981
self . instantiate_lang_item_trait_ref (
971
- lang_item, span, hir_id, args, param_ty, bounds,
982
+ lang_item,
983
+ span,
984
+ hir_id,
985
+ args,
986
+ param_ty,
987
+ bounds,
988
+ only_self_bounds,
972
989
) ;
973
990
}
974
991
hir:: GenericBound :: Outlives ( lifetime) => {
@@ -1006,8 +1023,19 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1006
1023
& self ,
1007
1024
param_ty : Ty < ' tcx > ,
1008
1025
ast_bounds : & [ hir:: GenericBound < ' _ > ] ,
1026
+ only_self_bounds : OnlySelfBounds ,
1009
1027
) -> Bounds < ' tcx > {
1010
- self . compute_bounds_inner ( param_ty, ast_bounds)
1028
+ let mut bounds = Bounds :: default ( ) ;
1029
+ self . add_bounds (
1030
+ param_ty,
1031
+ ast_bounds. iter ( ) ,
1032
+ & mut bounds,
1033
+ ty:: List :: empty ( ) ,
1034
+ only_self_bounds,
1035
+ ) ;
1036
+ debug ! ( ?bounds) ;
1037
+
1038
+ bounds
1011
1039
}
1012
1040
1013
1041
/// Convert the bounds in `ast_bounds` that refer to traits which define an associated type
@@ -1029,17 +1057,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1029
1057
}
1030
1058
}
1031
1059
1032
- self . compute_bounds_inner ( param_ty, & result)
1033
- }
1034
-
1035
- fn compute_bounds_inner (
1036
- & self ,
1037
- param_ty : Ty < ' tcx > ,
1038
- ast_bounds : & [ hir:: GenericBound < ' _ > ] ,
1039
- ) -> Bounds < ' tcx > {
1040
1060
let mut bounds = Bounds :: default ( ) ;
1041
-
1042
- self . add_bounds ( param_ty, ast_bounds. iter ( ) , & mut bounds, ty:: List :: empty ( ) ) ;
1061
+ self . add_bounds (
1062
+ param_ty,
1063
+ result. iter ( ) ,
1064
+ & mut bounds,
1065
+ ty:: List :: empty ( ) ,
1066
+ OnlySelfBounds ( true ) ,
1067
+ ) ;
1043
1068
debug ! ( ?bounds) ;
1044
1069
1045
1070
bounds
@@ -1062,6 +1087,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1062
1087
dup_bindings : & mut FxHashMap < DefId , Span > ,
1063
1088
path_span : Span ,
1064
1089
constness : ty:: BoundConstness ,
1090
+ only_self_bounds : OnlySelfBounds ,
1065
1091
) -> Result < ( ) , ErrorGuaranteed > {
1066
1092
// Given something like `U: SomeTrait<T = X>`, we want to produce a
1067
1093
// predicate like `<U as SomeTrait>::T = X`. This is somewhat
@@ -1361,8 +1387,20 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1361
1387
//
1362
1388
// Calling `skip_binder` is okay, because `add_bounds` expects the `param_ty`
1363
1389
// parameter to have a skipped binder.
1364
- let param_ty = tcx. mk_alias ( ty:: Projection , projection_ty. skip_binder ( ) ) ;
1365
- self . add_bounds ( param_ty, ast_bounds. iter ( ) , bounds, projection_ty. bound_vars ( ) ) ;
1390
+ //
1391
+ // NOTE: If `only_self_bounds` is true, do NOT expand this associated
1392
+ // type bound into a trait predicate, since we only want to add predicates
1393
+ // for the `Self` type.
1394
+ if !only_self_bounds. 0 {
1395
+ let param_ty = tcx. mk_alias ( ty:: Projection , projection_ty. skip_binder ( ) ) ;
1396
+ self . add_bounds (
1397
+ param_ty,
1398
+ ast_bounds. iter ( ) ,
1399
+ bounds,
1400
+ projection_ty. bound_vars ( ) ,
1401
+ only_self_bounds,
1402
+ ) ;
1403
+ }
1366
1404
}
1367
1405
}
1368
1406
Ok ( ( ) )
@@ -1403,6 +1441,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1403
1441
dummy_self,
1404
1442
& mut bounds,
1405
1443
false ,
1444
+ // FIXME: This should be `true`, but we don't really handle
1445
+ // associated type bounds or type aliases in objects in a way
1446
+ // that makes this meaningful, I think.
1447
+ OnlySelfBounds ( false ) ,
1406
1448
) {
1407
1449
potential_assoc_types. extend ( cur_potential_assoc_types) ;
1408
1450
}
0 commit comments