1 use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
4 use rustc_ast::ptr::P as AstP;
6 use rustc_data_structures::fx::FxHashMap;
7 use rustc_data_structures::stack::ensure_sufficient_stack;
8 use rustc_data_structures::thin_vec::ThinVec;
9 use rustc_errors::struct_span_err;
11 use rustc_hir::def::Res;
12 use rustc_span::hygiene::ForLoopLoc;
13 use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
14 use rustc_span::symbol::{sym, Ident, Symbol};
15 use rustc_target::asm;
16 use std::collections::hash_map::Entry;
19 impl<'hir> LoweringContext<'_, 'hir> {
20 fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> &'hir [hir::Expr<'hir>] {
21 self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
24 pub(super) fn lower_expr(&mut self, e: &Expr) -> &'hir hir::Expr<'hir> {
25 self.arena.alloc(self.lower_expr_mut(e))
28 pub(super) fn lower_expr_mut(&mut self, e: &Expr) -> hir::Expr<'hir> {
29 ensure_sufficient_stack(|| {
30 let kind = match e.kind {
31 ExprKind::Box(ref inner) => hir::ExprKind::Box(self.lower_expr(inner)),
32 ExprKind::Array(ref exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
33 ExprKind::ConstBlock(ref anon_const) => {
34 let anon_const = self.lower_anon_const(anon_const);
35 hir::ExprKind::ConstBlock(anon_const)
37 ExprKind::Repeat(ref expr, ref count) => {
38 let expr = self.lower_expr(expr);
39 let count = self.lower_anon_const(count);
40 hir::ExprKind::Repeat(expr, count)
42 ExprKind::Tup(ref elts) => hir::ExprKind::Tup(self.lower_exprs(elts)),
43 ExprKind::Call(ref f, ref args) => {
44 let f = self.lower_expr(f);
45 hir::ExprKind::Call(f, self.lower_exprs(args))
47 ExprKind::MethodCall(ref seg, ref args, span) => {
48 let hir_seg = self.arena.alloc(self.lower_path_segment(
53 ParenthesizedGenericArgs::Err,
54 ImplTraitContext::disallowed(),
57 let args = self.lower_exprs(args);
58 hir::ExprKind::MethodCall(hir_seg, seg.ident.span, args, span)
60 ExprKind::Binary(binop, ref lhs, ref rhs) => {
61 let binop = self.lower_binop(binop);
62 let lhs = self.lower_expr(lhs);
63 let rhs = self.lower_expr(rhs);
64 hir::ExprKind::Binary(binop, lhs, rhs)
66 ExprKind::Unary(op, ref ohs) => {
67 let op = self.lower_unop(op);
68 let ohs = self.lower_expr(ohs);
69 hir::ExprKind::Unary(op, ohs)
71 ExprKind::Lit(ref l) => hir::ExprKind::Lit(respan(l.span, l.kind.clone())),
72 ExprKind::Cast(ref expr, ref ty) => {
73 let expr = self.lower_expr(expr);
74 let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
75 hir::ExprKind::Cast(expr, ty)
77 ExprKind::Type(ref expr, ref ty) => {
78 let expr = self.lower_expr(expr);
79 let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
80 hir::ExprKind::Type(expr, ty)
82 ExprKind::AddrOf(k, m, ref ohs) => {
83 let ohs = self.lower_expr(ohs);
84 hir::ExprKind::AddrOf(k, m, ohs)
86 ExprKind::Let(ref pat, ref scrutinee) => {
87 self.lower_expr_let(e.span, pat, scrutinee)
89 ExprKind::If(ref cond, ref then, ref else_opt) => {
90 self.lower_expr_if(e.span, cond, then, else_opt.as_deref())
92 ExprKind::While(ref cond, ref body, opt_label) => self
93 .with_loop_scope(e.id, |this| {
94 this.lower_expr_while_in_loop_scope(e.span, cond, body, opt_label)
96 ExprKind::Loop(ref body, opt_label) => self.with_loop_scope(e.id, |this| {
98 this.lower_block(body, false),
100 hir::LoopSource::Loop,
103 ExprKind::TryBlock(ref body) => self.lower_expr_try_block(body),
104 ExprKind::Match(ref expr, ref arms) => hir::ExprKind::Match(
105 self.lower_expr(expr),
106 self.arena.alloc_from_iter(arms.iter().map(|x| self.lower_arm(x))),
107 hir::MatchSource::Normal,
109 ExprKind::Async(capture_clause, closure_node_id, ref block) => self
115 hir::AsyncGeneratorKind::Block,
116 |this| this.with_new_scopes(|this| this.lower_block_expr(block)),
118 ExprKind::Await(ref expr) => self.lower_expr_await(e.span, expr),
127 if let Async::Yes { closure_id, .. } = asyncness {
128 self.lower_expr_async_closure(
136 self.lower_expr_closure(
145 ExprKind::Block(ref blk, opt_label) => {
146 hir::ExprKind::Block(self.lower_block(blk, opt_label.is_some()), opt_label)
148 ExprKind::Assign(ref el, ref er, span) => {
149 hir::ExprKind::Assign(self.lower_expr(el), self.lower_expr(er), span)
151 ExprKind::AssignOp(op, ref el, ref er) => hir::ExprKind::AssignOp(
152 self.lower_binop(op),
156 ExprKind::Field(ref el, ident) => hir::ExprKind::Field(self.lower_expr(el), ident),
157 ExprKind::Index(ref el, ref er) => {
158 hir::ExprKind::Index(self.lower_expr(el), self.lower_expr(er))
160 ExprKind::Range(Some(ref e1), Some(ref e2), RangeLimits::Closed) => {
161 self.lower_expr_range_closed(e.span, e1, e2)
163 ExprKind::Range(ref e1, ref e2, lims) => {
164 self.lower_expr_range(e.span, e1.as_deref(), e2.as_deref(), lims)
166 ExprKind::Path(ref qself, ref path) => {
167 let qpath = self.lower_qpath(
172 ImplTraitContext::disallowed(),
174 hir::ExprKind::Path(qpath)
176 ExprKind::Break(opt_label, ref opt_expr) => {
177 let opt_expr = opt_expr.as_ref().map(|x| self.lower_expr(x));
178 hir::ExprKind::Break(self.lower_jump_destination(e.id, opt_label), opt_expr)
180 ExprKind::Continue(opt_label) => {
181 hir::ExprKind::Continue(self.lower_jump_destination(e.id, opt_label))
183 ExprKind::Ret(ref e) => {
184 let e = e.as_ref().map(|x| self.lower_expr(x));
185 hir::ExprKind::Ret(e)
187 ExprKind::InlineAsm(ref asm) => self.lower_expr_asm(e.span, asm),
188 ExprKind::LlvmInlineAsm(ref asm) => self.lower_expr_llvm_asm(asm),
189 ExprKind::Struct(ref path, ref fields, ref maybe_expr) => {
190 let maybe_expr = maybe_expr.as_ref().map(|x| self.lower_expr(x));
191 hir::ExprKind::Struct(
192 self.arena.alloc(self.lower_qpath(
197 ImplTraitContext::disallowed(),
199 self.arena.alloc_from_iter(fields.iter().map(|x| self.lower_field(x))),
203 ExprKind::Yield(ref opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()),
204 ExprKind::Err => hir::ExprKind::Err,
205 ExprKind::Try(ref sub_expr) => self.lower_expr_try(e.span, sub_expr),
206 ExprKind::Paren(ref ex) => {
207 let mut ex = self.lower_expr_mut(ex);
208 // Include parens in span, but only if it is a super-span.
209 if e.span.contains(ex.span) {
212 // Merge attributes into the inner expression.
213 let mut attrs: Vec<_> = e.attrs.iter().map(|a| self.lower_attr(a)).collect();
214 attrs.extend::<Vec<_>>(ex.attrs.into());
215 ex.attrs = attrs.into();
219 // Desugar `ExprForLoop`
220 // from: `[opt_ident]: for <pat> in <head> <body>`
221 ExprKind::ForLoop(ref pat, ref head, ref body, opt_label) => {
222 return self.lower_expr_for(e, pat, head, body, opt_label);
224 ExprKind::MacCall(_) => panic!("{:?} shouldn't exist here", e.span),
228 hir_id: self.lower_node_id(e.id),
231 attrs: e.attrs.iter().map(|a| self.lower_attr(a)).collect::<Vec<_>>().into(),
236 fn lower_unop(&mut self, u: UnOp) -> hir::UnOp {
238 UnOp::Deref => hir::UnOp::UnDeref,
239 UnOp::Not => hir::UnOp::UnNot,
240 UnOp::Neg => hir::UnOp::UnNeg,
244 fn lower_binop(&mut self, b: BinOp) -> hir::BinOp {
247 BinOpKind::Add => hir::BinOpKind::Add,
248 BinOpKind::Sub => hir::BinOpKind::Sub,
249 BinOpKind::Mul => hir::BinOpKind::Mul,
250 BinOpKind::Div => hir::BinOpKind::Div,
251 BinOpKind::Rem => hir::BinOpKind::Rem,
252 BinOpKind::And => hir::BinOpKind::And,
253 BinOpKind::Or => hir::BinOpKind::Or,
254 BinOpKind::BitXor => hir::BinOpKind::BitXor,
255 BinOpKind::BitAnd => hir::BinOpKind::BitAnd,
256 BinOpKind::BitOr => hir::BinOpKind::BitOr,
257 BinOpKind::Shl => hir::BinOpKind::Shl,
258 BinOpKind::Shr => hir::BinOpKind::Shr,
259 BinOpKind::Eq => hir::BinOpKind::Eq,
260 BinOpKind::Lt => hir::BinOpKind::Lt,
261 BinOpKind::Le => hir::BinOpKind::Le,
262 BinOpKind::Ne => hir::BinOpKind::Ne,
263 BinOpKind::Ge => hir::BinOpKind::Ge,
264 BinOpKind::Gt => hir::BinOpKind::Gt,
270 /// Emit an error and lower `ast::ExprKind::Let(pat, scrutinee)` into:
272 /// match scrutinee { pats => true, _ => false }
274 fn lower_expr_let(&mut self, span: Span, pat: &Pat, scrutinee: &Expr) -> hir::ExprKind<'hir> {
275 // If we got here, the `let` expression is not allowed.
277 if self.sess.opts.unstable_features.is_nightly_build() {
279 .struct_span_err(span, "`let` expressions are not supported here")
280 .note("only supported directly in conditions of `if`- and `while`-expressions")
281 .note("as well as when nested within `&&` and parenthesis in those conditions")
285 .struct_span_err(span, "expected expression, found statement (`let`)")
286 .note("variable declaration using `let` is a statement")
290 // For better recovery, we emit:
292 // match scrutinee { pat => true, _ => false }
294 // While this doesn't fully match the user's intent, it has key advantages:
295 // 1. We can avoid using `abort_if_errors`.
296 // 2. We can typeck both `pat` and `scrutinee`.
297 // 3. `pat` is allowed to be refutable.
298 // 4. The return type of the block is `bool` which seems like what the user wanted.
299 let scrutinee = self.lower_expr(scrutinee);
301 let pat = self.lower_pat(pat);
302 let expr = self.expr_bool(span, true);
306 let pat = self.pat_wild(span);
307 let expr = self.expr_bool(span, false);
310 hir::ExprKind::Match(
312 arena_vec![self; then_arm, else_arm],
313 hir::MatchSource::Normal,
322 else_opt: Option<&Expr>,
323 ) -> hir::ExprKind<'hir> {
324 // FIXME(#53667): handle lowering of && and parens.
326 // `_ => else_block` where `else_block` is `{}` if there's `None`:
327 let else_pat = self.pat_wild(span);
328 let (else_expr, contains_else_clause) = match else_opt {
329 None => (self.expr_block_empty(span), false),
330 Some(els) => (self.lower_expr(els), true),
332 let else_arm = self.arm(else_pat, else_expr);
334 // Handle then + scrutinee:
335 let then_expr = self.lower_block_expr(then);
336 let (then_pat, scrutinee, desugar) = match cond.kind {
337 // `<pat> => <then>`:
338 ExprKind::Let(ref pat, ref scrutinee) => {
339 let scrutinee = self.lower_expr(scrutinee);
340 let pat = self.lower_pat(pat);
341 (pat, scrutinee, hir::MatchSource::IfLetDesugar { contains_else_clause })
346 let cond = self.lower_expr(cond);
348 self.mark_span_with_reason(DesugaringKind::CondTemporary, cond.span, None);
349 // Wrap in a construct equivalent to `{ let _t = $cond; _t }`
350 // to preserve drop semantics since `if cond { ... }` does not
351 // let temporaries live outside of `cond`.
352 let cond = self.expr_drop_temps(span_block, cond, ThinVec::new());
353 let pat = self.pat_bool(span, true);
354 (pat, cond, hir::MatchSource::IfDesugar { contains_else_clause })
357 let then_arm = self.arm(then_pat, self.arena.alloc(then_expr));
359 hir::ExprKind::Match(scrutinee, arena_vec![self; then_arm, else_arm], desugar)
362 fn lower_expr_while_in_loop_scope(
367 opt_label: Option<Label>,
368 ) -> hir::ExprKind<'hir> {
369 // FIXME(#53667): handle lowering of && and parens.
371 // Note that the block AND the condition are evaluated in the loop scope.
372 // This is done to allow `break` from inside the condition of the loop.
376 let else_pat = self.pat_wild(span);
377 let else_expr = self.expr_break(span, ThinVec::new());
378 self.arm(else_pat, else_expr)
381 // Handle then + scrutinee:
382 let then_expr = self.lower_block_expr(body);
383 let (then_pat, scrutinee, desugar, source) = match cond.kind {
384 ExprKind::Let(ref pat, ref scrutinee) => {
387 // [opt_ident]: loop {
388 // match <sub_expr> {
393 let scrutinee = self.with_loop_condition_scope(|t| t.lower_expr(scrutinee));
394 let pat = self.lower_pat(pat);
395 (pat, scrutinee, hir::MatchSource::WhileLetDesugar, hir::LoopSource::WhileLet)
398 // We desugar: `'label: while $cond $body` into:
402 // match drop-temps { $cond } {
410 let cond = self.with_loop_condition_scope(|this| this.lower_expr(cond));
412 self.mark_span_with_reason(DesugaringKind::CondTemporary, cond.span, None);
413 // Wrap in a construct equivalent to `{ let _t = $cond; _t }`
414 // to preserve drop semantics since `while cond { ... }` does not
415 // let temporaries live outside of `cond`.
416 let cond = self.expr_drop_temps(span_block, cond, ThinVec::new());
418 let pat = self.pat_bool(span, true);
419 (pat, cond, hir::MatchSource::WhileDesugar, hir::LoopSource::While)
422 let then_arm = self.arm(then_pat, self.arena.alloc(then_expr));
424 // `match <scrutinee> { ... }`
426 self.expr_match(span, scrutinee, arena_vec![self; then_arm, else_arm], desugar);
428 // `[opt_ident]: loop { ... }`
429 hir::ExprKind::Loop(self.block_expr(self.arena.alloc(match_expr)), opt_label, source)
432 /// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_ok(<expr>) }`,
433 /// `try { <stmts>; }` into `{ <stmts>; ::std::ops::Try::from_ok(()) }`
434 /// and save the block id to use it as a break target for desugaring of the `?` operator.
435 fn lower_expr_try_block(&mut self, body: &Block) -> hir::ExprKind<'hir> {
436 self.with_catch_scope(body.id, |this| {
437 let mut block = this.lower_block_noalloc(body, true);
439 // Final expression of the block (if present) or `()` with span at the end of block
440 let (try_span, tail_expr) = if let Some(expr) = block.expr.take() {
442 this.mark_span_with_reason(
443 DesugaringKind::TryBlock,
445 this.allow_try_trait.clone(),
450 let try_span = this.mark_span_with_reason(
451 DesugaringKind::TryBlock,
452 this.sess.source_map().end_point(body.span),
453 this.allow_try_trait.clone(),
456 (try_span, this.expr_unit(try_span))
459 let ok_wrapped_span =
460 this.mark_span_with_reason(DesugaringKind::TryBlock, tail_expr.span, None);
462 // `::std::ops::Try::from_ok($tail_expr)`
463 block.expr = Some(this.wrap_in_try_constructor(
464 hir::LangItem::TryFromOk,
470 hir::ExprKind::Block(this.arena.alloc(block), None)
474 fn wrap_in_try_constructor(
476 lang_item: hir::LangItem,
478 expr: &'hir hir::Expr<'hir>,
480 ) -> &'hir hir::Expr<'hir> {
482 self.arena.alloc(self.expr_lang_item_path(method_span, lang_item, ThinVec::new()));
483 self.expr_call(overall_span, constructor, std::slice::from_ref(expr))
486 fn lower_arm(&mut self, arm: &Arm) -> hir::Arm<'hir> {
488 hir_id: self.next_id(),
489 attrs: self.lower_attrs(&arm.attrs),
490 pat: self.lower_pat(&arm.pat),
491 guard: match arm.guard {
492 Some(ref x) => Some(hir::Guard::If(self.lower_expr(x))),
495 body: self.lower_expr(&arm.body),
500 /// Lower an `async` construct to a generator that is then wrapped so it implements `Future`.
505 /// std::future::from_generator(static move? |_task_context| -> <ret_ty> {
509 pub(super) fn make_async_expr(
511 capture_clause: CaptureBy,
512 closure_node_id: NodeId,
513 ret_ty: Option<AstP<Ty>>,
515 async_gen_kind: hir::AsyncGeneratorKind,
516 body: impl FnOnce(&mut Self) -> hir::Expr<'hir>,
517 ) -> hir::ExprKind<'hir> {
518 let output = match ret_ty {
519 Some(ty) => hir::FnRetTy::Return(self.lower_ty(&ty, ImplTraitContext::disallowed())),
520 None => hir::FnRetTy::DefaultReturn(span),
523 // Resume argument type. We let the compiler infer this to simplify the lowering. It is
524 // fully constrained by `future::from_generator`.
525 let input_ty = hir::Ty { hir_id: self.next_id(), kind: hir::TyKind::Infer, span };
527 // The closure/generator `FnDecl` takes a single (resume) argument of type `input_ty`.
528 let decl = self.arena.alloc(hir::FnDecl {
529 inputs: arena_vec![self; input_ty],
532 implicit_self: hir::ImplicitSelfKind::None,
535 // Lower the argument pattern/ident. The ident is used again in the `.await` lowering.
536 let (pat, task_context_hid) = self.pat_ident_binding_mode(
538 Ident::with_dummy_span(sym::_task_context),
539 hir::BindingAnnotation::Mutable,
541 let param = hir::Param { attrs: &[], hir_id: self.next_id(), pat, ty_span: span, span };
542 let params = arena_vec![self; param];
544 let body_id = self.lower_body(move |this| {
545 this.generator_kind = Some(hir::GeneratorKind::Async(async_gen_kind));
547 let old_ctx = this.task_context;
548 this.task_context = Some(task_context_hid);
549 let res = body(this);
550 this.task_context = old_ctx;
554 // `static |_task_context| -> <ret_ty> { body }`:
555 let generator_kind = hir::ExprKind::Closure(
560 Some(hir::Movability::Static),
562 let generator = hir::Expr {
563 hir_id: self.lower_node_id(closure_node_id),
564 kind: generator_kind,
566 attrs: ThinVec::new(),
569 // `future::from_generator`:
571 self.mark_span_with_reason(DesugaringKind::Async, span, self.allow_gen_future.clone());
573 self.expr_lang_item_path(unstable_span, hir::LangItem::FromGenerator, ThinVec::new());
575 // `future::from_generator(generator)`:
576 hir::ExprKind::Call(self.arena.alloc(gen_future), arena_vec![self; generator])
579 /// Desugar `<expr>.await` into:
582 /// mut pinned => loop {
583 /// match unsafe { ::std::future::Future::poll(
584 /// <::std::pin::Pin>::new_unchecked(&mut pinned),
585 /// ::std::future::get_context(task_context),
587 /// ::std::task::Poll::Ready(result) => break result,
588 /// ::std::task::Poll::Pending => {}
590 /// task_context = yield ();
594 fn lower_expr_await(&mut self, await_span: Span, expr: &Expr) -> hir::ExprKind<'hir> {
595 match self.generator_kind {
596 Some(hir::GeneratorKind::Async(_)) => {}
597 Some(hir::GeneratorKind::Gen) | None => {
598 let mut err = struct_span_err!(
602 "`await` is only allowed inside `async` functions and blocks"
604 err.span_label(await_span, "only allowed inside `async` functions and blocks");
605 if let Some(item_sp) = self.current_item {
606 err.span_label(item_sp, "this is not `async`");
611 let span = self.mark_span_with_reason(DesugaringKind::Await, await_span, None);
612 let gen_future_span = self.mark_span_with_reason(
613 DesugaringKind::Await,
615 self.allow_gen_future.clone(),
617 let expr = self.lower_expr(expr);
619 let pinned_ident = Ident::with_dummy_span(sym::pinned);
620 let (pinned_pat, pinned_pat_hid) =
621 self.pat_ident_binding_mode(span, pinned_ident, hir::BindingAnnotation::Mutable);
623 let task_context_ident = Ident::with_dummy_span(sym::_task_context);
626 // ::std::future::Future::poll(
627 // ::std::pin::Pin::new_unchecked(&mut pinned),
628 // ::std::future::get_context(task_context),
632 let pinned = self.expr_ident(span, pinned_ident, pinned_pat_hid);
633 let ref_mut_pinned = self.expr_mut_addr_of(span, pinned);
634 let task_context = if let Some(task_context_hid) = self.task_context {
635 self.expr_ident_mut(span, task_context_ident, task_context_hid)
637 // Use of `await` outside of an async context, we cannot use `task_context` here.
640 let new_unchecked = self.expr_call_lang_item_fn_mut(
642 hir::LangItem::PinNewUnchecked,
643 arena_vec![self; ref_mut_pinned],
645 let get_context = self.expr_call_lang_item_fn_mut(
647 hir::LangItem::GetContext,
648 arena_vec![self; task_context],
650 let call = self.expr_call_lang_item_fn(
652 hir::LangItem::FuturePoll,
653 arena_vec![self; new_unchecked, get_context],
655 self.arena.alloc(self.expr_unsafe(call))
658 // `::std::task::Poll::Ready(result) => break result`
659 let loop_node_id = self.resolver.next_node_id();
660 let loop_hir_id = self.lower_node_id(loop_node_id);
662 let x_ident = Ident::with_dummy_span(sym::result);
663 let (x_pat, x_pat_hid) = self.pat_ident(span, x_ident);
664 let x_expr = self.expr_ident(span, x_ident, x_pat_hid);
665 let ready_field = self.single_pat_field(span, x_pat);
666 let ready_pat = self.pat_lang_item_variant(span, hir::LangItem::PollReady, ready_field);
667 let break_x = self.with_loop_scope(loop_node_id, move |this| {
669 hir::ExprKind::Break(this.lower_loop_destination(None), Some(x_expr));
670 this.arena.alloc(this.expr(await_span, expr_break, ThinVec::new()))
672 self.arm(ready_pat, break_x)
675 // `::std::task::Poll::Pending => {}`
677 let pending_pat = self.pat_lang_item_variant(span, hir::LangItem::PollPending, &[]);
678 let empty_block = self.expr_block_empty(span);
679 self.arm(pending_pat, empty_block)
682 let inner_match_stmt = {
683 let match_expr = self.expr_match(
686 arena_vec![self; ready_arm, pending_arm],
687 hir::MatchSource::AwaitDesugar,
689 self.stmt_expr(span, match_expr)
692 // task_context = yield ();
694 let unit = self.expr_unit(span);
695 let yield_expr = self.expr(
697 hir::ExprKind::Yield(unit, hir::YieldSource::Await { expr: Some(expr.hir_id) }),
700 let yield_expr = self.arena.alloc(yield_expr);
702 if let Some(task_context_hid) = self.task_context {
703 let lhs = self.expr_ident(span, task_context_ident, task_context_hid);
705 self.expr(span, hir::ExprKind::Assign(lhs, yield_expr, span), AttrVec::new());
706 self.stmt_expr(span, assign)
708 // Use of `await` outside of an async context. Return `yield_expr` so that we can
709 // proceed with type checking.
710 self.stmt(span, hir::StmtKind::Semi(yield_expr))
714 let loop_block = self.block_all(span, arena_vec![self; inner_match_stmt, yield_stmt], None);
717 let loop_expr = self.arena.alloc(hir::Expr {
719 kind: hir::ExprKind::Loop(loop_block, None, hir::LoopSource::Loop),
721 attrs: ThinVec::new(),
724 // mut pinned => loop { ... }
725 let pinned_arm = self.arm(pinned_pat, loop_expr);
728 // mut pinned => loop { .. }
730 hir::ExprKind::Match(expr, arena_vec![self; pinned_arm], hir::MatchSource::AwaitDesugar)
733 fn lower_expr_closure(
735 capture_clause: CaptureBy,
736 movability: Movability,
740 ) -> hir::ExprKind<'hir> {
741 // Lower outside new scope to preserve `is_in_loop_condition`.
742 let fn_decl = self.lower_fn_decl(decl, None, false, None);
744 self.with_new_scopes(move |this| {
745 let prev = this.current_item;
746 this.current_item = Some(fn_decl_span);
747 let mut generator_kind = None;
748 let body_id = this.lower_fn_body(decl, |this| {
749 let e = this.lower_expr_mut(body);
750 generator_kind = this.generator_kind;
753 let generator_option =
754 this.generator_movability_for_fn(&decl, fn_decl_span, generator_kind, movability);
755 this.current_item = prev;
756 hir::ExprKind::Closure(capture_clause, fn_decl, body_id, fn_decl_span, generator_option)
760 fn generator_movability_for_fn(
764 generator_kind: Option<hir::GeneratorKind>,
765 movability: Movability,
766 ) -> Option<hir::Movability> {
767 match generator_kind {
768 Some(hir::GeneratorKind::Gen) => {
769 if decl.inputs.len() > 1 {
774 "too many parameters for a generator (expected 0 or 1 parameters)"
780 Some(hir::GeneratorKind::Async(_)) => {
781 panic!("non-`async` closure body turned `async` during lowering");
784 if movability == Movability::Static {
785 struct_span_err!(self.sess, fn_decl_span, E0697, "closures cannot be static")
793 fn lower_expr_async_closure(
795 capture_clause: CaptureBy,
800 ) -> hir::ExprKind<'hir> {
802 FnDecl { inputs: decl.inputs.clone(), output: FnRetTy::Default(fn_decl_span) };
803 // We need to lower the declaration outside the new scope, because we
804 // have to conserve the state of being inside a loop condition for the
805 // closure argument types.
806 let fn_decl = self.lower_fn_decl(&outer_decl, None, false, None);
808 self.with_new_scopes(move |this| {
809 // FIXME(cramertj): allow `async` non-`move` closures with arguments.
810 if capture_clause == CaptureBy::Ref && !decl.inputs.is_empty() {
815 "`async` non-`move` closures with parameters are not currently supported",
818 "consider using `let` statements to manually capture \
819 variables by reference before entering an `async move` closure",
824 // Transform `async |x: u8| -> X { ... }` into
825 // `|x: u8| future_from_generator(|| -> X { ... })`.
826 let body_id = this.lower_fn_body(&outer_decl, |this| {
828 if let FnRetTy::Ty(ty) = &decl.output { Some(ty.clone()) } else { None };
829 let async_body = this.make_async_expr(
834 hir::AsyncGeneratorKind::Closure,
835 |this| this.with_new_scopes(|this| this.lower_expr_mut(body)),
837 this.expr(fn_decl_span, async_body, ThinVec::new())
839 hir::ExprKind::Closure(capture_clause, fn_decl, body_id, fn_decl_span, None)
843 /// Desugar `<start>..=<end>` into `std::ops::RangeInclusive::new(<start>, <end>)`.
844 fn lower_expr_range_closed(&mut self, span: Span, e1: &Expr, e2: &Expr) -> hir::ExprKind<'hir> {
845 let e1 = self.lower_expr_mut(e1);
846 let e2 = self.lower_expr_mut(e2);
847 let fn_path = hir::QPath::LangItem(hir::LangItem::RangeInclusiveNew, span);
849 self.arena.alloc(self.expr(span, hir::ExprKind::Path(fn_path), ThinVec::new()));
850 hir::ExprKind::Call(fn_expr, arena_vec![self; e1, e2])
859 ) -> hir::ExprKind<'hir> {
860 use rustc_ast::RangeLimits::*;
862 let lang_item = match (e1, e2, lims) {
863 (None, None, HalfOpen) => hir::LangItem::RangeFull,
864 (Some(..), None, HalfOpen) => hir::LangItem::RangeFrom,
865 (None, Some(..), HalfOpen) => hir::LangItem::RangeTo,
866 (Some(..), Some(..), HalfOpen) => hir::LangItem::Range,
867 (None, Some(..), Closed) => hir::LangItem::RangeToInclusive,
868 (Some(..), Some(..), Closed) => unreachable!(),
869 (_, None, Closed) => {
870 self.diagnostic().span_fatal(span, "inclusive range with no end").raise()
874 let fields = self.arena.alloc_from_iter(
875 e1.iter().map(|e| ("start", e)).chain(e2.iter().map(|e| ("end", e))).map(|(s, e)| {
876 let expr = self.lower_expr(&e);
877 let ident = Ident::new(Symbol::intern(s), e.span);
878 self.field(ident, expr, e.span)
882 hir::ExprKind::Struct(self.arena.alloc(hir::QPath::LangItem(lang_item, span)), fields, None)
885 fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
886 let target_id = match destination {
888 if let Some(loop_id) = self.resolver.get_label_res(id) {
889 Ok(self.lower_node_id(loop_id))
891 Err(hir::LoopIdError::UnresolvedLabel)
898 .map(|id| Ok(self.lower_node_id(id)))
899 .unwrap_or(Err(hir::LoopIdError::OutsideLoopScope)),
901 hir::Destination { label: destination.map(|(_, label)| label), target_id }
904 fn lower_jump_destination(&mut self, id: NodeId, opt_label: Option<Label>) -> hir::Destination {
905 if self.is_in_loop_condition && opt_label.is_none() {
908 target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition),
911 self.lower_loop_destination(opt_label.map(|label| (id, label)))
915 fn with_catch_scope<T>(&mut self, catch_id: NodeId, f: impl FnOnce(&mut Self) -> T) -> T {
916 let len = self.catch_scopes.len();
917 self.catch_scopes.push(catch_id);
919 let result = f(self);
922 self.catch_scopes.len(),
923 "catch scopes should be added and removed in stack order"
926 self.catch_scopes.pop().unwrap();
931 fn with_loop_scope<T>(&mut self, loop_id: NodeId, f: impl FnOnce(&mut Self) -> T) -> T {
932 // We're no longer in the base loop's condition; we're in another loop.
933 let was_in_loop_condition = self.is_in_loop_condition;
934 self.is_in_loop_condition = false;
936 let len = self.loop_scopes.len();
937 self.loop_scopes.push(loop_id);
939 let result = f(self);
942 self.loop_scopes.len(),
943 "loop scopes should be added and removed in stack order"
946 self.loop_scopes.pop().unwrap();
948 self.is_in_loop_condition = was_in_loop_condition;
953 fn with_loop_condition_scope<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T {
954 let was_in_loop_condition = self.is_in_loop_condition;
955 self.is_in_loop_condition = true;
957 let result = f(self);
959 self.is_in_loop_condition = was_in_loop_condition;
964 fn lower_expr_asm(&mut self, sp: Span, asm: &InlineAsm) -> hir::ExprKind<'hir> {
965 if self.sess.asm_arch.is_none() {
966 struct_span_err!(self.sess, sp, E0472, "asm! is unsupported on this target").emit();
968 if asm.options.contains(InlineAsmOptions::ATT_SYNTAX)
971 Some(asm::InlineAsmArch::X86 | asm::InlineAsmArch::X86_64)
975 .struct_span_err(sp, "the `att_syntax` option is only supported on x86")
979 // Lower operands to HIR, filter_map skips any operands with invalid
981 let sess = self.sess;
982 let operands: Vec<_> = asm
985 .filter_map(|(op, op_sp)| {
986 let lower_reg = |reg| {
988 InlineAsmRegOrRegClass::Reg(s) => asm::InlineAsmRegOrRegClass::Reg(
989 asm::InlineAsmReg::parse(
991 |feature| sess.target_features.contains(&Symbol::intern(feature)),
996 let msg = format!("invalid register `{}`: {}", s.as_str(), e);
997 sess.struct_span_err(*op_sp, &msg).emit();
1001 InlineAsmRegOrRegClass::RegClass(s) => {
1002 asm::InlineAsmRegOrRegClass::RegClass(
1003 asm::InlineAsmRegClass::parse(sess.asm_arch?, s)
1006 "invalid register class `{}`: {}",
1010 sess.struct_span_err(*op_sp, &msg).emit();
1018 // lower_reg is executed last because we need to lower all
1019 // sub-expressions even if we throw them away later.
1020 let op = match *op {
1021 InlineAsmOperand::In { reg, ref expr } => hir::InlineAsmOperand::In {
1022 expr: self.lower_expr_mut(expr),
1023 reg: lower_reg(reg)?,
1025 InlineAsmOperand::Out { reg, late, ref expr } => hir::InlineAsmOperand::Out {
1027 expr: expr.as_ref().map(|expr| self.lower_expr_mut(expr)),
1028 reg: lower_reg(reg)?,
1030 InlineAsmOperand::InOut { reg, late, ref expr } => {
1031 hir::InlineAsmOperand::InOut {
1033 expr: self.lower_expr_mut(expr),
1034 reg: lower_reg(reg)?,
1037 InlineAsmOperand::SplitInOut { reg, late, ref in_expr, ref out_expr } => {
1038 hir::InlineAsmOperand::SplitInOut {
1040 in_expr: self.lower_expr_mut(in_expr),
1041 out_expr: out_expr.as_ref().map(|expr| self.lower_expr_mut(expr)),
1042 reg: lower_reg(reg)?,
1045 InlineAsmOperand::Const { ref expr } => {
1046 hir::InlineAsmOperand::Const { expr: self.lower_expr_mut(expr) }
1048 InlineAsmOperand::Sym { ref expr } => {
1049 hir::InlineAsmOperand::Sym { expr: self.lower_expr_mut(expr) }
1056 // Stop if there were any errors when lowering the register classes
1057 if operands.len() != asm.operands.len() || sess.asm_arch.is_none() {
1058 return hir::ExprKind::Err;
1061 // Validate template modifiers against the register classes for the operands
1062 let asm_arch = sess.asm_arch.unwrap();
1063 for p in &asm.template {
1064 if let InlineAsmTemplatePiece::Placeholder {
1066 modifier: Some(modifier),
1067 span: placeholder_span,
1070 let op_sp = asm.operands[operand_idx].1;
1071 match &operands[operand_idx] {
1072 hir::InlineAsmOperand::In { reg, .. }
1073 | hir::InlineAsmOperand::Out { reg, .. }
1074 | hir::InlineAsmOperand::InOut { reg, .. }
1075 | hir::InlineAsmOperand::SplitInOut { reg, .. } => {
1076 let class = reg.reg_class();
1077 let valid_modifiers = class.valid_modifiers(asm_arch);
1078 if !valid_modifiers.contains(&modifier) {
1079 let mut err = sess.struct_span_err(
1081 "invalid asm template modifier for this register class",
1083 err.span_label(placeholder_span, "template modifier");
1084 err.span_label(op_sp, "argument");
1085 if !valid_modifiers.is_empty() {
1086 let mut mods = format!("`{}`", valid_modifiers[0]);
1087 for m in &valid_modifiers[1..] {
1088 let _ = write!(mods, ", `{}`", m);
1091 "the `{}` register class supports \
1092 the following template modifiers: {}",
1098 "the `{}` register class does not support template modifiers",
1105 hir::InlineAsmOperand::Const { .. } => {
1106 let mut err = sess.struct_span_err(
1108 "asm template modifiers are not allowed for `const` arguments",
1110 err.span_label(placeholder_span, "template modifier");
1111 err.span_label(op_sp, "argument");
1114 hir::InlineAsmOperand::Sym { .. } => {
1115 let mut err = sess.struct_span_err(
1117 "asm template modifiers are not allowed for `sym` arguments",
1119 err.span_label(placeholder_span, "template modifier");
1120 err.span_label(op_sp, "argument");
1127 let mut used_input_regs = FxHashMap::default();
1128 let mut used_output_regs = FxHashMap::default();
1129 for (idx, op) in operands.iter().enumerate() {
1130 let op_sp = asm.operands[idx].1;
1131 if let Some(reg) = op.reg() {
1132 // Validate register classes against currently enabled target
1133 // features. We check that at least one type is available for
1134 // the current target.
1135 let reg_class = reg.reg_class();
1136 let mut required_features: Vec<&str> = vec![];
1137 for &(_, feature) in reg_class.supported_types(asm_arch) {
1138 if let Some(feature) = feature {
1139 if self.sess.target_features.contains(&Symbol::intern(feature)) {
1140 required_features.clear();
1143 required_features.push(feature);
1146 required_features.clear();
1150 // We are sorting primitive strs here and can use unstable sort here
1151 required_features.sort_unstable();
1152 required_features.dedup();
1153 match &required_features[..] {
1157 "register class `{}` requires the `{}` target feature",
1161 sess.struct_span_err(op_sp, &msg).emit();
1165 "register class `{}` requires at least one target feature: {}",
1169 sess.struct_span_err(op_sp, &msg).emit();
1173 // Check for conflicts between explicit register operands.
1174 if let asm::InlineAsmRegOrRegClass::Reg(reg) = reg {
1175 let (input, output) = match op {
1176 hir::InlineAsmOperand::In { .. } => (true, false),
1177 // Late output do not conflict with inputs, but normal outputs do
1178 hir::InlineAsmOperand::Out { late, .. } => (!late, true),
1179 hir::InlineAsmOperand::InOut { .. }
1180 | hir::InlineAsmOperand::SplitInOut { .. } => (true, true),
1181 hir::InlineAsmOperand::Const { .. } | hir::InlineAsmOperand::Sym { .. } => {
1186 // Flag to output the error only once per operand
1187 let mut skip = false;
1188 reg.overlapping_regs(|r| {
1189 let mut check = |used_regs: &mut FxHashMap<asm::InlineAsmReg, usize>,
1191 match used_regs.entry(r) {
1192 Entry::Occupied(o) => {
1198 let idx2 = *o.get();
1199 let op2 = &operands[idx2];
1200 let op_sp2 = asm.operands[idx2].1;
1201 let reg2 = match op2.reg() {
1202 Some(asm::InlineAsmRegOrRegClass::Reg(r)) => r,
1203 _ => unreachable!(),
1207 "register `{}` conflicts with register `{}`",
1211 let mut err = sess.struct_span_err(op_sp, &msg);
1212 err.span_label(op_sp, &format!("register `{}`", reg.name()));
1213 err.span_label(op_sp2, &format!("register `{}`", reg2.name()));
1217 hir::InlineAsmOperand::In { .. },
1218 hir::InlineAsmOperand::Out { late, .. },
1221 hir::InlineAsmOperand::Out { late, .. },
1222 hir::InlineAsmOperand::In { .. },
1225 let out_op_sp = if input { op_sp2 } else { op_sp };
1226 let msg = "use `lateout` instead of \
1227 `out` to avoid conflict";
1228 err.span_help(out_op_sp, msg);
1235 Entry::Vacant(v) => {
1241 check(&mut used_input_regs, true);
1244 check(&mut used_output_regs, false);
1251 let operands = self.arena.alloc_from_iter(operands);
1252 let template = self.arena.alloc_from_iter(asm.template.iter().cloned());
1253 let line_spans = self.arena.alloc_slice(&asm.line_spans[..]);
1254 let hir_asm = hir::InlineAsm { template, operands, options: asm.options, line_spans };
1255 hir::ExprKind::InlineAsm(self.arena.alloc(hir_asm))
1258 fn lower_expr_llvm_asm(&mut self, asm: &LlvmInlineAsm) -> hir::ExprKind<'hir> {
1259 let inner = hir::LlvmInlineAsmInner {
1260 inputs: asm.inputs.iter().map(|&(c, _)| c).collect(),
1264 .map(|out| hir::LlvmInlineAsmOutput {
1265 constraint: out.constraint,
1267 is_indirect: out.is_indirect,
1268 span: out.expr.span,
1272 asm_str_style: asm.asm_str_style,
1273 clobbers: asm.clobbers.clone(),
1274 volatile: asm.volatile,
1275 alignstack: asm.alignstack,
1276 dialect: asm.dialect,
1278 let hir_asm = hir::LlvmInlineAsm {
1280 inputs_exprs: self.arena.alloc_from_iter(
1281 asm.inputs.iter().map(|&(_, ref input)| self.lower_expr_mut(input)),
1285 .alloc_from_iter(asm.outputs.iter().map(|out| self.lower_expr_mut(&out.expr))),
1287 hir::ExprKind::LlvmInlineAsm(self.arena.alloc(hir_asm))
1290 fn lower_field(&mut self, f: &Field) -> hir::Field<'hir> {
1292 hir_id: self.next_id(),
1294 expr: self.lower_expr(&f.expr),
1296 is_shorthand: f.is_shorthand,
1300 fn lower_expr_yield(&mut self, span: Span, opt_expr: Option<&Expr>) -> hir::ExprKind<'hir> {
1301 match self.generator_kind {
1302 Some(hir::GeneratorKind::Gen) => {}
1303 Some(hir::GeneratorKind::Async(_)) => {
1308 "`async` generators are not yet supported"
1312 None => self.generator_kind = Some(hir::GeneratorKind::Gen),
1316 opt_expr.as_ref().map(|x| self.lower_expr(x)).unwrap_or_else(|| self.expr_unit(span));
1318 hir::ExprKind::Yield(expr, hir::YieldSource::Yield)
1321 /// Desugar `ExprForLoop` from: `[opt_ident]: for <pat> in <head> <body>` into:
1324 /// let result = match ::std::iter::IntoIterator::into_iter(<head>) {
1326 /// [opt_ident]: loop {
1328 /// match ::std::iter::Iterator::next(&mut iter) {
1329 /// ::std::option::Option::Some(val) => __next = val,
1330 /// ::std::option::Option::None => break
1332 /// let <pat> = __next;
1333 /// StmtKind::Expr(<body>);
1346 opt_label: Option<Label>,
1347 ) -> hir::Expr<'hir> {
1348 let orig_head_span = head.span;
1350 let mut head = self.lower_expr_mut(head);
1351 let desugared_span = self.mark_span_with_reason(
1352 DesugaringKind::ForLoop(ForLoopLoc::Head),
1356 head.span = desugared_span;
1358 let iter = Ident::with_dummy_span(sym::iter);
1360 let next_ident = Ident::with_dummy_span(sym::__next);
1361 let (next_pat, next_pat_hid) = self.pat_ident_binding_mode(
1364 hir::BindingAnnotation::Mutable,
1367 // `::std::option::Option::Some(val) => __next = val`
1369 let val_ident = Ident::with_dummy_span(sym::val);
1370 let (val_pat, val_pat_hid) = self.pat_ident(pat.span, val_ident);
1371 let val_expr = self.expr_ident(pat.span, val_ident, val_pat_hid);
1372 let next_expr = self.expr_ident(pat.span, next_ident, next_pat_hid);
1373 let assign = self.arena.alloc(self.expr(
1375 hir::ExprKind::Assign(next_expr, val_expr, pat.span),
1378 let some_pat = self.pat_some(pat.span, val_pat);
1379 self.arm(some_pat, assign)
1382 // `::std::option::Option::None => break`
1385 self.with_loop_scope(e.id, |this| this.expr_break(e.span, ThinVec::new()));
1386 let pat = self.pat_none(e.span);
1387 self.arm(pat, break_expr)
1391 let (iter_pat, iter_pat_nid) =
1392 self.pat_ident_binding_mode(desugared_span, iter, hir::BindingAnnotation::Mutable);
1394 // `match ::std::iter::Iterator::next(&mut iter) { ... }`
1396 let iter = self.expr_ident(desugared_span, iter, iter_pat_nid);
1397 let ref_mut_iter = self.expr_mut_addr_of(desugared_span, iter);
1398 let next_expr = self.expr_call_lang_item_fn(
1400 hir::LangItem::IteratorNext,
1401 arena_vec![self; ref_mut_iter],
1403 let arms = arena_vec![self; pat_arm, break_arm];
1405 self.expr_match(desugared_span, next_expr, arms, hir::MatchSource::ForLoopDesugar)
1407 let match_stmt = self.stmt_expr(desugared_span, match_expr);
1409 let next_expr = self.expr_ident(desugared_span, next_ident, next_pat_hid);
1412 let next_let = self.stmt_let_pat(
1417 hir::LocalSource::ForLoopDesugar,
1420 // `let <pat> = __next`
1421 let pat = self.lower_pat(pat);
1422 let pat_let = self.stmt_let_pat(
1427 hir::LocalSource::ForLoopDesugar,
1430 let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body, false));
1431 let body_expr = self.expr_block(body_block, ThinVec::new());
1432 let body_stmt = self.stmt_expr(body.span, body_expr);
1434 let loop_block = self.block_all(
1436 arena_vec![self; next_let, match_stmt, pat_let, body_stmt],
1440 // `[opt_ident]: loop { ... }`
1441 let kind = hir::ExprKind::Loop(loop_block, opt_label, hir::LoopSource::ForLoop);
1442 let loop_expr = self.arena.alloc(hir::Expr {
1443 hir_id: self.lower_node_id(e.id),
1446 attrs: ThinVec::new(),
1449 // `mut iter => { ... }`
1450 let iter_arm = self.arm(iter_pat, loop_expr);
1452 let into_iter_span = self.mark_span_with_reason(
1453 DesugaringKind::ForLoop(ForLoopLoc::IntoIter),
1458 // `match ::std::iter::IntoIterator::into_iter(<head>) { ... }`
1459 let into_iter_expr = {
1460 self.expr_call_lang_item_fn(
1462 hir::LangItem::IntoIterIntoIter,
1463 arena_vec![self; head],
1467 let match_expr = self.arena.alloc(self.expr_match(
1470 arena_vec![self; iter_arm],
1471 hir::MatchSource::ForLoopDesugar,
1474 let attrs: Vec<_> = e.attrs.iter().map(|a| self.lower_attr(a)).collect();
1476 // This is effectively `{ let _result = ...; _result }`.
1477 // The construct was introduced in #21984 and is necessary to make sure that
1478 // temporaries in the `head` expression are dropped and do not leak to the
1479 // surrounding scope of the `match` since the `match` is not a terminating scope.
1481 // Also, add the attributes to the outer returned expr node.
1482 self.expr_drop_temps_mut(desugared_span, match_expr, attrs.into())
1485 /// Desugar `ExprKind::Try` from: `<expr>?` into:
1487 /// match Try::into_result(<expr>) {
1488 /// Ok(val) => #[allow(unreachable_code)] val,
1489 /// Err(err) => #[allow(unreachable_code)]
1490 /// // If there is an enclosing `try {...}`:
1491 /// break 'catch_target Try::from_error(From::from(err)),
1493 /// return Try::from_error(From::from(err)),
1496 fn lower_expr_try(&mut self, span: Span, sub_expr: &Expr) -> hir::ExprKind<'hir> {
1497 let unstable_span = self.mark_span_with_reason(
1498 DesugaringKind::QuestionMark,
1500 self.allow_try_trait.clone(),
1502 let try_span = self.sess.source_map().end_point(span);
1503 let try_span = self.mark_span_with_reason(
1504 DesugaringKind::QuestionMark,
1506 self.allow_try_trait.clone(),
1509 // `Try::into_result(<expr>)`
1512 let sub_expr = self.lower_expr_mut(sub_expr);
1514 self.expr_call_lang_item_fn(
1516 hir::LangItem::TryIntoResult,
1517 arena_vec![self; sub_expr],
1521 // `#[allow(unreachable_code)]`
1523 // `allow(unreachable_code)`
1525 let allow_ident = Ident::new(sym::allow, span);
1526 let uc_ident = Ident::new(sym::unreachable_code, span);
1527 let uc_nested = attr::mk_nested_word_item(uc_ident);
1528 attr::mk_list_item(allow_ident, vec![uc_nested])
1530 attr::mk_attr_outer(allow)
1532 let attrs = vec![attr];
1534 // `Ok(val) => #[allow(unreachable_code)] val,`
1536 let val_ident = Ident::with_dummy_span(sym::val);
1537 let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
1538 let val_expr = self.arena.alloc(self.expr_ident_with_attrs(
1542 ThinVec::from(attrs.clone()),
1544 let ok_pat = self.pat_ok(span, val_pat);
1545 self.arm(ok_pat, val_expr)
1548 // `Err(err) => #[allow(unreachable_code)]
1549 // return Try::from_error(From::from(err)),`
1551 let err_ident = Ident::with_dummy_span(sym::err);
1552 let (err_local, err_local_nid) = self.pat_ident(try_span, err_ident);
1554 let err_expr = self.expr_ident_mut(try_span, err_ident, err_local_nid);
1555 self.expr_call_lang_item_fn(
1557 hir::LangItem::FromFrom,
1558 arena_vec![self; err_expr],
1561 let from_err_expr = self.wrap_in_try_constructor(
1562 hir::LangItem::TryFromError,
1567 let thin_attrs = ThinVec::from(attrs);
1568 let catch_scope = self.catch_scopes.last().copied();
1569 let ret_expr = if let Some(catch_node) = catch_scope {
1570 let target_id = Ok(self.lower_node_id(catch_node));
1571 self.arena.alloc(self.expr(
1573 hir::ExprKind::Break(
1574 hir::Destination { label: None, target_id },
1575 Some(from_err_expr),
1580 self.arena.alloc(self.expr(
1582 hir::ExprKind::Ret(Some(from_err_expr)),
1587 let err_pat = self.pat_err(try_span, err_local);
1588 self.arm(err_pat, ret_expr)
1591 hir::ExprKind::Match(
1593 arena_vec![self; err_arm, ok_arm],
1594 hir::MatchSource::TryDesugar,
1598 // =========================================================================
1599 // Helper methods for building HIR.
1600 // =========================================================================
1602 /// Constructs a `true` or `false` literal expression.
1603 pub(super) fn expr_bool(&mut self, span: Span, val: bool) -> &'hir hir::Expr<'hir> {
1604 let lit = Spanned { span, node: LitKind::Bool(val) };
1605 self.arena.alloc(self.expr(span, hir::ExprKind::Lit(lit), ThinVec::new()))
1608 /// Wrap the given `expr` in a terminating scope using `hir::ExprKind::DropTemps`.
1610 /// In terms of drop order, it has the same effect as wrapping `expr` in
1611 /// `{ let _t = $expr; _t }` but should provide better compile-time performance.
1613 /// The drop order can be important in e.g. `if expr { .. }`.
1614 pub(super) fn expr_drop_temps(
1617 expr: &'hir hir::Expr<'hir>,
1619 ) -> &'hir hir::Expr<'hir> {
1620 self.arena.alloc(self.expr_drop_temps_mut(span, expr, attrs))
1623 pub(super) fn expr_drop_temps_mut(
1626 expr: &'hir hir::Expr<'hir>,
1628 ) -> hir::Expr<'hir> {
1629 self.expr(span, hir::ExprKind::DropTemps(expr), attrs)
1635 arg: &'hir hir::Expr<'hir>,
1636 arms: &'hir [hir::Arm<'hir>],
1637 source: hir::MatchSource,
1638 ) -> hir::Expr<'hir> {
1639 self.expr(span, hir::ExprKind::Match(arg, arms, source), ThinVec::new())
1642 fn expr_break(&mut self, span: Span, attrs: AttrVec) -> &'hir hir::Expr<'hir> {
1643 let expr_break = hir::ExprKind::Break(self.lower_loop_destination(None), None);
1644 self.arena.alloc(self.expr(span, expr_break, attrs))
1647 fn expr_mut_addr_of(&mut self, span: Span, e: &'hir hir::Expr<'hir>) -> hir::Expr<'hir> {
1650 hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, e),
1655 fn expr_unit(&mut self, sp: Span) -> &'hir hir::Expr<'hir> {
1656 self.arena.alloc(self.expr(sp, hir::ExprKind::Tup(&[]), ThinVec::new()))
1662 e: &'hir hir::Expr<'hir>,
1663 args: &'hir [hir::Expr<'hir>],
1664 ) -> hir::Expr<'hir> {
1665 self.expr(span, hir::ExprKind::Call(e, args), ThinVec::new())
1671 e: &'hir hir::Expr<'hir>,
1672 args: &'hir [hir::Expr<'hir>],
1673 ) -> &'hir hir::Expr<'hir> {
1674 self.arena.alloc(self.expr_call_mut(span, e, args))
1677 fn expr_call_lang_item_fn_mut(
1680 lang_item: hir::LangItem,
1681 args: &'hir [hir::Expr<'hir>],
1682 ) -> hir::Expr<'hir> {
1683 let path = self.arena.alloc(self.expr_lang_item_path(span, lang_item, ThinVec::new()));
1684 self.expr_call_mut(span, path, args)
1687 fn expr_call_lang_item_fn(
1690 lang_item: hir::LangItem,
1691 args: &'hir [hir::Expr<'hir>],
1692 ) -> &'hir hir::Expr<'hir> {
1693 self.arena.alloc(self.expr_call_lang_item_fn_mut(span, lang_item, args))
1696 fn expr_lang_item_path(
1699 lang_item: hir::LangItem,
1701 ) -> hir::Expr<'hir> {
1702 self.expr(span, hir::ExprKind::Path(hir::QPath::LangItem(lang_item, span)), attrs)
1705 pub(super) fn expr_ident(
1709 binding: hir::HirId,
1710 ) -> &'hir hir::Expr<'hir> {
1711 self.arena.alloc(self.expr_ident_mut(sp, ident, binding))
1714 pub(super) fn expr_ident_mut(
1718 binding: hir::HirId,
1719 ) -> hir::Expr<'hir> {
1720 self.expr_ident_with_attrs(sp, ident, binding, ThinVec::new())
1723 fn expr_ident_with_attrs(
1727 binding: hir::HirId,
1729 ) -> hir::Expr<'hir> {
1730 let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
1732 self.arena.alloc(hir::Path {
1734 res: Res::Local(binding),
1735 segments: arena_vec![self; hir::PathSegment::from_ident(ident)],
1739 self.expr(span, expr_path, attrs)
1742 fn expr_unsafe(&mut self, expr: &'hir hir::Expr<'hir>) -> hir::Expr<'hir> {
1743 let hir_id = self.next_id();
1744 let span = expr.span;
1747 hir::ExprKind::Block(
1748 self.arena.alloc(hir::Block {
1752 rules: hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::CompilerGenerated),
1754 targeted_by_break: false,
1762 fn expr_block_empty(&mut self, span: Span) -> &'hir hir::Expr<'hir> {
1763 let blk = self.block_all(span, &[], None);
1764 let expr = self.expr_block(blk, ThinVec::new());
1765 self.arena.alloc(expr)
1768 pub(super) fn expr_block(
1770 b: &'hir hir::Block<'hir>,
1772 ) -> hir::Expr<'hir> {
1773 self.expr(b.span, hir::ExprKind::Block(b, None), attrs)
1779 kind: hir::ExprKind<'hir>,
1781 ) -> hir::Expr<'hir> {
1782 hir::Expr { hir_id: self.next_id(), kind, span, attrs }
1785 fn field(&mut self, ident: Ident, expr: &'hir hir::Expr<'hir>, span: Span) -> hir::Field<'hir> {
1786 hir::Field { hir_id: self.next_id(), ident, span, expr, is_shorthand: false }
1789 fn arm(&mut self, pat: &'hir hir::Pat<'hir>, expr: &'hir hir::Expr<'hir>) -> hir::Arm<'hir> {
1791 hir_id: self.next_id(),