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