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