2929use rustc_hir:: def:: DefKind ;
3030use rustc_middle:: ty:: * ;
3131use rustc_span:: def_id:: DefId ;
32- use rustc_span:: DUMMY_SP ;
32+ use rustc_span:: { Span , DUMMY_SP } ;
33+ use std:: borrow:: Cow ;
34+
35+ pub type Predicates < ' tcx > = Cow < ' tcx , [ ( Clause < ' tcx > , Span ) ] > ;
3336
3437/// Returns a list of type predicates for the definition with ID `def_id`, including inferred
3538/// lifetime constraints. This is the basic list of predicates we use for essentially all items.
36- pub fn predicates_defined_on ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> GenericPredicates < ' _ > {
37- let mut result = tcx. explicit_predicates_of ( def_id) ;
39+ pub fn predicates_defined_on ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> Predicates < ' _ > {
40+ let mut result = Cow :: Borrowed ( tcx. explicit_predicates_of ( def_id) . predicates ) ;
3841 let inferred_outlives = tcx. inferred_outlives_of ( def_id) ;
3942 if !inferred_outlives. is_empty ( ) {
40- let inferred_outlives_iter = inferred_outlives
41- . iter ( )
42- . map ( |( clause, span) | ( ( * clause) . upcast ( tcx) , * span) ) ;
43- result. predicates = tcx. arena . alloc_from_iter (
44- result
45- . predicates
46- . into_iter ( )
47- . copied ( )
48- . chain ( inferred_outlives_iter) ,
43+ result. to_mut ( ) . extend (
44+ inferred_outlives
45+ . iter ( )
46+ . map ( |( clause, span) | ( ( * clause) . upcast ( tcx) , * span) ) ,
4947 ) ;
5048 }
5149 result
@@ -66,7 +64,7 @@ pub fn required_predicates<'tcx>(
6664 tcx : TyCtxt < ' tcx > ,
6765 def_id : DefId ,
6866 add_drop : bool ,
69- ) -> GenericPredicates < ' tcx > {
67+ ) -> Predicates < ' tcx > {
7068 use DefKind :: * ;
7169 let def_kind = tcx. def_kind ( def_id) ;
7270 let mut predicates = match def_kind {
@@ -103,9 +101,7 @@ pub fn required_predicates<'tcx>(
103101 . map ( |ty| Binder :: dummy ( TraitRef :: new ( tcx, drop_trait, [ ty] ) ) )
104102 . map ( |tref| tref. upcast ( tcx) )
105103 . map ( |clause| ( clause, DUMMY_SP ) ) ;
106- predicates. predicates = tcx
107- . arena
108- . alloc_from_iter ( predicates. predicates . iter ( ) . copied ( ) . chain ( extra_bounds) ) ;
104+ predicates. to_mut ( ) . extend ( extra_bounds) ;
109105 }
110106 }
111107 predicates
@@ -134,36 +130,26 @@ pub fn implied_predicates<'tcx>(
134130 tcx : TyCtxt < ' tcx > ,
135131 def_id : DefId ,
136132 add_drop : bool ,
137- ) -> GenericPredicates < ' tcx > {
133+ ) -> Predicates < ' tcx > {
138134 use DefKind :: * ;
139135 let parent = tcx. opt_parent ( def_id) ;
140136 match tcx. def_kind ( def_id) {
141137 // We consider all predicates on traits to be outputs
142138 Trait | TraitAlias => predicates_defined_on ( tcx, def_id) ,
143139 AssocTy if matches ! ( tcx. def_kind( parent. unwrap( ) ) , Trait ) => {
144- let mut predicates = GenericPredicates {
145- parent,
146- // `skip_binder` is for the GAT `EarlyBinder`
147- predicates : tcx. explicit_item_bounds ( def_id) . skip_binder ( ) ,
148- ..GenericPredicates :: default ( )
149- } ;
140+ // `skip_binder` is for the GAT `EarlyBinder`
141+ let mut predicates = Cow :: Borrowed ( tcx. explicit_item_bounds ( def_id) . skip_binder ( ) ) ;
150142 if add_drop {
151143 // Add a `Drop` bound to the assoc item.
152144 let drop_trait = tcx. lang_items ( ) . drop_trait ( ) . unwrap ( ) ;
153145 let ty =
154146 Ty :: new_projection ( tcx, def_id, GenericArgs :: identity_for_item ( tcx, def_id) ) ;
155147 let tref = Binder :: dummy ( TraitRef :: new ( tcx, drop_trait, [ ty] ) ) ;
156- predicates. predicates = tcx. arena . alloc_from_iter (
157- predicates
158- . predicates
159- . iter ( )
160- . copied ( )
161- . chain ( [ ( tref. upcast ( tcx) , DUMMY_SP ) ] ) ,
162- ) ;
148+ predicates. to_mut ( ) . push ( ( tref. upcast ( tcx) , DUMMY_SP ) ) ;
163149 }
164150 predicates
165151 }
166- _ => GenericPredicates :: default ( ) ,
152+ _ => Predicates :: default ( ) ,
167153 }
168154}
169155
0 commit comments