@@ -114,7 +114,7 @@ struct LoweringContext<'a, 'hir: 'a> {
114
114
115
115
generator_kind : Option < hir:: GeneratorKind > ,
116
116
117
- attrs : hir:: HirIdVec < & ' hir [ Attribute ] > ,
117
+ attrs : BTreeMap < hir:: HirId , & ' hir [ Attribute ] > ,
118
118
119
119
/// When inside an `async` context, this is the `HirId` of the
120
120
/// `task_context` local bound to the resume argument of the generator.
@@ -311,7 +311,7 @@ pub fn lower_crate<'a, 'hir>(
311
311
bodies : BTreeMap :: new ( ) ,
312
312
trait_impls : BTreeMap :: new ( ) ,
313
313
modules : BTreeMap :: new ( ) ,
314
- attrs : hir :: HirIdVec :: default ( ) ,
314
+ attrs : BTreeMap :: default ( ) ,
315
315
exported_macros : Vec :: new ( ) ,
316
316
non_exported_macro_attrs : Vec :: new ( ) ,
317
317
catch_scopes : Vec :: new ( ) ,
@@ -595,8 +595,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
595
595
596
596
self . resolver . definitions ( ) . init_def_id_to_hir_id_mapping ( def_id_to_hir_id) ;
597
597
598
- // Not all HIR owners have declared attrs. Complete with empty IndexVecs.
599
- self . attrs . push_owner ( Idx :: new ( self . resolver . definitions ( ) . def_index_count ( ) - 1 ) ) ;
598
+ #[ cfg( debug_assertions) ]
599
+ for ( & id, attrs) in self . attrs . iter ( ) {
600
+ // Verify that we do not store empty slices in the map.
601
+ if attrs. is_empty ( ) {
602
+ panic ! ( "Stored empty attributes for {:?}" , id) ;
603
+ }
604
+ }
600
605
601
606
hir:: Crate {
602
607
item : hir:: CrateItem { module, span : c. span } ,
@@ -973,10 +978,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
973
978
ret
974
979
}
975
980
976
- fn lower_attrs ( & mut self , id : hir:: HirId , attrs : & [ Attribute ] ) -> & ' hir [ Attribute ] {
977
- let ret = self . arena . alloc_from_iter ( attrs. iter ( ) . map ( |a| self . lower_attr ( a) ) ) ;
978
- self . attrs . push_sparse ( id, ret) ;
979
- ret
981
+ fn lower_attrs ( & mut self , id : hir:: HirId , attrs : & [ Attribute ] ) -> Option < & ' hir [ Attribute ] > {
982
+ if attrs. is_empty ( ) {
983
+ None
984
+ } else {
985
+ let ret = self . arena . alloc_from_iter ( attrs. iter ( ) . map ( |a| self . lower_attr ( a) ) ) ;
986
+ debug_assert ! ( !ret. is_empty( ) ) ;
987
+ self . attrs . insert ( id, ret) ;
988
+ Some ( ret)
989
+ }
980
990
}
981
991
982
992
fn lower_attr ( & self , attr : & Attribute ) -> Attribute {
@@ -999,6 +1009,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
999
1009
Attribute { kind, id : attr. id , style : attr. style , span : attr. span }
1000
1010
}
1001
1011
1012
+ fn alias_attrs ( & mut self , id : hir:: HirId , target_id : hir:: HirId ) {
1013
+ if let Some ( & a) = self . attrs . get ( & target_id) {
1014
+ debug_assert ! ( !a. is_empty( ) ) ;
1015
+ self . attrs . insert ( id, a) ;
1016
+ }
1017
+ }
1018
+
1002
1019
fn lower_mac_args ( & self , args : & MacArgs ) -> MacArgs {
1003
1020
match * args {
1004
1021
MacArgs :: Empty => MacArgs :: Empty ,
@@ -2447,7 +2464,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2447
2464
} )
2448
2465
. collect ( ) ;
2449
2466
let hir_id = self . lower_node_id ( s. id ) ;
2450
- self . attrs . push_sparse ( hir_id, self . attrs [ l. hir_id ] ) ;
2467
+ self . alias_attrs ( hir_id, l. hir_id ) ;
2451
2468
ids. push ( {
2452
2469
hir:: Stmt {
2453
2470
hir_id,
@@ -2476,13 +2493,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2476
2493
StmtKind :: Expr ( ref e) => {
2477
2494
let e = self . lower_expr ( e) ;
2478
2495
let hir_id = self . lower_node_id ( s. id ) ;
2479
- self . attrs . push_sparse ( hir_id, self . attrs [ e. hir_id ] ) ;
2496
+ self . alias_attrs ( hir_id, e. hir_id ) ;
2480
2497
( hir_id, hir:: StmtKind :: Expr ( e) )
2481
2498
}
2482
2499
StmtKind :: Semi ( ref e) => {
2483
2500
let e = self . lower_expr ( e) ;
2484
2501
let hir_id = self . lower_node_id ( s. id ) ;
2485
- self . attrs . push_sparse ( hir_id, self . attrs [ e. hir_id ] ) ;
2502
+ self . alias_attrs ( hir_id, e. hir_id ) ;
2486
2503
( hir_id, hir:: StmtKind :: Semi ( e) )
2487
2504
}
2488
2505
StmtKind :: Empty => return smallvec ! [ ] ,
@@ -2532,14 +2549,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2532
2549
2533
2550
fn stmt_let_pat (
2534
2551
& mut self ,
2535
- attrs : & ' hir [ Attribute ] ,
2552
+ attrs : Option < & ' hir [ Attribute ] > ,
2536
2553
span : Span ,
2537
2554
init : Option < & ' hir hir:: Expr < ' hir > > ,
2538
2555
pat : & ' hir hir:: Pat < ' hir > ,
2539
2556
source : hir:: LocalSource ,
2540
2557
) -> hir:: Stmt < ' hir > {
2541
2558
let hir_id = self . next_id ( ) ;
2542
- self . attrs . push_sparse ( hir_id, attrs) ;
2559
+ if let Some ( a) = attrs {
2560
+ debug_assert ! ( !a. is_empty( ) ) ;
2561
+ self . attrs . insert ( hir_id, a) ;
2562
+ }
2543
2563
let local = hir:: Local { hir_id, init, pat, source, span, ty : None } ;
2544
2564
self . stmt ( span, hir:: StmtKind :: Local ( self . arena . alloc ( local) ) )
2545
2565
}
0 commit comments