@@ -31,6 +31,41 @@ impl<'hir> LoweringContext<'_, 'hir> {
3131
3232 pub ( super ) fn lower_expr_mut ( & mut self , e : & Expr ) -> hir:: Expr < ' hir > {
3333 ensure_sufficient_stack ( || {
34+ match & e. kind {
35+ // Paranthesis expression does not have a HirId and is handled specially.
36+ ExprKind :: Paren ( ex) => {
37+ let mut ex = self . lower_expr_mut ( ex) ;
38+ // Include parens in span, but only if it is a super-span.
39+ if e. span . contains ( ex. span ) {
40+ ex. span = self . lower_span ( e. span ) ;
41+ }
42+ // Merge attributes into the inner expression.
43+ if !e. attrs . is_empty ( ) {
44+ let old_attrs =
45+ self . attrs . get ( & ex. hir_id . local_id ) . map ( |la| * la) . unwrap_or ( & [ ] ) ;
46+ self . attrs . insert (
47+ ex. hir_id . local_id ,
48+ & * self . arena . alloc_from_iter (
49+ e. attrs
50+ . iter ( )
51+ . map ( |a| self . lower_attr ( a) )
52+ . chain ( old_attrs. iter ( ) . cloned ( ) ) ,
53+ ) ,
54+ ) ;
55+ }
56+ return ex;
57+ }
58+ // Desugar `ExprForLoop`
59+ // from: `[opt_ident]: for <pat> in <head> <body>`
60+ //
61+ // This also needs special handling because the HirId of the returned `hir::Expr` will not
62+ // correspond to the `e.id`, so `lower_expr_for` handles attribute lowering itself.
63+ ExprKind :: ForLoop ( pat, head, body, opt_label) => {
64+ return self . lower_expr_for ( e, pat, head, body, * opt_label) ;
65+ }
66+ _ => ( ) ,
67+ }
68+
3469 let hir_id = self . lower_node_id ( e. id ) ;
3570 self . lower_attrs ( hir_id, & e. attrs ) ;
3671
@@ -51,7 +86,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
5186 if e. attrs . get ( 0 ) . map_or ( false , |a| a. has_name ( sym:: rustc_box) ) {
5287 if let [ inner] = & args[ ..] && e. attrs . len ( ) == 1 {
5388 let kind = hir:: ExprKind :: Box ( self . lower_expr ( & inner) ) ;
54- let hir_id = self . lower_node_id ( e. id ) ;
5589 return hir:: Expr { hir_id, kind, span : self . lower_span ( e. span ) } ;
5690 } else {
5791 self . tcx . sess . emit_err ( RustcBoxAttributeError { span : e. span } ) ;
@@ -283,34 +317,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
283317 ExprKind :: Yield ( opt_expr) => self . lower_expr_yield ( e. span , opt_expr. as_deref ( ) ) ,
284318 ExprKind :: Err => hir:: ExprKind :: Err ,
285319 ExprKind :: Try ( sub_expr) => self . lower_expr_try ( e. span , sub_expr) ,
286- ExprKind :: Paren ( ex) => {
287- let mut ex = self . lower_expr_mut ( ex) ;
288- // Include parens in span, but only if it is a super-span.
289- if e. span . contains ( ex. span ) {
290- ex. span = self . lower_span ( e. span ) ;
291- }
292- // Merge attributes into the inner expression.
293- if !e. attrs . is_empty ( ) {
294- let old_attrs =
295- self . attrs . get ( & ex. hir_id . local_id ) . map ( |la| * la) . unwrap_or ( & [ ] ) ;
296- self . attrs . insert (
297- ex. hir_id . local_id ,
298- & * self . arena . alloc_from_iter (
299- e. attrs
300- . iter ( )
301- . map ( |a| self . lower_attr ( a) )
302- . chain ( old_attrs. iter ( ) . cloned ( ) ) ,
303- ) ,
304- ) ;
305- }
306- return ex;
307- }
308320
309- // Desugar `ExprForLoop`
310- // from: `[opt_ident]: for <pat> in <head> <body>`
311- ExprKind :: ForLoop ( pat, head, body, opt_label) => {
312- return self . lower_expr_for ( e, pat, head, body, * opt_label) ;
313- }
321+ ExprKind :: Paren ( _) | ExprKind :: ForLoop ( ..) => unreachable ! ( "already handled" ) ,
322+
314323 ExprKind :: MacCall ( _) => panic ! ( "{:?} shouldn't exist here" , e. span) ,
315324 } ;
316325
0 commit comments