@@ -37,7 +37,7 @@ pub struct ConstraintContext<'a, 'tcx: 'a> {
37
37
invariant : VarianceTermPtr < ' a > ,
38
38
bivariant : VarianceTermPtr < ' a > ,
39
39
40
- pub constraints : Vec < Constraint < ' a > > ,
40
+ pub constraints : Vec < Constraint < ' a > > ,
41
41
}
42
42
43
43
/// Declares that the variable `decl_id` appears in a location with
@@ -49,8 +49,7 @@ pub struct Constraint<'a> {
49
49
}
50
50
51
51
pub fn add_constraints_from_crate < ' a , ' tcx > ( terms_cx : TermsContext < ' a , ' tcx > )
52
- -> ConstraintContext < ' a , ' tcx >
53
- {
52
+ -> ConstraintContext < ' a , ' tcx > {
54
53
let tcx = terms_cx. tcx ;
55
54
let covariant = terms_cx. arena . alloc ( ConstantTerm ( ty:: Covariant ) ) ;
56
55
let contravariant = terms_cx. arena . alloc ( ConstantTerm ( ty:: Contravariant ) ) ;
@@ -80,7 +79,9 @@ impl<'a, 'tcx, 'v> Visitor<'v> for ConstraintContext<'a, 'tcx> {
80
79
debug ! ( "visit_item item={}" , tcx. map. node_to_string( item. id) ) ;
81
80
82
81
match item. node {
83
- hir:: ItemEnum ( ..) | hir:: ItemStruct ( ..) | hir:: ItemUnion ( ..) => {
82
+ hir:: ItemEnum ( ..) |
83
+ hir:: ItemStruct ( ..) |
84
+ hir:: ItemUnion ( ..) => {
84
85
let scheme = tcx. lookup_item_type ( did) ;
85
86
86
87
// Not entirely obvious: constraints on structs/enums do not
@@ -111,16 +112,16 @@ impl<'a, 'tcx, 'v> Visitor<'v> for ConstraintContext<'a, 'tcx> {
111
112
hir:: ItemForeignMod ( ..) |
112
113
hir:: ItemTy ( ..) |
113
114
hir:: ItemImpl ( ..) |
114
- hir:: ItemDefaultImpl ( ..) => {
115
- }
115
+ hir:: ItemDefaultImpl ( ..) => { }
116
116
}
117
117
}
118
118
}
119
119
120
120
/// Is `param_id` a lifetime according to `map`?
121
121
fn is_lifetime ( map : & hir_map:: Map , param_id : ast:: NodeId ) -> bool {
122
122
match map. find ( param_id) {
123
- Some ( hir_map:: NodeLifetime ( ..) ) => true , _ => false
123
+ Some ( hir_map:: NodeLifetime ( ..) ) => true ,
124
+ _ => false ,
124
125
}
125
126
}
126
127
@@ -143,13 +144,12 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
143
144
let tcx = self . terms_cx . tcx ;
144
145
assert ! ( is_lifetime( & tcx. map, param_id) ) ;
145
146
match tcx. named_region_map . defs . get ( & param_id) {
146
- Some ( & rl:: DefEarlyBoundRegion ( _, lifetime_decl_id) )
147
- => lifetime_decl_id,
147
+ Some ( & rl:: DefEarlyBoundRegion ( _, lifetime_decl_id) ) => lifetime_decl_id,
148
148
Some ( _) => bug ! ( "should not encounter non early-bound cases" ) ,
149
149
150
150
// The lookup should only fail when `param_id` is
151
151
// itself a lifetime binding: use it as the decl_id.
152
- None => param_id,
152
+ None => param_id,
153
153
}
154
154
155
155
}
@@ -163,14 +163,15 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
163
163
// parameter (by inspecting parent of its binding declaration
164
164
// to see if it is introduced by a type or by a fn/impl).
165
165
166
- let check_result = |this : & ConstraintContext | -> bool {
166
+ let check_result = |this : & ConstraintContext | -> bool {
167
167
let tcx = this. terms_cx . tcx ;
168
168
let decl_id = this. find_binding_for_lifetime ( param_id) ;
169
169
// Currently only called on lifetimes; double-checking that.
170
170
assert ! ( is_lifetime( & tcx. map, param_id) ) ;
171
171
let parent_id = tcx. map . get_parent ( decl_id) ;
172
- let parent = tcx. map . find ( parent_id) . unwrap_or_else (
173
- || bug ! ( "tcx.map missing entry for id: {}" , parent_id) ) ;
172
+ let parent = tcx. map
173
+ . find ( parent_id)
174
+ . unwrap_or_else ( || bug ! ( "tcx.map missing entry for id: {}" , parent_id) ) ;
174
175
175
176
let is_inferred;
176
177
macro_rules! cannot_happen { ( ) => { {
@@ -186,14 +187,14 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
186
187
hir:: ItemEnum ( ..) |
187
188
hir:: ItemStruct ( ..) |
188
189
hir:: ItemUnion ( ..) |
189
- hir:: ItemTrait ( ..) => is_inferred = true ,
190
- hir:: ItemFn ( ..) => is_inferred = false ,
191
- _ => cannot_happen ! ( ) ,
190
+ hir:: ItemTrait ( ..) => is_inferred = true ,
191
+ hir:: ItemFn ( ..) => is_inferred = false ,
192
+ _ => cannot_happen ! ( ) ,
192
193
}
193
194
}
194
- hir_map:: NodeTraitItem ( ..) => is_inferred = false ,
195
- hir_map:: NodeImplItem ( ..) => is_inferred = false ,
196
- _ => cannot_happen ! ( ) ,
195
+ hir_map:: NodeTraitItem ( ..) => is_inferred = false ,
196
+ hir_map:: NodeImplItem ( ..) => is_inferred = false ,
197
+ _ => cannot_happen ! ( ) ,
197
198
}
198
199
199
200
return is_inferred;
@@ -230,21 +231,18 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
230
231
fn add_constraint ( & mut self ,
231
232
InferredIndex ( index) : InferredIndex ,
232
233
variance : VarianceTermPtr < ' a > ) {
233
- debug ! ( "add_constraint(index={}, variance={:?})" ,
234
- index, variance) ;
235
- self . constraints . push ( Constraint { inferred : InferredIndex ( index) ,
236
- variance : variance } ) ;
234
+ debug ! ( "add_constraint(index={}, variance={:?})" , index, variance) ;
235
+ self . constraints . push ( Constraint {
236
+ inferred : InferredIndex ( index) ,
237
+ variance : variance,
238
+ } ) ;
237
239
}
238
240
239
- fn contravariant ( & mut self ,
240
- variance : VarianceTermPtr < ' a > )
241
- -> VarianceTermPtr < ' a > {
241
+ fn contravariant ( & mut self , variance : VarianceTermPtr < ' a > ) -> VarianceTermPtr < ' a > {
242
242
self . xform ( variance, self . contravariant )
243
243
}
244
244
245
- fn invariant ( & mut self ,
246
- variance : VarianceTermPtr < ' a > )
247
- -> VarianceTermPtr < ' a > {
245
+ fn invariant ( & mut self , variance : VarianceTermPtr < ' a > ) -> VarianceTermPtr < ' a > {
248
246
self . xform ( variance, self . invariant )
249
247
}
250
248
@@ -257,23 +255,16 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
257
255
}
258
256
}
259
257
260
- fn xform ( & mut self ,
261
- v1 : VarianceTermPtr < ' a > ,
262
- v2 : VarianceTermPtr < ' a > )
263
- -> VarianceTermPtr < ' a > {
258
+ fn xform ( & mut self , v1 : VarianceTermPtr < ' a > , v2 : VarianceTermPtr < ' a > ) -> VarianceTermPtr < ' a > {
264
259
match ( * v1, * v2) {
265
260
( _, ConstantTerm ( ty:: Covariant ) ) => {
266
261
// Applying a "covariant" transform is always a no-op
267
262
v1
268
263
}
269
264
270
- ( ConstantTerm ( c1) , ConstantTerm ( c2) ) => {
271
- self . constant_term ( c1. xform ( c2) )
272
- }
265
+ ( ConstantTerm ( c1) , ConstantTerm ( c2) ) => self . constant_term ( c1. xform ( c2) ) ,
273
266
274
- _ => {
275
- & * self . terms_cx . arena . alloc ( TransformTerm ( v1, v2) )
276
- }
267
+ _ => & * self . terms_cx . arena . alloc ( TransformTerm ( v1, v2) ) ,
277
268
}
278
269
}
279
270
@@ -292,13 +283,12 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
292
283
// README.md for a discussion on dep-graph management.
293
284
self . tcx ( ) . dep_graph . read ( ItemVariances :: to_dep_node ( & trait_ref. def_id ) ) ;
294
285
295
- self . add_constraints_from_substs (
296
- generics,
297
- trait_ref. def_id ,
298
- & trait_def. generics . types ,
299
- & trait_def. generics . regions ,
300
- trait_ref. substs ,
301
- variance) ;
286
+ self . add_constraints_from_substs ( generics,
287
+ trait_ref. def_id ,
288
+ & trait_def. generics . types ,
289
+ & trait_def. generics . regions ,
290
+ trait_ref. substs ,
291
+ variance) ;
302
292
}
303
293
304
294
/// Adds constraints appropriate for an instance of `ty` appearing
@@ -313,13 +303,13 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
313
303
variance) ;
314
304
315
305
match ty. sty {
316
- ty:: TyBool |
317
- ty:: TyChar | ty:: TyInt ( _) | ty:: TyUint ( _) |
318
- ty:: TyFloat ( _) | ty:: TyStr | ty:: TyNever => {
319
- /* leaf type -- noop */
306
+ ty:: TyBool | ty:: TyChar | ty:: TyInt ( _) | ty:: TyUint ( _) | ty:: TyFloat ( _) |
307
+ ty:: TyStr | ty:: TyNever => {
308
+ // leaf type -- noop
320
309
}
321
310
322
- ty:: TyClosure ( ..) | ty:: TyAnon ( ..) => {
311
+ ty:: TyClosure ( ..) |
312
+ ty:: TyAnon ( ..) => {
323
313
bug ! ( "Unexpected closure type in variance computation" ) ;
324
314
}
325
315
@@ -329,11 +319,12 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
329
319
self . add_constraints_from_mt ( generics, mt, variance) ;
330
320
}
331
321
332
- ty:: TyBox ( typ) | ty:: TyArray ( typ, _) | ty:: TySlice ( typ) => {
322
+ ty:: TyBox ( typ) |
323
+ ty:: TyArray ( typ, _) |
324
+ ty:: TySlice ( typ) => {
333
325
self . add_constraints_from_ty ( generics, typ, variance) ;
334
326
}
335
327
336
-
337
328
ty:: TyRawPtr ( ref mt) => {
338
329
self . add_constraints_from_mt ( generics, mt, variance) ;
339
330
}
@@ -352,13 +343,12 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
352
343
// README.md for a discussion on dep-graph management.
353
344
self . tcx ( ) . dep_graph . read ( ItemVariances :: to_dep_node ( & def. did ) ) ;
354
345
355
- self . add_constraints_from_substs (
356
- generics,
357
- def. did ,
358
- & item_type. generics . types ,
359
- & item_type. generics . regions ,
360
- substs,
361
- variance) ;
346
+ self . add_constraints_from_substs ( generics,
347
+ def. did ,
348
+ & item_type. generics . types ,
349
+ & item_type. generics . regions ,
350
+ substs,
351
+ variance) ;
362
352
}
363
353
364
354
ty:: TyProjection ( ref data) => {
@@ -370,22 +360,20 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
370
360
// README.md for a discussion on dep-graph management.
371
361
self . tcx ( ) . dep_graph . read ( ItemVariances :: to_dep_node ( & trait_ref. def_id ) ) ;
372
362
373
- self . add_constraints_from_substs (
374
- generics,
375
- trait_ref. def_id ,
376
- & trait_def. generics . types ,
377
- & trait_def. generics . regions ,
378
- trait_ref. substs ,
379
- variance) ;
363
+ self . add_constraints_from_substs ( generics,
364
+ trait_ref. def_id ,
365
+ & trait_def. generics . types ,
366
+ & trait_def. generics . regions ,
367
+ trait_ref. substs ,
368
+ variance) ;
380
369
}
381
370
382
371
ty:: TyTrait ( ref data) => {
383
372
// The type `Foo<T+'a>` is contravariant w/r/t `'a`:
384
373
let contra = self . contravariant ( variance) ;
385
374
self . add_constraints_from_region ( generics, data. region_bound , contra) ;
386
375
387
- let poly_trait_ref =
388
- data. principal . with_self_ty ( self . tcx ( ) , self . tcx ( ) . types . err ) ;
376
+ let poly_trait_ref = data. principal . with_self_ty ( self . tcx ( ) , self . tcx ( ) . types . err ) ;
389
377
self . add_constraints_from_trait_ref ( generics, poly_trait_ref. 0 , variance) ;
390
378
391
379
for projection in & data. projection_bounds {
@@ -425,7 +413,8 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
425
413
426
414
ty:: TyInfer ( ..) => {
427
415
bug ! ( "unexpected type encountered in \
428
- variance inference: {}", ty) ;
416
+ variance inference: {}",
417
+ ty) ;
429
418
}
430
419
}
431
420
}
@@ -445,18 +434,17 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
445
434
variance) ;
446
435
447
436
for p in type_param_defs {
448
- let variance_decl =
449
- self . declared_variance ( p. def_id , def_id, p. index as usize ) ;
437
+ let variance_decl = self . declared_variance ( p. def_id , def_id, p. index as usize ) ;
450
438
let variance_i = self . xform ( variance, variance_decl) ;
451
439
let substs_ty = substs. type_for_def ( p) ;
452
440
debug ! ( "add_constraints_from_substs: variance_decl={:?} variance_i={:?}" ,
453
- variance_decl, variance_i) ;
441
+ variance_decl,
442
+ variance_i) ;
454
443
self . add_constraints_from_ty ( generics, substs_ty, variance_i) ;
455
444
}
456
445
457
446
for p in region_param_defs {
458
- let variance_decl =
459
- self . declared_variance ( p. def_id , def_id, p. index as usize ) ;
447
+ let variance_decl = self . declared_variance ( p. def_id , def_id, p. index as usize ) ;
460
448
let variance_i = self . xform ( variance, variance_decl) ;
461
449
let substs_r = substs. region_for_def ( p) ;
462
450
self . add_constraints_from_region ( generics, substs_r, variance_i) ;
@@ -494,15 +482,19 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
494
482
}
495
483
}
496
484
497
- ty:: ReStatic => { }
485
+ ty:: ReStatic => { }
498
486
499
487
ty:: ReLateBound ( ..) => {
500
488
// We do not infer variance for region parameters on
501
489
// methods or in fn types.
502
490
}
503
491
504
- ty:: ReFree ( ..) | ty:: ReScope ( ..) | ty:: ReVar ( ..) |
505
- ty:: ReSkolemized ( ..) | ty:: ReEmpty | ty:: ReErased => {
492
+ ty:: ReFree ( ..) |
493
+ ty:: ReScope ( ..) |
494
+ ty:: ReVar ( ..) |
495
+ ty:: ReSkolemized ( ..) |
496
+ ty:: ReEmpty |
497
+ ty:: ReErased => {
506
498
// We don't expect to see anything but 'static or bound
507
499
// regions when visiting member types or method types.
508
500
bug ! ( "unexpected region encountered in variance \
0 commit comments