@@ -547,11 +547,7 @@ impl<'a> LoweringContext<'a> {
547
547
// Creates a new hir::GenericParam for every new lifetime and type parameter
548
548
// encountered while evaluating `f`. Definitions are created with the parent
549
549
// provided. If no `parent_id` is provided, no definitions will be returned.
550
- fn collect_in_band_defs < T , F > (
551
- & mut self ,
552
- parent_id : Option < DefId > ,
553
- f : F ,
554
- ) -> ( Vec < hir:: GenericParam > , T )
550
+ fn collect_in_band_defs < T , F > ( & mut self , parent_id : DefId , f : F ) -> ( Vec < hir:: GenericParam > , T )
555
551
where
556
552
F : FnOnce ( & mut LoweringContext ) -> T ,
557
553
{
@@ -568,42 +564,38 @@ impl<'a> LoweringContext<'a> {
568
564
let in_band_ty_params = self . in_band_ty_params . split_off ( 0 ) ;
569
565
let lifetimes_to_define = self . lifetimes_to_define . split_off ( 0 ) ;
570
566
571
- let mut params = match parent_id {
572
- Some ( parent_id) => lifetimes_to_define
573
- . into_iter ( )
574
- . map ( |( span, name) | {
575
- let def_node_id = self . next_id ( ) . node_id ;
567
+ let params = lifetimes_to_define
568
+ . into_iter ( )
569
+ . map ( |( span, name) | {
570
+ let def_node_id = self . next_id ( ) . node_id ;
571
+
572
+ // Add a definition for the in-band lifetime def
573
+ self . resolver . definitions ( ) . create_def_with_parent (
574
+ parent_id. index ,
575
+ def_node_id,
576
+ DefPathData :: LifetimeDef ( name. as_str ( ) ) ,
577
+ DefIndexAddressSpace :: High ,
578
+ Mark :: root ( ) ,
579
+ span,
580
+ ) ;
576
581
577
- // Add a definition for the in-band lifetime def
578
- self . resolver . definitions ( ) . create_def_with_parent (
579
- parent_id. index ,
580
- def_node_id,
581
- DefPathData :: LifetimeDef ( name. as_str ( ) ) ,
582
- DefIndexAddressSpace :: High ,
583
- Mark :: root ( ) ,
582
+ hir:: GenericParam :: Lifetime ( hir:: LifetimeDef {
583
+ lifetime : hir:: Lifetime {
584
+ id : def_node_id,
584
585
span,
585
- ) ;
586
-
587
- hir:: GenericParam :: Lifetime ( hir:: LifetimeDef {
588
- lifetime : hir:: Lifetime {
589
- id : def_node_id,
590
- span,
591
- name : hir:: LifetimeName :: Name ( name) ,
592
- } ,
593
- bounds : Vec :: new ( ) . into ( ) ,
594
- pure_wrt_drop : false ,
595
- in_band : true ,
596
- } )
586
+ name : hir:: LifetimeName :: Name ( name) ,
587
+ } ,
588
+ bounds : Vec :: new ( ) . into ( ) ,
589
+ pure_wrt_drop : false ,
590
+ in_band : true ,
597
591
} )
598
- . collect ( ) ,
599
- None => Vec :: new ( ) ,
600
- } ;
601
-
602
- params. extend (
603
- in_band_ty_params
604
- . into_iter ( )
605
- . map ( |tp| hir:: GenericParam :: Type ( tp) ) ,
606
- ) ;
592
+ } )
593
+ . chain (
594
+ in_band_ty_params
595
+ . into_iter ( )
596
+ . map ( |tp| hir:: GenericParam :: Type ( tp) ) ,
597
+ )
598
+ . collect ( ) ;
607
599
608
600
( params, res)
609
601
}
@@ -654,20 +646,17 @@ impl<'a> LoweringContext<'a> {
654
646
fn add_in_band_defs < F , T > (
655
647
& mut self ,
656
648
generics : & Generics ,
657
- parent_id : Option < DefId > ,
649
+ parent_id : DefId ,
658
650
f : F ,
659
651
) -> ( hir:: Generics , T )
660
652
where
661
653
F : FnOnce ( & mut LoweringContext ) -> T ,
662
654
{
663
655
let ( in_band_defs, ( mut lowered_generics, res) ) = self . with_in_scope_lifetime_defs (
664
- generics
665
- . params
666
- . iter ( )
667
- . filter_map ( |p| match p {
668
- GenericParam :: Lifetime ( ld) => Some ( ld) ,
669
- _ => None ,
670
- } ) ,
656
+ generics. params . iter ( ) . filter_map ( |p| match p {
657
+ GenericParam :: Lifetime ( ld) => Some ( ld) ,
658
+ _ => None ,
659
+ } ) ,
671
660
|this| {
672
661
this. collect_in_band_defs ( parent_id, |this| {
673
662
( this. lower_generics ( generics) , f ( this) )
@@ -926,12 +915,10 @@ impl<'a> LoweringContext<'a> {
926
915
hir:: TyRptr ( lifetime, self . lower_mt ( mt, itctx) )
927
916
}
928
917
TyKind :: BareFn ( ref f) => self . with_in_scope_lifetime_defs (
929
- f. generic_params
930
- . iter ( )
931
- . filter_map ( |p| match p {
932
- GenericParam :: Lifetime ( ld) => Some ( ld) ,
933
- _ => None ,
934
- } ) ,
918
+ f. generic_params . iter ( ) . filter_map ( |p| match p {
919
+ GenericParam :: Lifetime ( ld) => Some ( ld) ,
920
+ _ => None ,
921
+ } ) ,
935
922
|this| {
936
923
hir:: TyBareFn ( P ( hir:: BareFnTy {
937
924
generic_params : this. lower_generic_params ( & f. generic_params , & NodeMap ( ) ) ,
@@ -1876,12 +1863,10 @@ impl<'a> LoweringContext<'a> {
1876
1863
span,
1877
1864
} ) => {
1878
1865
self . with_in_scope_lifetime_defs (
1879
- bound_generic_params
1880
- . iter ( )
1881
- . filter_map ( |p| match p {
1882
- GenericParam :: Lifetime ( ld) => Some ( ld) ,
1883
- _ => None ,
1884
- } ) ,
1866
+ bound_generic_params. iter ( ) . filter_map ( |p| match p {
1867
+ GenericParam :: Lifetime ( ld) => Some ( ld) ,
1868
+ _ => None ,
1869
+ } ) ,
1885
1870
|this| {
1886
1871
hir:: WherePredicate :: BoundPredicate ( hir:: WhereBoundPredicate {
1887
1872
bound_generic_params : this. lower_generic_params (
@@ -2097,14 +2082,14 @@ impl<'a> LoweringContext<'a> {
2097
2082
hir:: ItemConst ( self . lower_ty ( t, ImplTraitContext :: Disallowed ) , value)
2098
2083
}
2099
2084
ItemKind :: Fn ( ref decl, unsafety, constness, abi, ref generics, ref body) => {
2100
- let fn_def_id = self . resolver . definitions ( ) . opt_local_def_id ( id) ;
2085
+ let fn_def_id = self . resolver . definitions ( ) . local_def_id ( id) ;
2101
2086
self . with_new_scopes ( |this| {
2102
2087
let body_id = this. lower_body ( Some ( decl) , |this| {
2103
2088
let body = this. lower_block ( body, false ) ;
2104
2089
this. expr_block ( body, ThinVec :: new ( ) )
2105
2090
} ) ;
2106
2091
let ( generics, fn_decl) = this. add_in_band_defs ( generics, fn_def_id, |this| {
2107
- this. lower_fn_decl ( decl, fn_def_id, true )
2092
+ this. lower_fn_decl ( decl, Some ( fn_def_id) , true )
2108
2093
} ) ;
2109
2094
2110
2095
hir:: ItemFn (
@@ -2151,7 +2136,7 @@ impl<'a> LoweringContext<'a> {
2151
2136
ref ty,
2152
2137
ref impl_items,
2153
2138
) => {
2154
- let def_id = self . resolver . definitions ( ) . opt_local_def_id ( id) ;
2139
+ let def_id = self . resolver . definitions ( ) . local_def_id ( id) ;
2155
2140
let ( generics, ( ifce, lowered_ty) ) =
2156
2141
self . add_in_band_defs ( ast_generics, def_id, |this| {
2157
2142
let ifce = ifce. as_ref ( ) . map ( |trait_ref| {
@@ -2170,13 +2155,10 @@ impl<'a> LoweringContext<'a> {
2170
2155
} ) ;
2171
2156
2172
2157
let new_impl_items = self . with_in_scope_lifetime_defs (
2173
- ast_generics
2174
- . params
2175
- . iter ( )
2176
- . filter_map ( |p| match p {
2177
- GenericParam :: Lifetime ( ld) => Some ( ld) ,
2178
- _ => None ,
2179
- } ) ,
2158
+ ast_generics. params . iter ( ) . filter_map ( |p| match p {
2159
+ GenericParam :: Lifetime ( ld) => Some ( ld) ,
2160
+ _ => None ,
2161
+ } ) ,
2180
2162
|this| {
2181
2163
impl_items
2182
2164
. iter ( )
@@ -2341,7 +2323,7 @@ impl<'a> LoweringContext<'a> {
2341
2323
fn lower_trait_item ( & mut self , i : & TraitItem ) -> hir:: TraitItem {
2342
2324
self . with_parent_def ( i. id , |this| {
2343
2325
let LoweredNodeId { node_id, hir_id } = this. lower_node_id ( i. id ) ;
2344
- let fn_def_id = this. resolver . definitions ( ) . opt_local_def_id ( node_id) ;
2326
+ let trait_item_def_id = this. resolver . definitions ( ) . local_def_id ( node_id) ;
2345
2327
2346
2328
let ( generics, node) = match i. node {
2347
2329
TraitItemKind :: Const ( ref ty, ref default) => (
@@ -2355,9 +2337,9 @@ impl<'a> LoweringContext<'a> {
2355
2337
) ,
2356
2338
TraitItemKind :: Method ( ref sig, None ) => {
2357
2339
let names = this. lower_fn_args_to_names ( & sig. decl ) ;
2358
- this. add_in_band_defs ( & i. generics , fn_def_id , |this| {
2340
+ this. add_in_band_defs ( & i. generics , trait_item_def_id , |this| {
2359
2341
hir:: TraitItemKind :: Method (
2360
- this. lower_method_sig ( sig, fn_def_id , false ) ,
2342
+ this. lower_method_sig ( sig, trait_item_def_id , false ) ,
2361
2343
hir:: TraitMethod :: Required ( names) ,
2362
2344
)
2363
2345
} )
@@ -2368,9 +2350,9 @@ impl<'a> LoweringContext<'a> {
2368
2350
this. expr_block ( body, ThinVec :: new ( ) )
2369
2351
} ) ;
2370
2352
2371
- this. add_in_band_defs ( & i. generics , fn_def_id , |this| {
2353
+ this. add_in_band_defs ( & i. generics , trait_item_def_id , |this| {
2372
2354
hir:: TraitItemKind :: Method (
2373
- this. lower_method_sig ( sig, fn_def_id , false ) ,
2355
+ this. lower_method_sig ( sig, trait_item_def_id , false ) ,
2374
2356
hir:: TraitMethod :: Provided ( body_id) ,
2375
2357
)
2376
2358
} )
@@ -2427,7 +2409,7 @@ impl<'a> LoweringContext<'a> {
2427
2409
fn lower_impl_item ( & mut self , i : & ImplItem ) -> hir:: ImplItem {
2428
2410
self . with_parent_def ( i. id , |this| {
2429
2411
let LoweredNodeId { node_id, hir_id } = this. lower_node_id ( i. id ) ;
2430
- let fn_def_id = this. resolver . definitions ( ) . opt_local_def_id ( node_id) ;
2412
+ let impl_item_def_id = this. resolver . definitions ( ) . local_def_id ( node_id) ;
2431
2413
2432
2414
let ( generics, node) = match i. node {
2433
2415
ImplItemKind :: Const ( ref ty, ref expr) => {
@@ -2447,9 +2429,9 @@ impl<'a> LoweringContext<'a> {
2447
2429
} ) ;
2448
2430
let impl_trait_return_allow = !this. is_in_trait_impl ;
2449
2431
2450
- this. add_in_band_defs ( & i. generics , fn_def_id , |this| {
2432
+ this. add_in_band_defs ( & i. generics , impl_item_def_id , |this| {
2451
2433
hir:: ImplItemKind :: Method (
2452
- this. lower_method_sig ( sig, fn_def_id , impl_trait_return_allow) ,
2434
+ this. lower_method_sig ( sig, impl_item_def_id , impl_trait_return_allow) ,
2453
2435
body_id,
2454
2436
)
2455
2437
} )
@@ -2575,10 +2557,10 @@ impl<'a> LoweringContext<'a> {
2575
2557
attrs : this. lower_attrs ( & i. attrs ) ,
2576
2558
node : match i. node {
2577
2559
ForeignItemKind :: Fn ( ref fdec, ref generics) => {
2578
- // Disallow impl Trait in foreign items
2579
2560
let ( generics, ( fn_dec, fn_args) ) =
2580
- this. add_in_band_defs ( generics, Some ( def_id) , |this| {
2561
+ this. add_in_band_defs ( generics, def_id, |this| {
2581
2562
(
2563
+ // Disallow impl Trait in foreign items
2582
2564
this. lower_fn_decl ( fdec, None , false ) ,
2583
2565
this. lower_fn_args_to_names ( fdec) ,
2584
2566
)
@@ -2600,14 +2582,14 @@ impl<'a> LoweringContext<'a> {
2600
2582
fn lower_method_sig (
2601
2583
& mut self ,
2602
2584
sig : & MethodSig ,
2603
- fn_def_id : Option < DefId > ,
2585
+ fn_def_id : DefId ,
2604
2586
impl_trait_return_allow : bool ,
2605
2587
) -> hir:: MethodSig {
2606
2588
hir:: MethodSig {
2607
2589
abi : sig. abi ,
2608
2590
unsafety : self . lower_unsafety ( sig. unsafety ) ,
2609
2591
constness : self . lower_constness ( sig. constness ) ,
2610
- decl : self . lower_fn_decl ( & sig. decl , fn_def_id, impl_trait_return_allow) ,
2592
+ decl : self . lower_fn_decl ( & sig. decl , Some ( fn_def_id) , impl_trait_return_allow) ,
2611
2593
}
2612
2594
}
2613
2595
0 commit comments