@@ -31,6 +31,36 @@ impl<'hir> LoweringContext<'_, 'hir> {
3131
3232 pub ( super ) fn lower_expr_mut ( & mut self , e : & Expr ) -> hir:: Expr < ' hir > {
3333 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 ) ;
3464 let kind = match e. kind {
3565 ExprKind :: Box ( ref inner) => hir:: ExprKind :: Box ( self . lower_expr ( inner) ) ,
3666 ExprKind :: Array ( ref exprs) => hir:: ExprKind :: Array ( self . lower_exprs ( exprs) ) ,
@@ -48,7 +78,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
4878 if e. attrs . get ( 0 ) . map_or ( false , |a| a. has_name ( sym:: rustc_box) ) {
4979 if let [ inner] = & args[ ..] && e. attrs . len ( ) == 1 {
5080 let kind = hir:: ExprKind :: Box ( self . lower_expr ( & inner) ) ;
51- let hir_id = self . lower_node_id ( e. id ) ;
5281 return hir:: Expr { hir_id, kind, span : self . lower_span ( e. span ) } ;
5382 } else {
5483 self . tcx . sess . emit_err ( RustcBoxAttributeError { span : e. span } ) ;
@@ -281,38 +310,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
281310 ExprKind :: Yield ( ref opt_expr) => self . lower_expr_yield ( e. span , opt_expr. as_deref ( ) ) ,
282311 ExprKind :: Err => hir:: ExprKind :: Err ,
283312 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) ,
313314 } ;
314315
315- let hir_id = self . lower_node_id ( e. id ) ;
316+ // let hir_id = self.lower_node_id(e.id);
316317 self . lower_attrs ( hir_id, & e. attrs ) ;
317318 hir:: Expr { hir_id, kind, span : self . lower_span ( e. span ) }
318319 } )
@@ -480,8 +481,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
480481 let lowered_cond = self . with_loop_condition_scope ( |t| t. lower_cond ( cond) ) ;
481482 let then = self . lower_block_expr ( body) ;
482483 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 ) ;
485484 let else_expr = self . arena . alloc ( self . expr_block ( else_blk, AttrVec :: new ( ) ) ) ;
486485 let if_kind = hir:: ExprKind :: If ( lowered_cond, self . arena . alloc ( then) , Some ( else_expr) ) ;
487486 let if_expr = self . expr ( span, if_kind, AttrVec :: new ( ) ) ;
@@ -1495,8 +1494,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
14951494 // Some(<pat>) => <body>,
14961495 let some_arm = {
14971496 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+ ) ) ;
15001502 self . arm ( some_pat, body_expr)
15011503 } ;
15021504
@@ -1881,17 +1883,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
18811883 }
18821884
18831885 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 ( ) ) ;
18861888 self . arena . alloc ( expr)
18871889 }
18881890
18891891 pub ( super ) fn expr_block (
18901892 & 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 > ,
18921895 attrs : AttrVec ,
18931896 ) -> 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)
18951903 }
18961904
18971905 pub ( super ) fn expr (
@@ -1900,7 +1908,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
19001908 kind : hir:: ExprKind < ' hir > ,
19011909 attrs : AttrVec ,
19021910 ) -> hir:: Expr < ' hir > {
1911+ debug ! ( "yukang expr({:?}, {:?})" , span, kind) ;
19031912 let hir_id = self . next_id ( ) ;
1913+ debug ! ( "yuakng expr hir_id: {:?}" , hir_id) ;
19041914 self . lower_attrs ( hir_id, & attrs) ;
19051915 hir:: Expr { hir_id, kind, span : self . lower_span ( span) }
19061916 }
0 commit comments