@@ -92,9 +92,8 @@ impl<'tcx> TyCtxt<'tcx> {
92
92
-> Vec < ObjectSafetyViolation >
93
93
{
94
94
debug_assert ! ( self . generics_of( trait_def_id) . has_self) ;
95
- let self_ty = self . mk_self_type ( ) ;
96
95
let violations = traits:: supertrait_def_ids ( self , trait_def_id)
97
- . filter ( |& def_id| self . predicates_reference_self ( def_id, self_ty , true ) )
96
+ . filter ( |& def_id| self . predicates_reference_self ( def_id, true ) )
98
97
. map ( |_| ObjectSafetyViolation :: SupertraitSelf )
99
98
. collect ( ) ;
100
99
@@ -109,11 +108,10 @@ impl<'tcx> TyCtxt<'tcx> {
109
108
-> Vec < ObjectSafetyViolation >
110
109
{
111
110
debug_assert ! ( self . generics_of( trait_def_id) . has_self) ;
112
- let self_ty = self . mk_self_type ( ) ;
113
111
debug ! ( "object_safety_violations: {:?}" , trait_def_id) ;
114
112
115
113
traits:: supertrait_def_ids ( self , trait_def_id)
116
- . flat_map ( |def_id| self . object_safety_violations_for_trait ( def_id, self_ty ) )
114
+ . flat_map ( |def_id| self . object_safety_violations_for_trait ( def_id) )
117
115
. collect ( )
118
116
}
119
117
@@ -123,29 +121,24 @@ impl<'tcx> TyCtxt<'tcx> {
123
121
/// otherwise ensure that they cannot be used when `Self=Trait`.
124
122
pub fn is_vtable_safe_method ( self , trait_def_id : DefId , method : & ty:: AssocItem ) -> bool {
125
123
debug_assert ! ( self . generics_of( trait_def_id) . has_self) ;
126
- let self_ty = self . mk_self_type ( ) ;
127
124
debug ! ( "is_vtable_safe_method({:?}, {:?})" , trait_def_id, method) ;
128
125
// Any method that has a `Self : Sized` requisite can't be called.
129
- if self . generics_require_sized_self ( method. def_id , self_ty ) {
126
+ if self . generics_require_sized_self ( method. def_id ) {
130
127
return false ;
131
128
}
132
129
133
- match self . virtual_call_violation_for_method ( trait_def_id, self_ty , method) {
130
+ match self . virtual_call_violation_for_method ( trait_def_id, method) {
134
131
None | Some ( MethodViolationCode :: WhereClauseReferencesSelf ( _) ) => true ,
135
132
Some ( _) => false ,
136
133
}
137
134
}
138
135
139
- fn object_safety_violations_for_trait (
140
- self ,
141
- trait_def_id : DefId ,
142
- self_ty : Ty < ' tcx > ,
143
- ) -> Vec < ObjectSafetyViolation > {
136
+ fn object_safety_violations_for_trait ( self , trait_def_id : DefId ) -> Vec < ObjectSafetyViolation > {
144
137
// Check methods for violations.
145
138
let mut violations: Vec < _ > = self . associated_items ( trait_def_id)
146
139
. filter ( |item| item. kind == ty:: AssocKind :: Method )
147
140
. filter_map ( |item|
148
- self . object_safety_violation_for_method ( trait_def_id, self_ty , & item)
141
+ self . object_safety_violation_for_method ( trait_def_id, & item)
149
142
. map ( |code| ObjectSafetyViolation :: Method ( item. ident . name , code) )
150
143
) . filter ( |violation| {
151
144
if let ObjectSafetyViolation :: Method ( _,
@@ -167,10 +160,10 @@ impl<'tcx> TyCtxt<'tcx> {
167
160
} ) . collect ( ) ;
168
161
169
162
// Check the trait itself.
170
- if self . trait_has_sized_self ( trait_def_id, self_ty ) {
163
+ if self . trait_has_sized_self ( trait_def_id) {
171
164
violations. push ( ObjectSafetyViolation :: SizedSelf ) ;
172
165
}
173
- if self . predicates_reference_self ( trait_def_id, self_ty , false ) {
166
+ if self . predicates_reference_self ( trait_def_id, false ) {
174
167
violations. push ( ObjectSafetyViolation :: SupertraitSelf ) ;
175
168
}
176
169
@@ -188,7 +181,6 @@ impl<'tcx> TyCtxt<'tcx> {
188
181
fn predicates_reference_self (
189
182
self ,
190
183
trait_def_id : DefId ,
191
- self_ty : Ty < ' tcx > ,
192
184
supertraits_only : bool ,
193
185
) -> bool {
194
186
let trait_ref = ty:: Binder :: dummy ( ty:: TraitRef :: identity ( self , trait_def_id) ) ;
@@ -197,6 +189,7 @@ impl<'tcx> TyCtxt<'tcx> {
197
189
} else {
198
190
self . predicates_of ( trait_def_id)
199
191
} ;
192
+ let self_ty = self . types . self_param ;
200
193
let has_self_ty = |t : Ty < ' tcx > | t. walk ( ) . any ( |t| t == self_ty) ;
201
194
predicates
202
195
. predicates
@@ -241,11 +234,11 @@ impl<'tcx> TyCtxt<'tcx> {
241
234
} )
242
235
}
243
236
244
- fn trait_has_sized_self ( self , trait_def_id : DefId , self_ty : Ty < ' tcx > ) -> bool {
245
- self . generics_require_sized_self ( trait_def_id, self_ty )
237
+ fn trait_has_sized_self ( self , trait_def_id : DefId ) -> bool {
238
+ self . generics_require_sized_self ( trait_def_id)
246
239
}
247
240
248
- fn generics_require_sized_self ( self , def_id : DefId , self_ty : Ty < ' tcx > ) -> bool {
241
+ fn generics_require_sized_self ( self , def_id : DefId ) -> bool {
249
242
let sized_def_id = match self . lang_items ( ) . sized_trait ( ) {
250
243
Some ( def_id) => def_id,
251
244
None => { return false ; /* No Sized trait, can't require it! */ }
@@ -258,7 +251,7 @@ impl<'tcx> TyCtxt<'tcx> {
258
251
. any ( |predicate| match predicate {
259
252
ty:: Predicate :: Trait ( ref trait_pred) => {
260
253
trait_pred. def_id ( ) == sized_def_id
261
- && trait_pred. skip_binder ( ) . self_ty ( ) == self_ty
254
+ && trait_pred. skip_binder ( ) . self_ty ( ) . is_param ( 0 )
262
255
}
263
256
ty:: Predicate :: Projection ( ..) |
264
257
ty:: Predicate :: Subtype ( ..) |
@@ -278,17 +271,16 @@ impl<'tcx> TyCtxt<'tcx> {
278
271
fn object_safety_violation_for_method (
279
272
self ,
280
273
trait_def_id : DefId ,
281
- self_ty : Ty < ' tcx > ,
282
274
method : & ty:: AssocItem ,
283
275
) -> Option < MethodViolationCode > {
284
276
debug ! ( "object_safety_violation_for_method({:?}, {:?})" , trait_def_id, method) ;
285
277
// Any method that has a `Self : Sized` requisite is otherwise
286
278
// exempt from the regulations.
287
- if self . generics_require_sized_self ( method. def_id , self_ty ) {
279
+ if self . generics_require_sized_self ( method. def_id ) {
288
280
return None ;
289
281
}
290
282
291
- self . virtual_call_violation_for_method ( trait_def_id, self_ty , method)
283
+ self . virtual_call_violation_for_method ( trait_def_id, method)
292
284
}
293
285
294
286
/// Returns `Some(_)` if this method cannot be called on a trait
@@ -298,7 +290,6 @@ impl<'tcx> TyCtxt<'tcx> {
298
290
fn virtual_call_violation_for_method (
299
291
self ,
300
292
trait_def_id : DefId ,
301
- self_ty : Ty < ' tcx > ,
302
293
method : & ty:: AssocItem ,
303
294
) -> Option < MethodViolationCode > {
304
295
// The method's first parameter must be named `self`
@@ -309,15 +300,11 @@ impl<'tcx> TyCtxt<'tcx> {
309
300
let sig = self . fn_sig ( method. def_id ) ;
310
301
311
302
for input_ty in & sig. skip_binder ( ) . inputs ( ) [ 1 ..] {
312
- if self . contains_illegal_self_type_reference ( trait_def_id, self_ty , input_ty) {
303
+ if self . contains_illegal_self_type_reference ( trait_def_id, input_ty) {
313
304
return Some ( MethodViolationCode :: ReferencesSelf ) ;
314
305
}
315
306
}
316
- if self . contains_illegal_self_type_reference (
317
- trait_def_id,
318
- self_ty,
319
- sig. output ( ) . skip_binder ( ) ,
320
- ) {
307
+ if self . contains_illegal_self_type_reference ( trait_def_id, sig. output ( ) . skip_binder ( ) ) {
321
308
return Some ( MethodViolationCode :: ReferencesSelf ) ;
322
309
}
323
310
@@ -336,7 +323,7 @@ impl<'tcx> TyCtxt<'tcx> {
336
323
// Do a shallow visit so that `contains_illegal_self_type_reference`
337
324
// may apply it's custom visiting.
338
325
. visit_tys_shallow ( |t| {
339
- self . contains_illegal_self_type_reference ( trait_def_id, self_ty , t)
326
+ self . contains_illegal_self_type_reference ( trait_def_id, t)
340
327
} ) {
341
328
let span = self . def_span ( method. def_id ) ;
342
329
return Some ( MethodViolationCode :: WhereClauseReferencesSelf ( span) ) ;
@@ -351,7 +338,7 @@ impl<'tcx> TyCtxt<'tcx> {
351
338
// However, this is already considered object-safe. We allow it as a special case here.
352
339
// FIXME(mikeyhew) get rid of this `if` statement once `receiver_is_dispatchable` allows
353
340
// `Receiver: Unsize<Receiver[Self => dyn Trait]>`
354
- if receiver_ty != self_ty {
341
+ if receiver_ty != self . types . self_param {
355
342
if !self . receiver_is_dispatchable ( method, receiver_ty) {
356
343
return Some ( MethodViolationCode :: UndispatchableReceiver ) ;
357
344
} else {
@@ -572,7 +559,7 @@ impl<'tcx> TyCtxt<'tcx> {
572
559
// Self: Unsize<U>
573
560
let unsize_predicate = ty:: TraitRef {
574
561
def_id : unsize_did,
575
- substs : self . mk_substs_trait ( self . mk_self_type ( ) , & [ unsized_self_ty. into ( ) ] ) ,
562
+ substs : self . mk_substs_trait ( self . types . self_param , & [ unsized_self_ty. into ( ) ] ) ,
576
563
} . to_predicate ( ) ;
577
564
578
565
// U: Trait<Arg1, ..., ArgN>
@@ -628,7 +615,6 @@ impl<'tcx> TyCtxt<'tcx> {
628
615
fn contains_illegal_self_type_reference (
629
616
self ,
630
617
trait_def_id : DefId ,
631
- self_ty : Ty < ' tcx > ,
632
618
ty : Ty < ' tcx > ,
633
619
) -> bool {
634
620
// This is somewhat subtle. In general, we want to forbid
@@ -672,6 +658,7 @@ impl<'tcx> TyCtxt<'tcx> {
672
658
673
659
let mut supertraits: Option < Vec < ty:: PolyTraitRef < ' tcx > > > = None ;
674
660
let mut error = false ;
661
+ let self_ty = self . types . self_param ;
675
662
ty. maybe_walk ( |ty| {
676
663
match ty. sty {
677
664
ty:: Param ( _) => {
0 commit comments