@@ -48,6 +48,7 @@ use rustc_ast::{self as ast, *};
48
48
use rustc_ast_pretty:: pprust;
49
49
use rustc_data_structures:: captures:: Captures ;
50
50
use rustc_data_structures:: fingerprint:: Fingerprint ;
51
+ use rustc_data_structures:: fx:: FxIndexSet ;
51
52
use rustc_data_structures:: sorted_map:: SortedMap ;
52
53
use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
53
54
use rustc_data_structures:: sync:: Lrc ;
@@ -1525,7 +1526,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1525
1526
bounds : & GenericBounds ,
1526
1527
fn_kind : Option < FnDeclKind > ,
1527
1528
itctx : ImplTraitContext ,
1528
- precise_capturing : Option < & [ ast :: PreciseCapturingArg ] > ,
1529
+ precise_capturing_args : Option < & [ PreciseCapturingArg ] > ,
1529
1530
) -> hir:: TyKind < ' hir > {
1530
1531
// Make sure we know that some funky desugaring has been going on here.
1531
1532
// This is a first: there is code in other places like for loop
@@ -1541,9 +1542,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1541
1542
precise_capturing
1542
1543
. iter ( )
1543
1544
. filter_map ( |arg| match arg {
1544
- ast :: PreciseCapturingArg :: Lifetime ( lt) => Some ( * lt) ,
1545
- ast :: PreciseCapturingArg :: Arg ( ..) => None ,
1545
+ PreciseCapturingArg :: Lifetime ( lt) => Some ( * lt) ,
1546
+ PreciseCapturingArg :: Arg ( ..) => None ,
1546
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) )
1547
1552
. collect ( )
1548
1553
} else {
1549
1554
match origin {
@@ -1592,6 +1597,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1592
1597
captured_lifetimes_to_duplicate,
1593
1598
span,
1594
1599
opaque_ty_span,
1600
+ precise_capturing_args,
1595
1601
|this| this. lower_param_bounds ( bounds, itctx) ,
1596
1602
)
1597
1603
}
@@ -1601,9 +1607,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1601
1607
opaque_ty_node_id : NodeId ,
1602
1608
origin : hir:: OpaqueTyOrigin ,
1603
1609
in_trait : bool ,
1604
- captured_lifetimes_to_duplicate : Vec < Lifetime > ,
1610
+ captured_lifetimes_to_duplicate : FxIndexSet < Lifetime > ,
1605
1611
span : Span ,
1606
1612
opaque_ty_span : Span ,
1613
+ precise_capturing_args : Option < & [ PreciseCapturingArg ] > ,
1607
1614
lower_item_bounds : impl FnOnce ( & mut Self ) -> & ' hir [ hir:: GenericBound < ' hir > ] ,
1608
1615
) -> hir:: TyKind < ' hir > {
1609
1616
let opaque_ty_def_id = self . create_def (
@@ -1690,8 +1697,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1690
1697
// Install the remapping from old to new (if any). This makes sure that
1691
1698
// any lifetimes that would have resolved to the def-id of captured
1692
1699
// lifetimes are remapped to the new *synthetic* lifetimes of the opaque.
1693
- let bounds = this
1694
- . with_remapping ( captured_to_synthesized_mapping, |this| lower_item_bounds ( this) ) ;
1700
+ let ( bounds, precise_capturing_args) =
1701
+ this. with_remapping ( captured_to_synthesized_mapping, |this| {
1702
+ (
1703
+ lower_item_bounds ( this) ,
1704
+ precise_capturing_args. map ( |precise_capturing| {
1705
+ this. lower_precise_capturing_args ( precise_capturing)
1706
+ } ) ,
1707
+ )
1708
+ } ) ;
1695
1709
1696
1710
let generic_params =
1697
1711
this. arena . alloc_from_iter ( synthesized_lifetime_definitions. iter ( ) . map (
@@ -1736,6 +1750,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1736
1750
origin,
1737
1751
lifetime_mapping,
1738
1752
in_trait,
1753
+ precise_capturing_args,
1739
1754
} ;
1740
1755
1741
1756
// Generate an `type Foo = impl Trait;` declaration.
@@ -1768,6 +1783,23 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1768
1783
)
1769
1784
}
1770
1785
1786
+ fn lower_precise_capturing_args (
1787
+ & mut self ,
1788
+ precise_capturing_args : & [ PreciseCapturingArg ] ,
1789
+ ) -> & ' hir [ hir:: PreciseCapturingArg < ' hir > ] {
1790
+ self . arena . alloc_from_iter ( precise_capturing_args. iter ( ) . map ( |arg| match arg {
1791
+ PreciseCapturingArg :: Lifetime ( lt) => {
1792
+ hir:: PreciseCapturingArg :: Lifetime ( self . lower_lifetime ( lt) )
1793
+ }
1794
+ PreciseCapturingArg :: Arg ( _, node_id) => {
1795
+ let res = self . resolver . get_partial_res ( * node_id) . map_or ( Res :: Err , |partial_res| {
1796
+ partial_res. full_res ( ) . expect ( "no partial res expected for precise capture arg" )
1797
+ } ) ;
1798
+ hir:: PreciseCapturingArg :: Param ( self . lower_res ( res) , self . lower_node_id ( * node_id) )
1799
+ }
1800
+ } ) )
1801
+ }
1802
+
1771
1803
fn lower_fn_params_to_names ( & mut self , decl : & FnDecl ) -> & ' hir [ Ident ] {
1772
1804
self . arena . alloc_from_iter ( decl. inputs . iter ( ) . map ( |param| match param. pat . kind {
1773
1805
PatKind :: Ident ( _, ident, _) => self . lower_ident ( ident) ,
@@ -1908,7 +1940,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1908
1940
let opaque_ty_span =
1909
1941
self . mark_span_with_reason ( DesugaringKind :: Async , span, allowed_features) ;
1910
1942
1911
- let captured_lifetimes: Vec < _ > = self
1943
+ let captured_lifetimes = self
1912
1944
. resolver
1913
1945
. take_extra_lifetime_params ( opaque_ty_node_id)
1914
1946
. into_iter ( )
@@ -1922,6 +1954,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1922
1954
captured_lifetimes,
1923
1955
span,
1924
1956
opaque_ty_span,
1957
+ None ,
1925
1958
|this| {
1926
1959
let bound = this. lower_coroutine_fn_output_type_to_bound (
1927
1960
output,
0 commit comments