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