]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast_lowering/src/expr.rs
Auto merge of #98328 - topjohnwu:fix_cross, r=jyn514
[rust.git] / compiler / rustc_ast_lowering / src / expr.rs
1 use super::ResolverAstLoweringExt;
2 use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
3 use crate::{FnDeclKind, ImplTraitPosition};
4
5 use rustc_ast::attr;
6 use rustc_ast::ptr::P as AstP;
7 use rustc_ast::*;
8 use rustc_data_structures::stack::ensure_sufficient_stack;
9 use rustc_data_structures::thin_vec::ThinVec;
10 use rustc_errors::struct_span_err;
11 use rustc_hir as hir;
12 use rustc_hir::def::Res;
13 use rustc_hir::definitions::DefPathData;
14 use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
15 use rustc_span::symbol::{sym, Ident};
16 use rustc_span::DUMMY_SP;
17
18 impl<'hir> LoweringContext<'_, 'hir> {
19     fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> &'hir [hir::Expr<'hir>] {
20         self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
21     }
22
23     pub(super) fn lower_expr(&mut self, e: &Expr) -> &'hir hir::Expr<'hir> {
24         self.arena.alloc(self.lower_expr_mut(e))
25     }
26
27     pub(super) fn lower_expr_mut(&mut self, e: &Expr) -> hir::Expr<'hir> {
28         ensure_sufficient_stack(|| {
29             let kind = match e.kind {
30                 ExprKind::Box(ref inner) => hir::ExprKind::Box(self.lower_expr(inner)),
31                 ExprKind::Array(ref exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
32                 ExprKind::ConstBlock(ref anon_const) => {
33                     let anon_const = self.lower_anon_const(anon_const);
34                     hir::ExprKind::ConstBlock(anon_const)
35                 }
36                 ExprKind::Repeat(ref expr, ref count) => {
37                     let expr = self.lower_expr(expr);
38                     let count = self.lower_array_length(count);
39                     hir::ExprKind::Repeat(expr, count)
40                 }
41                 ExprKind::Tup(ref elts) => hir::ExprKind::Tup(self.lower_exprs(elts)),
42                 ExprKind::Call(ref f, ref args) => {
43                     if e.attrs.get(0).map_or(false, |a| a.has_name(sym::rustc_box)) {
44                         if let [inner] = &args[..] && e.attrs.len() == 1 {
45                             let kind = hir::ExprKind::Box(self.lower_expr(&inner));
46                             let hir_id = self.lower_node_id(e.id);
47                             return hir::Expr { hir_id, kind, span: self.lower_span(e.span) };
48                         } else {
49                             self.tcx.sess
50                                 .struct_span_err(
51                                     e.span,
52                                     "#[rustc_box] requires precisely one argument \
53                                     and no other attributes are allowed",
54                                 )
55                                 .emit();
56                             hir::ExprKind::Err
57                         }
58                     } else if let Some(legacy_args) = self.resolver.legacy_const_generic_args(f) {
59                         self.lower_legacy_const_generics((**f).clone(), args.clone(), &legacy_args)
60                     } else {
61                         let f = self.lower_expr(f);
62                         hir::ExprKind::Call(f, self.lower_exprs(args))
63                     }
64                 }
65                 ExprKind::MethodCall(ref seg, ref args, span) => {
66                     let hir_seg = self.arena.alloc(self.lower_path_segment(
67                         e.span,
68                         seg,
69                         ParamMode::Optional,
70                         ParenthesizedGenericArgs::Err,
71                         ImplTraitContext::Disallowed(ImplTraitPosition::Path),
72                     ));
73                     let args = self.lower_exprs(args);
74                     hir::ExprKind::MethodCall(hir_seg, args, self.lower_span(span))
75                 }
76                 ExprKind::Binary(binop, ref lhs, ref rhs) => {
77                     let binop = self.lower_binop(binop);
78                     let lhs = self.lower_expr(lhs);
79                     let rhs = self.lower_expr(rhs);
80                     hir::ExprKind::Binary(binop, lhs, rhs)
81                 }
82                 ExprKind::Unary(op, ref ohs) => {
83                     let op = self.lower_unop(op);
84                     let ohs = self.lower_expr(ohs);
85                     hir::ExprKind::Unary(op, ohs)
86                 }
87                 ExprKind::Lit(ref l) => {
88                     hir::ExprKind::Lit(respan(self.lower_span(l.span), l.kind.clone()))
89                 }
90                 ExprKind::Cast(ref expr, ref ty) => {
91                     let expr = self.lower_expr(expr);
92                     let ty =
93                         self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
94                     hir::ExprKind::Cast(expr, ty)
95                 }
96                 ExprKind::Type(ref expr, ref ty) => {
97                     let expr = self.lower_expr(expr);
98                     let ty =
99                         self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
100                     hir::ExprKind::Type(expr, ty)
101                 }
102                 ExprKind::AddrOf(k, m, ref ohs) => {
103                     let ohs = self.lower_expr(ohs);
104                     hir::ExprKind::AddrOf(k, m, ohs)
105                 }
106                 ExprKind::Let(ref pat, ref scrutinee, span) => {
107                     hir::ExprKind::Let(self.arena.alloc(hir::Let {
108                         hir_id: self.next_id(),
109                         span: self.lower_span(span),
110                         pat: self.lower_pat(pat),
111                         ty: None,
112                         init: self.lower_expr(scrutinee),
113                     }))
114                 }
115                 ExprKind::If(ref cond, ref then, ref else_opt) => {
116                     self.lower_expr_if(cond, then, else_opt.as_deref())
117                 }
118                 ExprKind::While(ref cond, ref body, opt_label) => {
119                     self.with_loop_scope(e.id, |this| {
120                         let span =
121                             this.mark_span_with_reason(DesugaringKind::WhileLoop, e.span, None);
122                         this.lower_expr_while_in_loop_scope(span, cond, body, opt_label)
123                     })
124                 }
125                 ExprKind::Loop(ref body, opt_label) => self.with_loop_scope(e.id, |this| {
126                     hir::ExprKind::Loop(
127                         this.lower_block(body, false),
128                         this.lower_label(opt_label),
129                         hir::LoopSource::Loop,
130                         DUMMY_SP,
131                     )
132                 }),
133                 ExprKind::TryBlock(ref body) => self.lower_expr_try_block(body),
134                 ExprKind::Match(ref expr, ref arms) => hir::ExprKind::Match(
135                     self.lower_expr(expr),
136                     self.arena.alloc_from_iter(arms.iter().map(|x| self.lower_arm(x))),
137                     hir::MatchSource::Normal,
138                 ),
139                 ExprKind::Async(capture_clause, closure_node_id, ref block) => self
140                     .make_async_expr(
141                         capture_clause,
142                         closure_node_id,
143                         None,
144                         block.span,
145                         hir::AsyncGeneratorKind::Block,
146                         |this| this.with_new_scopes(|this| this.lower_block_expr(block)),
147                     ),
148                 ExprKind::Await(ref expr) => {
149                     let span = if expr.span.hi() < e.span.hi() {
150                         expr.span.shrink_to_hi().with_hi(e.span.hi())
151                     } else {
152                         // this is a recovered `await expr`
153                         e.span
154                     };
155                     self.lower_expr_await(span, expr)
156                 }
157                 ExprKind::Closure(
158                     capture_clause,
159                     asyncness,
160                     movability,
161                     ref decl,
162                     ref body,
163                     fn_decl_span,
164                 ) => {
165                     if let Async::Yes { closure_id, .. } = asyncness {
166                         self.lower_expr_async_closure(
167                             capture_clause,
168                             e.id,
169                             closure_id,
170                             decl,
171                             body,
172                             fn_decl_span,
173                         )
174                     } else {
175                         self.lower_expr_closure(
176                             capture_clause,
177                             e.id,
178                             movability,
179                             decl,
180                             body,
181                             fn_decl_span,
182                         )
183                     }
184                 }
185                 ExprKind::Block(ref blk, opt_label) => {
186                     let opt_label = self.lower_label(opt_label);
187                     hir::ExprKind::Block(self.lower_block(blk, opt_label.is_some()), opt_label)
188                 }
189                 ExprKind::Assign(ref el, ref er, span) => {
190                     self.lower_expr_assign(el, er, span, e.span)
191                 }
192                 ExprKind::AssignOp(op, ref el, ref er) => hir::ExprKind::AssignOp(
193                     self.lower_binop(op),
194                     self.lower_expr(el),
195                     self.lower_expr(er),
196                 ),
197                 ExprKind::Field(ref el, ident) => {
198                     hir::ExprKind::Field(self.lower_expr(el), self.lower_ident(ident))
199                 }
200                 ExprKind::Index(ref el, ref er) => {
201                     hir::ExprKind::Index(self.lower_expr(el), self.lower_expr(er))
202                 }
203                 ExprKind::Range(Some(ref e1), Some(ref e2), RangeLimits::Closed) => {
204                     self.lower_expr_range_closed(e.span, e1, e2)
205                 }
206                 ExprKind::Range(ref e1, ref e2, lims) => {
207                     self.lower_expr_range(e.span, e1.as_deref(), e2.as_deref(), lims)
208                 }
209                 ExprKind::Underscore => {
210                     self.tcx
211                         .sess.struct_span_err(
212                             e.span,
213                             "in expressions, `_` can only be used on the left-hand side of an assignment",
214                         )
215                         .span_label(e.span, "`_` not allowed here")
216                         .emit();
217                     hir::ExprKind::Err
218                 }
219                 ExprKind::Path(ref qself, ref path) => {
220                     let qpath = self.lower_qpath(
221                         e.id,
222                         qself,
223                         path,
224                         ParamMode::Optional,
225                         ImplTraitContext::Disallowed(ImplTraitPosition::Path),
226                     );
227                     hir::ExprKind::Path(qpath)
228                 }
229                 ExprKind::Break(opt_label, ref opt_expr) => {
230                     let opt_expr = opt_expr.as_ref().map(|x| self.lower_expr(x));
231                     hir::ExprKind::Break(self.lower_jump_destination(e.id, opt_label), opt_expr)
232                 }
233                 ExprKind::Continue(opt_label) => {
234                     hir::ExprKind::Continue(self.lower_jump_destination(e.id, opt_label))
235                 }
236                 ExprKind::Ret(ref e) => {
237                     let e = e.as_ref().map(|x| self.lower_expr(x));
238                     hir::ExprKind::Ret(e)
239                 }
240                 ExprKind::Yeet(ref sub_expr) => self.lower_expr_yeet(e.span, sub_expr.as_deref()),
241                 ExprKind::InlineAsm(ref asm) => {
242                     hir::ExprKind::InlineAsm(self.lower_inline_asm(e.span, asm))
243                 }
244                 ExprKind::Struct(ref se) => {
245                     let rest = match &se.rest {
246                         StructRest::Base(e) => Some(self.lower_expr(e)),
247                         StructRest::Rest(sp) => {
248                             self.tcx
249                                 .sess
250                                 .struct_span_err(*sp, "base expression required after `..`")
251                                 .span_label(*sp, "add a base expression here")
252                                 .emit();
253                             Some(&*self.arena.alloc(self.expr_err(*sp)))
254                         }
255                         StructRest::None => None,
256                     };
257                     hir::ExprKind::Struct(
258                         self.arena.alloc(self.lower_qpath(
259                             e.id,
260                             &se.qself,
261                             &se.path,
262                             ParamMode::Optional,
263                             ImplTraitContext::Disallowed(ImplTraitPosition::Path),
264                         )),
265                         self.arena
266                             .alloc_from_iter(se.fields.iter().map(|x| self.lower_expr_field(x))),
267                         rest,
268                     )
269                 }
270                 ExprKind::Yield(ref opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()),
271                 ExprKind::Err => hir::ExprKind::Err,
272                 ExprKind::Try(ref sub_expr) => self.lower_expr_try(e.span, sub_expr),
273                 ExprKind::Paren(ref ex) => {
274                     let mut ex = self.lower_expr_mut(ex);
275                     // Include parens in span, but only if it is a super-span.
276                     if e.span.contains(ex.span) {
277                         ex.span = self.lower_span(e.span);
278                     }
279                     // Merge attributes into the inner expression.
280                     if !e.attrs.is_empty() {
281                         let old_attrs =
282                             self.attrs.get(&ex.hir_id.local_id).map(|la| *la).unwrap_or(&[]);
283                         self.attrs.insert(
284                             ex.hir_id.local_id,
285                             &*self.arena.alloc_from_iter(
286                                 e.attrs
287                                     .iter()
288                                     .map(|a| self.lower_attr(a))
289                                     .chain(old_attrs.iter().cloned()),
290                             ),
291                         );
292                     }
293                     return ex;
294                 }
295
296                 // Desugar `ExprForLoop`
297                 // from: `[opt_ident]: for <pat> in <head> <body>`
298                 ExprKind::ForLoop(ref pat, ref head, ref body, opt_label) => {
299                     return self.lower_expr_for(e, pat, head, body, opt_label);
300                 }
301                 ExprKind::MacCall(_) => panic!("{:?} shouldn't exist here", e.span),
302             };
303
304             let hir_id = self.lower_node_id(e.id);
305             self.lower_attrs(hir_id, &e.attrs);
306             hir::Expr { hir_id, kind, span: self.lower_span(e.span) }
307         })
308     }
309
310     fn lower_unop(&mut self, u: UnOp) -> hir::UnOp {
311         match u {
312             UnOp::Deref => hir::UnOp::Deref,
313             UnOp::Not => hir::UnOp::Not,
314             UnOp::Neg => hir::UnOp::Neg,
315         }
316     }
317
318     fn lower_binop(&mut self, b: BinOp) -> hir::BinOp {
319         Spanned {
320             node: match b.node {
321                 BinOpKind::Add => hir::BinOpKind::Add,
322                 BinOpKind::Sub => hir::BinOpKind::Sub,
323                 BinOpKind::Mul => hir::BinOpKind::Mul,
324                 BinOpKind::Div => hir::BinOpKind::Div,
325                 BinOpKind::Rem => hir::BinOpKind::Rem,
326                 BinOpKind::And => hir::BinOpKind::And,
327                 BinOpKind::Or => hir::BinOpKind::Or,
328                 BinOpKind::BitXor => hir::BinOpKind::BitXor,
329                 BinOpKind::BitAnd => hir::BinOpKind::BitAnd,
330                 BinOpKind::BitOr => hir::BinOpKind::BitOr,
331                 BinOpKind::Shl => hir::BinOpKind::Shl,
332                 BinOpKind::Shr => hir::BinOpKind::Shr,
333                 BinOpKind::Eq => hir::BinOpKind::Eq,
334                 BinOpKind::Lt => hir::BinOpKind::Lt,
335                 BinOpKind::Le => hir::BinOpKind::Le,
336                 BinOpKind::Ne => hir::BinOpKind::Ne,
337                 BinOpKind::Ge => hir::BinOpKind::Ge,
338                 BinOpKind::Gt => hir::BinOpKind::Gt,
339             },
340             span: self.lower_span(b.span),
341         }
342     }
343
344     fn lower_legacy_const_generics(
345         &mut self,
346         mut f: Expr,
347         args: Vec<AstP<Expr>>,
348         legacy_args_idx: &[usize],
349     ) -> hir::ExprKind<'hir> {
350         let ExprKind::Path(None, ref mut path) = f.kind else {
351             unreachable!();
352         };
353
354         // Split the arguments into const generics and normal arguments
355         let mut real_args = vec![];
356         let mut generic_args = vec![];
357         for (idx, arg) in args.into_iter().enumerate() {
358             if legacy_args_idx.contains(&idx) {
359                 let parent_def_id = self.current_hir_id_owner;
360                 let node_id = self.next_node_id();
361
362                 // Add a definition for the in-band const def.
363                 self.create_def(parent_def_id, node_id, DefPathData::AnonConst);
364
365                 let anon_const = AnonConst { id: node_id, value: arg };
366                 generic_args.push(AngleBracketedArg::Arg(GenericArg::Const(anon_const)));
367             } else {
368                 real_args.push(arg);
369             }
370         }
371
372         // Add generic args to the last element of the path.
373         let last_segment = path.segments.last_mut().unwrap();
374         assert!(last_segment.args.is_none());
375         last_segment.args = Some(AstP(GenericArgs::AngleBracketed(AngleBracketedArgs {
376             span: DUMMY_SP,
377             args: generic_args,
378         })));
379
380         // Now lower everything as normal.
381         let f = self.lower_expr(&f);
382         hir::ExprKind::Call(f, self.lower_exprs(&real_args))
383     }
384
385     fn lower_expr_if(
386         &mut self,
387         cond: &Expr,
388         then: &Block,
389         else_opt: Option<&Expr>,
390     ) -> hir::ExprKind<'hir> {
391         let lowered_cond = self.lower_expr(cond);
392         let new_cond = self.manage_let_cond(lowered_cond);
393         let then_expr = self.lower_block_expr(then);
394         if let Some(rslt) = else_opt {
395             hir::ExprKind::If(new_cond, self.arena.alloc(then_expr), Some(self.lower_expr(rslt)))
396         } else {
397             hir::ExprKind::If(new_cond, self.arena.alloc(then_expr), None)
398         }
399     }
400
401     // If `cond` kind is `let`, returns `let`. Otherwise, wraps and returns `cond`
402     // in a temporary block.
403     fn manage_let_cond(&mut self, cond: &'hir hir::Expr<'hir>) -> &'hir hir::Expr<'hir> {
404         fn has_let_expr<'hir>(expr: &'hir hir::Expr<'hir>) -> bool {
405             match expr.kind {
406                 hir::ExprKind::Binary(_, lhs, rhs) => has_let_expr(lhs) || has_let_expr(rhs),
407                 hir::ExprKind::Let(..) => true,
408                 _ => false,
409             }
410         }
411         if has_let_expr(cond) {
412             cond
413         } else {
414             let reason = DesugaringKind::CondTemporary;
415             let span_block = self.mark_span_with_reason(reason, cond.span, None);
416             self.expr_drop_temps(span_block, cond, AttrVec::new())
417         }
418     }
419
420     // We desugar: `'label: while $cond $body` into:
421     //
422     // ```
423     // 'label: loop {
424     //   if { let _t = $cond; _t } {
425     //     $body
426     //   }
427     //   else {
428     //     break;
429     //   }
430     // }
431     // ```
432     //
433     // Wrap in a construct equivalent to `{ let _t = $cond; _t }`
434     // to preserve drop semantics since `while $cond { ... }` does not
435     // let temporaries live outside of `cond`.
436     fn lower_expr_while_in_loop_scope(
437         &mut self,
438         span: Span,
439         cond: &Expr,
440         body: &Block,
441         opt_label: Option<Label>,
442     ) -> hir::ExprKind<'hir> {
443         let lowered_cond = self.with_loop_condition_scope(|t| t.lower_expr(cond));
444         let new_cond = self.manage_let_cond(lowered_cond);
445         let then = self.lower_block_expr(body);
446         let expr_break = self.expr_break(span, ThinVec::new());
447         let stmt_break = self.stmt_expr(span, expr_break);
448         let else_blk = self.block_all(span, arena_vec![self; stmt_break], None);
449         let else_expr = self.arena.alloc(self.expr_block(else_blk, ThinVec::new()));
450         let if_kind = hir::ExprKind::If(new_cond, self.arena.alloc(then), Some(else_expr));
451         let if_expr = self.expr(span, if_kind, ThinVec::new());
452         let block = self.block_expr(self.arena.alloc(if_expr));
453         let span = self.lower_span(span.with_hi(cond.span.hi()));
454         let opt_label = self.lower_label(opt_label);
455         hir::ExprKind::Loop(block, opt_label, hir::LoopSource::While, span)
456     }
457
458     /// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_output(<expr>) }`,
459     /// `try { <stmts>; }` into `{ <stmts>; ::std::ops::Try::from_output(()) }`
460     /// and save the block id to use it as a break target for desugaring of the `?` operator.
461     fn lower_expr_try_block(&mut self, body: &Block) -> hir::ExprKind<'hir> {
462         self.with_catch_scope(body.id, |this| {
463             let mut block = this.lower_block_noalloc(body, true);
464
465             // Final expression of the block (if present) or `()` with span at the end of block
466             let (try_span, tail_expr) = if let Some(expr) = block.expr.take() {
467                 (
468                     this.mark_span_with_reason(
469                         DesugaringKind::TryBlock,
470                         expr.span,
471                         this.allow_try_trait.clone(),
472                     ),
473                     expr,
474                 )
475             } else {
476                 let try_span = this.mark_span_with_reason(
477                     DesugaringKind::TryBlock,
478                     this.tcx.sess.source_map().end_point(body.span),
479                     this.allow_try_trait.clone(),
480                 );
481
482                 (try_span, this.expr_unit(try_span))
483             };
484
485             let ok_wrapped_span =
486                 this.mark_span_with_reason(DesugaringKind::TryBlock, tail_expr.span, None);
487
488             // `::std::ops::Try::from_output($tail_expr)`
489             block.expr = Some(this.wrap_in_try_constructor(
490                 hir::LangItem::TryTraitFromOutput,
491                 try_span,
492                 tail_expr,
493                 ok_wrapped_span,
494             ));
495
496             hir::ExprKind::Block(this.arena.alloc(block), None)
497         })
498     }
499
500     fn wrap_in_try_constructor(
501         &mut self,
502         lang_item: hir::LangItem,
503         method_span: Span,
504         expr: &'hir hir::Expr<'hir>,
505         overall_span: Span,
506     ) -> &'hir hir::Expr<'hir> {
507         let constructor = self.arena.alloc(self.expr_lang_item_path(
508             method_span,
509             lang_item,
510             ThinVec::new(),
511             None,
512         ));
513         self.expr_call(overall_span, constructor, std::slice::from_ref(expr))
514     }
515
516     fn lower_arm(&mut self, arm: &Arm) -> hir::Arm<'hir> {
517         let pat = self.lower_pat(&arm.pat);
518         let guard = arm.guard.as_ref().map(|cond| {
519             if let ExprKind::Let(ref pat, ref scrutinee, span) = cond.kind {
520                 hir::Guard::IfLet(self.arena.alloc(hir::Let {
521                     hir_id: self.next_id(),
522                     span: self.lower_span(span),
523                     pat: self.lower_pat(pat),
524                     ty: None,
525                     init: self.lower_expr(scrutinee),
526                 }))
527             } else {
528                 hir::Guard::If(self.lower_expr(cond))
529             }
530         });
531         let hir_id = self.next_id();
532         self.lower_attrs(hir_id, &arm.attrs);
533         hir::Arm {
534             hir_id,
535             pat,
536             guard,
537             body: self.lower_expr(&arm.body),
538             span: self.lower_span(arm.span),
539         }
540     }
541
542     /// Lower an `async` construct to a generator that is then wrapped so it implements `Future`.
543     ///
544     /// This results in:
545     ///
546     /// ```text
547     /// std::future::from_generator(static move? |_task_context| -> <ret_ty> {
548     ///     <body>
549     /// })
550     /// ```
551     pub(super) fn make_async_expr(
552         &mut self,
553         capture_clause: CaptureBy,
554         closure_node_id: NodeId,
555         ret_ty: Option<AstP<Ty>>,
556         span: Span,
557         async_gen_kind: hir::AsyncGeneratorKind,
558         body: impl FnOnce(&mut Self) -> hir::Expr<'hir>,
559     ) -> hir::ExprKind<'hir> {
560         let output = match ret_ty {
561             Some(ty) => hir::FnRetTy::Return(
562                 self.lower_ty(&ty, ImplTraitContext::Disallowed(ImplTraitPosition::AsyncBlock)),
563             ),
564             None => hir::FnRetTy::DefaultReturn(self.lower_span(span)),
565         };
566
567         // Resume argument type. We let the compiler infer this to simplify the lowering. It is
568         // fully constrained by `future::from_generator`.
569         let input_ty = hir::Ty {
570             hir_id: self.next_id(),
571             kind: hir::TyKind::Infer,
572             span: self.lower_span(span),
573         };
574
575         // The closure/generator `FnDecl` takes a single (resume) argument of type `input_ty`.
576         let fn_decl = self.arena.alloc(hir::FnDecl {
577             inputs: arena_vec![self; input_ty],
578             output,
579             c_variadic: false,
580             implicit_self: hir::ImplicitSelfKind::None,
581         });
582
583         // Lower the argument pattern/ident. The ident is used again in the `.await` lowering.
584         let (pat, task_context_hid) = self.pat_ident_binding_mode(
585             span,
586             Ident::with_dummy_span(sym::_task_context),
587             hir::BindingAnnotation::Mutable,
588         );
589         let param = hir::Param {
590             hir_id: self.next_id(),
591             pat,
592             ty_span: self.lower_span(span),
593             span: self.lower_span(span),
594         };
595         let params = arena_vec![self; param];
596
597         let body = self.lower_body(move |this| {
598             this.generator_kind = Some(hir::GeneratorKind::Async(async_gen_kind));
599
600             let old_ctx = this.task_context;
601             this.task_context = Some(task_context_hid);
602             let res = body(this);
603             this.task_context = old_ctx;
604             (params, res)
605         });
606
607         // `static |_task_context| -> <ret_ty> { body }`:
608         let generator_kind = hir::ExprKind::Closure {
609             capture_clause,
610             bound_generic_params: &[],
611             fn_decl,
612             body,
613             fn_decl_span: self.lower_span(span),
614             movability: Some(hir::Movability::Static),
615         };
616         let generator = hir::Expr {
617             hir_id: self.lower_node_id(closure_node_id),
618             kind: generator_kind,
619             span: self.lower_span(span),
620         };
621
622         // `future::from_generator`:
623         let unstable_span =
624             self.mark_span_with_reason(DesugaringKind::Async, span, self.allow_gen_future.clone());
625         let gen_future = self.expr_lang_item_path(
626             unstable_span,
627             hir::LangItem::FromGenerator,
628             ThinVec::new(),
629             None,
630         );
631
632         // `future::from_generator(generator)`:
633         hir::ExprKind::Call(self.arena.alloc(gen_future), arena_vec![self; generator])
634     }
635
636     /// Desugar `<expr>.await` into:
637     /// ```ignore (pseudo-rust)
638     /// match ::std::future::IntoFuture::into_future(<expr>) {
639     ///     mut __awaitee => loop {
640     ///         match unsafe { ::std::future::Future::poll(
641     ///             <::std::pin::Pin>::new_unchecked(&mut __awaitee),
642     ///             ::std::future::get_context(task_context),
643     ///         ) } {
644     ///             ::std::task::Poll::Ready(result) => break result,
645     ///             ::std::task::Poll::Pending => {}
646     ///         }
647     ///         task_context = yield ();
648     ///     }
649     /// }
650     /// ```
651     fn lower_expr_await(&mut self, dot_await_span: Span, expr: &Expr) -> hir::ExprKind<'hir> {
652         let full_span = expr.span.to(dot_await_span);
653         match self.generator_kind {
654             Some(hir::GeneratorKind::Async(_)) => {}
655             Some(hir::GeneratorKind::Gen) | None => {
656                 let mut err = struct_span_err!(
657                     self.tcx.sess,
658                     dot_await_span,
659                     E0728,
660                     "`await` is only allowed inside `async` functions and blocks"
661                 );
662                 err.span_label(dot_await_span, "only allowed inside `async` functions and blocks");
663                 if let Some(item_sp) = self.current_item {
664                     err.span_label(item_sp, "this is not `async`");
665                 }
666                 err.emit();
667             }
668         }
669         let span = self.mark_span_with_reason(DesugaringKind::Await, dot_await_span, None);
670         let gen_future_span = self.mark_span_with_reason(
671             DesugaringKind::Await,
672             full_span,
673             self.allow_gen_future.clone(),
674         );
675         let expr = self.lower_expr_mut(expr);
676         let expr_hir_id = expr.hir_id;
677
678         // Note that the name of this binding must not be changed to something else because
679         // debuggers and debugger extensions expect it to be called `__awaitee`. They use
680         // this name to identify what is being awaited by a suspended async functions.
681         let awaitee_ident = Ident::with_dummy_span(sym::__awaitee);
682         let (awaitee_pat, awaitee_pat_hid) =
683             self.pat_ident_binding_mode(span, awaitee_ident, hir::BindingAnnotation::Mutable);
684
685         let task_context_ident = Ident::with_dummy_span(sym::_task_context);
686
687         // unsafe {
688         //     ::std::future::Future::poll(
689         //         ::std::pin::Pin::new_unchecked(&mut __awaitee),
690         //         ::std::future::get_context(task_context),
691         //     )
692         // }
693         let poll_expr = {
694             let awaitee = self.expr_ident(span, awaitee_ident, awaitee_pat_hid);
695             let ref_mut_awaitee = self.expr_mut_addr_of(span, awaitee);
696             let task_context = if let Some(task_context_hid) = self.task_context {
697                 self.expr_ident_mut(span, task_context_ident, task_context_hid)
698             } else {
699                 // Use of `await` outside of an async context, we cannot use `task_context` here.
700                 self.expr_err(span)
701             };
702             let new_unchecked = self.expr_call_lang_item_fn_mut(
703                 span,
704                 hir::LangItem::PinNewUnchecked,
705                 arena_vec![self; ref_mut_awaitee],
706                 Some(expr_hir_id),
707             );
708             let get_context = self.expr_call_lang_item_fn_mut(
709                 gen_future_span,
710                 hir::LangItem::GetContext,
711                 arena_vec![self; task_context],
712                 Some(expr_hir_id),
713             );
714             let call = self.expr_call_lang_item_fn(
715                 span,
716                 hir::LangItem::FuturePoll,
717                 arena_vec![self; new_unchecked, get_context],
718                 Some(expr_hir_id),
719             );
720             self.arena.alloc(self.expr_unsafe(call))
721         };
722
723         // `::std::task::Poll::Ready(result) => break result`
724         let loop_node_id = self.next_node_id();
725         let loop_hir_id = self.lower_node_id(loop_node_id);
726         let ready_arm = {
727             let x_ident = Ident::with_dummy_span(sym::result);
728             let (x_pat, x_pat_hid) = self.pat_ident(gen_future_span, x_ident);
729             let x_expr = self.expr_ident(gen_future_span, x_ident, x_pat_hid);
730             let ready_field = self.single_pat_field(gen_future_span, x_pat);
731             let ready_pat = self.pat_lang_item_variant(
732                 span,
733                 hir::LangItem::PollReady,
734                 ready_field,
735                 Some(expr_hir_id),
736             );
737             let break_x = self.with_loop_scope(loop_node_id, move |this| {
738                 let expr_break =
739                     hir::ExprKind::Break(this.lower_loop_destination(None), Some(x_expr));
740                 this.arena.alloc(this.expr(gen_future_span, expr_break, ThinVec::new()))
741             });
742             self.arm(ready_pat, break_x)
743         };
744
745         // `::std::task::Poll::Pending => {}`
746         let pending_arm = {
747             let pending_pat = self.pat_lang_item_variant(
748                 span,
749                 hir::LangItem::PollPending,
750                 &[],
751                 Some(expr_hir_id),
752             );
753             let empty_block = self.expr_block_empty(span);
754             self.arm(pending_pat, empty_block)
755         };
756
757         let inner_match_stmt = {
758             let match_expr = self.expr_match(
759                 span,
760                 poll_expr,
761                 arena_vec![self; ready_arm, pending_arm],
762                 hir::MatchSource::AwaitDesugar,
763             );
764             self.stmt_expr(span, match_expr)
765         };
766
767         // task_context = yield ();
768         let yield_stmt = {
769             let unit = self.expr_unit(span);
770             let yield_expr = self.expr(
771                 span,
772                 hir::ExprKind::Yield(unit, hir::YieldSource::Await { expr: Some(expr_hir_id) }),
773                 ThinVec::new(),
774             );
775             let yield_expr = self.arena.alloc(yield_expr);
776
777             if let Some(task_context_hid) = self.task_context {
778                 let lhs = self.expr_ident(span, task_context_ident, task_context_hid);
779                 let assign = self.expr(
780                     span,
781                     hir::ExprKind::Assign(lhs, yield_expr, self.lower_span(span)),
782                     AttrVec::new(),
783                 );
784                 self.stmt_expr(span, assign)
785             } else {
786                 // Use of `await` outside of an async context. Return `yield_expr` so that we can
787                 // proceed with type checking.
788                 self.stmt(span, hir::StmtKind::Semi(yield_expr))
789             }
790         };
791
792         let loop_block = self.block_all(span, arena_vec![self; inner_match_stmt, yield_stmt], None);
793
794         // loop { .. }
795         let loop_expr = self.arena.alloc(hir::Expr {
796             hir_id: loop_hir_id,
797             kind: hir::ExprKind::Loop(
798                 loop_block,
799                 None,
800                 hir::LoopSource::Loop,
801                 self.lower_span(span),
802             ),
803             span: self.lower_span(span),
804         });
805
806         // mut __awaitee => loop { ... }
807         let awaitee_arm = self.arm(awaitee_pat, loop_expr);
808
809         // `match ::std::future::IntoFuture::into_future(<expr>) { ... }`
810         let into_future_span = self.mark_span_with_reason(
811             DesugaringKind::Await,
812             dot_await_span,
813             self.allow_into_future.clone(),
814         );
815         let into_future_expr = self.expr_call_lang_item_fn(
816             into_future_span,
817             hir::LangItem::IntoFutureIntoFuture,
818             arena_vec![self; expr],
819             Some(expr_hir_id),
820         );
821
822         // match <into_future_expr> {
823         //     mut __awaitee => loop { .. }
824         // }
825         hir::ExprKind::Match(
826             into_future_expr,
827             arena_vec![self; awaitee_arm],
828             hir::MatchSource::AwaitDesugar,
829         )
830     }
831
832     fn lower_expr_closure(
833         &mut self,
834         capture_clause: CaptureBy,
835         closure_id: NodeId,
836         movability: Movability,
837         decl: &FnDecl,
838         body: &Expr,
839         fn_decl_span: Span,
840     ) -> hir::ExprKind<'hir> {
841         let (body, generator_option) = self.with_new_scopes(move |this| {
842             let prev = this.current_item;
843             this.current_item = Some(fn_decl_span);
844             let mut generator_kind = None;
845             let body_id = this.lower_fn_body(decl, |this| {
846                 let e = this.lower_expr_mut(body);
847                 generator_kind = this.generator_kind;
848                 e
849             });
850             let generator_option =
851                 this.generator_movability_for_fn(&decl, fn_decl_span, generator_kind, movability);
852             this.current_item = prev;
853             (body_id, generator_option)
854         });
855
856         self.with_lifetime_binder(closure_id, &[], |this, bound_generic_params| {
857             // Lower outside new scope to preserve `is_in_loop_condition`.
858             let fn_decl = this.lower_fn_decl(decl, None, FnDeclKind::Closure, None);
859
860             hir::ExprKind::Closure {
861                 capture_clause,
862                 bound_generic_params,
863                 fn_decl,
864                 body,
865                 fn_decl_span: this.lower_span(fn_decl_span),
866                 movability: generator_option,
867             }
868         })
869     }
870
871     fn generator_movability_for_fn(
872         &mut self,
873         decl: &FnDecl,
874         fn_decl_span: Span,
875         generator_kind: Option<hir::GeneratorKind>,
876         movability: Movability,
877     ) -> Option<hir::Movability> {
878         match generator_kind {
879             Some(hir::GeneratorKind::Gen) => {
880                 if decl.inputs.len() > 1 {
881                     struct_span_err!(
882                         self.tcx.sess,
883                         fn_decl_span,
884                         E0628,
885                         "too many parameters for a generator (expected 0 or 1 parameters)"
886                     )
887                     .emit();
888                 }
889                 Some(movability)
890             }
891             Some(hir::GeneratorKind::Async(_)) => {
892                 panic!("non-`async` closure body turned `async` during lowering");
893             }
894             None => {
895                 if movability == Movability::Static {
896                     struct_span_err!(
897                         self.tcx.sess,
898                         fn_decl_span,
899                         E0697,
900                         "closures cannot be static"
901                     )
902                     .emit();
903                 }
904                 None
905             }
906         }
907     }
908
909     fn lower_expr_async_closure(
910         &mut self,
911         capture_clause: CaptureBy,
912         closure_id: NodeId,
913         inner_closure_id: NodeId,
914         decl: &FnDecl,
915         body: &Expr,
916         fn_decl_span: Span,
917     ) -> hir::ExprKind<'hir> {
918         let outer_decl =
919             FnDecl { inputs: decl.inputs.clone(), output: FnRetTy::Default(fn_decl_span) };
920
921         let body = self.with_new_scopes(|this| {
922             // FIXME(cramertj): allow `async` non-`move` closures with arguments.
923             if capture_clause == CaptureBy::Ref && !decl.inputs.is_empty() {
924                 struct_span_err!(
925                     this.tcx.sess,
926                     fn_decl_span,
927                     E0708,
928                     "`async` non-`move` closures with parameters are not currently supported",
929                 )
930                 .help(
931                     "consider using `let` statements to manually capture \
932                     variables by reference before entering an `async move` closure",
933                 )
934                 .emit();
935             }
936
937             // Transform `async |x: u8| -> X { ... }` into
938             // `|x: u8| future_from_generator(|| -> X { ... })`.
939             let body_id = this.lower_fn_body(&outer_decl, |this| {
940                 let async_ret_ty =
941                     if let FnRetTy::Ty(ty) = &decl.output { Some(ty.clone()) } else { None };
942                 let async_body = this.make_async_expr(
943                     capture_clause,
944                     inner_closure_id,
945                     async_ret_ty,
946                     body.span,
947                     hir::AsyncGeneratorKind::Closure,
948                     |this| this.with_new_scopes(|this| this.lower_expr_mut(body)),
949                 );
950                 this.expr(fn_decl_span, async_body, ThinVec::new())
951             });
952             body_id
953         });
954
955         self.with_lifetime_binder(closure_id, &[], |this, bound_generic_params| {
956             // We need to lower the declaration outside the new scope, because we
957             // have to conserve the state of being inside a loop condition for the
958             // closure argument types.
959             let fn_decl = this.lower_fn_decl(&outer_decl, None, FnDeclKind::Closure, None);
960
961             hir::ExprKind::Closure {
962                 capture_clause,
963                 bound_generic_params,
964                 fn_decl,
965                 body,
966                 fn_decl_span: this.lower_span(fn_decl_span),
967                 movability: None,
968             }
969         })
970     }
971
972     /// Destructure the LHS of complex assignments.
973     /// For instance, lower `(a, b) = t` to `{ let (lhs1, lhs2) = t; a = lhs1; b = lhs2; }`.
974     fn lower_expr_assign(
975         &mut self,
976         lhs: &Expr,
977         rhs: &Expr,
978         eq_sign_span: Span,
979         whole_span: Span,
980     ) -> hir::ExprKind<'hir> {
981         // Return early in case of an ordinary assignment.
982         fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_>, lhs: &Expr) -> bool {
983             match &lhs.kind {
984                 ExprKind::Array(..)
985                 | ExprKind::Struct(..)
986                 | ExprKind::Tup(..)
987                 | ExprKind::Underscore => false,
988                 // Check for tuple struct constructor.
989                 ExprKind::Call(callee, ..) => lower_ctx.extract_tuple_struct_path(callee).is_none(),
990                 ExprKind::Paren(e) => {
991                     match e.kind {
992                         // We special-case `(..)` for consistency with patterns.
993                         ExprKind::Range(None, None, RangeLimits::HalfOpen) => false,
994                         _ => is_ordinary(lower_ctx, e),
995                     }
996                 }
997                 _ => true,
998             }
999         }
1000         if is_ordinary(self, lhs) {
1001             return hir::ExprKind::Assign(
1002                 self.lower_expr(lhs),
1003                 self.lower_expr(rhs),
1004                 self.lower_span(eq_sign_span),
1005             );
1006         }
1007
1008         let mut assignments = vec![];
1009
1010         // The LHS becomes a pattern: `(lhs1, lhs2)`.
1011         let pat = self.destructure_assign(lhs, eq_sign_span, &mut assignments);
1012         let rhs = self.lower_expr(rhs);
1013
1014         // Introduce a `let` for destructuring: `let (lhs1, lhs2) = t`.
1015         let destructure_let = self.stmt_let_pat(
1016             None,
1017             whole_span,
1018             Some(rhs),
1019             pat,
1020             hir::LocalSource::AssignDesugar(self.lower_span(eq_sign_span)),
1021         );
1022
1023         // `a = lhs1; b = lhs2;`.
1024         let stmts = self
1025             .arena
1026             .alloc_from_iter(std::iter::once(destructure_let).chain(assignments.into_iter()));
1027
1028         // Wrap everything in a block.
1029         hir::ExprKind::Block(&self.block_all(whole_span, stmts, None), None)
1030     }
1031
1032     /// If the given expression is a path to a tuple struct, returns that path.
1033     /// It is not a complete check, but just tries to reject most paths early
1034     /// if they are not tuple structs.
1035     /// Type checking will take care of the full validation later.
1036     fn extract_tuple_struct_path<'a>(
1037         &mut self,
1038         expr: &'a Expr,
1039     ) -> Option<(&'a Option<QSelf>, &'a Path)> {
1040         if let ExprKind::Path(qself, path) = &expr.kind {
1041             // Does the path resolve to something disallowed in a tuple struct/variant pattern?
1042             if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
1043                 if partial_res.unresolved_segments() == 0
1044                     && !partial_res.base_res().expected_in_tuple_struct_pat()
1045                 {
1046                     return None;
1047                 }
1048             }
1049             return Some((qself, path));
1050         }
1051         None
1052     }
1053
1054     /// If the given expression is a path to a unit struct, returns that path.
1055     /// It is not a complete check, but just tries to reject most paths early
1056     /// if they are not unit structs.
1057     /// Type checking will take care of the full validation later.
1058     fn extract_unit_struct_path<'a>(
1059         &mut self,
1060         expr: &'a Expr,
1061     ) -> Option<(&'a Option<QSelf>, &'a Path)> {
1062         if let ExprKind::Path(qself, path) = &expr.kind {
1063             // Does the path resolve to something disallowed in a unit struct/variant pattern?
1064             if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
1065                 if partial_res.unresolved_segments() == 0
1066                     && !partial_res.base_res().expected_in_unit_struct_pat()
1067                 {
1068                     return None;
1069                 }
1070             }
1071             return Some((qself, path));
1072         }
1073         None
1074     }
1075
1076     /// Convert the LHS of a destructuring assignment to a pattern.
1077     /// Each sub-assignment is recorded in `assignments`.
1078     fn destructure_assign(
1079         &mut self,
1080         lhs: &Expr,
1081         eq_sign_span: Span,
1082         assignments: &mut Vec<hir::Stmt<'hir>>,
1083     ) -> &'hir hir::Pat<'hir> {
1084         self.arena.alloc(self.destructure_assign_mut(lhs, eq_sign_span, assignments))
1085     }
1086
1087     fn destructure_assign_mut(
1088         &mut self,
1089         lhs: &Expr,
1090         eq_sign_span: Span,
1091         assignments: &mut Vec<hir::Stmt<'hir>>,
1092     ) -> hir::Pat<'hir> {
1093         match &lhs.kind {
1094             // Underscore pattern.
1095             ExprKind::Underscore => {
1096                 return self.pat_without_dbm(lhs.span, hir::PatKind::Wild);
1097             }
1098             // Slice patterns.
1099             ExprKind::Array(elements) => {
1100                 let (pats, rest) =
1101                     self.destructure_sequence(elements, "slice", eq_sign_span, assignments);
1102                 let slice_pat = if let Some((i, span)) = rest {
1103                     let (before, after) = pats.split_at(i);
1104                     hir::PatKind::Slice(
1105                         before,
1106                         Some(self.arena.alloc(self.pat_without_dbm(span, hir::PatKind::Wild))),
1107                         after,
1108                     )
1109                 } else {
1110                     hir::PatKind::Slice(pats, None, &[])
1111                 };
1112                 return self.pat_without_dbm(lhs.span, slice_pat);
1113             }
1114             // Tuple structs.
1115             ExprKind::Call(callee, args) => {
1116                 if let Some((qself, path)) = self.extract_tuple_struct_path(callee) {
1117                     let (pats, rest) = self.destructure_sequence(
1118                         args,
1119                         "tuple struct or variant",
1120                         eq_sign_span,
1121                         assignments,
1122                     );
1123                     let qpath = self.lower_qpath(
1124                         callee.id,
1125                         qself,
1126                         path,
1127                         ParamMode::Optional,
1128                         ImplTraitContext::Disallowed(ImplTraitPosition::Path),
1129                     );
1130                     // Destructure like a tuple struct.
1131                     let tuple_struct_pat =
1132                         hir::PatKind::TupleStruct(qpath, pats, rest.map(|r| r.0));
1133                     return self.pat_without_dbm(lhs.span, tuple_struct_pat);
1134                 }
1135             }
1136             // Unit structs and enum variants.
1137             ExprKind::Path(..) => {
1138                 if let Some((qself, path)) = self.extract_unit_struct_path(lhs) {
1139                     let qpath = self.lower_qpath(
1140                         lhs.id,
1141                         qself,
1142                         path,
1143                         ParamMode::Optional,
1144                         ImplTraitContext::Disallowed(ImplTraitPosition::Path),
1145                     );
1146                     // Destructure like a unit struct.
1147                     let unit_struct_pat = hir::PatKind::Path(qpath);
1148                     return self.pat_without_dbm(lhs.span, unit_struct_pat);
1149                 }
1150             }
1151             // Structs.
1152             ExprKind::Struct(se) => {
1153                 let field_pats = self.arena.alloc_from_iter(se.fields.iter().map(|f| {
1154                     let pat = self.destructure_assign(&f.expr, eq_sign_span, assignments);
1155                     hir::PatField {
1156                         hir_id: self.next_id(),
1157                         ident: self.lower_ident(f.ident),
1158                         pat,
1159                         is_shorthand: f.is_shorthand,
1160                         span: self.lower_span(f.span),
1161                     }
1162                 }));
1163                 let qpath = self.lower_qpath(
1164                     lhs.id,
1165                     &se.qself,
1166                     &se.path,
1167                     ParamMode::Optional,
1168                     ImplTraitContext::Disallowed(ImplTraitPosition::Path),
1169                 );
1170                 let fields_omitted = match &se.rest {
1171                     StructRest::Base(e) => {
1172                         self.tcx
1173                             .sess
1174                             .struct_span_err(
1175                                 e.span,
1176                                 "functional record updates are not allowed in destructuring \
1177                                     assignments",
1178                             )
1179                             .span_suggestion(
1180                                 e.span,
1181                                 "consider removing the trailing pattern",
1182                                 "",
1183                                 rustc_errors::Applicability::MachineApplicable,
1184                             )
1185                             .emit();
1186                         true
1187                     }
1188                     StructRest::Rest(_) => true,
1189                     StructRest::None => false,
1190                 };
1191                 let struct_pat = hir::PatKind::Struct(qpath, field_pats, fields_omitted);
1192                 return self.pat_without_dbm(lhs.span, struct_pat);
1193             }
1194             // Tuples.
1195             ExprKind::Tup(elements) => {
1196                 let (pats, rest) =
1197                     self.destructure_sequence(elements, "tuple", eq_sign_span, assignments);
1198                 let tuple_pat = hir::PatKind::Tuple(pats, rest.map(|r| r.0));
1199                 return self.pat_without_dbm(lhs.span, tuple_pat);
1200             }
1201             ExprKind::Paren(e) => {
1202                 // We special-case `(..)` for consistency with patterns.
1203                 if let ExprKind::Range(None, None, RangeLimits::HalfOpen) = e.kind {
1204                     let tuple_pat = hir::PatKind::Tuple(&[], Some(0));
1205                     return self.pat_without_dbm(lhs.span, tuple_pat);
1206                 } else {
1207                     return self.destructure_assign_mut(e, eq_sign_span, assignments);
1208                 }
1209             }
1210             _ => {}
1211         }
1212         // Treat all other cases as normal lvalue.
1213         let ident = Ident::new(sym::lhs, self.lower_span(lhs.span));
1214         let (pat, binding) = self.pat_ident_mut(lhs.span, ident);
1215         let ident = self.expr_ident(lhs.span, ident, binding);
1216         let assign =
1217             hir::ExprKind::Assign(self.lower_expr(lhs), ident, self.lower_span(eq_sign_span));
1218         let expr = self.expr(lhs.span, assign, ThinVec::new());
1219         assignments.push(self.stmt_expr(lhs.span, expr));
1220         pat
1221     }
1222
1223     /// Destructure a sequence of expressions occurring on the LHS of an assignment.
1224     /// Such a sequence occurs in a tuple (struct)/slice.
1225     /// Return a sequence of corresponding patterns, and the index and the span of `..` if it
1226     /// exists.
1227     /// Each sub-assignment is recorded in `assignments`.
1228     fn destructure_sequence(
1229         &mut self,
1230         elements: &[AstP<Expr>],
1231         ctx: &str,
1232         eq_sign_span: Span,
1233         assignments: &mut Vec<hir::Stmt<'hir>>,
1234     ) -> (&'hir [hir::Pat<'hir>], Option<(usize, Span)>) {
1235         let mut rest = None;
1236         let elements =
1237             self.arena.alloc_from_iter(elements.iter().enumerate().filter_map(|(i, e)| {
1238                 // Check for `..` pattern.
1239                 if let ExprKind::Range(None, None, RangeLimits::HalfOpen) = e.kind {
1240                     if let Some((_, prev_span)) = rest {
1241                         self.ban_extra_rest_pat(e.span, prev_span, ctx);
1242                     } else {
1243                         rest = Some((i, e.span));
1244                     }
1245                     None
1246                 } else {
1247                     Some(self.destructure_assign_mut(e, eq_sign_span, assignments))
1248                 }
1249             }));
1250         (elements, rest)
1251     }
1252
1253     /// Desugar `<start>..=<end>` into `std::ops::RangeInclusive::new(<start>, <end>)`.
1254     fn lower_expr_range_closed(&mut self, span: Span, e1: &Expr, e2: &Expr) -> hir::ExprKind<'hir> {
1255         let e1 = self.lower_expr_mut(e1);
1256         let e2 = self.lower_expr_mut(e2);
1257         let fn_path =
1258             hir::QPath::LangItem(hir::LangItem::RangeInclusiveNew, self.lower_span(span), None);
1259         let fn_expr =
1260             self.arena.alloc(self.expr(span, hir::ExprKind::Path(fn_path), ThinVec::new()));
1261         hir::ExprKind::Call(fn_expr, arena_vec![self; e1, e2])
1262     }
1263
1264     fn lower_expr_range(
1265         &mut self,
1266         span: Span,
1267         e1: Option<&Expr>,
1268         e2: Option<&Expr>,
1269         lims: RangeLimits,
1270     ) -> hir::ExprKind<'hir> {
1271         use rustc_ast::RangeLimits::*;
1272
1273         let lang_item = match (e1, e2, lims) {
1274             (None, None, HalfOpen) => hir::LangItem::RangeFull,
1275             (Some(..), None, HalfOpen) => hir::LangItem::RangeFrom,
1276             (None, Some(..), HalfOpen) => hir::LangItem::RangeTo,
1277             (Some(..), Some(..), HalfOpen) => hir::LangItem::Range,
1278             (None, Some(..), Closed) => hir::LangItem::RangeToInclusive,
1279             (Some(..), Some(..), Closed) => unreachable!(),
1280             (_, None, Closed) => self.diagnostic().span_fatal(span, "inclusive range with no end"),
1281         };
1282
1283         let fields = self.arena.alloc_from_iter(
1284             e1.iter().map(|e| (sym::start, e)).chain(e2.iter().map(|e| (sym::end, e))).map(
1285                 |(s, e)| {
1286                     let expr = self.lower_expr(&e);
1287                     let ident = Ident::new(s, self.lower_span(e.span));
1288                     self.expr_field(ident, expr, e.span)
1289                 },
1290             ),
1291         );
1292
1293         hir::ExprKind::Struct(
1294             self.arena.alloc(hir::QPath::LangItem(lang_item, self.lower_span(span), None)),
1295             fields,
1296             None,
1297         )
1298     }
1299
1300     fn lower_label(&self, opt_label: Option<Label>) -> Option<Label> {
1301         let label = opt_label?;
1302         Some(Label { ident: self.lower_ident(label.ident) })
1303     }
1304
1305     fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
1306         let target_id = match destination {
1307             Some((id, _)) => {
1308                 if let Some(loop_id) = self.resolver.get_label_res(id) {
1309                     Ok(self.lower_node_id(loop_id))
1310                 } else {
1311                     Err(hir::LoopIdError::UnresolvedLabel)
1312                 }
1313             }
1314             None => self
1315                 .loop_scope
1316                 .map(|id| Ok(self.lower_node_id(id)))
1317                 .unwrap_or(Err(hir::LoopIdError::OutsideLoopScope)),
1318         };
1319         let label = self.lower_label(destination.map(|(_, label)| label));
1320         hir::Destination { label, target_id }
1321     }
1322
1323     fn lower_jump_destination(&mut self, id: NodeId, opt_label: Option<Label>) -> hir::Destination {
1324         if self.is_in_loop_condition && opt_label.is_none() {
1325             hir::Destination {
1326                 label: None,
1327                 target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition),
1328             }
1329         } else {
1330             self.lower_loop_destination(opt_label.map(|label| (id, label)))
1331         }
1332     }
1333
1334     fn with_catch_scope<T>(&mut self, catch_id: NodeId, f: impl FnOnce(&mut Self) -> T) -> T {
1335         let old_scope = self.catch_scope.replace(catch_id);
1336         let result = f(self);
1337         self.catch_scope = old_scope;
1338         result
1339     }
1340
1341     fn with_loop_scope<T>(&mut self, loop_id: NodeId, f: impl FnOnce(&mut Self) -> T) -> T {
1342         // We're no longer in the base loop's condition; we're in another loop.
1343         let was_in_loop_condition = self.is_in_loop_condition;
1344         self.is_in_loop_condition = false;
1345
1346         let old_scope = self.loop_scope.replace(loop_id);
1347         let result = f(self);
1348         self.loop_scope = old_scope;
1349
1350         self.is_in_loop_condition = was_in_loop_condition;
1351
1352         result
1353     }
1354
1355     fn with_loop_condition_scope<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T {
1356         let was_in_loop_condition = self.is_in_loop_condition;
1357         self.is_in_loop_condition = true;
1358
1359         let result = f(self);
1360
1361         self.is_in_loop_condition = was_in_loop_condition;
1362
1363         result
1364     }
1365
1366     fn lower_expr_field(&mut self, f: &ExprField) -> hir::ExprField<'hir> {
1367         hir::ExprField {
1368             hir_id: self.next_id(),
1369             ident: self.lower_ident(f.ident),
1370             expr: self.lower_expr(&f.expr),
1371             span: self.lower_span(f.span),
1372             is_shorthand: f.is_shorthand,
1373         }
1374     }
1375
1376     fn lower_expr_yield(&mut self, span: Span, opt_expr: Option<&Expr>) -> hir::ExprKind<'hir> {
1377         match self.generator_kind {
1378             Some(hir::GeneratorKind::Gen) => {}
1379             Some(hir::GeneratorKind::Async(_)) => {
1380                 struct_span_err!(
1381                     self.tcx.sess,
1382                     span,
1383                     E0727,
1384                     "`async` generators are not yet supported"
1385                 )
1386                 .emit();
1387             }
1388             None => self.generator_kind = Some(hir::GeneratorKind::Gen),
1389         }
1390
1391         let expr =
1392             opt_expr.as_ref().map(|x| self.lower_expr(x)).unwrap_or_else(|| self.expr_unit(span));
1393
1394         hir::ExprKind::Yield(expr, hir::YieldSource::Yield)
1395     }
1396
1397     /// Desugar `ExprForLoop` from: `[opt_ident]: for <pat> in <head> <body>` into:
1398     /// ```ignore (pseudo-rust)
1399     /// {
1400     ///     let result = match IntoIterator::into_iter(<head>) {
1401     ///         mut iter => {
1402     ///             [opt_ident]: loop {
1403     ///                 match Iterator::next(&mut iter) {
1404     ///                     None => break,
1405     ///                     Some(<pat>) => <body>,
1406     ///                 };
1407     ///             }
1408     ///         }
1409     ///     };
1410     ///     result
1411     /// }
1412     /// ```
1413     fn lower_expr_for(
1414         &mut self,
1415         e: &Expr,
1416         pat: &Pat,
1417         head: &Expr,
1418         body: &Block,
1419         opt_label: Option<Label>,
1420     ) -> hir::Expr<'hir> {
1421         let head = self.lower_expr_mut(head);
1422         let pat = self.lower_pat(pat);
1423         let for_span =
1424             self.mark_span_with_reason(DesugaringKind::ForLoop, self.lower_span(e.span), None);
1425         let head_span = self.mark_span_with_reason(DesugaringKind::ForLoop, head.span, None);
1426         let pat_span = self.mark_span_with_reason(DesugaringKind::ForLoop, pat.span, None);
1427
1428         // `None => break`
1429         let none_arm = {
1430             let break_expr =
1431                 self.with_loop_scope(e.id, |this| this.expr_break_alloc(for_span, ThinVec::new()));
1432             let pat = self.pat_none(for_span);
1433             self.arm(pat, break_expr)
1434         };
1435
1436         // Some(<pat>) => <body>,
1437         let some_arm = {
1438             let some_pat = self.pat_some(pat_span, pat);
1439             let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body, false));
1440             let body_expr = self.arena.alloc(self.expr_block(body_block, ThinVec::new()));
1441             self.arm(some_pat, body_expr)
1442         };
1443
1444         // `mut iter`
1445         let iter = Ident::with_dummy_span(sym::iter);
1446         let (iter_pat, iter_pat_nid) =
1447             self.pat_ident_binding_mode(head_span, iter, hir::BindingAnnotation::Mutable);
1448
1449         // `match Iterator::next(&mut iter) { ... }`
1450         let match_expr = {
1451             let iter = self.expr_ident(head_span, iter, iter_pat_nid);
1452             let ref_mut_iter = self.expr_mut_addr_of(head_span, iter);
1453             let next_expr = self.expr_call_lang_item_fn(
1454                 head_span,
1455                 hir::LangItem::IteratorNext,
1456                 arena_vec![self; ref_mut_iter],
1457                 None,
1458             );
1459             let arms = arena_vec![self; none_arm, some_arm];
1460
1461             self.expr_match(head_span, next_expr, arms, hir::MatchSource::ForLoopDesugar)
1462         };
1463         let match_stmt = self.stmt_expr(for_span, match_expr);
1464
1465         let loop_block = self.block_all(for_span, arena_vec![self; match_stmt], None);
1466
1467         // `[opt_ident]: loop { ... }`
1468         let kind = hir::ExprKind::Loop(
1469             loop_block,
1470             self.lower_label(opt_label),
1471             hir::LoopSource::ForLoop,
1472             self.lower_span(for_span.with_hi(head.span.hi())),
1473         );
1474         let loop_expr =
1475             self.arena.alloc(hir::Expr { hir_id: self.lower_node_id(e.id), kind, span: for_span });
1476
1477         // `mut iter => { ... }`
1478         let iter_arm = self.arm(iter_pat, loop_expr);
1479
1480         // `match ::std::iter::IntoIterator::into_iter(<head>) { ... }`
1481         let into_iter_expr = {
1482             self.expr_call_lang_item_fn(
1483                 head_span,
1484                 hir::LangItem::IntoIterIntoIter,
1485                 arena_vec![self; head],
1486                 None,
1487             )
1488         };
1489
1490         let match_expr = self.arena.alloc(self.expr_match(
1491             for_span,
1492             into_iter_expr,
1493             arena_vec![self; iter_arm],
1494             hir::MatchSource::ForLoopDesugar,
1495         ));
1496
1497         let attrs: Vec<_> = e.attrs.iter().map(|a| self.lower_attr(a)).collect();
1498
1499         // This is effectively `{ let _result = ...; _result }`.
1500         // The construct was introduced in #21984 and is necessary to make sure that
1501         // temporaries in the `head` expression are dropped and do not leak to the
1502         // surrounding scope of the `match` since the `match` is not a terminating scope.
1503         //
1504         // Also, add the attributes to the outer returned expr node.
1505         self.expr_drop_temps_mut(for_span, match_expr, attrs.into())
1506     }
1507
1508     /// Desugar `ExprKind::Try` from: `<expr>?` into:
1509     /// ```ignore (pseudo-rust)
1510     /// match Try::branch(<expr>) {
1511     ///     ControlFlow::Continue(val) => #[allow(unreachable_code)] val,,
1512     ///     ControlFlow::Break(residual) =>
1513     ///         #[allow(unreachable_code)]
1514     ///         // If there is an enclosing `try {...}`:
1515     ///         break 'catch_target Try::from_residual(residual),
1516     ///         // Otherwise:
1517     ///         return Try::from_residual(residual),
1518     /// }
1519     /// ```
1520     fn lower_expr_try(&mut self, span: Span, sub_expr: &Expr) -> hir::ExprKind<'hir> {
1521         let unstable_span = self.mark_span_with_reason(
1522             DesugaringKind::QuestionMark,
1523             span,
1524             self.allow_try_trait.clone(),
1525         );
1526         let try_span = self.tcx.sess.source_map().end_point(span);
1527         let try_span = self.mark_span_with_reason(
1528             DesugaringKind::QuestionMark,
1529             try_span,
1530             self.allow_try_trait.clone(),
1531         );
1532
1533         // `Try::branch(<expr>)`
1534         let scrutinee = {
1535             // expand <expr>
1536             let sub_expr = self.lower_expr_mut(sub_expr);
1537
1538             self.expr_call_lang_item_fn(
1539                 unstable_span,
1540                 hir::LangItem::TryTraitBranch,
1541                 arena_vec![self; sub_expr],
1542                 None,
1543             )
1544         };
1545
1546         // `#[allow(unreachable_code)]`
1547         let attr = {
1548             // `allow(unreachable_code)`
1549             let allow = {
1550                 let allow_ident = Ident::new(sym::allow, self.lower_span(span));
1551                 let uc_ident = Ident::new(sym::unreachable_code, self.lower_span(span));
1552                 let uc_nested = attr::mk_nested_word_item(uc_ident);
1553                 attr::mk_list_item(allow_ident, vec![uc_nested])
1554             };
1555             attr::mk_attr_outer(allow)
1556         };
1557         let attrs = vec![attr];
1558
1559         // `ControlFlow::Continue(val) => #[allow(unreachable_code)] val,`
1560         let continue_arm = {
1561             let val_ident = Ident::with_dummy_span(sym::val);
1562             let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
1563             let val_expr = self.arena.alloc(self.expr_ident_with_attrs(
1564                 span,
1565                 val_ident,
1566                 val_pat_nid,
1567                 ThinVec::from(attrs.clone()),
1568             ));
1569             let continue_pat = self.pat_cf_continue(unstable_span, val_pat);
1570             self.arm(continue_pat, val_expr)
1571         };
1572
1573         // `ControlFlow::Break(residual) =>
1574         //     #[allow(unreachable_code)]
1575         //     return Try::from_residual(residual),`
1576         let break_arm = {
1577             let residual_ident = Ident::with_dummy_span(sym::residual);
1578             let (residual_local, residual_local_nid) = self.pat_ident(try_span, residual_ident);
1579             let residual_expr = self.expr_ident_mut(try_span, residual_ident, residual_local_nid);
1580             let from_residual_expr = self.wrap_in_try_constructor(
1581                 hir::LangItem::TryTraitFromResidual,
1582                 try_span,
1583                 self.arena.alloc(residual_expr),
1584                 unstable_span,
1585             );
1586             let thin_attrs = ThinVec::from(attrs);
1587             let ret_expr = if let Some(catch_node) = self.catch_scope {
1588                 let target_id = Ok(self.lower_node_id(catch_node));
1589                 self.arena.alloc(self.expr(
1590                     try_span,
1591                     hir::ExprKind::Break(
1592                         hir::Destination { label: None, target_id },
1593                         Some(from_residual_expr),
1594                     ),
1595                     thin_attrs,
1596                 ))
1597             } else {
1598                 self.arena.alloc(self.expr(
1599                     try_span,
1600                     hir::ExprKind::Ret(Some(from_residual_expr)),
1601                     thin_attrs,
1602                 ))
1603             };
1604
1605             let break_pat = self.pat_cf_break(try_span, residual_local);
1606             self.arm(break_pat, ret_expr)
1607         };
1608
1609         hir::ExprKind::Match(
1610             scrutinee,
1611             arena_vec![self; break_arm, continue_arm],
1612             hir::MatchSource::TryDesugar,
1613         )
1614     }
1615
1616     /// Desugar `ExprKind::Yeet` from: `do yeet <expr>` into:
1617     /// ```rust
1618     /// // If there is an enclosing `try {...}`:
1619     /// break 'catch_target FromResidual::from_residual(Yeet(residual)),
1620     /// // Otherwise:
1621     /// return FromResidual::from_residual(Yeet(residual)),
1622     /// ```
1623     /// But to simplify this, there's a `from_yeet` lang item function which
1624     /// handles the combined `FromResidual::from_residual(Yeet(residual))`.
1625     fn lower_expr_yeet(&mut self, span: Span, sub_expr: Option<&Expr>) -> hir::ExprKind<'hir> {
1626         // The expression (if present) or `()` otherwise.
1627         let (yeeted_span, yeeted_expr) = if let Some(sub_expr) = sub_expr {
1628             (sub_expr.span, self.lower_expr(sub_expr))
1629         } else {
1630             (self.mark_span_with_reason(DesugaringKind::YeetExpr, span, None), self.expr_unit(span))
1631         };
1632
1633         let unstable_span = self.mark_span_with_reason(
1634             DesugaringKind::YeetExpr,
1635             span,
1636             self.allow_try_trait.clone(),
1637         );
1638
1639         let from_yeet_expr = self.wrap_in_try_constructor(
1640             hir::LangItem::TryTraitFromYeet,
1641             unstable_span,
1642             yeeted_expr,
1643             yeeted_span,
1644         );
1645
1646         if let Some(catch_node) = self.catch_scope {
1647             let target_id = Ok(self.lower_node_id(catch_node));
1648             hir::ExprKind::Break(hir::Destination { label: None, target_id }, Some(from_yeet_expr))
1649         } else {
1650             hir::ExprKind::Ret(Some(from_yeet_expr))
1651         }
1652     }
1653
1654     // =========================================================================
1655     // Helper methods for building HIR.
1656     // =========================================================================
1657
1658     /// Wrap the given `expr` in a terminating scope using `hir::ExprKind::DropTemps`.
1659     ///
1660     /// In terms of drop order, it has the same effect as wrapping `expr` in
1661     /// `{ let _t = $expr; _t }` but should provide better compile-time performance.
1662     ///
1663     /// The drop order can be important in e.g. `if expr { .. }`.
1664     pub(super) fn expr_drop_temps(
1665         &mut self,
1666         span: Span,
1667         expr: &'hir hir::Expr<'hir>,
1668         attrs: AttrVec,
1669     ) -> &'hir hir::Expr<'hir> {
1670         self.arena.alloc(self.expr_drop_temps_mut(span, expr, attrs))
1671     }
1672
1673     pub(super) fn expr_drop_temps_mut(
1674         &mut self,
1675         span: Span,
1676         expr: &'hir hir::Expr<'hir>,
1677         attrs: AttrVec,
1678     ) -> hir::Expr<'hir> {
1679         self.expr(span, hir::ExprKind::DropTemps(expr), attrs)
1680     }
1681
1682     fn expr_match(
1683         &mut self,
1684         span: Span,
1685         arg: &'hir hir::Expr<'hir>,
1686         arms: &'hir [hir::Arm<'hir>],
1687         source: hir::MatchSource,
1688     ) -> hir::Expr<'hir> {
1689         self.expr(span, hir::ExprKind::Match(arg, arms, source), ThinVec::new())
1690     }
1691
1692     fn expr_break(&mut self, span: Span, attrs: AttrVec) -> hir::Expr<'hir> {
1693         let expr_break = hir::ExprKind::Break(self.lower_loop_destination(None), None);
1694         self.expr(span, expr_break, attrs)
1695     }
1696
1697     fn expr_break_alloc(&mut self, span: Span, attrs: AttrVec) -> &'hir hir::Expr<'hir> {
1698         let expr_break = self.expr_break(span, attrs);
1699         self.arena.alloc(expr_break)
1700     }
1701
1702     fn expr_mut_addr_of(&mut self, span: Span, e: &'hir hir::Expr<'hir>) -> hir::Expr<'hir> {
1703         self.expr(
1704             span,
1705             hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, e),
1706             ThinVec::new(),
1707         )
1708     }
1709
1710     fn expr_unit(&mut self, sp: Span) -> &'hir hir::Expr<'hir> {
1711         self.arena.alloc(self.expr(sp, hir::ExprKind::Tup(&[]), ThinVec::new()))
1712     }
1713
1714     fn expr_call_mut(
1715         &mut self,
1716         span: Span,
1717         e: &'hir hir::Expr<'hir>,
1718         args: &'hir [hir::Expr<'hir>],
1719     ) -> hir::Expr<'hir> {
1720         self.expr(span, hir::ExprKind::Call(e, args), ThinVec::new())
1721     }
1722
1723     fn expr_call(
1724         &mut self,
1725         span: Span,
1726         e: &'hir hir::Expr<'hir>,
1727         args: &'hir [hir::Expr<'hir>],
1728     ) -> &'hir hir::Expr<'hir> {
1729         self.arena.alloc(self.expr_call_mut(span, e, args))
1730     }
1731
1732     fn expr_call_lang_item_fn_mut(
1733         &mut self,
1734         span: Span,
1735         lang_item: hir::LangItem,
1736         args: &'hir [hir::Expr<'hir>],
1737         hir_id: Option<hir::HirId>,
1738     ) -> hir::Expr<'hir> {
1739         let path =
1740             self.arena.alloc(self.expr_lang_item_path(span, lang_item, ThinVec::new(), hir_id));
1741         self.expr_call_mut(span, path, args)
1742     }
1743
1744     fn expr_call_lang_item_fn(
1745         &mut self,
1746         span: Span,
1747         lang_item: hir::LangItem,
1748         args: &'hir [hir::Expr<'hir>],
1749         hir_id: Option<hir::HirId>,
1750     ) -> &'hir hir::Expr<'hir> {
1751         self.arena.alloc(self.expr_call_lang_item_fn_mut(span, lang_item, args, hir_id))
1752     }
1753
1754     fn expr_lang_item_path(
1755         &mut self,
1756         span: Span,
1757         lang_item: hir::LangItem,
1758         attrs: AttrVec,
1759         hir_id: Option<hir::HirId>,
1760     ) -> hir::Expr<'hir> {
1761         self.expr(
1762             span,
1763             hir::ExprKind::Path(hir::QPath::LangItem(lang_item, self.lower_span(span), hir_id)),
1764             attrs,
1765         )
1766     }
1767
1768     pub(super) fn expr_ident(
1769         &mut self,
1770         sp: Span,
1771         ident: Ident,
1772         binding: hir::HirId,
1773     ) -> &'hir hir::Expr<'hir> {
1774         self.arena.alloc(self.expr_ident_mut(sp, ident, binding))
1775     }
1776
1777     pub(super) fn expr_ident_mut(
1778         &mut self,
1779         sp: Span,
1780         ident: Ident,
1781         binding: hir::HirId,
1782     ) -> hir::Expr<'hir> {
1783         self.expr_ident_with_attrs(sp, ident, binding, ThinVec::new())
1784     }
1785
1786     fn expr_ident_with_attrs(
1787         &mut self,
1788         span: Span,
1789         ident: Ident,
1790         binding: hir::HirId,
1791         attrs: AttrVec,
1792     ) -> hir::Expr<'hir> {
1793         let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
1794             None,
1795             self.arena.alloc(hir::Path {
1796                 span: self.lower_span(span),
1797                 res: Res::Local(binding),
1798                 segments: arena_vec![self; hir::PathSegment::from_ident(ident)],
1799             }),
1800         ));
1801
1802         self.expr(span, expr_path, attrs)
1803     }
1804
1805     fn expr_unsafe(&mut self, expr: &'hir hir::Expr<'hir>) -> hir::Expr<'hir> {
1806         let hir_id = self.next_id();
1807         let span = expr.span;
1808         self.expr(
1809             span,
1810             hir::ExprKind::Block(
1811                 self.arena.alloc(hir::Block {
1812                     stmts: &[],
1813                     expr: Some(expr),
1814                     hir_id,
1815                     rules: hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::CompilerGenerated),
1816                     span: self.lower_span(span),
1817                     targeted_by_break: false,
1818                 }),
1819                 None,
1820             ),
1821             ThinVec::new(),
1822         )
1823     }
1824
1825     fn expr_block_empty(&mut self, span: Span) -> &'hir hir::Expr<'hir> {
1826         let blk = self.block_all(span, &[], None);
1827         let expr = self.expr_block(blk, ThinVec::new());
1828         self.arena.alloc(expr)
1829     }
1830
1831     pub(super) fn expr_block(
1832         &mut self,
1833         b: &'hir hir::Block<'hir>,
1834         attrs: AttrVec,
1835     ) -> hir::Expr<'hir> {
1836         self.expr(b.span, hir::ExprKind::Block(b, None), attrs)
1837     }
1838
1839     pub(super) fn expr(
1840         &mut self,
1841         span: Span,
1842         kind: hir::ExprKind<'hir>,
1843         attrs: AttrVec,
1844     ) -> hir::Expr<'hir> {
1845         let hir_id = self.next_id();
1846         self.lower_attrs(hir_id, &attrs);
1847         hir::Expr { hir_id, kind, span: self.lower_span(span) }
1848     }
1849
1850     fn expr_field(
1851         &mut self,
1852         ident: Ident,
1853         expr: &'hir hir::Expr<'hir>,
1854         span: Span,
1855     ) -> hir::ExprField<'hir> {
1856         hir::ExprField {
1857             hir_id: self.next_id(),
1858             ident,
1859             span: self.lower_span(span),
1860             expr,
1861             is_shorthand: false,
1862         }
1863     }
1864
1865     fn arm(&mut self, pat: &'hir hir::Pat<'hir>, expr: &'hir hir::Expr<'hir>) -> hir::Arm<'hir> {
1866         hir::Arm {
1867             hir_id: self.next_id(),
1868             pat,
1869             guard: None,
1870             span: self.lower_span(expr.span),
1871             body: expr,
1872         }
1873     }
1874 }