@@ -114,7 +114,7 @@ struct LoweringContext<'a, 'hir: 'a> {
114114
115115 generator_kind : Option < hir:: GeneratorKind > ,
116116
117- attrs : hir:: HirIdVec < & ' hir [ Attribute ] > ,
117+ attrs : BTreeMap < hir:: HirId , & ' hir [ Attribute ] > ,
118118
119119 /// When inside an `async` context, this is the `HirId` of the
120120 /// `task_context` local bound to the resume argument of the generator.
@@ -311,7 +311,7 @@ pub fn lower_crate<'a, 'hir>(
311311 bodies : BTreeMap :: new ( ) ,
312312 trait_impls : BTreeMap :: new ( ) ,
313313 modules : BTreeMap :: new ( ) ,
314- attrs : hir :: HirIdVec :: default ( ) ,
314+ attrs : BTreeMap :: default ( ) ,
315315 exported_macros : Vec :: new ( ) ,
316316 non_exported_macro_attrs : Vec :: new ( ) ,
317317 catch_scopes : Vec :: new ( ) ,
@@ -595,8 +595,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
595595
596596 self . resolver . definitions ( ) . init_def_id_to_hir_id_mapping ( def_id_to_hir_id) ;
597597
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+ }
600605
601606 hir:: Crate {
602607 item : hir:: CrateItem { module, span : c. span } ,
@@ -973,10 +978,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
973978 ret
974979 }
975980
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+ }
980990 }
981991
982992 fn lower_attr ( & self , attr : & Attribute ) -> Attribute {
@@ -999,6 +1009,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
9991009 Attribute { kind, id : attr. id , style : attr. style , span : attr. span }
10001010 }
10011011
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+
10021019 fn lower_mac_args ( & self , args : & MacArgs ) -> MacArgs {
10031020 match * args {
10041021 MacArgs :: Empty => MacArgs :: Empty ,
@@ -2447,7 +2464,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24472464 } )
24482465 . collect ( ) ;
24492466 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 ) ;
24512468 ids. push ( {
24522469 hir:: Stmt {
24532470 hir_id,
@@ -2476,13 +2493,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24762493 StmtKind :: Expr ( ref e) => {
24772494 let e = self . lower_expr ( e) ;
24782495 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 ) ;
24802497 ( hir_id, hir:: StmtKind :: Expr ( e) )
24812498 }
24822499 StmtKind :: Semi ( ref e) => {
24832500 let e = self . lower_expr ( e) ;
24842501 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 ) ;
24862503 ( hir_id, hir:: StmtKind :: Semi ( e) )
24872504 }
24882505 StmtKind :: Empty => return smallvec ! [ ] ,
@@ -2532,14 +2549,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25322549
25332550 fn stmt_let_pat (
25342551 & mut self ,
2535- attrs : & ' hir [ Attribute ] ,
2552+ attrs : Option < & ' hir [ Attribute ] > ,
25362553 span : Span ,
25372554 init : Option < & ' hir hir:: Expr < ' hir > > ,
25382555 pat : & ' hir hir:: Pat < ' hir > ,
25392556 source : hir:: LocalSource ,
25402557 ) -> hir:: Stmt < ' hir > {
25412558 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+ }
25432563 let local = hir:: Local { hir_id, init, pat, source, span, ty : None } ;
25442564 self . stmt ( span, hir:: StmtKind :: Local ( self . arena . alloc ( local) ) )
25452565 }
0 commit comments