@@ -344,12 +344,14 @@ where
344
344
// the problem is to add `T: 'r`, which isn't true. So, if there are no
345
345
// inference variables, we use a verify constraint instead of adding
346
346
// edges, which winds up enforcing the same condition.
347
+ let is_opaque = alias_ty. kind ( self . tcx ) == ty:: Opaque ;
347
348
if approx_env_bounds. is_empty ( )
348
349
&& trait_bounds. is_empty ( )
349
- && ( alias_ty. needs_infer ( ) || alias_ty . kind ( self . tcx ) == ty :: Opaque )
350
+ && ( alias_ty. needs_infer ( ) || is_opaque )
350
351
{
351
352
debug ! ( "no declared bounds" ) ;
352
- self . substs_must_outlive ( alias_ty. substs , origin, region) ;
353
+ let opt_variances = is_opaque. then ( || self . tcx . variances_of ( alias_ty. def_id ) ) ;
354
+ self . substs_must_outlive ( alias_ty. substs , origin, region, opt_variances) ;
353
355
return ;
354
356
}
355
357
@@ -395,22 +397,31 @@ where
395
397
self . delegate . push_verify ( origin, GenericKind :: Alias ( alias_ty) , region, verify_bound) ;
396
398
}
397
399
400
+ #[ instrument( level = "debug" , skip( self ) ) ]
398
401
fn substs_must_outlive (
399
402
& mut self ,
400
403
substs : SubstsRef < ' tcx > ,
401
404
origin : infer:: SubregionOrigin < ' tcx > ,
402
405
region : ty:: Region < ' tcx > ,
406
+ opt_variances : Option < & [ ty:: Variance ] > ,
403
407
) {
404
408
let constraint = origin. to_constraint_category ( ) ;
405
- for k in substs {
409
+ for ( index , k ) in substs. iter ( ) . enumerate ( ) {
406
410
match k. unpack ( ) {
407
411
GenericArgKind :: Lifetime ( lt) => {
408
- self . delegate . push_sub_region_constraint (
409
- origin. clone ( ) ,
410
- region,
411
- lt,
412
- constraint,
413
- ) ;
412
+ let variance = if let Some ( variances) = opt_variances {
413
+ variances[ index]
414
+ } else {
415
+ ty:: Invariant
416
+ } ;
417
+ if variance == ty:: Invariant {
418
+ self . delegate . push_sub_region_constraint (
419
+ origin. clone ( ) ,
420
+ region,
421
+ lt,
422
+ constraint,
423
+ ) ;
424
+ }
414
425
}
415
426
GenericArgKind :: Type ( ty) => {
416
427
self . type_must_outlive ( origin. clone ( ) , ty, region, constraint) ;
0 commit comments