@@ -893,7 +893,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
893
893
// in-band-lifetimes introduced by generics or where-clauses
894
894
// wouldn't have been added yet.
895
895
let generics =
896
- this. lower_generics ( generics, ImplTraitContext :: Universal ( & mut params) ) ;
896
+ this. lower_generics_mut ( generics, ImplTraitContext :: Universal ( & mut params) ) ;
897
897
let res = f ( this, & mut params) ;
898
898
( params, ( generics, res) )
899
899
} )
@@ -914,6 +914,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
914
914
915
915
lowered_generics. params = lowered_params. into ( ) ;
916
916
917
+ let lowered_generics = lowered_generics. into_generics ( self . arena ) ;
917
918
( lowered_generics, res)
918
919
}
919
920
@@ -1224,28 +1225,25 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1224
1225
} ;
1225
1226
hir:: TyKind :: Rptr ( lifetime, self . lower_mt ( mt, itctx) )
1226
1227
}
1227
- TyKind :: BareFn ( ref f) => self . with_in_scope_lifetime_defs ( & f. generic_params , |this| {
1228
- this. with_anonymous_lifetime_mode ( AnonymousLifetimeMode :: PassThrough , |this| {
1229
- hir:: TyKind :: BareFn (
1230
- this. arena . alloc ( hir:: BareFnTy {
1231
- generic_params : this. arena . alloc_from_iter (
1232
- this. lower_generic_params (
1233
- & f. generic_params ,
1234
- & NodeMap :: default ( ) ,
1235
- ImplTraitContext :: disallowed ( ) ,
1236
- )
1237
- . into_iter ( ) ,
1228
+ TyKind :: BareFn ( ref f) => {
1229
+ self . with_in_scope_lifetime_defs ( & f. generic_params , |this| {
1230
+ this. with_anonymous_lifetime_mode ( AnonymousLifetimeMode :: PassThrough , |this| {
1231
+ hir:: TyKind :: BareFn ( this. arena . alloc ( hir:: BareFnTy {
1232
+ generic_params : this. lower_generic_params (
1233
+ & f. generic_params ,
1234
+ & NodeMap :: default ( ) ,
1235
+ ImplTraitContext :: disallowed ( ) ,
1238
1236
) ,
1239
1237
unsafety : f. unsafety ,
1240
1238
abi : this. lower_extern ( f. ext ) ,
1241
1239
decl : this. lower_fn_decl ( & f. decl , None , false , None ) ,
1242
1240
param_names : this. arena . alloc_from_iter (
1243
1241
this. lower_fn_params_to_names ( & f. decl ) . into_iter ( ) ,
1244
1242
) ,
1245
- } ) ,
1246
- )
1243
+ } ) )
1244
+ } )
1247
1245
} )
1248
- } ) ,
1246
+ }
1249
1247
TyKind :: Never => hir:: TyKind :: Never ,
1250
1248
TyKind :: Tup ( ref tys) => {
1251
1249
hir:: TyKind :: Tup ( self . arena . alloc_from_iter (
@@ -1414,12 +1412,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1414
1412
opaque_ty_def_index,
1415
1413
& hir_bounds,
1416
1414
) ;
1415
+ let lifetime_defs = self . arena . alloc_from_iter ( lifetime_defs. into_iter ( ) ) ;
1417
1416
1418
1417
debug ! ( "lower_opaque_impl_trait: lifetimes={:#?}" , lifetimes, ) ;
1419
1418
1420
1419
debug ! ( "lower_opaque_impl_trait: lifetime_defs={:#?}" , lifetime_defs, ) ;
1421
1420
1422
- self . with_hir_id_owner ( opaque_ty_node_id, |lctx| {
1421
+ self . with_hir_id_owner ( opaque_ty_node_id, move |lctx| {
1423
1422
let opaque_ty_item = hir:: OpaqueTy {
1424
1423
generics : hir:: Generics {
1425
1424
params : lifetime_defs,
@@ -1978,7 +1977,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1978
1977
hir_id : Some ( id) ,
1979
1978
res : Some ( self . lower_res ( res) ) ,
1980
1979
infer_args,
1981
- args : if generic_args. is_empty ( ) { None } else { Some ( self . arena . alloc ( generic_args) ) } ,
1980
+ args : if generic_args. is_empty ( ) {
1981
+ None
1982
+ } else {
1983
+ Some ( self . arena . alloc ( generic_args. into_generic_args ( self . arena ) ) )
1984
+ } ,
1982
1985
}
1983
1986
}
1984
1987
@@ -1987,15 +1990,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1987
1990
data : & AngleBracketedArgs ,
1988
1991
param_mode : ParamMode ,
1989
1992
mut itctx : ImplTraitContext < ' _ , ' hir > ,
1990
- ) -> ( hir :: GenericArgs < ' hir > , bool ) {
1993
+ ) -> ( GenericArgsCtor < ' hir > , bool ) {
1991
1994
let & AngleBracketedArgs { ref args, ref constraints, .. } = data;
1992
1995
let has_non_lt_args = args. iter ( ) . any ( |arg| match arg {
1993
1996
ast:: GenericArg :: Lifetime ( _) => false ,
1994
1997
ast:: GenericArg :: Type ( _) => true ,
1995
1998
ast:: GenericArg :: Const ( _) => true ,
1996
1999
} ) ;
1997
2000
(
1998
- hir :: GenericArgs {
2001
+ GenericArgsCtor {
1999
2002
args : args. iter ( ) . map ( |a| self . lower_generic_arg ( a, itctx. reborrow ( ) ) ) . collect ( ) ,
2000
2003
bindings : self . arena . alloc_from_iter (
2001
2004
constraints. iter ( ) . map ( |b| self . lower_assoc_ty_constraint ( b, itctx. reborrow ( ) ) ) ,
@@ -2009,7 +2012,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2009
2012
fn lower_parenthesized_parameter_data (
2010
2013
& mut self ,
2011
2014
data : & ParenthesizedArgs ,
2012
- ) -> ( hir :: GenericArgs < ' hir > , bool ) {
2015
+ ) -> ( GenericArgsCtor < ' hir > , bool ) {
2013
2016
// Switch to `PassThrough` mode for anonymous lifetimes; this
2014
2017
// means that we permit things like `&Ref<T>`, where `Ref` has
2015
2018
// a hidden lifetime parameter. This is needed for backwards
@@ -2024,15 +2027,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2024
2027
FunctionRetTy :: Ty ( ty) => this. lower_ty ( & ty, ImplTraitContext :: disallowed ( ) ) ,
2025
2028
FunctionRetTy :: Default ( _) => this. arena . alloc ( this. ty_tup ( span, & [ ] ) ) ,
2026
2029
} ;
2027
- let args = hir_vec ! [ GenericArg :: Type ( this. ty_tup( span, inputs) ) ] ;
2030
+ let args = vec ! [ GenericArg :: Type ( this. ty_tup( span, inputs) ) ] ;
2028
2031
let binding = hir:: TypeBinding {
2029
2032
hir_id : this. next_id ( ) ,
2030
2033
ident : Ident :: with_dummy_span ( FN_OUTPUT_NAME ) ,
2031
2034
span : output_ty. span ,
2032
2035
kind : hir:: TypeBindingKind :: Equality { ty : output_ty } ,
2033
2036
} ;
2034
2037
(
2035
- hir :: GenericArgs { args, bindings : arena_vec ! [ this; binding] , parenthesized : true } ,
2038
+ GenericArgsCtor { args, bindings : arena_vec ! [ this; binding] , parenthesized : true } ,
2036
2039
false ,
2037
2040
)
2038
2041
} )
@@ -2310,12 +2313,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2310
2313
debug ! ( "lower_async_fn_ret_ty: lifetimes_to_define={:#?}" , this. lifetimes_to_define) ;
2311
2314
debug ! ( "lower_async_fn_ret_ty: lifetime_params={:#?}" , lifetime_params) ;
2312
2315
2313
- let generic_params = lifetime_params
2314
- . iter ( )
2315
- . map ( |( span, hir_name) | {
2316
+ let generic_params =
2317
+ this. arena . alloc_from_iter ( lifetime_params. iter ( ) . map ( |( span, hir_name) | {
2316
2318
this. lifetime_to_generic_param ( * span, * hir_name, opaque_ty_def_index)
2317
- } )
2318
- . collect ( ) ;
2319
+ } ) ) ;
2319
2320
2320
2321
let opaque_ty_item = hir:: OpaqueTy {
2321
2322
generics : hir:: Generics {
@@ -2395,7 +2396,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2395
2396
2396
2397
// "<Output = T>"
2397
2398
let future_params = self . arena . alloc ( hir:: GenericArgs {
2398
- args : HirVec :: new ( ) ,
2399
+ args : & [ ] ,
2399
2400
bindings : arena_vec ! [ self ; hir:: TypeBinding {
2400
2401
ident: Ident :: with_dummy_span( FN_OUTPUT_NAME ) ,
2401
2402
kind: hir:: TypeBindingKind :: Equality { ty: output_ty } ,
@@ -2474,18 +2475,27 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2474
2475
hir:: Lifetime { hir_id : self . lower_node_id ( id) , span, name }
2475
2476
}
2476
2477
2477
- fn lower_generic_params (
2478
+ fn lower_generic_params_mut (
2478
2479
& mut self ,
2479
2480
params : & [ GenericParam ] ,
2480
2481
add_bounds : & NodeMap < Vec < GenericBound > > ,
2481
2482
mut itctx : ImplTraitContext < ' _ , ' hir > ,
2482
- ) -> HirVec < hir:: GenericParam < ' hir > > {
2483
+ ) -> Vec < hir:: GenericParam < ' hir > > {
2483
2484
params
2484
2485
. iter ( )
2485
2486
. map ( |param| self . lower_generic_param ( param, add_bounds, itctx. reborrow ( ) ) )
2486
2487
. collect ( )
2487
2488
}
2488
2489
2490
+ fn lower_generic_params (
2491
+ & mut self ,
2492
+ params : & [ GenericParam ] ,
2493
+ add_bounds : & NodeMap < Vec < GenericBound > > ,
2494
+ itctx : ImplTraitContext < ' _ , ' hir > ,
2495
+ ) -> & ' hir [ hir:: GenericParam < ' hir > ] {
2496
+ self . arena . alloc_from_iter ( self . lower_generic_params_mut ( params, add_bounds, itctx) )
2497
+ }
2498
+
2489
2499
fn lower_generic_param (
2490
2500
& mut self ,
2491
2501
param : & GenericParam ,
@@ -2593,11 +2603,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2593
2603
this. lower_trait_ref ( & p. trait_ref , itctx)
2594
2604
} ) ;
2595
2605
2596
- hir:: PolyTraitRef {
2597
- bound_generic_params : self . arena . alloc_from_iter ( bound_generic_params. into_iter ( ) ) ,
2598
- trait_ref,
2599
- span : p. span ,
2600
- }
2606
+ hir:: PolyTraitRef { bound_generic_params, trait_ref, span : p. span }
2601
2607
}
2602
2608
2603
2609
fn lower_mt ( & mut self , mt : & MutTy , itctx : ImplTraitContext < ' _ , ' hir > ) -> hir:: MutTy < ' hir > {
@@ -3304,3 +3310,24 @@ fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body<'hir>>) -> Vec<hir::BodyId>
3304
3310
body_ids. sort_by_key ( |b| bodies[ b] . value . span ) ;
3305
3311
body_ids
3306
3312
}
3313
+
3314
+ /// Helper struct for delayed construction of GenericArgs.
3315
+ struct GenericArgsCtor < ' hir > {
3316
+ args : Vec < hir:: GenericArg < ' hir > > ,
3317
+ bindings : & ' hir [ hir:: TypeBinding < ' hir > ] ,
3318
+ parenthesized : bool ,
3319
+ }
3320
+
3321
+ impl GenericArgsCtor < ' hir > {
3322
+ fn is_empty ( & self ) -> bool {
3323
+ self . args . is_empty ( ) && self . bindings . is_empty ( ) && !self . parenthesized
3324
+ }
3325
+
3326
+ fn into_generic_args ( self , arena : & ' hir Arena < ' hir > ) -> hir:: GenericArgs < ' hir > {
3327
+ hir:: GenericArgs {
3328
+ args : arena. alloc_from_iter ( self . args ) ,
3329
+ bindings : self . bindings ,
3330
+ parenthesized : self . parenthesized ,
3331
+ }
3332
+ }
3333
+ }
0 commit comments