@@ -26,61 +26,81 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
26
26
hir:: Block { hir_id, stmts, expr, rules, span : self . lower_span ( b. span ) , targeted_by_break }
27
27
}
28
28
29
- fn lower_stmts (
29
+ fn lower_one_stmt (
30
30
& mut self ,
31
- mut ast_stmts : & [ Stmt ] ,
32
- ) -> ( & ' hir [ hir:: Stmt < ' hir > ] , Option < & ' hir hir:: Expr < ' hir > > ) {
33
- let mut stmts = SmallVec :: < [ hir:: Stmt < ' hir > ; 8 ] > :: new ( ) ;
34
- let mut expr = None ;
35
- while let [ s, tail @ ..] = ast_stmts {
36
- match & s. kind {
37
- StmtKind :: Let ( local) => {
38
- let hir_id = self . lower_node_id ( s. id ) ;
39
- let local = self . lower_local ( local) ;
40
- self . alias_attrs ( hir_id, local. hir_id ) ;
41
- let kind = hir:: StmtKind :: Let ( local) ;
42
- let span = self . lower_span ( s. span ) ;
43
- stmts. push ( hir:: Stmt { hir_id, kind, span } ) ;
44
- }
45
- StmtKind :: Item ( it) => {
46
- stmts. extend ( self . lower_item_ref ( it) . into_iter ( ) . enumerate ( ) . map (
47
- |( i, item_id) | {
48
- let hir_id = match i {
49
- 0 => self . lower_node_id ( s. id ) ,
50
- _ => self . next_id ( ) ,
51
- } ;
52
- let kind = hir:: StmtKind :: Item ( item_id) ;
53
- let span = self . lower_span ( s. span ) ;
54
- hir:: Stmt { hir_id, kind, span }
55
- } ,
56
- ) ) ;
57
- }
58
- StmtKind :: Expr ( e) => {
59
- let e = self . lower_expr ( e) ;
60
- if tail. is_empty ( ) {
61
- expr = Some ( e) ;
62
- } else {
63
- let hir_id = self . lower_node_id ( s. id ) ;
64
- self . alias_attrs ( hir_id, e. hir_id ) ;
65
- let kind = hir:: StmtKind :: Expr ( e) ;
31
+ s : & Stmt ,
32
+ at_tail : Option < & mut Option < & ' hir hir:: Expr < ' hir > > > ,
33
+ hir_stmts : & mut impl Extend < hir:: Stmt < ' hir > > ,
34
+ ) {
35
+ match & s. kind {
36
+ StmtKind :: Let ( local) => {
37
+ let hir_id = self . lower_node_id ( s. id ) ;
38
+ let local = self . lower_local ( local) ;
39
+ self . alias_attrs ( hir_id, local. hir_id ) ;
40
+ let kind = hir:: StmtKind :: Let ( local) ;
41
+ let span = self . lower_span ( s. span ) ;
42
+ hir_stmts. extend ( [ hir:: Stmt { hir_id, kind, span } ] ) ;
43
+ }
44
+ StmtKind :: Item ( it) => {
45
+ hir_stmts. extend ( self . lower_item_ref ( it) . into_iter ( ) . enumerate ( ) . map (
46
+ |( i, item_id) | {
47
+ let hir_id = match i {
48
+ 0 => self . lower_node_id ( s. id ) ,
49
+ _ => self . next_id ( ) ,
50
+ } ;
51
+ let kind = hir:: StmtKind :: Item ( item_id) ;
66
52
let span = self . lower_span ( s. span ) ;
67
- stmts. push ( hir:: Stmt { hir_id, kind, span } ) ;
68
- }
69
- }
70
- StmtKind :: Semi ( e) => {
53
+ hir:: Stmt { hir_id, kind, span }
54
+ } ,
55
+ ) ) ;
56
+ }
57
+ StmtKind :: Expr ( e) => {
58
+ if let Some ( expr) = at_tail {
59
+ let e = if self . is_in_init_tail {
60
+ let hir_id = self . lower_node_id ( s. id ) ;
61
+ let kind = self . lower_implicit_init_tail ( e) ;
62
+ self . arena . alloc ( hir:: Expr { hir_id, kind, span : s. span } )
63
+ } else {
64
+ self . lower_expr ( e)
65
+ } ;
66
+ * expr = Some ( e) ;
67
+ } else {
71
68
let e = self . lower_expr ( e) ;
72
69
let hir_id = self . lower_node_id ( s. id ) ;
73
70
self . alias_attrs ( hir_id, e. hir_id ) ;
74
- let kind = hir:: StmtKind :: Semi ( e) ;
71
+ let kind = hir:: StmtKind :: Expr ( e) ;
75
72
let span = self . lower_span ( s. span ) ;
76
- stmts . push ( hir:: Stmt { hir_id, kind, span } ) ;
73
+ hir_stmts . extend ( [ hir:: Stmt { hir_id, kind, span } ] ) ;
77
74
}
78
- StmtKind :: Empty => { }
79
- StmtKind :: MacCall ( ..) => panic ! ( "shouldn't exist here" ) ,
80
75
}
81
- ast_stmts = tail;
76
+ StmtKind :: Semi ( e) => {
77
+ let e = self . lower_expr ( e) ;
78
+ let hir_id = self . lower_node_id ( s. id ) ;
79
+ self . alias_attrs ( hir_id, e. hir_id ) ;
80
+ let kind = hir:: StmtKind :: Semi ( e) ;
81
+ let span = self . lower_span ( s. span ) ;
82
+ hir_stmts. extend ( [ hir:: Stmt { hir_id, kind, span } ] ) ;
83
+ }
84
+ StmtKind :: Empty => { }
85
+ StmtKind :: MacCall ( ..) => panic ! ( "shouldn't exist here" ) ,
86
+ }
87
+ }
88
+
89
+ fn lower_stmts (
90
+ & mut self ,
91
+ ast_stmts : & [ Stmt ] ,
92
+ ) -> ( & ' hir [ hir:: Stmt < ' hir > ] , Option < & ' hir hir:: Expr < ' hir > > ) {
93
+ let mut hir_stmts = SmallVec :: < [ hir:: Stmt < ' hir > ; 8 ] > :: new ( ) ;
94
+ let mut expr = None ;
95
+ if let [ stmts @ .., tail] = ast_stmts {
96
+ self . enter_non_init_tail_lowering ( |this| {
97
+ for s in stmts {
98
+ this. lower_one_stmt ( s, None , & mut hir_stmts) ;
99
+ }
100
+ } ) ;
101
+ self . lower_one_stmt ( tail, Some ( & mut expr) , & mut hir_stmts) ;
82
102
}
83
- ( self . arena . alloc_from_iter ( stmts ) , expr)
103
+ ( self . arena . alloc_from_iter ( hir_stmts ) , expr)
84
104
}
85
105
86
106
/// Return an `ImplTraitContext` that allows impl trait in bindings if
0 commit comments