@@ -268,8 +268,8 @@ impl ResolverAstLowering {
268
268
///
269
269
/// The extra lifetimes that appear from the parenthesized `Fn`-trait desugaring
270
270
/// should appear at the enclosing `PolyTraitRef`.
271
- fn take_extra_lifetime_params ( & mut self , id : NodeId ) -> Vec < ( Ident , NodeId , LifetimeRes ) > {
272
- self . extra_lifetime_params_map . remove ( & id) . unwrap_or_default ( )
271
+ fn extra_lifetime_params ( & mut self , id : NodeId ) -> Vec < ( Ident , NodeId , LifetimeRes ) > {
272
+ self . extra_lifetime_params_map . get ( & id) . cloned ( ) . unwrap_or_default ( )
273
273
}
274
274
}
275
275
@@ -885,7 +885,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
885
885
let mut generic_params: Vec < _ > = self
886
886
. lower_generic_params_mut ( generic_params, hir:: GenericParamSource :: Binder )
887
887
. collect ( ) ;
888
- let extra_lifetimes = self . resolver . take_extra_lifetime_params ( binder) ;
888
+ let extra_lifetimes = self . resolver . extra_lifetime_params ( binder) ;
889
889
debug ! ( ?extra_lifetimes) ;
890
890
generic_params. extend ( extra_lifetimes. into_iter ( ) . filter_map ( |( ident, node_id, res) | {
891
891
self . lifetime_res_to_generic_param ( ident, node_id, res, hir:: GenericParamSource :: Binder )
@@ -1495,62 +1495,25 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1495
1495
// frequently opened issues show.
1496
1496
let opaque_ty_span = self . mark_span_with_reason ( DesugaringKind :: OpaqueTy , span, None ) ;
1497
1497
1498
- let captured_lifetimes_to_duplicate = if let Some ( args) =
1499
- // We only look for one `use<...>` syntax since we syntactially reject more than one.
1500
- bounds. iter ( ) . find_map (
1501
- |bound| match bound {
1502
- ast:: GenericBound :: Use ( a, _) => Some ( a) ,
1503
- _ => None ,
1504
- } ,
1505
- ) {
1506
- // We'll actually validate these later on; all we need is the list of
1507
- // lifetimes to duplicate during this portion of lowering.
1508
- args. iter ( )
1509
- . filter_map ( |arg| match arg {
1510
- PreciseCapturingArg :: Lifetime ( lt) => Some ( * lt) ,
1511
- PreciseCapturingArg :: Arg ( ..) => None ,
1512
- } )
1513
- // Add in all the lifetimes mentioned in the bounds. We will error
1514
- // them out later, but capturing them here is important to make sure
1515
- // they actually get resolved in resolve_bound_vars.
1516
- . chain ( lifetime_collector:: lifetimes_in_bounds ( self . resolver , bounds) )
1517
- . collect ( )
1518
- } else {
1519
- match origin {
1520
- hir:: OpaqueTyOrigin :: TyAlias { .. } => {
1521
- // type alias impl trait and associated type position impl trait were
1522
- // decided to capture all in-scope lifetimes, which we collect for
1523
- // all opaques during resolution.
1524
- self . resolver
1525
- . take_extra_lifetime_params ( opaque_ty_node_id)
1526
- . into_iter ( )
1527
- . map ( |( ident, id, _) | Lifetime { id, ident } )
1528
- . collect ( )
1529
- }
1530
- hir:: OpaqueTyOrigin :: FnReturn { in_trait_or_impl, .. } => {
1531
- if in_trait_or_impl. is_some ( )
1532
- || self . tcx . features ( ) . lifetime_capture_rules_2024
1533
- || span. at_least_rust_2024 ( )
1534
- {
1535
- // return-position impl trait in trait was decided to capture all
1536
- // in-scope lifetimes, which we collect for all opaques during resolution.
1537
- self . resolver
1538
- . take_extra_lifetime_params ( opaque_ty_node_id)
1539
- . into_iter ( )
1540
- . map ( |( ident, id, _) | Lifetime { id, ident } )
1541
- . collect ( )
1542
- } else {
1543
- // in fn return position, like the `fn test<'a>() -> impl Debug + 'a`
1544
- // example, we only need to duplicate lifetimes that appear in the
1545
- // bounds, since those are the only ones that are captured by the opaque.
1546
- lifetime_collector:: lifetimes_in_bounds ( self . resolver , bounds)
1547
- }
1548
- }
1549
- hir:: OpaqueTyOrigin :: AsyncFn { .. } => {
1550
- unreachable ! ( "should be using `lower_async_fn_ret_ty`" )
1551
- }
1498
+ // Whether this opaque always captures lifetimes in scope.
1499
+ // Right now, this is all RPITIT and TAITs, and when `lifetime_capture_rules_2024`
1500
+ // is enabled. We don't check the span of the edition, since this is done
1501
+ // on a per-opaque basis to account for nested opaques.
1502
+ let always_capture_in_scope = match origin {
1503
+ _ if self . tcx . features ( ) . lifetime_capture_rules_2024 => true ,
1504
+ hir:: OpaqueTyOrigin :: TyAlias { .. } => true ,
1505
+ hir:: OpaqueTyOrigin :: FnReturn { in_trait_or_impl, .. } => in_trait_or_impl. is_some ( ) ,
1506
+ hir:: OpaqueTyOrigin :: AsyncFn { .. } => {
1507
+ unreachable ! ( "should be using `lower_coroutine_fn_ret_ty`" )
1552
1508
}
1553
1509
} ;
1510
+ let captured_lifetimes_to_duplicate = lifetime_collector:: lifetimes_for_opaque (
1511
+ self . resolver ,
1512
+ always_capture_in_scope,
1513
+ opaque_ty_node_id,
1514
+ bounds,
1515
+ span,
1516
+ ) ;
1554
1517
debug ! ( ?captured_lifetimes_to_duplicate) ;
1555
1518
1556
1519
// Feature gate for RPITIT + use<..>
@@ -1920,7 +1883,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1920
1883
1921
1884
let captured_lifetimes = self
1922
1885
. resolver
1923
- . take_extra_lifetime_params ( opaque_ty_node_id)
1886
+ . extra_lifetime_params ( opaque_ty_node_id)
1924
1887
. into_iter ( )
1925
1888
. map ( |( ident, id, _) | Lifetime { id, ident } )
1926
1889
. collect ( ) ;
0 commit comments