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