@@ -258,9 +258,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
258
258
ex. span = e. span ;
259
259
}
260
260
// Merge attributes into the inner expression.
261
- let mut attrs: Vec < _ > = e. attrs . iter ( ) . map ( |a| self . lower_attr ( a) ) . collect ( ) ;
262
- attrs. extend :: < Vec < _ > > ( ex. attrs . into ( ) ) ;
263
- ex. attrs = attrs. into ( ) ;
261
+ if !e. attrs . is_empty ( ) {
262
+ let old_attrs = self . attrs . get ( & ex. hir_id ) . map ( |la| * la) . unwrap_or ( & [ ] ) ;
263
+ self . attrs . insert (
264
+ ex. hir_id ,
265
+ & * self . arena . alloc_from_iter (
266
+ e. attrs
267
+ . iter ( )
268
+ . map ( |a| self . lower_attr ( a) )
269
+ . chain ( old_attrs. iter ( ) . cloned ( ) ) ,
270
+ ) ,
271
+ ) ;
272
+ }
264
273
return ex;
265
274
}
266
275
@@ -272,12 +281,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
272
281
ExprKind :: MacCall ( _) => panic ! ( "{:?} shouldn't exist here" , e. span) ,
273
282
} ;
274
283
275
- hir:: Expr {
276
- hir_id : self . lower_node_id ( e. id ) ,
277
- kind,
278
- span : e. span ,
279
- attrs : e. attrs . iter ( ) . map ( |a| self . lower_attr ( a) ) . collect :: < Vec < _ > > ( ) . into ( ) ,
280
- }
284
+ let hir_id = self . lower_node_id ( e. id ) ;
285
+ self . lower_attrs ( hir_id, & e. attrs ) ;
286
+ hir:: Expr { hir_id, kind, span : e. span }
281
287
} )
282
288
}
283
289
@@ -618,14 +624,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
618
624
hir:: Guard :: If ( self . lower_expr ( cond) )
619
625
}
620
626
} ) ;
621
- hir:: Arm {
622
- hir_id : self . next_id ( ) ,
623
- attrs : self . lower_attrs ( & arm. attrs ) ,
624
- pat,
625
- guard,
626
- body : self . lower_expr ( & arm. body ) ,
627
- span : arm. span ,
628
- }
627
+ let hir_id = self . next_id ( ) ;
628
+ self . lower_attrs ( hir_id, & arm. attrs ) ;
629
+ hir:: Arm { hir_id, pat, guard, body : self . lower_expr ( & arm. body ) , span : arm. span }
629
630
}
630
631
631
632
/// Lower an `async` construct to a generator that is then wrapped so it implements `Future`.
@@ -669,7 +670,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
669
670
Ident :: with_dummy_span ( sym:: _task_context) ,
670
671
hir:: BindingAnnotation :: Mutable ,
671
672
) ;
672
- let param = hir:: Param { attrs : & [ ] , hir_id : self . next_id ( ) , pat, ty_span : span, span } ;
673
+ let param = hir:: Param { hir_id : self . next_id ( ) , pat, ty_span : span, span } ;
673
674
let params = arena_vec ! [ self ; param] ;
674
675
675
676
let body_id = self . lower_body ( move |this| {
@@ -690,12 +691,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
690
691
span,
691
692
Some ( hir:: Movability :: Static ) ,
692
693
) ;
693
- let generator = hir:: Expr {
694
- hir_id : self . lower_node_id ( closure_node_id) ,
695
- kind : generator_kind,
696
- span,
697
- attrs : ThinVec :: new ( ) ,
698
- } ;
694
+ let generator =
695
+ hir:: Expr { hir_id : self . lower_node_id ( closure_node_id) , kind : generator_kind, span } ;
699
696
700
697
// `future::from_generator`:
701
698
let unstable_span =
@@ -849,7 +846,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
849
846
hir_id : loop_hir_id,
850
847
kind : hir:: ExprKind :: Loop ( loop_block, None , hir:: LoopSource :: Loop , span) ,
851
848
span,
852
- attrs : ThinVec :: new ( ) ,
853
849
} ) ;
854
850
855
851
// mut pinned => loop { ... }
@@ -1026,7 +1022,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1026
1022
1027
1023
// Introduce a `let` for destructuring: `let (lhs1, lhs2) = t`.
1028
1024
let destructure_let = self . stmt_let_pat (
1029
- ThinVec :: new ( ) ,
1025
+ None ,
1030
1026
whole_span,
1031
1027
Some ( rhs) ,
1032
1028
pat,
@@ -1785,7 +1781,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1785
1781
1786
1782
// `let mut __next`
1787
1783
let next_let = self . stmt_let_pat (
1788
- ThinVec :: new ( ) ,
1784
+ None ,
1789
1785
desugared_span,
1790
1786
None ,
1791
1787
next_pat,
@@ -1795,7 +1791,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1795
1791
// `let <pat> = __next`
1796
1792
let pat = self . lower_pat ( pat) ;
1797
1793
let pat_let = self . stmt_let_pat (
1798
- ThinVec :: new ( ) ,
1794
+ None ,
1799
1795
desugared_span,
1800
1796
Some ( next_expr) ,
1801
1797
pat,
@@ -1819,12 +1815,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
1819
1815
hir:: LoopSource :: ForLoop ,
1820
1816
e. span . with_hi ( orig_head_span. hi ( ) ) ,
1821
1817
) ;
1822
- let loop_expr = self . arena . alloc ( hir:: Expr {
1823
- hir_id : self . lower_node_id ( e. id ) ,
1824
- kind,
1825
- span : e. span ,
1826
- attrs : ThinVec :: new ( ) ,
1827
- } ) ;
1818
+ let loop_expr =
1819
+ self . arena . alloc ( hir:: Expr { hir_id : self . lower_node_id ( e. id ) , kind, span : e. span } ) ;
1828
1820
1829
1821
// `mut iter => { ... }`
1830
1822
let iter_arm = self . arm ( iter_pat, loop_expr) ;
@@ -2159,21 +2151,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
2159
2151
kind : hir:: ExprKind < ' hir > ,
2160
2152
attrs : AttrVec ,
2161
2153
) -> hir:: Expr < ' hir > {
2162
- hir:: Expr { hir_id : self . next_id ( ) , kind, span, attrs }
2154
+ let hir_id = self . next_id ( ) ;
2155
+ self . lower_attrs ( hir_id, & attrs) ;
2156
+ hir:: Expr { hir_id, kind, span }
2163
2157
}
2164
2158
2165
2159
fn field ( & mut self , ident : Ident , expr : & ' hir hir:: Expr < ' hir > , span : Span ) -> hir:: Field < ' hir > {
2166
2160
hir:: Field { hir_id : self . next_id ( ) , ident, span, expr, is_shorthand : false }
2167
2161
}
2168
2162
2169
2163
fn arm ( & mut self , pat : & ' hir hir:: Pat < ' hir > , expr : & ' hir hir:: Expr < ' hir > ) -> hir:: Arm < ' hir > {
2170
- hir:: Arm {
2171
- hir_id : self . next_id ( ) ,
2172
- attrs : & [ ] ,
2173
- pat,
2174
- guard : None ,
2175
- span : expr. span ,
2176
- body : expr,
2177
- }
2164
+ hir:: Arm { hir_id : self . next_id ( ) , pat, guard : None , span : expr. span , body : expr }
2178
2165
}
2179
2166
}
0 commit comments