@@ -48,6 +48,7 @@ use rustc_ast::{self as ast, *};
4848use rustc_ast_pretty:: pprust;
4949use rustc_data_structures:: captures:: Captures ;
5050use rustc_data_structures:: fingerprint:: Fingerprint ;
51+ use rustc_data_structures:: fx:: FxIndexSet ;
5152use rustc_data_structures:: sorted_map:: SortedMap ;
5253use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
5354use rustc_data_structures:: sync:: Lrc ;
@@ -1525,7 +1526,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15251526 bounds : & GenericBounds ,
15261527 fn_kind : Option < FnDeclKind > ,
15271528 itctx : ImplTraitContext ,
1528- precise_capturing : Option < & [ ast :: PreciseCapturingArg ] > ,
1529+ precise_capturing_args : Option < & [ PreciseCapturingArg ] > ,
15291530 ) -> hir:: TyKind < ' hir > {
15301531 // Make sure we know that some funky desugaring has been going on here.
15311532 // This is a first: there is code in other places like for loop
@@ -1541,9 +1542,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15411542 precise_capturing
15421543 . iter ( )
15431544 . 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 ,
15461547 } )
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) )
15471552 . collect ( )
15481553 } else {
15491554 match origin {
@@ -1592,6 +1597,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15921597 captured_lifetimes_to_duplicate,
15931598 span,
15941599 opaque_ty_span,
1600+ precise_capturing_args,
15951601 |this| this. lower_param_bounds ( bounds, itctx) ,
15961602 )
15971603 }
@@ -1601,9 +1607,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16011607 opaque_ty_node_id : NodeId ,
16021608 origin : hir:: OpaqueTyOrigin ,
16031609 in_trait : bool ,
1604- captured_lifetimes_to_duplicate : Vec < Lifetime > ,
1610+ captured_lifetimes_to_duplicate : FxIndexSet < Lifetime > ,
16051611 span : Span ,
16061612 opaque_ty_span : Span ,
1613+ precise_capturing_args : Option < & [ PreciseCapturingArg ] > ,
16071614 lower_item_bounds : impl FnOnce ( & mut Self ) -> & ' hir [ hir:: GenericBound < ' hir > ] ,
16081615 ) -> hir:: TyKind < ' hir > {
16091616 let opaque_ty_def_id = self . create_def (
@@ -1690,8 +1697,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16901697 // Install the remapping from old to new (if any). This makes sure that
16911698 // any lifetimes that would have resolved to the def-id of captured
16921699 // 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+ } ) ;
16951709
16961710 let generic_params =
16971711 this. arena . alloc_from_iter ( synthesized_lifetime_definitions. iter ( ) . map (
@@ -1736,6 +1750,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17361750 origin,
17371751 lifetime_mapping,
17381752 in_trait,
1753+ precise_capturing_args,
17391754 } ;
17401755
17411756 // Generate an `type Foo = impl Trait;` declaration.
@@ -1768,6 +1783,23 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17681783 )
17691784 }
17701785
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+
17711803 fn lower_fn_params_to_names ( & mut self , decl : & FnDecl ) -> & ' hir [ Ident ] {
17721804 self . arena . alloc_from_iter ( decl. inputs . iter ( ) . map ( |param| match param. pat . kind {
17731805 PatKind :: Ident ( _, ident, _) => self . lower_ident ( ident) ,
@@ -1908,7 +1940,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19081940 let opaque_ty_span =
19091941 self . mark_span_with_reason ( DesugaringKind :: Async , span, allowed_features) ;
19101942
1911- let captured_lifetimes: Vec < _ > = self
1943+ let captured_lifetimes = self
19121944 . resolver
19131945 . take_extra_lifetime_params ( opaque_ty_node_id)
19141946 . into_iter ( )
@@ -1922,6 +1954,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19221954 captured_lifetimes,
19231955 span,
19241956 opaque_ty_span,
1957+ None ,
19251958 |this| {
19261959 let bound = this. lower_coroutine_fn_output_type_to_bound (
19271960 output,
0 commit comments