@@ -64,7 +64,7 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
64
64
use rustc_span:: { Span , DUMMY_SP } ;
65
65
use rustc_target:: spec:: abi:: Abi ;
66
66
67
- use smallvec:: { smallvec , SmallVec } ;
67
+ use smallvec:: SmallVec ;
68
68
use std:: collections:: BTreeMap ;
69
69
use std:: mem;
70
70
use tracing:: { debug, trace} ;
@@ -1787,22 +1787,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1787
1787
)
1788
1788
}
1789
1789
1790
- fn lower_local ( & mut self , l : & Local ) -> hir:: Local < ' hir > {
1790
+ fn lower_local ( & mut self , l : & Local ) -> & ' hir hir:: Local < ' hir > {
1791
1791
let ty = l
1792
1792
. ty
1793
1793
. as_ref ( )
1794
1794
. map ( |t| self . lower_ty ( t, ImplTraitContext :: Disallowed ( ImplTraitPosition :: Binding ) ) ) ;
1795
1795
let init = l. init . as_ref ( ) . map ( |e| self . lower_expr ( e) ) ;
1796
1796
let hir_id = self . lower_node_id ( l. id ) ;
1797
1797
self . lower_attrs ( hir_id, & l. attrs ) ;
1798
- hir:: Local {
1798
+ self . arena . alloc ( hir:: Local {
1799
1799
hir_id,
1800
1800
ty,
1801
1801
pat : self . lower_pat ( & l. pat ) ,
1802
1802
init,
1803
1803
span : l. span ,
1804
1804
source : hir:: LocalSource :: Normal ,
1805
- }
1805
+ } )
1806
1806
}
1807
1807
1808
1808
fn lower_fn_params_to_names ( & mut self , decl : & FnDecl ) -> & ' hir [ Ident ] {
@@ -2388,15 +2388,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2388
2388
}
2389
2389
2390
2390
fn lower_block_noalloc ( & mut self , b : & Block , targeted_by_break : bool ) -> hir:: Block < ' hir > {
2391
- let ( stmts, expr) = match & * b. stmts {
2392
- [ stmts @ .., Stmt { kind : StmtKind :: Expr ( e) , .. } ] => ( stmts, Some ( & * e) ) ,
2393
- stmts => ( stmts, None ) ,
2394
- } ;
2395
- let stmts = self . arena . alloc_from_iter ( stmts. iter ( ) . flat_map ( |stmt| self . lower_stmt ( stmt) ) ) ;
2396
- let expr = expr. map ( |e| self . lower_expr ( e) ) ;
2391
+ let ( stmts, expr) = self . lower_stmts ( & b. stmts ) ;
2397
2392
let rules = self . lower_block_check_mode ( & b. rules ) ;
2398
2393
let hir_id = self . lower_node_id ( b. id ) ;
2399
-
2400
2394
hir:: Block { hir_id, stmts, expr, rules, span : b. span , targeted_by_break }
2401
2395
}
2402
2396
@@ -2414,50 +2408,56 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2414
2408
} )
2415
2409
}
2416
2410
2417
- fn lower_stmt ( & mut self , s : & Stmt ) -> SmallVec < [ hir:: Stmt < ' hir > ; 1 ] > {
2418
- let ( hir_id, kind) = match s. kind {
2419
- StmtKind :: Local ( ref l) => {
2420
- let l = self . lower_local ( l) ;
2421
- let hir_id = self . lower_node_id ( s. id ) ;
2422
- self . alias_attrs ( hir_id, l. hir_id ) ;
2423
- return smallvec ! [ hir:: Stmt {
2424
- hir_id,
2425
- kind: hir:: StmtKind :: Local ( self . arena. alloc( l) ) ,
2426
- span: s. span,
2427
- } ] ;
2428
- }
2429
- StmtKind :: Item ( ref it) => {
2430
- // Can only use the ID once.
2431
- let mut id = Some ( s. id ) ;
2432
- return self
2433
- . lower_item_id ( it)
2434
- . into_iter ( )
2435
- . map ( |item_id| {
2436
- let hir_id = id
2437
- . take ( )
2438
- . map ( |id| self . lower_node_id ( id) )
2439
- . unwrap_or_else ( || self . next_id ( ) ) ;
2440
-
2441
- hir:: Stmt { hir_id, kind : hir:: StmtKind :: Item ( item_id) , span : s. span }
2442
- } )
2443
- . collect ( ) ;
2444
- }
2445
- StmtKind :: Expr ( ref e) => {
2446
- let e = self . lower_expr ( e) ;
2447
- let hir_id = self . lower_node_id ( s. id ) ;
2448
- self . alias_attrs ( hir_id, e. hir_id ) ;
2449
- ( hir_id, hir:: StmtKind :: Expr ( e) )
2450
- }
2451
- StmtKind :: Semi ( ref e) => {
2452
- let e = self . lower_expr ( e) ;
2453
- let hir_id = self . lower_node_id ( s. id ) ;
2454
- self . alias_attrs ( hir_id, e. hir_id ) ;
2455
- ( hir_id, hir:: StmtKind :: Semi ( e) )
2411
+ fn lower_stmts (
2412
+ & mut self ,
2413
+ ast_stmts : & [ Stmt ] ,
2414
+ ) -> ( & ' hir [ hir:: Stmt < ' hir > ] , Option < & ' hir hir:: Expr < ' hir > > ) {
2415
+ let mut stmts = SmallVec :: < [ hir:: Stmt < ' hir > ; 8 ] > :: new ( ) ;
2416
+ let mut expr = None ;
2417
+ for ( i, s) in ast_stmts. iter ( ) . enumerate ( ) {
2418
+ match s. kind {
2419
+ StmtKind :: Local ( ref l) => {
2420
+ let l = self . lower_local ( l) ;
2421
+ let hir_id = self . lower_node_id ( s. id ) ;
2422
+ self . alias_attrs ( hir_id, l. hir_id ) ;
2423
+ let kind = hir:: StmtKind :: Local ( l) ;
2424
+ stmts. push ( hir:: Stmt { hir_id, kind, span : s. span } ) ;
2425
+ }
2426
+ StmtKind :: Item ( ref it) => {
2427
+ stmts. extend ( self . lower_item_id ( it) . into_iter ( ) . enumerate ( ) . map (
2428
+ |( i, item_id) | {
2429
+ let hir_id = match i {
2430
+ 0 => self . lower_node_id ( s. id ) ,
2431
+ _ => self . next_id ( ) ,
2432
+ } ;
2433
+
2434
+ hir:: Stmt { hir_id, kind : hir:: StmtKind :: Item ( item_id) , span : s. span }
2435
+ } ,
2436
+ ) ) ;
2437
+ }
2438
+ StmtKind :: Expr ( ref e) => {
2439
+ let e = self . lower_expr ( e) ;
2440
+ if i == ast_stmts. len ( ) - 1 {
2441
+ expr = Some ( e) ;
2442
+ } else {
2443
+ let hir_id = self . lower_node_id ( s. id ) ;
2444
+ self . alias_attrs ( hir_id, e. hir_id ) ;
2445
+ let kind = hir:: StmtKind :: Expr ( e) ;
2446
+ stmts. push ( hir:: Stmt { hir_id, kind, span : s. span } ) ;
2447
+ }
2448
+ }
2449
+ StmtKind :: Semi ( ref e) => {
2450
+ let e = self . lower_expr ( e) ;
2451
+ let hir_id = self . lower_node_id ( s. id ) ;
2452
+ self . alias_attrs ( hir_id, e. hir_id ) ;
2453
+ let kind = hir:: StmtKind :: Semi ( e) ;
2454
+ stmts. push ( hir:: Stmt { hir_id, kind, span : s. span } ) ;
2455
+ }
2456
+ StmtKind :: Empty => { }
2457
+ StmtKind :: MacCall ( ..) => panic ! ( "shouldn't exist here" ) ,
2456
2458
}
2457
- StmtKind :: Empty => return smallvec ! [ ] ,
2458
- StmtKind :: MacCall ( ..) => panic ! ( "shouldn't exist here" ) ,
2459
- } ;
2460
- smallvec ! [ hir:: Stmt { hir_id, kind, span: s. span } ]
2459
+ }
2460
+ ( self . arena . alloc_from_iter ( stmts) , expr)
2461
2461
}
2462
2462
2463
2463
fn lower_block_check_mode ( & mut self , b : & BlockCheckMode ) -> hir:: BlockCheckMode {
0 commit comments