1+ use std:: assert_matches:: debug_assert_matches;
12use std:: cell:: LazyCell ;
23
34use rustc_data_structures:: fx:: { FxHashMap , FxIndexMap , FxIndexSet } ;
@@ -178,7 +179,7 @@ fn check_fn(tcx: TyCtxt<'_>, parent_def_id: LocalDefId) {
178179 ambient_variance : ty:: Covariant ,
179180 generics : tcx. generics_of ( parent_def_id) ,
180181 } ;
181- let _ = functional_variances. relate ( sig, sig) ;
182+ functional_variances. relate ( sig, sig) . unwrap ( ) ;
182183 functional_variances. variances
183184 } ) ,
184185 outlives_env : LazyCell :: new ( || {
@@ -208,10 +209,7 @@ where
208209 VarFn : FnOnce ( ) -> FxHashMap < DefId , ty:: Variance > ,
209210 OutlivesFn : FnOnce ( ) -> OutlivesEnvironment < ' tcx > ,
210211{
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 > ) {
215213 // When we get into a binder, we need to add its own bound vars to the scope.
216214 let mut added = vec ! [ ] ;
217215 for arg in t. bound_vars ( ) {
@@ -241,7 +239,7 @@ where
241239 }
242240 }
243241
244- fn visit_ty ( & mut self , t : Ty < ' tcx > ) -> Self :: Result {
242+ fn visit_ty ( & mut self , t : Ty < ' tcx > ) {
245243 if !t. has_aliases ( ) {
246244 return ;
247245 }
@@ -293,50 +291,43 @@ where
293291 current_def_id = generics. parent ;
294292 }
295293
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.
298295 let mut uncaptured_args: FxIndexSet < _ > = self
299296 . in_scope_parameters
300297 . iter ( )
301298 . filter ( |& ( def_id, _) | !captured. contains ( def_id) )
302299 . 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)
336329 } )
337- . collect ( ) ;
338- // We don't care to warn on these args.
339- uncaptured_args. retain ( |arg| !covariant_long_args. contains ( arg) ) ;
330+ } ) ;
340331
341332 // If we have uncaptured args, and if the opaque doesn't already have
342333 // `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> {
546537 ) -> RelateResult < ' tcx , T > {
547538 let old_variance = self . ambient_variance ;
548539 self . ambient_variance = self . ambient_variance . xform ( variance) ;
549- self . relate ( a, b) ? ;
540+ self . relate ( a, b) . unwrap ( ) ;
550541 self . ambient_variance = old_variance;
551542 Ok ( a)
552543 }
553544
554545 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 ( ) ;
556547 Ok ( a)
557548 }
558549
@@ -590,7 +581,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for FunctionalVariances<'tcx> {
590581 a : ty:: Const < ' tcx > ,
591582 b : ty:: Const < ' tcx > ,
592583 ) -> RelateResult < ' tcx , ty:: Const < ' tcx > > {
593- structurally_relate_consts ( self , a, b) ? ;
584+ structurally_relate_consts ( self , a, b) . unwrap ( ) ;
594585 Ok ( a)
595586 }
596587
@@ -602,7 +593,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for FunctionalVariances<'tcx> {
602593 where
603594 T : Relate < TyCtxt < ' tcx > > ,
604595 {
605- self . relate ( a. skip_binder ( ) , b. skip_binder ( ) ) ? ;
596+ self . relate ( a. skip_binder ( ) , b. skip_binder ( ) ) . unwrap ( ) ;
606597 Ok ( a)
607598 }
608599}
0 commit comments