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