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