@@ -1345,61 +1345,13 @@ impl<'tcx> OpaqueHiddenType<'tcx> {
1345
1345
// type Foo<'a, 'b, 'c> = impl Trait<'a> + 'b;
1346
1346
// ```
1347
1347
// we may not use `'c` in the hidden type.
1348
- struct OpaqueTypeLifetimeCollector < ' tcx > {
1349
- lifetimes : FxHashSet < ty:: Region < ' tcx > > ,
1350
- }
1351
-
1352
- impl < ' tcx > ty:: TypeVisitor < ' tcx > for OpaqueTypeLifetimeCollector < ' tcx > {
1353
- fn visit_region ( & mut self , r : ty:: Region < ' tcx > ) -> ControlFlow < Self :: BreakTy > {
1354
- self . lifetimes . insert ( r) ;
1355
- r. super_visit_with ( self )
1356
- }
1357
- }
1348
+ let variances = tcx. variances_of ( def_id) ;
1349
+ debug ! ( ?variances) ;
1358
1350
1359
- let mut collector = OpaqueTypeLifetimeCollector { lifetimes : Default :: default ( ) } ;
1360
-
1361
- for pred in tcx. bound_explicit_item_bounds ( def_id. to_def_id ( ) ) . transpose_iter ( ) {
1362
- let pred = pred. map_bound ( |( pred, _) | * pred) . subst ( tcx, id_substs) ;
1363
-
1364
- trace ! ( pred=?pred. kind( ) ) ;
1365
-
1366
- // We only ignore opaque type substs if the opaque type is the outermost type.
1367
- // The opaque type may be nested within itself via recursion in e.g.
1368
- // type Foo<'a> = impl PartialEq<Foo<'a>>;
1369
- // which thus mentions `'a` and should thus accept hidden types that borrow 'a
1370
- // instead of requiring an additional `+ 'a`.
1371
- match pred. kind ( ) . skip_binder ( ) {
1372
- ty:: PredicateKind :: Trait ( TraitPredicate {
1373
- trait_ref : ty:: TraitRef { def_id : _, substs } ,
1374
- constness : _,
1375
- polarity : _,
1376
- } ) => {
1377
- trace ! ( ?substs) ;
1378
- for subst in & substs[ 1 ..] {
1379
- subst. visit_with ( & mut collector) ;
1380
- }
1381
- }
1382
- ty:: PredicateKind :: Projection ( ty:: ProjectionPredicate {
1383
- projection_ty : ty:: ProjectionTy { substs, item_def_id : _ } ,
1384
- term,
1385
- } ) => {
1386
- for subst in & substs[ 1 ..] {
1387
- subst. visit_with ( & mut collector) ;
1388
- }
1389
- term. visit_with ( & mut collector) ;
1390
- }
1391
- _ => {
1392
- pred. visit_with ( & mut collector) ;
1393
- }
1394
- }
1395
- }
1396
- let lifetimes = collector. lifetimes ;
1397
- trace ! ( ?lifetimes) ;
1398
1351
map. filter ( |( _, v) | {
1399
- let ty:: GenericArgKind :: Lifetime ( lt) = v. unpack ( ) else {
1400
- return true ;
1401
- } ;
1402
- lifetimes. contains ( & lt)
1352
+ let ty:: GenericArgKind :: Lifetime ( lt) = v. unpack ( ) else { return true } ;
1353
+ let ty:: ReEarlyBound ( ebr) = lt. kind ( ) else { bug ! ( ) } ;
1354
+ variances[ ebr. index as usize ] == ty:: Variance :: Invariant
1403
1355
} )
1404
1356
. collect ( )
1405
1357
}
0 commit comments