1
+ use std:: assert_matches:: debug_assert_matches;
1
2
use std:: cell:: LazyCell ;
2
3
3
4
use rustc_data_structures:: fx:: { FxHashMap , FxIndexMap , FxIndexSet } ;
@@ -178,7 +179,7 @@ fn check_fn(tcx: TyCtxt<'_>, parent_def_id: LocalDefId) {
178
179
ambient_variance : ty:: Covariant ,
179
180
generics : tcx. generics_of ( parent_def_id) ,
180
181
} ;
181
- let _ = functional_variances. relate ( sig, sig) ;
182
+ functional_variances. relate ( sig, sig) . unwrap ( ) ;
182
183
functional_variances. variances
183
184
} ) ,
184
185
outlives_env : LazyCell :: new ( || {
@@ -208,10 +209,7 @@ where
208
209
VarFn : FnOnce ( ) -> FxHashMap < DefId , ty:: Variance > ,
209
210
OutlivesFn : FnOnce ( ) -> OutlivesEnvironment < ' tcx > ,
210
211
{
211
- fn visit_binder < T : TypeVisitable < TyCtxt < ' tcx > > > (
212
- & mut self ,
213
- t : & ty:: Binder < ' tcx , T > ,
214
- ) -> Self :: Result {
212
+ fn visit_binder < T : TypeVisitable < TyCtxt < ' tcx > > > ( & mut self , t : & ty:: Binder < ' tcx , T > ) {
215
213
// When we get into a binder, we need to add its own bound vars to the scope.
216
214
let mut added = vec ! [ ] ;
217
215
for arg in t. bound_vars ( ) {
@@ -241,7 +239,7 @@ where
241
239
}
242
240
}
243
241
244
- fn visit_ty ( & mut self , t : Ty < ' tcx > ) -> Self :: Result {
242
+ fn visit_ty ( & mut self , t : Ty < ' tcx > ) {
245
243
if !t. has_aliases ( ) {
246
244
return ;
247
245
}
@@ -293,50 +291,43 @@ where
293
291
current_def_id = generics. parent ;
294
292
}
295
293
296
- // Compute the set of in scope params that are not captured. Get their spans,
297
- // since that's all we really care about them for emitting the diagnostic.
294
+ // Compute the set of in scope params that are not captured.
298
295
let mut uncaptured_args: FxIndexSet < _ > = self
299
296
. in_scope_parameters
300
297
. iter ( )
301
298
. filter ( |& ( def_id, _) | !captured. contains ( def_id) )
302
299
. collect ( ) ;
303
-
304
- // These are args that we know are likely fine to "overcapture", since they can be
305
- // contravariantly shortened to one of the already-captured lifetimes that they
306
- // outlive.
307
- let covariant_long_args: FxIndexSet < _ > = uncaptured_args
308
- . iter ( )
309
- . copied ( )
310
- . filter ( |& ( def_id, kind) | {
311
- let Some ( ty:: Bivariant | ty:: Contravariant ) = self . variances . get ( def_id)
312
- else {
313
- return false ;
314
- } ;
315
- let DefKind :: LifetimeParam = self . tcx . def_kind ( def_id) else {
316
- return false ;
317
- } ;
318
- let uncaptured = match * kind {
319
- ParamKind :: Early ( name, index) => ty:: Region :: new_early_param (
320
- self . tcx ,
321
- ty:: EarlyParamRegion { name, index } ,
322
- ) ,
323
- ParamKind :: Free ( def_id, name) => ty:: Region :: new_late_param (
324
- self . tcx ,
325
- self . parent_def_id . to_def_id ( ) ,
326
- ty:: BoundRegionKind :: BrNamed ( def_id, name) ,
327
- ) ,
328
- ParamKind :: Late => return false ,
329
- } ;
330
- // Does this region outlive any captured region?
331
- captured_regions. iter ( ) . any ( |r| {
332
- self . outlives_env
333
- . free_region_map ( )
334
- . sub_free_regions ( self . tcx , * r, uncaptured)
335
- } )
300
+ // Remove the set of lifetimes that are in-scope that outlive some other captured
301
+ // lifetime and are contravariant (i.e. covariant in argument position).
302
+ uncaptured_args. retain ( |& ( def_id, kind) | {
303
+ let Some ( ty:: Bivariant | ty:: Contravariant ) = self . variances . get ( def_id) else {
304
+ // Keep all covariant/invariant args. Also if variance is `None`,
305
+ // then that means it's either not a lifetime, or it didn't show up
306
+ // anywhere in the signature.
307
+ return true ;
308
+ } ;
309
+ // We only computed variance of lifetimes...
310
+ debug_assert_matches ! ( self . tcx. def_kind( def_id) , DefKind :: LifetimeParam ) ;
311
+ let uncaptured = match * kind {
312
+ ParamKind :: Early ( name, index) => ty:: Region :: new_early_param (
313
+ self . tcx ,
314
+ ty:: EarlyParamRegion { name, index } ,
315
+ ) ,
316
+ ParamKind :: Free ( def_id, name) => ty:: Region :: new_late_param (
317
+ self . tcx ,
318
+ self . parent_def_id . to_def_id ( ) ,
319
+ ty:: BoundRegionKind :: BrNamed ( def_id, name) ,
320
+ ) ,
321
+ // Totally ignore late bound args from binders.
322
+ ParamKind :: Late => return true ,
323
+ } ;
324
+ // Does this region outlive any captured region?
325
+ !captured_regions. iter ( ) . any ( |r| {
326
+ self . outlives_env
327
+ . free_region_map ( )
328
+ . sub_free_regions ( self . tcx , * r, uncaptured)
336
329
} )
337
- . collect ( ) ;
338
- // We don't care to warn on these args.
339
- uncaptured_args. retain ( |arg| !covariant_long_args. contains ( arg) ) ;
330
+ } ) ;
340
331
341
332
// If we have uncaptured args, and if the opaque doesn't already have
342
333
// `use<>` syntax on it, and we're < edition 2024, then warn the user.
@@ -546,13 +537,13 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for FunctionalVariances<'tcx> {
546
537
) -> RelateResult < ' tcx , T > {
547
538
let old_variance = self . ambient_variance ;
548
539
self . ambient_variance = self . ambient_variance . xform ( variance) ;
549
- self . relate ( a, b) ? ;
540
+ self . relate ( a, b) . unwrap ( ) ;
550
541
self . ambient_variance = old_variance;
551
542
Ok ( a)
552
543
}
553
544
554
545
fn tys ( & mut self , a : Ty < ' tcx > , b : Ty < ' tcx > ) -> RelateResult < ' tcx , Ty < ' tcx > > {
555
- structurally_relate_tys ( self , a, b) ? ;
546
+ structurally_relate_tys ( self , a, b) . unwrap ( ) ;
556
547
Ok ( a)
557
548
}
558
549
@@ -590,7 +581,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for FunctionalVariances<'tcx> {
590
581
a : ty:: Const < ' tcx > ,
591
582
b : ty:: Const < ' tcx > ,
592
583
) -> RelateResult < ' tcx , ty:: Const < ' tcx > > {
593
- structurally_relate_consts ( self , a, b) ? ;
584
+ structurally_relate_consts ( self , a, b) . unwrap ( ) ;
594
585
Ok ( a)
595
586
}
596
587
@@ -602,7 +593,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for FunctionalVariances<'tcx> {
602
593
where
603
594
T : Relate < TyCtxt < ' tcx > > ,
604
595
{
605
- self . relate ( a. skip_binder ( ) , b. skip_binder ( ) ) ? ;
596
+ self . relate ( a. skip_binder ( ) , b. skip_binder ( ) ) . unwrap ( ) ;
606
597
Ok ( a)
607
598
}
608
599
}
0 commit comments