@@ -223,6 +223,12 @@ impl ResolverAstLoweringExt for ResolverAstLowering {
223223 }
224224
225225 /// Obtain the list of lifetimes parameters to add to an item.
226+ ///
227+ /// Extra lifetime parameters should only be added in places that can appear
228+ /// as a `binder` in `LifetimeRes`.
229+ ///
230+ /// The extra lifetimes that appear from the parenthesized `Fn`-trait desugaring
231+ /// should appear at the enclosing `PolyTraitRef`.
226232 fn take_extra_lifetime_params ( & mut self , id : NodeId ) -> Vec < ( Ident , NodeId , LifetimeRes ) > {
227233 self . extra_lifetime_params_map . remove ( & id) . unwrap_or_default ( )
228234 }
@@ -721,6 +727,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
721727 }
722728
723729 /// Converts a lifetime into a new generic parameter.
730+ #[ tracing:: instrument( level = "debug" , skip( self ) ) ]
724731 fn lifetime_res_to_generic_param (
725732 & mut self ,
726733 ident : Ident ,
@@ -731,7 +738,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
731738 LifetimeRes :: Param { .. } => {
732739 ( hir:: ParamName :: Plain ( ident) , hir:: LifetimeParamKind :: Explicit )
733740 }
734- LifetimeRes :: Fresh { .. } => ( hir:: ParamName :: Fresh , hir:: LifetimeParamKind :: Elided ) ,
741+ LifetimeRes :: Fresh { param, .. } => {
742+ // Late resolution delegates to us the creation of the `LocalDefId`.
743+ let _def_id = self . create_def (
744+ self . current_hir_id_owner ,
745+ param,
746+ DefPathData :: LifetimeNs ( kw:: UnderscoreLifetime ) ,
747+ ) ;
748+ debug ! ( ?_def_id) ;
749+
750+ ( hir:: ParamName :: Fresh , hir:: LifetimeParamKind :: Elided )
751+ }
735752 LifetimeRes :: Static | LifetimeRes :: Error => return None ,
736753 res => panic ! (
737754 "Unexpected lifetime resolution {:?} for {:?} at {:?}" ,
@@ -777,11 +794,25 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
777794 /// Register a binder to be ignored for lifetime capture.
778795 #[ tracing:: instrument( level = "debug" , skip( self , f) ) ]
779796 #[ inline]
780- fn with_lifetime_binder < T > ( & mut self , binder : NodeId , f : impl FnOnce ( & mut Self ) -> T ) -> T {
797+ fn with_lifetime_binder < T > (
798+ & mut self ,
799+ binder : NodeId ,
800+ generic_params : & [ GenericParam ] ,
801+ f : impl FnOnce ( & mut Self , & ' hir [ hir:: GenericParam < ' hir > ] ) -> T ,
802+ ) -> T {
803+ let mut generic_params: Vec < _ > = self . lower_generic_params_mut ( generic_params) . collect ( ) ;
804+ let extra_lifetimes = self . resolver . take_extra_lifetime_params ( binder) ;
805+ debug ! ( ?extra_lifetimes) ;
806+ generic_params. extend ( extra_lifetimes. into_iter ( ) . filter_map ( |( ident, node_id, res) | {
807+ self . lifetime_res_to_generic_param ( ident, node_id, res)
808+ } ) ) ;
809+ let generic_params = self . arena . alloc_from_iter ( generic_params) ;
810+ debug ! ( ?generic_params) ;
811+
781812 if let Some ( ctxt) = & mut self . captured_lifetimes {
782813 ctxt. binders_to_ignore . insert ( binder) ;
783814 }
784- let ret = f ( self ) ;
815+ let ret = f ( self , generic_params ) ;
785816 if let Some ( ctxt) = & mut self . captured_lifetimes {
786817 ctxt. binders_to_ignore . remove ( & binder) ;
787818 }
@@ -1178,15 +1209,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11781209 let lifetime = self . lower_lifetime ( & region) ;
11791210 hir:: TyKind :: Rptr ( lifetime, self . lower_mt ( mt, itctx) )
11801211 }
1181- TyKind :: BareFn ( ref f) => self . with_lifetime_binder ( t. id , |this| {
1182- hir:: TyKind :: BareFn ( this. arena . alloc ( hir:: BareFnTy {
1183- generic_params : this. lower_generic_params ( & f. generic_params ) ,
1184- unsafety : this. lower_unsafety ( f. unsafety ) ,
1185- abi : this. lower_extern ( f. ext ) ,
1186- decl : this. lower_fn_decl ( & f. decl , None , FnDeclKind :: Pointer , None ) ,
1187- param_names : this. lower_fn_params_to_names ( & f. decl ) ,
1188- } ) )
1189- } ) ,
1212+ TyKind :: BareFn ( ref f) => {
1213+ self . with_lifetime_binder ( t. id , & f. generic_params , |this, generic_params| {
1214+ hir:: TyKind :: BareFn ( this. arena . alloc ( hir:: BareFnTy {
1215+ generic_params,
1216+ unsafety : this. lower_unsafety ( f. unsafety ) ,
1217+ abi : this. lower_extern ( f. ext ) ,
1218+ decl : this. lower_fn_decl ( & f. decl , None , FnDeclKind :: Pointer , None ) ,
1219+ param_names : this. lower_fn_params_to_names ( & f. decl ) ,
1220+ } ) )
1221+ } )
1222+ }
11901223 TyKind :: Never => hir:: TyKind :: Never ,
11911224 TyKind :: Tup ( ref tys) => hir:: TyKind :: Tup (
11921225 self . arena . alloc_from_iter ( tys. iter ( ) . map ( |ty| self . lower_ty_direct ( ty, itctx) ) ) ,
@@ -1814,8 +1847,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18141847 }
18151848 hir:: LifetimeName :: Param ( param, p_name)
18161849 }
1817- LifetimeRes :: Fresh { mut param, binder } => {
1850+ LifetimeRes :: Fresh { param, binder } => {
18181851 debug_assert_eq ! ( ident. name, kw:: UnderscoreLifetime ) ;
1852+ let mut param = self . local_def_id ( param) ;
18191853 if let Some ( mut captured_lifetimes) = self . captured_lifetimes . take ( ) {
18201854 if !captured_lifetimes. binders_to_ignore . contains ( & binder) {
18211855 match captured_lifetimes. captures . entry ( param) {
@@ -1952,13 +1986,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19521986 p : & PolyTraitRef ,
19531987 itctx : ImplTraitContext ,
19541988 ) -> hir:: PolyTraitRef < ' hir > {
1955- let bound_generic_params = self . lower_generic_params ( & p. bound_generic_params ) ;
1956-
1957- let trait_ref = self . with_lifetime_binder ( p. trait_ref . ref_id , |this| {
1958- this. lower_trait_ref ( & p. trait_ref , itctx)
1959- } ) ;
1960-
1961- hir:: PolyTraitRef { bound_generic_params, trait_ref, span : self . lower_span ( p. span ) }
1989+ self . with_lifetime_binder (
1990+ p. trait_ref . ref_id ,
1991+ & p. bound_generic_params ,
1992+ |this, bound_generic_params| {
1993+ let trait_ref = this. lower_trait_ref ( & p. trait_ref , itctx) ;
1994+ hir:: PolyTraitRef { bound_generic_params, trait_ref, span : this. lower_span ( p. span ) }
1995+ } ,
1996+ )
19621997 }
19631998
19641999 fn lower_mt ( & mut self , mt : & MutTy , itctx : ImplTraitContext ) -> hir:: MutTy < ' hir > {
0 commit comments