@@ -64,7 +64,7 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
6464use rustc_span:: { Span , DUMMY_SP } ;
6565use rustc_target:: spec:: abi:: Abi ;
6666
67- use smallvec:: { smallvec , SmallVec } ;
67+ use smallvec:: SmallVec ;
6868use std:: collections:: BTreeMap ;
6969use std:: mem;
7070use tracing:: { debug, trace} ;
@@ -1787,22 +1787,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17871787 )
17881788 }
17891789
1790- fn lower_local ( & mut self , l : & Local ) -> hir:: Local < ' hir > {
1790+ fn lower_local ( & mut self , l : & Local ) -> & ' hir hir:: Local < ' hir > {
17911791 let ty = l
17921792 . ty
17931793 . as_ref ( )
17941794 . map ( |t| self . lower_ty ( t, ImplTraitContext :: Disallowed ( ImplTraitPosition :: Binding ) ) ) ;
17951795 let init = l. init . as_ref ( ) . map ( |e| self . lower_expr ( e) ) ;
17961796 let hir_id = self . lower_node_id ( l. id ) ;
17971797 self . lower_attrs ( hir_id, & l. attrs ) ;
1798- hir:: Local {
1798+ self . arena . alloc ( hir:: Local {
17991799 hir_id,
18001800 ty,
18011801 pat : self . lower_pat ( & l. pat ) ,
18021802 init,
18031803 span : l. span ,
18041804 source : hir:: LocalSource :: Normal ,
1805- }
1805+ } )
18061806 }
18071807
18081808 fn lower_fn_params_to_names ( & mut self , decl : & FnDecl ) -> & ' hir [ Ident ] {
@@ -2388,15 +2388,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23882388 }
23892389
23902390 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 ) ;
23972392 let rules = self . lower_block_check_mode ( & b. rules ) ;
23982393 let hir_id = self . lower_node_id ( b. id ) ;
2399-
24002394 hir:: Block { hir_id, stmts, expr, rules, span : b. span , targeted_by_break }
24012395 }
24022396
@@ -2414,50 +2408,56 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24142408 } )
24152409 }
24162410
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" ) ,
24562458 }
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)
24612461 }
24622462
24632463 fn lower_block_check_mode ( & mut self , b : & BlockCheckMode ) -> hir:: BlockCheckMode {
0 commit comments