]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/lowering/expr.rs
4ba61e9d4fdc8ad8deb8c6b9557c565fe17e5781
[rust.git] / src / librustc / hir / lowering / expr.rs
1 use super::{LoweringContext, ParamMode, ParenthesizedGenericArgs, ImplTraitContext};
2 use crate::hir::{self, HirVec};
3 use crate::hir::def::Res;
4 use crate::hir::ptr::P;
5
6 use rustc_data_structures::thin_vec::ThinVec;
7
8 use syntax::attr;
9 use syntax::ptr::P as AstP;
10 use syntax::ast::*;
11 use syntax::source_map::{respan, DesugaringKind, Span, Spanned};
12 use syntax::symbol::{sym, Symbol};
13
14 impl LoweringContext<'_> {
15     fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> HirVec<hir::Expr> {
16         exprs.iter().map(|x| self.lower_expr(x)).collect()
17     }
18
19     pub(super) fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
20         let kind = match e.node {
21             ExprKind::Box(ref inner) => hir::ExprKind::Box(P(self.lower_expr(inner))),
22             ExprKind::Array(ref exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
23             ExprKind::Repeat(ref expr, ref count) => {
24                 let expr = P(self.lower_expr(expr));
25                 let count = self.lower_anon_const(count);
26                 hir::ExprKind::Repeat(expr, count)
27             }
28             ExprKind::Tup(ref elts) => hir::ExprKind::Tup(self.lower_exprs(elts)),
29             ExprKind::Call(ref f, ref args) => {
30                 let f = P(self.lower_expr(f));
31                 hir::ExprKind::Call(f, self.lower_exprs(args))
32             }
33             ExprKind::MethodCall(ref seg, ref args) => {
34                 let hir_seg = P(self.lower_path_segment(
35                     e.span,
36                     seg,
37                     ParamMode::Optional,
38                     0,
39                     ParenthesizedGenericArgs::Err,
40                     ImplTraitContext::disallowed(),
41                     None,
42                 ));
43                 let args = self.lower_exprs(args);
44                 hir::ExprKind::MethodCall(hir_seg, seg.ident.span, args)
45             }
46             ExprKind::Binary(binop, ref lhs, ref rhs) => {
47                 let binop = self.lower_binop(binop);
48                 let lhs = P(self.lower_expr(lhs));
49                 let rhs = P(self.lower_expr(rhs));
50                 hir::ExprKind::Binary(binop, lhs, rhs)
51             }
52             ExprKind::Unary(op, ref ohs) => {
53                 let op = self.lower_unop(op);
54                 let ohs = P(self.lower_expr(ohs));
55                 hir::ExprKind::Unary(op, ohs)
56             }
57             ExprKind::Lit(ref l) => hir::ExprKind::Lit(respan(l.span, l.node.clone())),
58             ExprKind::Cast(ref expr, ref ty) => {
59                 let expr = P(self.lower_expr(expr));
60                 hir::ExprKind::Cast(expr, self.lower_ty(ty, ImplTraitContext::disallowed()))
61             }
62             ExprKind::Type(ref expr, ref ty) => {
63                 let expr = P(self.lower_expr(expr));
64                 hir::ExprKind::Type(expr, self.lower_ty(ty, ImplTraitContext::disallowed()))
65             }
66             ExprKind::AddrOf(m, ref ohs) => {
67                 let m = self.lower_mutability(m);
68                 let ohs = P(self.lower_expr(ohs));
69                 hir::ExprKind::AddrOf(m, ohs)
70             }
71             ExprKind::Let(ref pats, ref scrutinee) => self.lower_expr_let(e.span, pats, scrutinee),
72             ExprKind::If(ref cond, ref then, ref else_opt) => {
73                 self.lower_expr_if(e.span, cond, then, else_opt.as_deref())
74             }
75             ExprKind::While(ref cond, ref body, opt_label) => self.with_loop_scope(e.id, |this| {
76                 this.lower_expr_while_in_loop_scope(e.span, cond, body, opt_label)
77             }),
78             ExprKind::Loop(ref body, opt_label) => self.with_loop_scope(e.id, |this| {
79                 hir::ExprKind::Loop(
80                     this.lower_block(body, false),
81                     this.lower_label(opt_label),
82                     hir::LoopSource::Loop,
83                 )
84             }),
85             ExprKind::TryBlock(ref body) => self.lower_expr_try_block(body),
86             ExprKind::Match(ref expr, ref arms) => hir::ExprKind::Match(
87                 P(self.lower_expr(expr)),
88                 arms.iter().map(|x| self.lower_arm(x)).collect(),
89                 hir::MatchSource::Normal,
90             ),
91             ExprKind::Async(capture_clause, closure_node_id, ref block) => {
92                 self.make_async_expr(capture_clause, closure_node_id, None, block.span, |this| {
93                     this.with_new_scopes(|this| {
94                         let block = this.lower_block(block, false);
95                         this.expr_block(block, ThinVec::new())
96                     })
97                 })
98             }
99             ExprKind::Await(ref expr) => self.lower_expr_await(e.span, expr),
100             ExprKind::Closure(
101                 capture_clause, asyncness, movability, ref decl, ref body, fn_decl_span
102             ) => if let IsAsync::Async { closure_id, .. } = asyncness {
103                 self.lower_expr_async_closure(capture_clause, closure_id, decl, body, fn_decl_span)
104             } else {
105                 self.lower_expr_closure(capture_clause, movability, decl, body, fn_decl_span)
106             }
107             ExprKind::Block(ref blk, opt_label) => {
108                 hir::ExprKind::Block(self.lower_block(blk,
109                                                       opt_label.is_some()),
110                                                       self.lower_label(opt_label))
111             }
112             ExprKind::Assign(ref el, ref er) => {
113                 hir::ExprKind::Assign(P(self.lower_expr(el)), P(self.lower_expr(er)))
114             }
115             ExprKind::AssignOp(op, ref el, ref er) => hir::ExprKind::AssignOp(
116                 self.lower_binop(op),
117                 P(self.lower_expr(el)),
118                 P(self.lower_expr(er)),
119             ),
120             ExprKind::Field(ref el, ident) => hir::ExprKind::Field(P(self.lower_expr(el)), ident),
121             ExprKind::Index(ref el, ref er) => {
122                 hir::ExprKind::Index(P(self.lower_expr(el)), P(self.lower_expr(er)))
123             }
124             ExprKind::Range(Some(ref e1), Some(ref e2), RangeLimits::Closed) => {
125                 self.lower_expr_range_closed(e.span, e1, e2)
126             }
127             ExprKind::Range(ref e1, ref e2, lims) => {
128                 self.lower_expr_range(e.span, e1.as_deref(), e2.as_deref(), lims)
129             }
130             ExprKind::Path(ref qself, ref path) => {
131                 let qpath = self.lower_qpath(
132                     e.id,
133                     qself,
134                     path,
135                     ParamMode::Optional,
136                     ImplTraitContext::disallowed(),
137                 );
138                 hir::ExprKind::Path(qpath)
139             }
140             ExprKind::Break(opt_label, ref opt_expr) => {
141                 hir::ExprKind::Break(
142                     self.lower_jump_destination(e.id, opt_label),
143                     opt_expr.as_ref().map(|x| P(self.lower_expr(x))),
144                 )
145             }
146             ExprKind::Continue(opt_label) => {
147                 hir::ExprKind::Continue(self.lower_jump_destination(e.id, opt_label))
148             }
149             ExprKind::Ret(ref e) => hir::ExprKind::Ret(e.as_ref().map(|x| P(self.lower_expr(x)))),
150             ExprKind::InlineAsm(ref asm) => self.lower_expr_asm(asm),
151             ExprKind::Struct(ref path, ref fields, ref maybe_expr) => hir::ExprKind::Struct(
152                 P(self.lower_qpath(
153                     e.id,
154                     &None,
155                     path,
156                     ParamMode::Optional,
157                     ImplTraitContext::disallowed(),
158                 )),
159                 fields.iter().map(|x| self.lower_field(x)).collect(),
160                 maybe_expr.as_ref().map(|x| P(self.lower_expr(x))),
161             ),
162             ExprKind::Paren(ref ex) => {
163                 let mut ex = self.lower_expr(ex);
164                 // Include parens in span, but only if it is a super-span.
165                 if e.span.contains(ex.span) {
166                     ex.span = e.span;
167                 }
168                 // Merge attributes into the inner expression.
169                 let mut attrs = e.attrs.clone();
170                 attrs.extend::<Vec<_>>(ex.attrs.into());
171                 ex.attrs = attrs;
172                 return ex;
173             }
174
175             ExprKind::Yield(ref opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()),
176
177             ExprKind::Err => hir::ExprKind::Err,
178
179             // Desugar `ExprForLoop`
180             // from: `[opt_ident]: for <pat> in <head> <body>`
181             ExprKind::ForLoop(ref pat, ref head, ref body, opt_label) => {
182                 return self.lower_expr_for(e, pat, head, body, opt_label);
183             }
184             ExprKind::Try(ref sub_expr) => self.lower_expr_try(e.span, sub_expr),
185             ExprKind::Mac(_) => panic!("Shouldn't exist here"),
186         };
187
188         hir::Expr {
189             hir_id: self.lower_node_id(e.id),
190             node: kind,
191             span: e.span,
192             attrs: e.attrs.clone(),
193         }
194     }
195
196     fn lower_unop(&mut self, u: UnOp) -> hir::UnOp {
197         match u {
198             UnOp::Deref => hir::UnDeref,
199             UnOp::Not => hir::UnNot,
200             UnOp::Neg => hir::UnNeg,
201         }
202     }
203
204     fn lower_binop(&mut self, b: BinOp) -> hir::BinOp {
205         Spanned {
206             node: match b.node {
207                 BinOpKind::Add => hir::BinOpKind::Add,
208                 BinOpKind::Sub => hir::BinOpKind::Sub,
209                 BinOpKind::Mul => hir::BinOpKind::Mul,
210                 BinOpKind::Div => hir::BinOpKind::Div,
211                 BinOpKind::Rem => hir::BinOpKind::Rem,
212                 BinOpKind::And => hir::BinOpKind::And,
213                 BinOpKind::Or => hir::BinOpKind::Or,
214                 BinOpKind::BitXor => hir::BinOpKind::BitXor,
215                 BinOpKind::BitAnd => hir::BinOpKind::BitAnd,
216                 BinOpKind::BitOr => hir::BinOpKind::BitOr,
217                 BinOpKind::Shl => hir::BinOpKind::Shl,
218                 BinOpKind::Shr => hir::BinOpKind::Shr,
219                 BinOpKind::Eq => hir::BinOpKind::Eq,
220                 BinOpKind::Lt => hir::BinOpKind::Lt,
221                 BinOpKind::Le => hir::BinOpKind::Le,
222                 BinOpKind::Ne => hir::BinOpKind::Ne,
223                 BinOpKind::Ge => hir::BinOpKind::Ge,
224                 BinOpKind::Gt => hir::BinOpKind::Gt,
225             },
226             span: b.span,
227         }
228     }
229
230     /// Emit an error and lower `ast::ExprKind::Let(pats, scrutinee)` into:
231     /// ```rust
232     /// match scrutinee { pats => true, _ => false }
233     /// ```
234     fn lower_expr_let(
235         &mut self,
236         span: Span,
237         pats: &[AstP<Pat>],
238         scrutinee: &Expr
239     ) -> hir::ExprKind {
240         // If we got here, the `let` expression is not allowed.
241         self.sess
242             .struct_span_err(span, "`let` expressions are not supported here")
243             .note("only supported directly in conditions of `if`- and `while`-expressions")
244             .note("as well as when nested within `&&` and parenthesis in those conditions")
245             .emit();
246
247         // For better recovery, we emit:
248         // ```
249         // match scrutinee { pats => true, _ => false }
250         // ```
251         // While this doesn't fully match the user's intent, it has key advantages:
252         // 1. We can avoid using `abort_if_errors`.
253         // 2. We can typeck both `pats` and `scrutinee`.
254         // 3. `pats` is allowed to be refutable.
255         // 4. The return type of the block is `bool` which seems like what the user wanted.
256         let scrutinee = self.lower_expr(scrutinee);
257         let then_arm = {
258             let pats = pats.iter().map(|pat| self.lower_pat(pat)).collect();
259             let expr = self.expr_bool(span, true);
260             self.arm(pats, P(expr))
261         };
262         let else_arm = {
263             let pats = hir_vec![self.pat_wild(span)];
264             let expr = self.expr_bool(span, false);
265             self.arm(pats, P(expr))
266         };
267         hir::ExprKind::Match(
268             P(scrutinee),
269             vec![then_arm, else_arm].into(),
270             hir::MatchSource::Normal,
271         )
272     }
273
274     fn lower_expr_if(
275         &mut self,
276         span: Span,
277         cond: &Expr,
278         then: &Block,
279         else_opt: Option<&Expr>,
280     ) -> hir::ExprKind {
281         // FIXME(#53667): handle lowering of && and parens.
282
283         // `_ => else_block` where `else_block` is `{}` if there's `None`:
284         let else_pat = self.pat_wild(span);
285         let (else_expr, contains_else_clause) = match else_opt {
286             None => (self.expr_block_empty(span), false),
287             Some(els) => (self.lower_expr(els), true),
288         };
289         let else_arm = self.arm(hir_vec![else_pat], P(else_expr));
290
291         // Handle then + scrutinee:
292         let then_blk = self.lower_block(then, false);
293         let then_expr = self.expr_block(then_blk, ThinVec::new());
294         let (then_pats, scrutinee, desugar) = match cond.node {
295             // `<pat> => <then>`:
296             ExprKind::Let(ref pats, ref scrutinee) => {
297                 let scrutinee = self.lower_expr(scrutinee);
298                 let pats = pats.iter().map(|pat| self.lower_pat(pat)).collect();
299                 let desugar = hir::MatchSource::IfLetDesugar { contains_else_clause };
300                 (pats, scrutinee, desugar)
301             }
302             // `true => <then>`:
303             _ => {
304                 // Lower condition:
305                 let cond = self.lower_expr(cond);
306                 let span_block = self.mark_span_with_reason(
307                     DesugaringKind::CondTemporary,
308                     cond.span,
309                     None
310                 );
311                 // Wrap in a construct equivalent to `{ let _t = $cond; _t }`
312                 // to preserve drop semantics since `if cond { ... }` does not
313                 // let temporaries live outside of `cond`.
314                 let cond = self.expr_drop_temps(span_block, P(cond), ThinVec::new());
315
316                 let desugar = hir::MatchSource::IfDesugar { contains_else_clause };
317                 let pats = hir_vec![self.pat_bool(span, true)];
318                 (pats, cond, desugar)
319             }
320         };
321         let then_arm = self.arm(then_pats, P(then_expr));
322
323         hir::ExprKind::Match(P(scrutinee), vec![then_arm, else_arm].into(), desugar)
324     }
325
326     fn lower_expr_while_in_loop_scope(
327         &mut self,
328         span: Span,
329         cond: &Expr,
330         body: &Block,
331         opt_label: Option<Label>
332     ) -> hir::ExprKind {
333         // FIXME(#53667): handle lowering of && and parens.
334
335         // Note that the block AND the condition are evaluated in the loop scope.
336         // This is done to allow `break` from inside the condition of the loop.
337
338         // `_ => break`:
339         let else_arm = {
340             let else_pat = self.pat_wild(span);
341             let else_expr = self.expr_break(span, ThinVec::new());
342             self.arm(hir_vec![else_pat], else_expr)
343         };
344
345         // Handle then + scrutinee:
346         let then_blk = self.lower_block(body, false);
347         let then_expr = self.expr_block(then_blk, ThinVec::new());
348         let (then_pats, scrutinee, desugar, source) = match cond.node {
349             ExprKind::Let(ref pats, ref scrutinee) => {
350                 // to:
351                 //
352                 //   [opt_ident]: loop {
353                 //     match <sub_expr> {
354                 //       <pat> => <body>,
355                 //       _ => break
356                 //     }
357                 //   }
358                 let scrutinee = self.with_loop_condition_scope(|t| t.lower_expr(scrutinee));
359                 let pats = pats.iter().map(|pat| self.lower_pat(pat)).collect();
360                 let desugar = hir::MatchSource::WhileLetDesugar;
361                 (pats, scrutinee, desugar, hir::LoopSource::WhileLet)
362             }
363             _ => {
364                 // We desugar: `'label: while $cond $body` into:
365                 //
366                 // ```
367                 // 'label: loop {
368                 //     match DropTemps($cond) {
369                 //         true => $body,
370                 //         _ => break,
371                 //     }
372                 // }
373                 // ```
374
375                 // Lower condition:
376                 let cond = self.with_loop_condition_scope(|this| this.lower_expr(cond));
377                 let span_block = self.mark_span_with_reason(
378                     DesugaringKind::CondTemporary,
379                     cond.span,
380                     None,
381                 );
382                 // Wrap in a construct equivalent to `{ let _t = $cond; _t }`
383                 // to preserve drop semantics since `while cond { ... }` does not
384                 // let temporaries live outside of `cond`.
385                 let cond = self.expr_drop_temps(span_block, P(cond), ThinVec::new());
386
387                 let desugar = hir::MatchSource::WhileDesugar;
388                 // `true => <then>`:
389                 let pats = hir_vec![self.pat_bool(span, true)];
390                 (pats, cond, desugar, hir::LoopSource::While)
391             }
392         };
393         let then_arm = self.arm(then_pats, P(then_expr));
394
395         // `match <scrutinee> { ... }`
396         let match_expr = self.expr_match(
397             scrutinee.span,
398             P(scrutinee),
399             hir_vec![then_arm, else_arm],
400             desugar,
401         );
402
403         // `[opt_ident]: loop { ... }`
404         hir::ExprKind::Loop(
405             P(self.block_expr(P(match_expr))),
406             self.lower_label(opt_label),
407             source
408         )
409     }
410
411     fn lower_expr_try_block(&mut self, body: &Block) -> hir::ExprKind {
412         self.with_catch_scope(body.id, |this| {
413             let unstable_span = this.mark_span_with_reason(
414                 DesugaringKind::TryBlock,
415                 body.span,
416                 this.allow_try_trait.clone(),
417             );
418             let mut block = this.lower_block(body, true).into_inner();
419             let tail = block.expr.take().map_or_else(
420                 || this.expr_unit(this.sess.source_map().end_point(unstable_span)),
421                 |x: P<hir::Expr>| x.into_inner(),
422             );
423             block.expr = Some(this.wrap_in_try_constructor(sym::from_ok, tail, unstable_span));
424             hir::ExprKind::Block(P(block), None)
425         })
426     }
427
428     fn wrap_in_try_constructor(
429         &mut self,
430         method: Symbol,
431         e: hir::Expr,
432         unstable_span: Span,
433     ) -> P<hir::Expr> {
434         let path = &[sym::ops, sym::Try, method];
435         let from_err = P(self.expr_std_path(unstable_span, path, None, ThinVec::new()));
436         P(self.expr_call(e.span, from_err, hir_vec![e]))
437     }
438
439     fn lower_arm(&mut self, arm: &Arm) -> hir::Arm {
440         hir::Arm {
441             hir_id: self.next_id(),
442             attrs: self.lower_attrs(&arm.attrs),
443             pats: arm.pats.iter().map(|x| self.lower_pat(x)).collect(),
444             guard: match arm.guard {
445                 Some(ref x) => Some(hir::Guard::If(P(self.lower_expr(x)))),
446                 _ => None,
447             },
448             body: P(self.lower_expr(&arm.body)),
449             span: arm.span,
450         }
451     }
452
453     pub(super) fn make_async_expr(
454         &mut self,
455         capture_clause: CaptureBy,
456         closure_node_id: NodeId,
457         ret_ty: Option<AstP<Ty>>,
458         span: Span,
459         body: impl FnOnce(&mut LoweringContext<'_>) -> hir::Expr,
460     ) -> hir::ExprKind {
461         let capture_clause = self.lower_capture_clause(capture_clause);
462         let output = match ret_ty {
463             Some(ty) => FunctionRetTy::Ty(ty),
464             None => FunctionRetTy::Default(span),
465         };
466         let ast_decl = FnDecl {
467             inputs: vec![],
468             output,
469             c_variadic: false
470         };
471         let decl = self.lower_fn_decl(&ast_decl, None, /* impl trait allowed */ false, None);
472         let body_id = self.lower_fn_body(&ast_decl, |this| {
473             this.generator_kind = Some(hir::GeneratorKind::Async);
474             body(this)
475         });
476
477         // `static || -> <ret_ty> { body }`:
478         let generator_node = hir::ExprKind::Closure(
479             capture_clause,
480             decl,
481             body_id,
482             span,
483             Some(hir::GeneratorMovability::Static)
484         );
485         let generator = hir::Expr {
486             hir_id: self.lower_node_id(closure_node_id),
487             node: generator_node,
488             span,
489             attrs: ThinVec::new(),
490         };
491
492         // `future::from_generator`:
493         let unstable_span = self.mark_span_with_reason(
494             DesugaringKind::Async,
495             span,
496             self.allow_gen_future.clone(),
497         );
498         let gen_future = self.expr_std_path(
499             unstable_span,
500             &[sym::future, sym::from_generator],
501             None,
502             ThinVec::new()
503         );
504
505         // `future::from_generator(generator)`:
506         hir::ExprKind::Call(P(gen_future), hir_vec![generator])
507     }
508
509     /// Desugar `<expr>.await` into:
510     /// ```rust
511     /// {
512     ///     let mut pinned = <expr>;
513     ///     loop {
514     ///         match ::std::future::poll_with_tls_context(unsafe {
515     ///             ::std::pin::Pin::new_unchecked(&mut pinned)
516     ///         }) {
517     ///             ::std::task::Poll::Ready(result) => break result,
518     ///             ::std::task::Poll::Pending => {},
519     ///         }
520     ///         yield ();
521     ///     }
522     /// }
523     /// ```
524     fn lower_expr_await(&mut self, await_span: Span, expr: &Expr) -> hir::ExprKind {
525         match self.generator_kind {
526             Some(hir::GeneratorKind::Async) => {},
527             Some(hir::GeneratorKind::Gen) |
528             None => {
529                 let mut err = struct_span_err!(
530                     self.sess,
531                     await_span,
532                     E0728,
533                     "`await` is only allowed inside `async` functions and blocks"
534                 );
535                 err.span_label(await_span, "only allowed inside `async` functions and blocks");
536                 if let Some(item_sp) = self.current_item {
537                     err.span_label(item_sp, "this is not `async`");
538                 }
539                 err.emit();
540             }
541         }
542         let span = self.mark_span_with_reason(
543             DesugaringKind::Await,
544             await_span,
545             None,
546         );
547         let gen_future_span = self.mark_span_with_reason(
548             DesugaringKind::Await,
549             await_span,
550             self.allow_gen_future.clone(),
551         );
552
553         // let mut pinned = <expr>;
554         let expr = P(self.lower_expr(expr));
555         let pinned_ident = Ident::with_dummy_span(sym::pinned);
556         let (pinned_pat, pinned_pat_hid) = self.pat_ident_binding_mode(
557             span,
558             pinned_ident,
559             hir::BindingAnnotation::Mutable,
560         );
561         let pinned_let = self.stmt_let_pat(
562             ThinVec::new(),
563             span,
564             Some(expr),
565             pinned_pat,
566             hir::LocalSource::AwaitDesugar,
567         );
568
569         // ::std::future::poll_with_tls_context(unsafe {
570         //     ::std::pin::Pin::new_unchecked(&mut pinned)
571         // })`
572         let poll_expr = {
573             let pinned = P(self.expr_ident(span, pinned_ident, pinned_pat_hid));
574             let ref_mut_pinned = self.expr_mut_addr_of(span, pinned);
575             let pin_ty_id = self.next_id();
576             let new_unchecked_expr_kind = self.expr_call_std_assoc_fn(
577                 pin_ty_id,
578                 span,
579                 &[sym::pin, sym::Pin],
580                 "new_unchecked",
581                 hir_vec![ref_mut_pinned],
582             );
583             let new_unchecked = P(self.expr(span, new_unchecked_expr_kind, ThinVec::new()));
584             let unsafe_expr = self.expr_unsafe(new_unchecked);
585             P(self.expr_call_std_path(
586                 gen_future_span,
587                 &[sym::future, sym::poll_with_tls_context],
588                 hir_vec![unsafe_expr],
589             ))
590         };
591
592         // `::std::task::Poll::Ready(result) => break result`
593         let loop_node_id = self.sess.next_node_id();
594         let loop_hir_id = self.lower_node_id(loop_node_id);
595         let ready_arm = {
596             let x_ident = Ident::with_dummy_span(sym::result);
597             let (x_pat, x_pat_hid) = self.pat_ident(span, x_ident);
598             let x_expr = P(self.expr_ident(span, x_ident, x_pat_hid));
599             let ready_pat = self.pat_std_enum(
600                 span,
601                 &[sym::task, sym::Poll, sym::Ready],
602                 hir_vec![x_pat],
603             );
604             let break_x = self.with_loop_scope(loop_node_id, |this| {
605                 let expr_break = hir::ExprKind::Break(
606                     this.lower_loop_destination(None),
607                     Some(x_expr),
608                 );
609                 P(this.expr(await_span, expr_break, ThinVec::new()))
610             });
611             self.arm(hir_vec![ready_pat], break_x)
612         };
613
614         // `::std::task::Poll::Pending => {}`
615         let pending_arm = {
616             let pending_pat = self.pat_std_enum(
617                 span,
618                 &[sym::task, sym::Poll, sym::Pending],
619                 hir_vec![],
620             );
621             let empty_block = P(self.expr_block_empty(span));
622             self.arm(hir_vec![pending_pat], empty_block)
623         };
624
625         let match_stmt = {
626             let match_expr = self.expr_match(
627                 span,
628                 poll_expr,
629                 hir_vec![ready_arm, pending_arm],
630                 hir::MatchSource::AwaitDesugar,
631             );
632             self.stmt_expr(span, match_expr)
633         };
634
635         let yield_stmt = {
636             let unit = self.expr_unit(span);
637             let yield_expr = self.expr(
638                 span,
639                 hir::ExprKind::Yield(P(unit), hir::YieldSource::Await),
640                 ThinVec::new(),
641             );
642             self.stmt_expr(span, yield_expr)
643         };
644
645         let loop_block = P(self.block_all(
646             span,
647             hir_vec![match_stmt, yield_stmt],
648             None,
649         ));
650
651         let loop_expr = P(hir::Expr {
652             hir_id: loop_hir_id,
653             node: hir::ExprKind::Loop(
654                 loop_block,
655                 None,
656                 hir::LoopSource::Loop,
657             ),
658             span,
659             attrs: ThinVec::new(),
660         });
661
662         hir::ExprKind::Block(
663             P(self.block_all(span, hir_vec![pinned_let], Some(loop_expr))),
664             None,
665         )
666     }
667
668     fn lower_expr_closure(
669         &mut self,
670         capture_clause: CaptureBy,
671         movability: Movability,
672         decl: &FnDecl,
673         body: &Expr,
674         fn_decl_span: Span,
675     ) -> hir::ExprKind {
676         // Lower outside new scope to preserve `is_in_loop_condition`.
677         let fn_decl = self.lower_fn_decl(decl, None, false, None);
678
679         self.with_new_scopes(|this| {
680             let prev = this.current_item;
681             this.current_item = Some(fn_decl_span);
682             let mut generator_kind = None;
683             let body_id = this.lower_fn_body(decl, |this| {
684                 let e = this.lower_expr(body);
685                 generator_kind = this.generator_kind;
686                 e
687             });
688             let generator_option = this.generator_movability_for_fn(
689                 &decl,
690                 fn_decl_span,
691                 generator_kind,
692                 movability,
693             );
694             let capture_clause = this.lower_capture_clause(capture_clause);
695             this.current_item = prev;
696             hir::ExprKind::Closure(
697                 capture_clause,
698                 fn_decl,
699                 body_id,
700                 fn_decl_span,
701                 generator_option,
702             )
703         })
704     }
705
706     fn lower_capture_clause(&mut self, c: CaptureBy) -> hir::CaptureClause {
707         match c {
708             CaptureBy::Value => hir::CaptureByValue,
709             CaptureBy::Ref => hir::CaptureByRef,
710         }
711     }
712
713     fn generator_movability_for_fn(
714         &mut self,
715         decl: &FnDecl,
716         fn_decl_span: Span,
717         generator_kind: Option<hir::GeneratorKind>,
718         movability: Movability,
719     ) -> Option<hir::GeneratorMovability> {
720         match generator_kind {
721             Some(hir::GeneratorKind::Gen) =>  {
722                 if !decl.inputs.is_empty() {
723                     span_err!(
724                         self.sess,
725                         fn_decl_span,
726                         E0628,
727                         "generators cannot have explicit arguments"
728                     );
729                     self.sess.abort_if_errors();
730                 }
731                 Some(match movability {
732                     Movability::Movable => hir::GeneratorMovability::Movable,
733                     Movability::Static => hir::GeneratorMovability::Static,
734                 })
735             },
736             Some(hir::GeneratorKind::Async) => {
737                 bug!("non-`async` closure body turned `async` during lowering");
738             },
739             None => {
740                 if movability == Movability::Static {
741                     span_err!(
742                         self.sess,
743                         fn_decl_span,
744                         E0697,
745                         "closures cannot be static"
746                     );
747                 }
748                 None
749             },
750         }
751     }
752
753     fn lower_expr_async_closure(
754         &mut self,
755         capture_clause: CaptureBy,
756         closure_id: NodeId,
757         decl: &FnDecl,
758         body: &Expr,
759         fn_decl_span: Span,
760     ) -> hir::ExprKind {
761         let outer_decl = FnDecl {
762             inputs: decl.inputs.clone(),
763             output: FunctionRetTy::Default(fn_decl_span),
764             c_variadic: false,
765         };
766         // We need to lower the declaration outside the new scope, because we
767         // have to conserve the state of being inside a loop condition for the
768         // closure argument types.
769         let fn_decl = self.lower_fn_decl(&outer_decl, None, false, None);
770
771         self.with_new_scopes(|this| {
772             // FIXME(cramertj): allow `async` non-`move` closures with arguments.
773             if capture_clause == CaptureBy::Ref && !decl.inputs.is_empty() {
774                 struct_span_err!(
775                     this.sess,
776                     fn_decl_span,
777                     E0708,
778                     "`async` non-`move` closures with arguments are not currently supported",
779                 )
780                 .help(
781                     "consider using `let` statements to manually capture \
782                     variables by reference before entering an `async move` closure"
783                 )
784                 .emit();
785             }
786
787             // Transform `async |x: u8| -> X { ... }` into
788             // `|x: u8| future_from_generator(|| -> X { ... })`.
789             let body_id = this.lower_fn_body(&outer_decl, |this| {
790                 let async_ret_ty = if let FunctionRetTy::Ty(ty) = &decl.output {
791                     Some(ty.clone())
792                 } else {
793                     None
794                 };
795                 let async_body = this.make_async_expr(
796                     capture_clause, closure_id, async_ret_ty, body.span,
797                     |this| {
798                         this.with_new_scopes(|this| this.lower_expr(body))
799                     }
800                 );
801                 this.expr(fn_decl_span, async_body, ThinVec::new())
802             });
803             hir::ExprKind::Closure(
804                 this.lower_capture_clause(capture_clause),
805                 fn_decl,
806                 body_id,
807                 fn_decl_span,
808                 None,
809             )
810         })
811     }
812
813     /// Desugar `<start>..=<end>` into `std::ops::RangeInclusive::new(<start>, <end>)`.
814     fn lower_expr_range_closed(&mut self, span: Span, e1: &Expr, e2: &Expr) -> hir::ExprKind {
815         let id = self.next_id();
816         let e1 = self.lower_expr(e1);
817         let e2 = self.lower_expr(e2);
818         self.expr_call_std_assoc_fn(
819             id,
820             span,
821             &[sym::ops, sym::RangeInclusive],
822             "new",
823             hir_vec![e1, e2],
824         )
825     }
826
827     fn lower_expr_range(
828         &mut self,
829         span: Span,
830         e1: Option<&Expr>,
831         e2: Option<&Expr>,
832         lims: RangeLimits,
833     ) -> hir::ExprKind {
834         use syntax::ast::RangeLimits::*;
835
836         let path = match (e1, e2, lims) {
837             (None, None, HalfOpen) => sym::RangeFull,
838             (Some(..), None, HalfOpen) => sym::RangeFrom,
839             (None, Some(..), HalfOpen) => sym::RangeTo,
840             (Some(..), Some(..), HalfOpen) => sym::Range,
841             (None, Some(..), Closed) => sym::RangeToInclusive,
842             (Some(..), Some(..), Closed) => unreachable!(),
843             (_, None, Closed) => self.diagnostic()
844                 .span_fatal(span, "inclusive range with no end")
845                 .raise(),
846         };
847
848         let fields = e1.iter()
849             .map(|e| ("start", e))
850             .chain(e2.iter().map(|e| ("end", e)))
851             .map(|(s, e)| {
852                 let expr = P(self.lower_expr(&e));
853                 let ident = Ident::new(Symbol::intern(s), e.span);
854                 self.field(ident, expr, e.span)
855             })
856             .collect::<P<[hir::Field]>>();
857
858         let is_unit = fields.is_empty();
859         let struct_path = [sym::ops, path];
860         let struct_path = self.std_path(span, &struct_path, None, is_unit);
861         let struct_path = hir::QPath::Resolved(None, P(struct_path));
862
863         if is_unit {
864             hir::ExprKind::Path(struct_path)
865         } else {
866             hir::ExprKind::Struct(P(struct_path), fields, None)
867         }
868     }
869
870     fn lower_label(&mut self, label: Option<Label>) -> Option<hir::Label> {
871         label.map(|label| hir::Label {
872             ident: label.ident,
873         })
874     }
875
876     fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
877         let target_id = match destination {
878             Some((id, _)) => {
879                 if let Some(loop_id) = self.resolver.get_label_res(id) {
880                     Ok(self.lower_node_id(loop_id))
881                 } else {
882                     Err(hir::LoopIdError::UnresolvedLabel)
883                 }
884             }
885             None => {
886                 self.loop_scopes
887                     .last()
888                     .cloned()
889                     .map(|id| Ok(self.lower_node_id(id)))
890                     .unwrap_or(Err(hir::LoopIdError::OutsideLoopScope))
891                     .into()
892             }
893         };
894         hir::Destination {
895             label: self.lower_label(destination.map(|(_, label)| label)),
896             target_id,
897         }
898     }
899
900     fn lower_jump_destination(&mut self, id: NodeId, opt_label: Option<Label>) -> hir::Destination {
901         if self.is_in_loop_condition && opt_label.is_none() {
902             hir::Destination {
903                 label: None,
904                 target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
905             }
906         } else {
907             self.lower_loop_destination(opt_label.map(|label| (id, label)))
908         }
909     }
910
911     fn with_catch_scope<T, F>(&mut self, catch_id: NodeId, f: F) -> T
912     where
913         F: FnOnce(&mut LoweringContext<'_>) -> T,
914     {
915         let len = self.catch_scopes.len();
916         self.catch_scopes.push(catch_id);
917
918         let result = f(self);
919         assert_eq!(
920             len + 1,
921             self.catch_scopes.len(),
922             "catch scopes should be added and removed in stack order"
923         );
924
925         self.catch_scopes.pop().unwrap();
926
927         result
928     }
929
930     fn with_loop_scope<T, F>(&mut self, loop_id: NodeId, f: F) -> T
931     where
932         F: FnOnce(&mut LoweringContext<'_>) -> T,
933     {
934         // We're no longer in the base loop's condition; we're in another loop.
935         let was_in_loop_condition = self.is_in_loop_condition;
936         self.is_in_loop_condition = false;
937
938         let len = self.loop_scopes.len();
939         self.loop_scopes.push(loop_id);
940
941         let result = f(self);
942         assert_eq!(
943             len + 1,
944             self.loop_scopes.len(),
945             "loop scopes should be added and removed in stack order"
946         );
947
948         self.loop_scopes.pop().unwrap();
949
950         self.is_in_loop_condition = was_in_loop_condition;
951
952         result
953     }
954
955     fn with_loop_condition_scope<T, F>(&mut self, f: F) -> T
956     where
957         F: FnOnce(&mut LoweringContext<'_>) -> T,
958     {
959         let was_in_loop_condition = self.is_in_loop_condition;
960         self.is_in_loop_condition = true;
961
962         let result = f(self);
963
964         self.is_in_loop_condition = was_in_loop_condition;
965
966         result
967     }
968
969     fn lower_expr_asm(&mut self, asm: &InlineAsm) -> hir::ExprKind {
970         let hir_asm = hir::InlineAsm {
971             inputs: asm.inputs.iter().map(|&(ref c, _)| c.clone()).collect(),
972             outputs: asm.outputs
973                 .iter()
974                 .map(|out| hir::InlineAsmOutput {
975                     constraint: out.constraint.clone(),
976                     is_rw: out.is_rw,
977                     is_indirect: out.is_indirect,
978                     span: out.expr.span,
979                 })
980                 .collect(),
981             asm: asm.asm.clone(),
982             asm_str_style: asm.asm_str_style,
983             clobbers: asm.clobbers.clone().into(),
984             volatile: asm.volatile,
985             alignstack: asm.alignstack,
986             dialect: asm.dialect,
987             ctxt: asm.ctxt,
988         };
989
990         let outputs = asm.outputs
991             .iter()
992             .map(|out| self.lower_expr(&out.expr))
993             .collect();
994
995         let inputs = asm.inputs
996             .iter()
997             .map(|&(_, ref input)| self.lower_expr(input))
998             .collect();
999
1000         hir::ExprKind::InlineAsm(P(hir_asm), outputs, inputs)
1001     }
1002
1003     fn lower_field(&mut self, f: &Field) -> hir::Field {
1004         hir::Field {
1005             hir_id: self.next_id(),
1006             ident: f.ident,
1007             expr: P(self.lower_expr(&f.expr)),
1008             span: f.span,
1009             is_shorthand: f.is_shorthand,
1010         }
1011     }
1012
1013     fn lower_expr_yield(&mut self, span: Span, opt_expr: Option<&Expr>) -> hir::ExprKind {
1014         match self.generator_kind {
1015             Some(hir::GeneratorKind::Gen) => {},
1016             Some(hir::GeneratorKind::Async) => {
1017                 span_err!(
1018                     self.sess,
1019                     span,
1020                     E0727,
1021                     "`async` generators are not yet supported",
1022                 );
1023                 self.sess.abort_if_errors();
1024             },
1025             None => self.generator_kind = Some(hir::GeneratorKind::Gen),
1026         }
1027
1028         let expr = opt_expr
1029             .as_ref()
1030             .map(|x| self.lower_expr(x))
1031             .unwrap_or_else(|| self.expr_unit(span));
1032
1033         hir::ExprKind::Yield(P(expr), hir::YieldSource::Yield)
1034     }
1035
1036     /// Desugar `ExprForLoop` from: `[opt_ident]: for <pat> in <head> <body>` into:
1037     /// ```rust
1038     /// {
1039     ///     let result = match ::std::iter::IntoIterator::into_iter(<head>) {
1040     ///         mut iter => {
1041     ///             [opt_ident]: loop {
1042     ///                 let mut __next;
1043     ///                 match ::std::iter::Iterator::next(&mut iter) {
1044     ///                     ::std::option::Option::Some(val) => __next = val,
1045     ///                     ::std::option::Option::None => break
1046     ///                 };
1047     ///                 let <pat> = __next;
1048     ///                 StmtKind::Expr(<body>);
1049     ///             }
1050     ///         }
1051     ///     };
1052     ///     result
1053     /// }
1054     /// ```
1055     fn lower_expr_for(
1056         &mut self,
1057         e: &Expr,
1058         pat: &Pat,
1059         head: &Expr,
1060         body: &Block,
1061         opt_label: Option<Label>,
1062     ) -> hir::Expr {
1063         // expand <head>
1064         let mut head = self.lower_expr(head);
1065         let head_sp = head.span;
1066         let desugared_span = self.mark_span_with_reason(
1067             DesugaringKind::ForLoop,
1068             head_sp,
1069             None,
1070         );
1071         head.span = desugared_span;
1072
1073         let iter = Ident::with_dummy_span(sym::iter);
1074
1075         let next_ident = Ident::with_dummy_span(sym::__next);
1076         let (next_pat, next_pat_hid) = self.pat_ident_binding_mode(
1077             desugared_span,
1078             next_ident,
1079             hir::BindingAnnotation::Mutable,
1080         );
1081
1082         // `::std::option::Option::Some(val) => __next = val`
1083         let pat_arm = {
1084             let val_ident = Ident::with_dummy_span(sym::val);
1085             let (val_pat, val_pat_hid) = self.pat_ident(pat.span, val_ident);
1086             let val_expr = P(self.expr_ident(pat.span, val_ident, val_pat_hid));
1087             let next_expr = P(self.expr_ident(pat.span, next_ident, next_pat_hid));
1088             let assign = P(self.expr(
1089                 pat.span,
1090                 hir::ExprKind::Assign(next_expr, val_expr),
1091                 ThinVec::new(),
1092             ));
1093             let some_pat = self.pat_some(pat.span, val_pat);
1094             self.arm(hir_vec![some_pat], assign)
1095         };
1096
1097         // `::std::option::Option::None => break`
1098         let break_arm = {
1099             let break_expr =
1100                 self.with_loop_scope(e.id, |this| this.expr_break(e.span, ThinVec::new()));
1101             let pat = self.pat_none(e.span);
1102             self.arm(hir_vec![pat], break_expr)
1103         };
1104
1105         // `mut iter`
1106         let (iter_pat, iter_pat_nid) = self.pat_ident_binding_mode(
1107             desugared_span,
1108             iter,
1109             hir::BindingAnnotation::Mutable
1110         );
1111
1112         // `match ::std::iter::Iterator::next(&mut iter) { ... }`
1113         let match_expr = {
1114             let iter = P(self.expr_ident(head_sp, iter, iter_pat_nid));
1115             let ref_mut_iter = self.expr_mut_addr_of(head_sp, iter);
1116             let next_path = &[sym::iter, sym::Iterator, sym::next];
1117             let next_expr = P(self.expr_call_std_path(
1118                 head_sp,
1119                 next_path,
1120                 hir_vec![ref_mut_iter],
1121             ));
1122             let arms = hir_vec![pat_arm, break_arm];
1123
1124             self.expr_match(head_sp, next_expr, arms, hir::MatchSource::ForLoopDesugar)
1125         };
1126         let match_stmt = self.stmt_expr(head_sp, match_expr);
1127
1128         let next_expr = P(self.expr_ident(head_sp, next_ident, next_pat_hid));
1129
1130         // `let mut __next`
1131         let next_let = self.stmt_let_pat(
1132             ThinVec::new(),
1133             desugared_span,
1134             None,
1135             next_pat,
1136             hir::LocalSource::ForLoopDesugar,
1137         );
1138
1139         // `let <pat> = __next`
1140         let pat = self.lower_pat(pat);
1141         let pat_let = self.stmt_let_pat(
1142             ThinVec::new(),
1143             head_sp,
1144             Some(next_expr),
1145             pat,
1146             hir::LocalSource::ForLoopDesugar,
1147         );
1148
1149         let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body, false));
1150         let body_expr = self.expr_block(body_block, ThinVec::new());
1151         let body_stmt = self.stmt_expr(body.span, body_expr);
1152
1153         let loop_block = P(self.block_all(
1154             e.span,
1155             hir_vec![next_let, match_stmt, pat_let, body_stmt],
1156             None,
1157         ));
1158
1159         // `[opt_ident]: loop { ... }`
1160         let loop_expr = hir::ExprKind::Loop(
1161             loop_block,
1162             self.lower_label(opt_label),
1163             hir::LoopSource::ForLoop,
1164         );
1165         let loop_expr = P(hir::Expr {
1166             hir_id: self.lower_node_id(e.id),
1167             node: loop_expr,
1168             span: e.span,
1169             attrs: ThinVec::new(),
1170         });
1171
1172         // `mut iter => { ... }`
1173         let iter_arm = self.arm(hir_vec![iter_pat], loop_expr);
1174
1175         // `match ::std::iter::IntoIterator::into_iter(<head>) { ... }`
1176         let into_iter_expr = {
1177             let into_iter_path =
1178                 &[sym::iter, sym::IntoIterator, sym::into_iter];
1179             P(self.expr_call_std_path(
1180                 head_sp,
1181                 into_iter_path,
1182                 hir_vec![head],
1183             ))
1184         };
1185
1186         let match_expr = P(self.expr_match(
1187             head_sp,
1188             into_iter_expr,
1189             hir_vec![iter_arm],
1190             hir::MatchSource::ForLoopDesugar,
1191         ));
1192
1193         // This is effectively `{ let _result = ...; _result }`.
1194         // The construct was introduced in #21984 and is necessary to make sure that
1195         // temporaries in the `head` expression are dropped and do not leak to the
1196         // surrounding scope of the `match` since the `match` is not a terminating scope.
1197         //
1198         // Also, add the attributes to the outer returned expr node.
1199         self.expr_drop_temps(head_sp, match_expr, e.attrs.clone())
1200     }
1201
1202     /// Desugar `ExprKind::Try` from: `<expr>?` into:
1203     /// ```rust
1204     /// match Try::into_result(<expr>) {
1205     ///     Ok(val) => #[allow(unreachable_code)] val,
1206     ///     Err(err) => #[allow(unreachable_code)]
1207     ///                 // If there is an enclosing `try {...}`:
1208     ///                 break 'catch_target Try::from_error(From::from(err)),
1209     ///                 // Otherwise:
1210     ///                 return Try::from_error(From::from(err)),
1211     /// }
1212     /// ```
1213     fn lower_expr_try(&mut self, span: Span, sub_expr: &Expr) -> hir::ExprKind {
1214         let unstable_span = self.mark_span_with_reason(
1215             DesugaringKind::QuestionMark,
1216             span,
1217             self.allow_try_trait.clone(),
1218         );
1219         let try_span = self.sess.source_map().end_point(span);
1220         let try_span = self.mark_span_with_reason(
1221             DesugaringKind::QuestionMark,
1222             try_span,
1223             self.allow_try_trait.clone(),
1224         );
1225
1226         // `Try::into_result(<expr>)`
1227         let scrutinee = {
1228             // expand <expr>
1229             let sub_expr = self.lower_expr(sub_expr);
1230
1231             let path = &[sym::ops, sym::Try, sym::into_result];
1232             P(self.expr_call_std_path(unstable_span, path, hir_vec![sub_expr]))
1233         };
1234
1235         // `#[allow(unreachable_code)]`
1236         let attr = {
1237             // `allow(unreachable_code)`
1238             let allow = {
1239                 let allow_ident = Ident::new(sym::allow, span);
1240                 let uc_ident = Ident::new(sym::unreachable_code, span);
1241                 let uc_nested = attr::mk_nested_word_item(uc_ident);
1242                 attr::mk_list_item(allow_ident, vec![uc_nested])
1243             };
1244             attr::mk_attr_outer(allow)
1245         };
1246         let attrs = vec![attr];
1247
1248         // `Ok(val) => #[allow(unreachable_code)] val,`
1249         let ok_arm = {
1250             let val_ident = Ident::with_dummy_span(sym::val);
1251             let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
1252             let val_expr = P(self.expr_ident_with_attrs(
1253                 span,
1254                 val_ident,
1255                 val_pat_nid,
1256                 ThinVec::from(attrs.clone()),
1257             ));
1258             let ok_pat = self.pat_ok(span, val_pat);
1259
1260             self.arm(hir_vec![ok_pat], val_expr)
1261         };
1262
1263         // `Err(err) => #[allow(unreachable_code)]
1264         //              return Try::from_error(From::from(err)),`
1265         let err_arm = {
1266             let err_ident = Ident::with_dummy_span(sym::err);
1267             let (err_local, err_local_nid) = self.pat_ident(try_span, err_ident);
1268             let from_expr = {
1269                 let from_path = &[sym::convert, sym::From, sym::from];
1270                 let err_expr = self.expr_ident(try_span, err_ident, err_local_nid);
1271                 self.expr_call_std_path(try_span, from_path, hir_vec![err_expr])
1272             };
1273             let from_err_expr =
1274                 self.wrap_in_try_constructor(sym::from_error, from_expr, unstable_span);
1275             let thin_attrs = ThinVec::from(attrs);
1276             let catch_scope = self.catch_scopes.last().map(|x| *x);
1277             let ret_expr = if let Some(catch_node) = catch_scope {
1278                 let target_id = Ok(self.lower_node_id(catch_node));
1279                 P(self.expr(
1280                     try_span,
1281                     hir::ExprKind::Break(
1282                         hir::Destination {
1283                             label: None,
1284                             target_id,
1285                         },
1286                         Some(from_err_expr),
1287                     ),
1288                     thin_attrs,
1289                 ))
1290             } else {
1291                 P(self.expr(try_span, hir::ExprKind::Ret(Some(from_err_expr)), thin_attrs))
1292             };
1293
1294             let err_pat = self.pat_err(try_span, err_local);
1295             self.arm(hir_vec![err_pat], ret_expr)
1296         };
1297
1298         hir::ExprKind::Match(
1299             scrutinee,
1300             hir_vec![err_arm, ok_arm],
1301             hir::MatchSource::TryDesugar,
1302         )
1303     }
1304
1305     // =========================================================================
1306     // Helper methods for building HIR.
1307     // =========================================================================
1308
1309     /// Constructs a `true` or `false` literal expression.
1310     pub(super) fn expr_bool(&mut self, span: Span, val: bool) -> hir::Expr {
1311         let lit = Spanned { span, node: LitKind::Bool(val) };
1312         self.expr(span, hir::ExprKind::Lit(lit), ThinVec::new())
1313     }
1314
1315     /// Wrap the given `expr` in a terminating scope using `hir::ExprKind::DropTemps`.
1316     ///
1317     /// In terms of drop order, it has the same effect as wrapping `expr` in
1318     /// `{ let _t = $expr; _t }` but should provide better compile-time performance.
1319     ///
1320     /// The drop order can be important in e.g. `if expr { .. }`.
1321     fn expr_drop_temps(
1322         &mut self,
1323         span: Span,
1324         expr: P<hir::Expr>,
1325         attrs: ThinVec<Attribute>
1326     ) -> hir::Expr {
1327         self.expr(span, hir::ExprKind::DropTemps(expr), attrs)
1328     }
1329
1330     fn expr_match(
1331         &mut self,
1332         span: Span,
1333         arg: P<hir::Expr>,
1334         arms: hir::HirVec<hir::Arm>,
1335         source: hir::MatchSource,
1336     ) -> hir::Expr {
1337         self.expr(span, hir::ExprKind::Match(arg, arms, source), ThinVec::new())
1338     }
1339
1340     fn expr_break(&mut self, span: Span, attrs: ThinVec<Attribute>) -> P<hir::Expr> {
1341         let expr_break = hir::ExprKind::Break(self.lower_loop_destination(None), None);
1342         P(self.expr(span, expr_break, attrs))
1343     }
1344
1345     fn expr_mut_addr_of(&mut self, span: Span, e: P<hir::Expr>) -> hir::Expr {
1346         self.expr(span, hir::ExprKind::AddrOf(hir::MutMutable, e), ThinVec::new())
1347     }
1348
1349     fn expr_unit(&mut self, sp: Span) -> hir::Expr {
1350         self.expr_tuple(sp, hir_vec![])
1351     }
1352
1353     fn expr_tuple(&mut self, sp: Span, exprs: hir::HirVec<hir::Expr>) -> hir::Expr {
1354         self.expr(sp, hir::ExprKind::Tup(exprs), ThinVec::new())
1355     }
1356
1357     fn expr_call(
1358         &mut self,
1359         span: Span,
1360         e: P<hir::Expr>,
1361         args: hir::HirVec<hir::Expr>,
1362     ) -> hir::Expr {
1363         self.expr(span, hir::ExprKind::Call(e, args), ThinVec::new())
1364     }
1365
1366     // Note: associated functions must use `expr_call_std_path`.
1367     fn expr_call_std_path(
1368         &mut self,
1369         span: Span,
1370         path_components: &[Symbol],
1371         args: hir::HirVec<hir::Expr>,
1372     ) -> hir::Expr {
1373         let path = P(self.expr_std_path(span, path_components, None, ThinVec::new()));
1374         self.expr_call(span, path, args)
1375     }
1376
1377     // Create an expression calling an associated function of an std type.
1378     //
1379     // Associated functions cannot be resolved through the normal `std_path` function,
1380     // as they are resolved differently and so cannot use `expr_call_std_path`.
1381     //
1382     // This function accepts the path component (`ty_path_components`) separately from
1383     // the name of the associated function (`assoc_fn_name`) in order to facilitate
1384     // separate resolution of the type and creation of a path referring to its associated
1385     // function.
1386     fn expr_call_std_assoc_fn(
1387         &mut self,
1388         ty_path_id: hir::HirId,
1389         span: Span,
1390         ty_path_components: &[Symbol],
1391         assoc_fn_name: &str,
1392         args: hir::HirVec<hir::Expr>,
1393     ) -> hir::ExprKind {
1394         let ty_path = P(self.std_path(span, ty_path_components, None, false));
1395         let ty = P(self.ty_path(ty_path_id, span, hir::QPath::Resolved(None, ty_path)));
1396         let fn_seg = P(hir::PathSegment::from_ident(Ident::from_str(assoc_fn_name)));
1397         let fn_path = hir::QPath::TypeRelative(ty, fn_seg);
1398         let fn_expr = P(self.expr(span, hir::ExprKind::Path(fn_path), ThinVec::new()));
1399         hir::ExprKind::Call(fn_expr, args)
1400     }
1401
1402     fn expr_std_path(
1403         &mut self,
1404         span: Span,
1405         components: &[Symbol],
1406         params: Option<P<hir::GenericArgs>>,
1407         attrs: ThinVec<Attribute>,
1408     ) -> hir::Expr {
1409         let path = self.std_path(span, components, params, true);
1410         self.expr(
1411             span,
1412             hir::ExprKind::Path(hir::QPath::Resolved(None, P(path))),
1413             attrs,
1414         )
1415     }
1416
1417     pub(super) fn expr_ident(&mut self, sp: Span, ident: Ident, binding: hir::HirId) -> hir::Expr {
1418         self.expr_ident_with_attrs(sp, ident, binding, ThinVec::new())
1419     }
1420
1421     fn expr_ident_with_attrs(
1422         &mut self,
1423         span: Span,
1424         ident: Ident,
1425         binding: hir::HirId,
1426         attrs: ThinVec<Attribute>,
1427     ) -> hir::Expr {
1428         let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
1429             None,
1430             P(hir::Path {
1431                 span,
1432                 res: Res::Local(binding),
1433                 segments: hir_vec![hir::PathSegment::from_ident(ident)],
1434             }),
1435         ));
1436
1437         self.expr(span, expr_path, attrs)
1438     }
1439
1440     fn expr_unsafe(&mut self, expr: P<hir::Expr>) -> hir::Expr {
1441         let hir_id = self.next_id();
1442         let span = expr.span;
1443         self.expr(
1444             span,
1445             hir::ExprKind::Block(P(hir::Block {
1446                 stmts: hir_vec![],
1447                 expr: Some(expr),
1448                 hir_id,
1449                 rules: hir::UnsafeBlock(hir::CompilerGenerated),
1450                 span,
1451                 targeted_by_break: false,
1452             }), None),
1453             ThinVec::new(),
1454         )
1455     }
1456
1457     fn expr_block_empty(&mut self, span: Span) -> hir::Expr {
1458         let blk = self.block_all(span, hir_vec![], None);
1459         self.expr_block(P(blk), ThinVec::new())
1460     }
1461
1462     pub(super) fn expr_block(&mut self, b: P<hir::Block>, attrs: ThinVec<Attribute>) -> hir::Expr {
1463         self.expr(b.span, hir::ExprKind::Block(b, None), attrs)
1464     }
1465
1466     pub(super) fn expr(
1467         &mut self,
1468         span: Span,
1469         node: hir::ExprKind,
1470         attrs: ThinVec<Attribute>
1471     ) -> hir::Expr {
1472         hir::Expr {
1473             hir_id: self.next_id(),
1474             node,
1475             span,
1476             attrs,
1477         }
1478     }
1479
1480     fn field(&mut self, ident: Ident, expr: P<hir::Expr>, span: Span) -> hir::Field {
1481         hir::Field {
1482             hir_id: self.next_id(),
1483             ident,
1484             span,
1485             expr,
1486             is_shorthand: false,
1487         }
1488     }
1489
1490     fn arm(&mut self, pats: hir::HirVec<P<hir::Pat>>, expr: P<hir::Expr>) -> hir::Arm {
1491         hir::Arm {
1492             hir_id: self.next_id(),
1493             attrs: hir_vec![],
1494             pats,
1495             guard: None,
1496             span: expr.span,
1497             body: expr,
1498         }
1499     }
1500 }