@@ -344,12 +344,14 @@ where
344344 // the problem is to add `T: 'r`, which isn't true. So, if there are no
345345 // inference variables, we use a verify constraint instead of adding
346346 // edges, which winds up enforcing the same condition.
347+ let is_opaque = alias_ty. kind ( self . tcx ) == ty:: Opaque ;
347348 if approx_env_bounds. is_empty ( )
348349 && trait_bounds. is_empty ( )
349- && ( alias_ty. needs_infer ( ) || alias_ty . kind ( self . tcx ) == ty :: Opaque )
350+ && ( alias_ty. needs_infer ( ) || is_opaque )
350351 {
351352 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) ;
353355 return ;
354356 }
355357
@@ -395,22 +397,31 @@ where
395397 self . delegate . push_verify ( origin, GenericKind :: Alias ( alias_ty) , region, verify_bound) ;
396398 }
397399
400+ #[ instrument( level = "debug" , skip( self ) ) ]
398401 fn substs_must_outlive (
399402 & mut self ,
400403 substs : SubstsRef < ' tcx > ,
401404 origin : infer:: SubregionOrigin < ' tcx > ,
402405 region : ty:: Region < ' tcx > ,
406+ opt_variances : Option < & [ ty:: Variance ] > ,
403407 ) {
404408 let constraint = origin. to_constraint_category ( ) ;
405- for k in substs {
409+ for ( index , k ) in substs. iter ( ) . enumerate ( ) {
406410 match k. unpack ( ) {
407411 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+ }
414425 }
415426 GenericArgKind :: Type ( ty) => {
416427 self . type_must_outlive ( origin. clone ( ) , ty, region, constraint) ;
0 commit comments