@@ -173,7 +173,7 @@ pub enum LifetimeRes {
173173 Fresh {
174174 /// Id of the generic parameter that introduced it.
175175 param : LocalDefId ,
176- /// Id to create the HirId.
176+ /// Id to create the HirId. This is used when creating the `Fresh` lifetime parameters.
177177 introducer : Option < NodeId > ,
178178 /// Id of the introducing place. See `Param`.
179179 binder : NodeId ,
@@ -693,27 +693,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
693693 Ident :: new ( ident. name , self . lower_span ( ident. span ) )
694694 }
695695
696- /// Creates a new `hir::GenericParam` for every new lifetime and
697- /// type parameter encountered while evaluating `f`. Definitions
698- /// are created with the parent provided. If no `parent_id` is
699- /// provided, no definitions will be returned.
700- fn collect_in_band_defs < T > (
701- & mut self ,
702- parent_def_id : LocalDefId ,
703- f : impl FnOnce ( & mut Self ) -> T ,
704- ) -> ( FxIndexMap < NodeId , Span > , T ) {
705- let lifetime_stash = std:: mem:: take ( & mut self . lifetimes_to_define ) ;
706- let was_collecting =
707- std:: mem:: replace ( & mut self . is_collecting_anonymous_lifetimes , Some ( parent_def_id) ) ;
708-
709- let res = f ( self ) ;
710-
711- self . is_collecting_anonymous_lifetimes = was_collecting;
712- let lifetimes_to_define = std:: mem:: replace ( & mut self . lifetimes_to_define , lifetime_stash) ;
713-
714- ( lifetimes_to_define, res)
715- }
716-
717696 /// Converts a lifetime into a new generic parameter.
718697 fn fresh_lifetime_to_generic_param (
719698 & mut self ,
@@ -733,9 +712,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
733712 }
734713
735714 /// Evaluates `f` with the lifetimes in `params` in-scope.
736- /// This is used to track which lifetimes have already been defined, and
737- /// which are new in-band lifetimes that need to have a definition created
738- /// for them.
715+ /// This is used to track which lifetimes have already been defined,
716+ /// which need to be duplicated for async fns.
739717 fn with_in_scope_lifetime_defs < T > (
740718 & mut self ,
741719 params : & [ GenericParam ] ,
@@ -758,37 +736,41 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
758736 res
759737 }
760738
761- /// Appends in-band lifetime defs and argument-position `impl
762- /// Trait` defs to the existing set of generics.
763- fn add_in_band_defs < T > (
739+ /// Creates a new `hir::GenericParam` for every new `Fresh` lifetime and
740+ /// universal `impl Trait` type parameter encountered while evaluating `f`.
741+ /// Definitions are created with the provided `parent_def_id`.
742+ fn add_implicit_generics < T > (
764743 & mut self ,
765744 generics : & Generics ,
766745 parent_def_id : LocalDefId ,
767746 f : impl FnOnce ( & mut Self , & mut Vec < hir:: GenericParam < ' hir > > ) -> T ,
768747 ) -> ( hir:: Generics < ' hir > , T ) {
769- let ( lifetimes_to_define , ( mut lowered_generics , impl_trait_defs , res ) ) = self
770- . collect_in_band_defs ( parent_def_id , |this| {
771- this . with_in_scope_lifetime_defs ( & generics . params , |this| {
772- let mut impl_trait_defs = Vec :: new ( ) ;
773- // Note: it is necessary to lower generics *before* calling `f`.
774- // When lowering `async fn`, there's a final step when lowering
775- // the return type that assumes that all in-scope lifetimes have
776- // already been added to either `in_scope_lifetimes` or
777- // `lifetimes_to_define`. If we swapped the order of these two,
778- // in-band-lifetimes introduced by generics or where-clauses
779- // wouldn't have been added yet.
780- let generics = this . lower_generics_mut (
781- generics ,
782- ImplTraitContext :: Universal (
783- & mut impl_trait_defs ,
784- this. current_hir_id_owner ,
785- ) ,
786- ) ;
787- let res = f ( this , & mut impl_trait_defs ) ;
788- ( generics , impl_trait_defs , res )
789- } )
748+ let lifetime_stash = std :: mem :: take ( & mut self . lifetimes_to_define ) ;
749+ let was_collecting =
750+ std :: mem :: replace ( & mut self . is_collecting_anonymous_lifetimes , Some ( parent_def_id ) ) ;
751+
752+ let mut impl_trait_defs = Vec :: new ( ) ;
753+
754+ let ( mut lowered_generics , res ) =
755+ self . with_in_scope_lifetime_defs ( & generics . params , |this| {
756+ // Note: it is necessary to lower generics *before* calling `f`.
757+ // When lowering `async fn`, there's a final step when lowering
758+ // the return type that assumes that all in-scope lifetimes have
759+ // already been added to either `in_scope_lifetimes` or
760+ // `lifetimes_to_define`. If we swapped the order of these two ,
761+ // fresh lifetimes introduced by generics or where-clauses
762+ // wouldn't have been added yet.
763+ let generics = this. lower_generics_mut (
764+ generics ,
765+ ImplTraitContext :: Universal ( & mut impl_trait_defs , this . current_hir_id_owner ) ,
766+ ) ;
767+ let res = f ( this , & mut impl_trait_defs ) ;
768+ ( generics , res )
790769 } ) ;
791770
771+ self . is_collecting_anonymous_lifetimes = was_collecting;
772+ let lifetimes_to_define = std:: mem:: replace ( & mut self . lifetimes_to_define , lifetime_stash) ;
773+
792774 lowered_generics. params . extend (
793775 lifetimes_to_define
794776 . into_iter ( )
@@ -1700,15 +1682,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17001682 // Calculate all the lifetimes that should be captured
17011683 // by the opaque type. This should include all in-scope
17021684 // lifetime parameters, including those defined in-band.
1703- //
17041685
17051686 // Input lifetime like `'a`:
17061687 let mut captures = FxHashMap :: default ( ) ;
17071688 for & ( p_name, def_id) in & self . in_scope_lifetimes {
17081689 let Ident { name, span } = p_name. ident ( ) ;
17091690 let node_id = self . resolver . next_node_id ( ) ;
17101691
1711- // Add a definition for the in-band lifetime def.
1692+ // Add a definition for the in scope lifetime def.
17121693 self . resolver . create_def (
17131694 opaque_ty_def_id,
17141695 node_id,
@@ -1735,7 +1716,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17351716 let def_id = self . resolver . local_def_id ( node_id) ;
17361717 let new_node_id = self . resolver . next_node_id ( ) ;
17371718
1738- // Add a definition for the in-band lifetime def.
1719+ // Add a definition for the `Fresh` lifetime def.
17391720 let new_def_id = self . resolver . create_def (
17401721 opaque_ty_def_id,
17411722 new_node_id,
0 commit comments