@@ -31,6 +31,36 @@ impl<'hir> LoweringContext<'_, 'hir> {
31
31
32
32
pub ( super ) fn lower_expr_mut ( & mut self , e : & Expr ) -> hir:: Expr < ' hir > {
33
33
ensure_sufficient_stack ( || {
34
+ // Desugar `ExprForLoop`
35
+ // from: `[opt_ident]: for <pat> in <head> <body>`
36
+ if let ExprKind :: ForLoop ( ref pat, ref head, ref body, opt_label) = e. kind {
37
+ return self . lower_expr_for ( e, pat, head, body, opt_label) ;
38
+ }
39
+
40
+ if let ExprKind :: Paren ( ref ex) = e. kind {
41
+ let mut ex = self . lower_expr_mut ( ex) ;
42
+ // Include parens in span, but only if it is a super-span.
43
+ if e. span . contains ( ex. span ) {
44
+ ex. span = self . lower_span ( e. span ) ;
45
+ }
46
+ // Merge attributes into the inner expression.
47
+ if !e. attrs . is_empty ( ) {
48
+ let old_attrs =
49
+ self . attrs . get ( & ex. hir_id . local_id ) . map ( |la| * la) . unwrap_or ( & [ ] ) ;
50
+ self . attrs . insert (
51
+ ex. hir_id . local_id ,
52
+ & * self . arena . alloc_from_iter (
53
+ e. attrs
54
+ . iter ( )
55
+ . map ( |a| self . lower_attr ( a) )
56
+ . chain ( old_attrs. iter ( ) . cloned ( ) ) ,
57
+ ) ,
58
+ ) ;
59
+ }
60
+ return ex;
61
+ }
62
+
63
+ let hir_id = self . lower_node_id ( e. id ) ;
34
64
let kind = match e. kind {
35
65
ExprKind :: Box ( ref inner) => hir:: ExprKind :: Box ( self . lower_expr ( inner) ) ,
36
66
ExprKind :: Array ( ref exprs) => hir:: ExprKind :: Array ( self . lower_exprs ( exprs) ) ,
@@ -48,7 +78,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
48
78
if e. attrs . get ( 0 ) . map_or ( false , |a| a. has_name ( sym:: rustc_box) ) {
49
79
if let [ inner] = & args[ ..] && e. attrs . len ( ) == 1 {
50
80
let kind = hir:: ExprKind :: Box ( self . lower_expr ( & inner) ) ;
51
- let hir_id = self . lower_node_id ( e. id ) ;
52
81
return hir:: Expr { hir_id, kind, span : self . lower_span ( e. span ) } ;
53
82
} else {
54
83
self . tcx . sess . emit_err ( RustcBoxAttributeError { span : e. span } ) ;
@@ -281,38 +310,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
281
310
ExprKind :: Yield ( ref opt_expr) => self . lower_expr_yield ( e. span , opt_expr. as_deref ( ) ) ,
282
311
ExprKind :: Err => hir:: ExprKind :: Err ,
283
312
ExprKind :: Try ( ref sub_expr) => self . lower_expr_try ( e. span , sub_expr) ,
284
- ExprKind :: Paren ( ref ex) => {
285
- let mut ex = self . lower_expr_mut ( ex) ;
286
- // Include parens in span, but only if it is a super-span.
287
- if e. span . contains ( ex. span ) {
288
- ex. span = self . lower_span ( e. span ) ;
289
- }
290
- // Merge attributes into the inner expression.
291
- if !e. attrs . is_empty ( ) {
292
- let old_attrs =
293
- self . attrs . get ( & ex. hir_id . local_id ) . map ( |la| * la) . unwrap_or ( & [ ] ) ;
294
- self . attrs . insert (
295
- ex. hir_id . local_id ,
296
- & * self . arena . alloc_from_iter (
297
- e. attrs
298
- . iter ( )
299
- . map ( |a| self . lower_attr ( a) )
300
- . chain ( old_attrs. iter ( ) . cloned ( ) ) ,
301
- ) ,
302
- ) ;
303
- }
304
- return ex;
305
- }
306
-
307
- // Desugar `ExprForLoop`
308
- // from: `[opt_ident]: for <pat> in <head> <body>`
309
- ExprKind :: ForLoop ( ref pat, ref head, ref body, opt_label) => {
310
- return self . lower_expr_for ( e, pat, head, body, opt_label) ;
311
- }
312
- ExprKind :: MacCall ( _) => panic ! ( "{:?} shouldn't exist here" , e. span) ,
313
+ ExprKind :: MacCall ( _) | _ => panic ! ( "{:?} shouldn't exist here" , e. span) ,
313
314
} ;
314
315
315
- let hir_id = self . lower_node_id ( e. id ) ;
316
+ // let hir_id = self.lower_node_id(e.id);
316
317
self . lower_attrs ( hir_id, & e. attrs ) ;
317
318
hir:: Expr { hir_id, kind, span : self . lower_span ( e. span ) }
318
319
} )
@@ -480,8 +481,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
480
481
let lowered_cond = self . with_loop_condition_scope ( |t| t. lower_cond ( cond) ) ;
481
482
let then = self . lower_block_expr ( body) ;
482
483
let expr_break = self . expr_break ( span, AttrVec :: new ( ) ) ;
483
- let stmt_break = self . stmt_expr ( span, expr_break) ;
484
- let else_blk = self . block_all ( span, arena_vec ! [ self ; stmt_break] , None ) ;
485
484
let else_expr = self . arena . alloc ( self . expr_block ( else_blk, AttrVec :: new ( ) ) ) ;
486
485
let if_kind = hir:: ExprKind :: If ( lowered_cond, self . arena . alloc ( then) , Some ( else_expr) ) ;
487
486
let if_expr = self . expr ( span, if_kind, AttrVec :: new ( ) ) ;
@@ -1495,8 +1494,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
1495
1494
// Some(<pat>) => <body>,
1496
1495
let some_arm = {
1497
1496
let some_pat = self . pat_some ( pat_span, pat) ;
1498
- let body_block = self . with_loop_scope ( e. id , |this| this. lower_block ( body, false ) ) ;
1499
- let body_expr = self . arena . alloc ( self . expr_block ( body_block, AttrVec :: new ( ) ) ) ;
1497
+ //let body_block = || self.with_loop_scope(e.id, |this| this.lower_block(body, false));
1498
+ let body_expr = self . arena . alloc ( self . expr_block (
1499
+ |this| this. with_loop_scope ( e. id , |this| this. lower_block ( body, false ) ) ,
1500
+ AttrVec :: new ( ) ,
1501
+ ) ) ;
1500
1502
self . arm ( some_pat, body_expr)
1501
1503
} ;
1502
1504
@@ -1881,17 +1883,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
1881
1883
}
1882
1884
1883
1885
fn expr_block_empty ( & mut self , span : Span ) -> & ' hir hir:: Expr < ' hir > {
1884
- let blk = self . block_all ( span, & [ ] , None ) ;
1885
- let expr = self . expr_block ( blk , AttrVec :: new ( ) ) ;
1886
+ // let blk = || self.block_all(span, &[], None);
1887
+ let expr = self . expr_block ( |this| this . block_all ( span , & [ ] , None ) , AttrVec :: new ( ) ) ;
1886
1888
self . arena . alloc ( expr)
1887
1889
}
1888
1890
1889
1891
pub ( super ) fn expr_block (
1890
1892
& mut self ,
1891
- b : & ' hir hir:: Block < ' hir > ,
1893
+ //b: &'hir hir::Block<'hir>,
1894
+ gen_block : impl FnOnce ( & mut Self ) -> & ' hir hir:: Block < ' hir > ,
1892
1895
attrs : AttrVec ,
1893
1896
) -> hir:: Expr < ' hir > {
1894
- self . expr ( b. span , hir:: ExprKind :: Block ( b, None ) , attrs)
1897
+ let hir_id = self . next_id ( ) ;
1898
+ debug ! ( "yuakng expr hir_id: {:?}" , hir_id) ;
1899
+ self . lower_attrs ( hir_id, & attrs) ;
1900
+ let b = gen_block ( self ) ;
1901
+ hir:: Expr { hir_id, kind : hir:: ExprKind :: Block ( b, None ) , span : self . lower_span ( b. span ) }
1902
+ //self.expr(b.span, hir::ExprKind::Block(b, None), attrs)
1895
1903
}
1896
1904
1897
1905
pub ( super ) fn expr (
@@ -1900,7 +1908,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
1900
1908
kind : hir:: ExprKind < ' hir > ,
1901
1909
attrs : AttrVec ,
1902
1910
) -> hir:: Expr < ' hir > {
1911
+ debug ! ( "yukang expr({:?}, {:?})" , span, kind) ;
1903
1912
let hir_id = self . next_id ( ) ;
1913
+ debug ! ( "yuakng expr hir_id: {:?}" , hir_id) ;
1904
1914
self . lower_attrs ( hir_id, & attrs) ;
1905
1915
hir:: Expr { hir_id, kind, span : self . lower_span ( span) }
1906
1916
}
0 commit comments