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