@@ -48,13 +48,6 @@ pub struct CombineFields<'infcx, 'tcx> {
48
48
pub define_opaque_types : DefineOpaqueTypes ,
49
49
}
50
50
51
- #[ derive( Copy , Clone , Debug ) ]
52
- pub enum RelationDir {
53
- SubtypeOf ,
54
- SupertypeOf ,
55
- EqTo ,
56
- }
57
-
58
51
impl < ' tcx > InferCtxt < ' tcx > {
59
52
pub fn super_combine_tys < R > (
60
53
& self ,
@@ -378,12 +371,10 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
378
371
pub fn instantiate (
379
372
& mut self ,
380
373
a_ty : Ty < ' tcx > ,
381
- dir : RelationDir ,
374
+ ambient_variance : ty :: Variance ,
382
375
b_vid : ty:: TyVid ,
383
376
a_is_expected : bool ,
384
377
) -> RelateResult < ' tcx , ( ) > {
385
- use self :: RelationDir :: * ;
386
-
387
378
// Get the actual variable that b_vid has been inferred to
388
379
debug_assert ! ( self . infcx. inner. borrow_mut( ) . type_variables( ) . probe( b_vid) . is_unknown( ) ) ;
389
380
@@ -398,7 +389,18 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
398
389
// `'?2` and `?3` are fresh region/type inference
399
390
// variables. (Down below, we will relate `a_ty <: b_ty`,
400
391
// adding constraints like `'x: '?2` and `?1 <: ?3`.)
401
- let Generalization { value : b_ty, needs_wf } = self . generalize ( a_ty, b_vid, dir) ?;
392
+ let Generalization { value : b_ty, needs_wf } = generalize:: generalize (
393
+ self . infcx ,
394
+ & mut CombineDelegate {
395
+ infcx : self . infcx ,
396
+ param_env : self . param_env ,
397
+ span : self . trace . span ( ) ,
398
+ } ,
399
+ a_ty,
400
+ b_vid,
401
+ ambient_variance,
402
+ ) ?;
403
+
402
404
debug ! ( ?b_ty) ;
403
405
self . infcx . inner . borrow_mut ( ) . type_variables ( ) . instantiate ( b_vid, b_ty) ;
404
406
@@ -417,62 +419,23 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
417
419
// relations wind up attributed to the same spans. We need
418
420
// to associate causes/spans with each of the relations in
419
421
// the stack to get this right.
420
- match dir {
421
- EqTo => self . equate ( a_is_expected) . relate ( a_ty, b_ty) ,
422
- SubtypeOf => self . sub ( a_is_expected) . relate ( a_ty, b_ty) ,
423
- SupertypeOf => self . sub ( a_is_expected) . relate_with_variance (
422
+ match ambient_variance {
423
+ ty :: Variance :: Invariant => self . equate ( a_is_expected) . relate ( a_ty, b_ty) ,
424
+ ty :: Variance :: Covariant => self . sub ( a_is_expected) . relate ( a_ty, b_ty) ,
425
+ ty :: Variance :: Contravariant => self . sub ( a_is_expected) . relate_with_variance (
424
426
ty:: Contravariant ,
425
427
ty:: VarianceDiagInfo :: default ( ) ,
426
428
a_ty,
427
429
b_ty,
428
430
) ,
431
+ ty:: Variance :: Bivariant => {
432
+ unreachable ! ( "no code should be generalizing bivariantly (currently)" )
433
+ }
429
434
} ?;
430
435
431
436
Ok ( ( ) )
432
437
}
433
438
434
- /// Attempts to generalize `ty` for the type variable `for_vid`.
435
- /// This checks for cycle -- that is, whether the type `ty`
436
- /// references `for_vid`. The `dir` is the "direction" for which we
437
- /// a performing the generalization (i.e., are we producing a type
438
- /// that can be used as a supertype etc).
439
- ///
440
- /// Preconditions:
441
- ///
442
- /// - `for_vid` is a "root vid"
443
- #[ instrument( skip( self ) , level = "trace" , ret) ]
444
- fn generalize (
445
- & mut self ,
446
- ty : Ty < ' tcx > ,
447
- for_vid : ty:: TyVid ,
448
- dir : RelationDir ,
449
- ) -> RelateResult < ' tcx , Generalization < Ty < ' tcx > > > {
450
- // Determine the ambient variance within which `ty` appears.
451
- // The surrounding equation is:
452
- //
453
- // ty [op] ty2
454
- //
455
- // where `op` is either `==`, `<:`, or `:>`. This maps quite
456
- // naturally.
457
- let ambient_variance = match dir {
458
- RelationDir :: EqTo => ty:: Invariant ,
459
- RelationDir :: SubtypeOf => ty:: Covariant ,
460
- RelationDir :: SupertypeOf => ty:: Contravariant ,
461
- } ;
462
-
463
- generalize:: generalize (
464
- self . infcx ,
465
- & mut CombineDelegate {
466
- infcx : self . infcx ,
467
- param_env : self . param_env ,
468
- span : self . trace . span ( ) ,
469
- } ,
470
- ty,
471
- for_vid,
472
- ambient_variance,
473
- )
474
- }
475
-
476
439
pub fn register_obligations ( & mut self , obligations : PredicateObligations < ' tcx > ) {
477
440
self . obligations . extend ( obligations. into_iter ( ) ) ;
478
441
}
0 commit comments