@@ -31,6 +31,41 @@ 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
+ 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
+
34
69
let hir_id = self . lower_node_id ( e. id ) ;
35
70
self . lower_attrs ( hir_id, & e. attrs ) ;
36
71
@@ -51,7 +86,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
51
86
if e. attrs . get ( 0 ) . map_or ( false , |a| a. has_name ( sym:: rustc_box) ) {
52
87
if let [ inner] = & args[ ..] && e. attrs . len ( ) == 1 {
53
88
let kind = hir:: ExprKind :: Box ( self . lower_expr ( & inner) ) ;
54
- let hir_id = self . lower_node_id ( e. id ) ;
55
89
return hir:: Expr { hir_id, kind, span : self . lower_span ( e. span ) } ;
56
90
} else {
57
91
self . tcx . sess . emit_err ( RustcBoxAttributeError { span : e. span } ) ;
@@ -283,34 +317,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
283
317
ExprKind :: Yield ( opt_expr) => self . lower_expr_yield ( e. span , opt_expr. as_deref ( ) ) ,
284
318
ExprKind :: Err => hir:: ExprKind :: Err ,
285
319
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
- }
308
320
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
+
314
323
ExprKind :: MacCall ( _) => panic ! ( "{:?} shouldn't exist here" , e. span) ,
315
324
} ;
316
325
0 commit comments