@@ -223,6 +223,12 @@ impl ResolverAstLoweringExt for ResolverAstLowering {
223
223
}
224
224
225
225
/// 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`.
226
232
fn take_extra_lifetime_params ( & mut self , id : NodeId ) -> Vec < ( Ident , NodeId , LifetimeRes ) > {
227
233
self . extra_lifetime_params_map . remove ( & id) . unwrap_or_default ( )
228
234
}
@@ -721,6 +727,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
721
727
}
722
728
723
729
/// Converts a lifetime into a new generic parameter.
730
+ #[ tracing:: instrument( level = "debug" , skip( self ) ) ]
724
731
fn lifetime_res_to_generic_param (
725
732
& mut self ,
726
733
ident : Ident ,
@@ -731,7 +738,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
731
738
LifetimeRes :: Param { .. } => {
732
739
( hir:: ParamName :: Plain ( ident) , hir:: LifetimeParamKind :: Explicit )
733
740
}
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
+ }
735
752
LifetimeRes :: Static | LifetimeRes :: Error => return None ,
736
753
res => panic ! (
737
754
"Unexpected lifetime resolution {:?} for {:?} at {:?}" ,
@@ -777,11 +794,25 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
777
794
/// Register a binder to be ignored for lifetime capture.
778
795
#[ tracing:: instrument( level = "debug" , skip( self , f) ) ]
779
796
#[ 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
+
781
812
if let Some ( ctxt) = & mut self . captured_lifetimes {
782
813
ctxt. binders_to_ignore . insert ( binder) ;
783
814
}
784
- let ret = f ( self ) ;
815
+ let ret = f ( self , generic_params ) ;
785
816
if let Some ( ctxt) = & mut self . captured_lifetimes {
786
817
ctxt. binders_to_ignore . remove ( & binder) ;
787
818
}
@@ -1178,15 +1209,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1178
1209
let lifetime = self . lower_lifetime ( & region) ;
1179
1210
hir:: TyKind :: Rptr ( lifetime, self . lower_mt ( mt, itctx) )
1180
1211
}
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
+ }
1190
1223
TyKind :: Never => hir:: TyKind :: Never ,
1191
1224
TyKind :: Tup ( ref tys) => hir:: TyKind :: Tup (
1192
1225
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> {
1814
1847
}
1815
1848
hir:: LifetimeName :: Param ( param, p_name)
1816
1849
}
1817
- LifetimeRes :: Fresh { mut param, binder } => {
1850
+ LifetimeRes :: Fresh { param, binder } => {
1818
1851
debug_assert_eq ! ( ident. name, kw:: UnderscoreLifetime ) ;
1852
+ let mut param = self . local_def_id ( param) ;
1819
1853
if let Some ( mut captured_lifetimes) = self . captured_lifetimes . take ( ) {
1820
1854
if !captured_lifetimes. binders_to_ignore . contains ( & binder) {
1821
1855
match captured_lifetimes. captures . entry ( param) {
@@ -1952,13 +1986,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1952
1986
p : & PolyTraitRef ,
1953
1987
itctx : ImplTraitContext ,
1954
1988
) -> 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
+ )
1962
1997
}
1963
1998
1964
1999
fn lower_mt ( & mut self , mt : & MutTy , itctx : ImplTraitContext ) -> hir:: MutTy < ' hir > {
0 commit comments