@@ -51,9 +51,9 @@ use rustc_data_structures::sorted_map::SortedMap;
51
51
use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
52
52
use rustc_data_structures:: sync:: Lrc ;
53
53
use rustc_errors:: { DiagArgFromDisplay , DiagCtxt , StashKey } ;
54
- use rustc_hir as hir;
55
54
use rustc_hir:: def:: { DefKind , LifetimeRes , Namespace , PartialRes , PerNS , Res } ;
56
55
use rustc_hir:: def_id:: { LocalDefId , LocalDefIdMap , CRATE_DEF_ID , LOCAL_CRATE } ;
56
+ use rustc_hir:: { self as hir} ;
57
57
use rustc_hir:: {
58
58
ConstArg , GenericArg , HirId , ItemLocalMap , MissingLifetimeKind , ParamName , TraitCandidate ,
59
59
} ;
@@ -1384,14 +1384,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1384
1384
}
1385
1385
None
1386
1386
}
1387
+ // Ignore `use` syntax since that is not valid in objects.
1388
+ GenericBound :: Use ( ..) => None ,
1387
1389
} ) ) ;
1388
1390
let lifetime_bound =
1389
1391
lifetime_bound. unwrap_or_else ( || this. elided_dyn_bound ( t. span ) ) ;
1390
1392
( bounds, lifetime_bound)
1391
1393
} ) ;
1392
1394
hir:: TyKind :: TraitObject ( bounds, lifetime_bound, * kind)
1393
1395
}
1394
- TyKind :: ImplTrait ( def_node_id, bounds, precise_capturing ) => {
1396
+ TyKind :: ImplTrait ( def_node_id, bounds) => {
1395
1397
let span = t. span ;
1396
1398
match itctx {
1397
1399
ImplTraitContext :: OpaqueTy { origin, fn_kind } => self . lower_opaque_impl_trait (
@@ -1401,12 +1403,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1401
1403
bounds,
1402
1404
fn_kind,
1403
1405
itctx,
1404
- precise_capturing. as_deref ( ) . map ( |( args, span) | ( args. as_slice ( ) , * span) ) ,
1405
1406
) ,
1406
1407
ImplTraitContext :: Universal => {
1407
- if let Some ( & ( _, span) ) = precise_capturing. as_deref ( ) {
1408
+ if let Some ( span) = bounds. iter ( ) . find_map ( |bound| match * bound {
1409
+ ast:: GenericBound :: Use ( _, span) => Some ( span) ,
1410
+ _ => None ,
1411
+ } ) {
1408
1412
self . tcx . dcx ( ) . emit_err ( errors:: NoPreciseCapturesOnApit { span } ) ;
1409
- } ;
1413
+ }
1414
+
1410
1415
let span = t. span ;
1411
1416
1412
1417
// HACK: pprust breaks strings with newlines when the type
@@ -1517,7 +1522,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1517
1522
bounds : & GenericBounds ,
1518
1523
fn_kind : Option < FnDeclKind > ,
1519
1524
itctx : ImplTraitContext ,
1520
- precise_capturing_args : Option < ( & [ PreciseCapturingArg ] , Span ) > ,
1521
1525
) -> hir:: TyKind < ' hir > {
1522
1526
// Make sure we know that some funky desugaring has been going on here.
1523
1527
// This is a first: there is code in other places like for loop
@@ -1526,59 +1530,61 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1526
1530
// frequently opened issues show.
1527
1531
let opaque_ty_span = self . mark_span_with_reason ( DesugaringKind :: OpaqueTy , span, None ) ;
1528
1532
1529
- let captured_lifetimes_to_duplicate =
1530
- if let Some ( ( precise_capturing, _) ) = precise_capturing_args {
1531
- // We'll actually validate these later on; all we need is the list of
1532
- // lifetimes to duplicate during this portion of lowering.
1533
- precise_capturing
1534
- . iter ( )
1535
- . filter_map ( |arg| match arg {
1536
- PreciseCapturingArg :: Lifetime ( lt) => Some ( * lt) ,
1537
- PreciseCapturingArg :: Arg ( ..) => None ,
1538
- } )
1539
- // Add in all the lifetimes mentioned in the bounds. We will error
1540
- // them out later, but capturing them here is important to make sure
1541
- // they actually get resolved in resolve_bound_vars.
1542
- . chain ( lifetime_collector:: lifetimes_in_bounds ( self . resolver , bounds) )
1543
- . collect ( )
1544
- } else {
1545
- match origin {
1546
- hir:: OpaqueTyOrigin :: TyAlias { .. } => {
1547
- // type alias impl trait and associated type position impl trait were
1548
- // decided to capture all in-scope lifetimes, which we collect for
1549
- // all opaques during resolution.
1533
+ let captured_lifetimes_to_duplicate = if let Some ( args) =
1534
+ bounds. iter ( ) . find_map ( |bound| match bound {
1535
+ ast:: GenericBound :: Use ( a, _) => Some ( a) ,
1536
+ _ => None ,
1537
+ } ) {
1538
+ // We'll actually validate these later on; all we need is the list of
1539
+ // lifetimes to duplicate during this portion of lowering.
1540
+ args. iter ( )
1541
+ . filter_map ( |arg| match arg {
1542
+ PreciseCapturingArg :: Lifetime ( lt) => Some ( * lt) ,
1543
+ PreciseCapturingArg :: Arg ( ..) => None ,
1544
+ } )
1545
+ // Add in all the lifetimes mentioned in the bounds. We will error
1546
+ // them out later, but capturing them here is important to make sure
1547
+ // they actually get resolved in resolve_bound_vars.
1548
+ . chain ( lifetime_collector:: lifetimes_in_bounds ( self . resolver , bounds) )
1549
+ . collect ( )
1550
+ } else {
1551
+ match origin {
1552
+ hir:: OpaqueTyOrigin :: TyAlias { .. } => {
1553
+ // type alias impl trait and associated type position impl trait were
1554
+ // decided to capture all in-scope lifetimes, which we collect for
1555
+ // all opaques during resolution.
1556
+ self . resolver
1557
+ . take_extra_lifetime_params ( opaque_ty_node_id)
1558
+ . into_iter ( )
1559
+ . map ( |( ident, id, _) | Lifetime { id, ident } )
1560
+ . collect ( )
1561
+ }
1562
+ hir:: OpaqueTyOrigin :: FnReturn ( ..) => {
1563
+ if matches ! (
1564
+ fn_kind. expect( "expected RPITs to be lowered with a FnKind" ) ,
1565
+ FnDeclKind :: Impl | FnDeclKind :: Trait
1566
+ ) || self . tcx . features ( ) . lifetime_capture_rules_2024
1567
+ || span. at_least_rust_2024 ( )
1568
+ {
1569
+ // return-position impl trait in trait was decided to capture all
1570
+ // in-scope lifetimes, which we collect for all opaques during resolution.
1550
1571
self . resolver
1551
1572
. take_extra_lifetime_params ( opaque_ty_node_id)
1552
1573
. into_iter ( )
1553
1574
. map ( |( ident, id, _) | Lifetime { id, ident } )
1554
1575
. collect ( )
1555
- }
1556
- hir:: OpaqueTyOrigin :: FnReturn ( ..) => {
1557
- if matches ! (
1558
- fn_kind. expect( "expected RPITs to be lowered with a FnKind" ) ,
1559
- FnDeclKind :: Impl | FnDeclKind :: Trait
1560
- ) || self . tcx . features ( ) . lifetime_capture_rules_2024
1561
- || span. at_least_rust_2024 ( )
1562
- {
1563
- // return-position impl trait in trait was decided to capture all
1564
- // in-scope lifetimes, which we collect for all opaques during resolution.
1565
- self . resolver
1566
- . take_extra_lifetime_params ( opaque_ty_node_id)
1567
- . into_iter ( )
1568
- . map ( |( ident, id, _) | Lifetime { id, ident } )
1569
- . collect ( )
1570
- } else {
1571
- // in fn return position, like the `fn test<'a>() -> impl Debug + 'a`
1572
- // example, we only need to duplicate lifetimes that appear in the
1573
- // bounds, since those are the only ones that are captured by the opaque.
1574
- lifetime_collector:: lifetimes_in_bounds ( self . resolver , bounds)
1575
- }
1576
- }
1577
- hir:: OpaqueTyOrigin :: AsyncFn ( ..) => {
1578
- unreachable ! ( "should be using `lower_async_fn_ret_ty`" )
1576
+ } else {
1577
+ // in fn return position, like the `fn test<'a>() -> impl Debug + 'a`
1578
+ // example, we only need to duplicate lifetimes that appear in the
1579
+ // bounds, since those are the only ones that are captured by the opaque.
1580
+ lifetime_collector:: lifetimes_in_bounds ( self . resolver , bounds)
1579
1581
}
1580
1582
}
1581
- } ;
1583
+ hir:: OpaqueTyOrigin :: AsyncFn ( ..) => {
1584
+ unreachable ! ( "should be using `lower_async_fn_ret_ty`" )
1585
+ }
1586
+ }
1587
+ } ;
1582
1588
debug ! ( ?captured_lifetimes_to_duplicate) ;
1583
1589
1584
1590
self . lower_opaque_inner (
@@ -1588,7 +1594,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1588
1594
captured_lifetimes_to_duplicate,
1589
1595
span,
1590
1596
opaque_ty_span,
1591
- precise_capturing_args,
1592
1597
|this| this. lower_param_bounds ( bounds, itctx) ,
1593
1598
)
1594
1599
}
@@ -1601,7 +1606,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1601
1606
captured_lifetimes_to_duplicate : FxIndexSet < Lifetime > ,
1602
1607
span : Span ,
1603
1608
opaque_ty_span : Span ,
1604
- precise_capturing_args : Option < ( & [ PreciseCapturingArg ] , Span ) > ,
1605
1609
lower_item_bounds : impl FnOnce ( & mut Self ) -> & ' hir [ hir:: GenericBound < ' hir > ] ,
1606
1610
) -> hir:: TyKind < ' hir > {
1607
1611
let opaque_ty_def_id = self . create_def (
@@ -1688,18 +1692,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1688
1692
// Install the remapping from old to new (if any). This makes sure that
1689
1693
// any lifetimes that would have resolved to the def-id of captured
1690
1694
// lifetimes are remapped to the new *synthetic* lifetimes of the opaque.
1691
- let ( bounds, precise_capturing_args) =
1692
- this. with_remapping ( captured_to_synthesized_mapping, |this| {
1693
- (
1694
- lower_item_bounds ( this) ,
1695
- precise_capturing_args. map ( |( precise_capturing, span) | {
1696
- (
1697
- this. lower_precise_capturing_args ( precise_capturing) ,
1698
- this. lower_span ( span) ,
1699
- )
1700
- } ) ,
1701
- )
1702
- } ) ;
1695
+ let bounds = this
1696
+ . with_remapping ( captured_to_synthesized_mapping, |this| lower_item_bounds ( this) ) ;
1703
1697
1704
1698
let generic_params =
1705
1699
this. arena . alloc_from_iter ( synthesized_lifetime_definitions. iter ( ) . map (
@@ -1744,7 +1738,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1744
1738
origin,
1745
1739
lifetime_mapping,
1746
1740
in_trait,
1747
- precise_capturing_args,
1748
1741
} ;
1749
1742
1750
1743
// Generate an `type Foo = impl Trait;` declaration.
@@ -1955,7 +1948,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1955
1948
captured_lifetimes,
1956
1949
span,
1957
1950
opaque_ty_span,
1958
- None ,
1959
1951
|this| {
1960
1952
let bound = this. lower_coroutine_fn_output_type_to_bound (
1961
1953
output,
@@ -2038,6 +2030,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2038
2030
GenericBound :: Outlives ( lifetime) => {
2039
2031
hir:: GenericBound :: Outlives ( self . lower_lifetime ( lifetime) )
2040
2032
}
2033
+ GenericBound :: Use ( args, span) => hir:: GenericBound :: Use (
2034
+ self . lower_precise_capturing_args ( args) ,
2035
+ self . lower_span ( * span) ,
2036
+ ) ,
2041
2037
}
2042
2038
}
2043
2039
0 commit comments