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