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