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