@@ -647,15 +647,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
647647 & mut self ,
648648 f : impl FnOnce ( & mut Self ) -> T ,
649649 ) -> ( Vec < ( Span , ParamName ) > , T ) {
650- assert ! ( !self . is_collecting_in_band_lifetimes) ;
651- assert ! ( self . lifetimes_to_define. is_empty( ) ) ;
652- self . is_collecting_in_band_lifetimes = true ;
650+ let was_collecting = std:: mem:: replace ( & mut self . is_collecting_in_band_lifetimes , true ) ;
651+ let len = self . lifetimes_to_define . len ( ) ;
653652
654653 let res = f ( self ) ;
655654
656- self . is_collecting_in_band_lifetimes = false ;
657-
658- let lifetimes_to_define = std:: mem:: take ( & mut self . lifetimes_to_define ) ;
655+ let lifetimes_to_define = self . lifetimes_to_define . split_off ( len) ;
656+ self . is_collecting_in_band_lifetimes = was_collecting;
659657 ( lifetimes_to_define, res)
660658 }
661659
@@ -1687,18 +1685,29 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16871685 // this is because the elided lifetimes from the return type
16881686 // should be figured out using the ordinary elision rules, and
16891687 // this desugaring achieves that.
1688+
1689+ debug ! ( "lower_async_fn_ret_ty: in_scope_lifetimes={:#?}" , self . in_scope_lifetimes) ;
1690+ debug ! ( "lower_async_fn_ret_ty: lifetimes_to_define={:#?}" , self . lifetimes_to_define) ;
1691+
1692+ // Calculate all the lifetimes that should be captured
1693+ // by the opaque type. This should include all in-scope
1694+ // lifetime parameters, including those defined in-band.
16901695 //
1691- // The variable `input_lifetimes_count` tracks the number of
1692- // lifetime parameters to the opaque type *not counting* those
1693- // lifetimes elided in the return type. This includes those
1694- // that are explicitly declared (`in_scope_lifetimes`) and
1695- // those elided lifetimes we found in the arguments (current
1696- // content of `lifetimes_to_define`). Next, we will process
1697- // the return type, which will cause `lifetimes_to_define` to
1698- // grow.
1699- let input_lifetimes_count = self . in_scope_lifetimes . len ( ) + self . lifetimes_to_define . len ( ) ;
1700-
1701- let mut lifetime_params = Vec :: new ( ) ;
1696+ // `lifetime_params` is a vector of tuple (span, parameter name, lifetime name).
1697+
1698+ // Input lifetime like `'a` or `'1`:
1699+ let mut lifetime_params: Vec < _ > = self
1700+ . in_scope_lifetimes
1701+ . iter ( )
1702+ . cloned ( )
1703+ . map ( |name| ( name. ident ( ) . span , name, hir:: LifetimeName :: Param ( name) ) )
1704+ . chain (
1705+ self . lifetimes_to_define
1706+ . iter ( )
1707+ . map ( |& ( span, name) | ( span, name, hir:: LifetimeName :: Param ( name) ) ) ,
1708+ )
1709+ . collect ( ) ;
1710+
17021711 self . with_hir_id_owner ( opaque_ty_node_id, |this| {
17031712 // We have to be careful to get elision right here. The
17041713 // idea is that we create a lifetime parameter for each
@@ -1708,34 +1717,26 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17081717 //
17091718 // Then, we will create `fn foo(..) -> Foo<'_, '_>`, and
17101719 // hence the elision takes place at the fn site.
1711- let future_bound = this
1712- . with_anonymous_lifetime_mode ( AnonymousLifetimeMode :: CreateParameter , |this| {
1713- this. lower_async_fn_output_type_to_future_bound ( output, fn_def_id, span)
1720+ let ( lifetimes_to_define, future_bound) =
1721+ this. with_anonymous_lifetime_mode ( AnonymousLifetimeMode :: CreateParameter , |this| {
1722+ this. collect_in_band_defs ( |this| {
1723+ this. lower_async_fn_output_type_to_future_bound ( output, fn_def_id, span)
1724+ } )
17141725 } ) ;
1715-
17161726 debug ! ( "lower_async_fn_ret_ty: future_bound={:#?}" , future_bound) ;
1727+ debug ! ( "lower_async_fn_ret_ty: lifetimes_to_define={:#?}" , lifetimes_to_define) ;
17171728
1718- // Calculate all the lifetimes that should be captured
1719- // by the opaque type. This should include all in-scope
1720- // lifetime parameters, including those defined in-band.
1721- //
1722- // Note: this must be done after lowering the output type,
1723- // as the output type may introduce new in-band lifetimes.
1724- lifetime_params = this
1725- . in_scope_lifetimes
1726- . iter ( )
1727- . cloned ( )
1728- . map ( |name| ( name. ident ( ) . span , name) )
1729- . chain ( this. lifetimes_to_define . iter ( ) . cloned ( ) )
1730- . collect ( ) ;
1731-
1732- debug ! ( "lower_async_fn_ret_ty: in_scope_lifetimes={:#?}" , this. in_scope_lifetimes) ;
1733- debug ! ( "lower_async_fn_ret_ty: lifetimes_to_define={:#?}" , this. lifetimes_to_define) ;
1729+ lifetime_params. extend (
1730+ // Output lifetime like `'_`:
1731+ lifetimes_to_define
1732+ . into_iter ( )
1733+ . map ( |( span, name) | ( span, name, hir:: LifetimeName :: Implicit ) ) ,
1734+ ) ;
17341735 debug ! ( "lower_async_fn_ret_ty: lifetime_params={:#?}" , lifetime_params) ;
17351736
17361737 let generic_params =
1737- this. arena . alloc_from_iter ( lifetime_params. iter ( ) . map ( |( span, hir_name) | {
1738- this. lifetime_to_generic_param ( * span, * hir_name, opaque_ty_def_id)
1738+ this. arena . alloc_from_iter ( lifetime_params. iter ( ) . map ( |& ( span, hir_name, _ ) | {
1739+ this. lifetime_to_generic_param ( span, hir_name, opaque_ty_def_id)
17391740 } ) ) ;
17401741
17411742 let opaque_ty_item = hir:: OpaqueTy {
@@ -1769,25 +1770,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17691770 //
17701771 // For the "output" lifetime parameters, we just want to
17711772 // generate `'_`.
1772- let mut generic_args = Vec :: with_capacity ( lifetime_params. len ( ) ) ;
1773- generic_args. extend ( lifetime_params[ ..input_lifetimes_count] . iter ( ) . map (
1774- |& ( span, hir_name) | {
1775- // Input lifetime like `'a` or `'1`:
1773+ let generic_args =
1774+ self . arena . alloc_from_iter ( lifetime_params. into_iter ( ) . map ( |( span, _, name) | {
17761775 GenericArg :: Lifetime ( hir:: Lifetime {
17771776 hir_id : self . next_id ( ) ,
17781777 span : self . lower_span ( span) ,
1779- name : hir :: LifetimeName :: Param ( hir_name ) ,
1778+ name,
17801779 } )
1781- } ,
1782- ) ) ;
1783- generic_args. extend ( lifetime_params[ input_lifetimes_count..] . iter ( ) . map ( |& ( span, _) |
1784- // Output lifetime like `'_`.
1785- GenericArg :: Lifetime ( hir:: Lifetime {
1786- hir_id : self . next_id ( ) ,
1787- span : self . lower_span ( span) ,
1788- name : hir:: LifetimeName :: Implicit ,
1789- } ) ) ) ;
1790- let generic_args = self . arena . alloc_from_iter ( generic_args) ;
1780+ } ) ) ;
17911781
17921782 // Create the `Foo<...>` reference itself. Note that the `type
17931783 // Foo = impl Trait` is, internally, created as a child of the
0 commit comments