2 AsyncGeneratorsNotSupported, AsyncNonMoveClosureNotSupported, AwaitOnlyInAsyncFnAndBlocks,
3 BaseExpressionDoubleDot, ClosureCannotBeStatic, FunctionalRecordUpdateDestructuringAssignemnt,
4 GeneratorTooManyParameters, InclusiveRangeWithNoEnd, NotSupportedForLifetimeBinderAsyncClosure,
5 RustcBoxAttributeError, UnderscoreExprLhsAssign,
7 use super::ResolverAstLoweringExt;
8 use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
9 use crate::{FnDeclKind, ImplTraitPosition};
11 use rustc_ast::ptr::P as AstP;
13 use rustc_data_structures::stack::ensure_sufficient_stack;
15 use rustc_hir::def::Res;
16 use rustc_hir::definitions::DefPathData;
17 use rustc_session::errors::report_lit_error;
18 use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
19 use rustc_span::symbol::{kw, sym, Ident};
20 use rustc_span::DUMMY_SP;
21 use thin_vec::thin_vec;
23 impl<'hir> LoweringContext<'_, 'hir> {
24 fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> &'hir [hir::Expr<'hir>] {
25 self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
28 pub(super) fn lower_expr(&mut self, e: &Expr) -> &'hir hir::Expr<'hir> {
29 self.arena.alloc(self.lower_expr_mut(e))
32 pub(super) fn lower_expr_mut(&mut self, e: &Expr) -> hir::Expr<'hir> {
33 ensure_sufficient_stack(|| {
35 // Paranthesis expression does not have a HirId and is handled specially.
36 ExprKind::Paren(ex) => {
37 let mut ex = self.lower_expr_mut(ex);
38 // Include parens in span, but only if it is a super-span.
39 if e.span.contains(ex.span) {
40 ex.span = self.lower_span(e.span);
42 // Merge attributes into the inner expression.
43 if !e.attrs.is_empty() {
45 self.attrs.get(&ex.hir_id.local_id).map(|la| *la).unwrap_or(&[]);
48 &*self.arena.alloc_from_iter(
51 .map(|a| self.lower_attr(a))
52 .chain(old_attrs.iter().cloned()),
58 // Desugar `ExprForLoop`
59 // from: `[opt_ident]: for <pat> in <head> <body>`
61 // This also needs special handling because the HirId of the returned `hir::Expr` will not
62 // correspond to the `e.id`, so `lower_expr_for` handles attribute lowering itself.
63 ExprKind::ForLoop(pat, head, body, opt_label) => {
64 return self.lower_expr_for(e, pat, head, body, *opt_label);
69 let hir_id = self.lower_node_id(e.id);
70 self.lower_attrs(hir_id, &e.attrs);
72 let kind = match &e.kind {
73 ExprKind::Box(inner) => hir::ExprKind::Box(self.lower_expr(inner)),
74 ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
75 ExprKind::ConstBlock(anon_const) => {
76 let anon_const = self.lower_anon_const(anon_const);
77 hir::ExprKind::ConstBlock(anon_const)
79 ExprKind::Repeat(expr, count) => {
80 let expr = self.lower_expr(expr);
81 let count = self.lower_array_length(count);
82 hir::ExprKind::Repeat(expr, count)
84 ExprKind::Tup(elts) => hir::ExprKind::Tup(self.lower_exprs(elts)),
85 ExprKind::Call(f, args) => {
86 if e.attrs.get(0).map_or(false, |a| a.has_name(sym::rustc_box)) {
87 if let [inner] = &args[..] && e.attrs.len() == 1 {
88 let kind = hir::ExprKind::Box(self.lower_expr(&inner));
89 return hir::Expr { hir_id, kind, span: self.lower_span(e.span) };
91 self.tcx.sess.emit_err(RustcBoxAttributeError { span: e.span });
94 } else if let Some(legacy_args) = self.resolver.legacy_const_generic_args(f) {
95 self.lower_legacy_const_generics((**f).clone(), args.clone(), &legacy_args)
97 let f = self.lower_expr(f);
98 hir::ExprKind::Call(f, self.lower_exprs(args))
101 ExprKind::MethodCall(box MethodCall { seg, receiver, args, span }) => {
102 let hir_seg = self.arena.alloc(self.lower_path_segment(
106 ParenthesizedGenericArgs::Err,
107 &ImplTraitContext::Disallowed(ImplTraitPosition::Path),
109 let receiver = self.lower_expr(receiver);
111 self.arena.alloc_from_iter(args.iter().map(|x| self.lower_expr_mut(x)));
112 hir::ExprKind::MethodCall(hir_seg, receiver, args, self.lower_span(*span))
114 ExprKind::Binary(binop, lhs, rhs) => {
115 let binop = self.lower_binop(*binop);
116 let lhs = self.lower_expr(lhs);
117 let rhs = self.lower_expr(rhs);
118 hir::ExprKind::Binary(binop, lhs, rhs)
120 ExprKind::Unary(op, ohs) => {
121 let op = self.lower_unop(*op);
122 let ohs = self.lower_expr(ohs);
123 hir::ExprKind::Unary(op, ohs)
125 ExprKind::Lit(token_lit) => {
126 let lit_kind = match LitKind::from_token_lit(*token_lit) {
127 Ok(lit_kind) => lit_kind,
129 report_lit_error(&self.tcx.sess.parse_sess, err, *token_lit, e.span);
133 hir::ExprKind::Lit(respan(self.lower_span(e.span), lit_kind))
135 ExprKind::IncludedBytes(bytes) => hir::ExprKind::Lit(respan(
136 self.lower_span(e.span),
137 LitKind::ByteStr(bytes.clone()),
139 ExprKind::Cast(expr, ty) => {
140 let expr = self.lower_expr(expr);
142 self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
143 hir::ExprKind::Cast(expr, ty)
145 ExprKind::Type(expr, ty) => {
146 let expr = self.lower_expr(expr);
148 self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
149 hir::ExprKind::Type(expr, ty)
151 ExprKind::AddrOf(k, m, ohs) => {
152 let ohs = self.lower_expr(ohs);
153 hir::ExprKind::AddrOf(*k, *m, ohs)
155 ExprKind::Let(pat, scrutinee, span) => {
156 hir::ExprKind::Let(self.arena.alloc(hir::Let {
157 hir_id: self.next_id(),
158 span: self.lower_span(*span),
159 pat: self.lower_pat(pat),
161 init: self.lower_expr(scrutinee),
164 ExprKind::If(cond, then, else_opt) => {
165 self.lower_expr_if(cond, then, else_opt.as_deref())
167 ExprKind::While(cond, body, opt_label) => self.with_loop_scope(e.id, |this| {
168 let span = this.mark_span_with_reason(DesugaringKind::WhileLoop, e.span, None);
169 this.lower_expr_while_in_loop_scope(span, cond, body, *opt_label)
171 ExprKind::Loop(body, opt_label, span) => self.with_loop_scope(e.id, |this| {
173 this.lower_block(body, false),
174 this.lower_label(*opt_label),
175 hir::LoopSource::Loop,
176 this.lower_span(*span),
179 ExprKind::TryBlock(body) => self.lower_expr_try_block(body),
180 ExprKind::Match(expr, arms) => hir::ExprKind::Match(
181 self.lower_expr(expr),
182 self.arena.alloc_from_iter(arms.iter().map(|x| self.lower_arm(x))),
183 hir::MatchSource::Normal,
185 ExprKind::Async(capture_clause, closure_node_id, block) => self.make_async_expr(
191 hir::AsyncGeneratorKind::Block,
192 |this| this.with_new_scopes(|this| this.lower_block_expr(block)),
194 ExprKind::Await(expr) => {
195 let dot_await_span = if expr.span.hi() < e.span.hi() {
196 let span_with_whitespace = self
200 .span_extend_while(expr.span, char::is_whitespace)
201 .unwrap_or(expr.span);
202 span_with_whitespace.shrink_to_hi().with_hi(e.span.hi())
204 // this is a recovered `await expr`
207 self.lower_expr_await(dot_await_span, expr)
209 ExprKind::Closure(box Closure {
219 if let Async::Yes { closure_id, .. } = asyncness {
220 self.lower_expr_async_closure(
232 self.lower_expr_closure(
244 ExprKind::Block(blk, opt_label) => {
245 let opt_label = self.lower_label(*opt_label);
246 hir::ExprKind::Block(self.lower_block(blk, opt_label.is_some()), opt_label)
248 ExprKind::Assign(el, er, span) => self.lower_expr_assign(el, er, *span, e.span),
249 ExprKind::AssignOp(op, el, er) => hir::ExprKind::AssignOp(
250 self.lower_binop(*op),
254 ExprKind::Field(el, ident) => {
255 hir::ExprKind::Field(self.lower_expr(el), self.lower_ident(*ident))
257 ExprKind::Index(el, er) => {
258 hir::ExprKind::Index(self.lower_expr(el), self.lower_expr(er))
260 ExprKind::Range(Some(e1), Some(e2), RangeLimits::Closed) => {
261 self.lower_expr_range_closed(e.span, e1, e2)
263 ExprKind::Range(e1, e2, lims) => {
264 self.lower_expr_range(e.span, e1.as_deref(), e2.as_deref(), *lims)
266 ExprKind::Underscore => {
267 self.tcx.sess.emit_err(UnderscoreExprLhsAssign { span: e.span });
270 ExprKind::Path(qself, path) => {
271 let qpath = self.lower_qpath(
276 &ImplTraitContext::Disallowed(ImplTraitPosition::Path),
278 hir::ExprKind::Path(qpath)
280 ExprKind::Break(opt_label, opt_expr) => {
281 let opt_expr = opt_expr.as_ref().map(|x| self.lower_expr(x));
282 hir::ExprKind::Break(self.lower_jump_destination(e.id, *opt_label), opt_expr)
284 ExprKind::Continue(opt_label) => {
285 hir::ExprKind::Continue(self.lower_jump_destination(e.id, *opt_label))
287 ExprKind::Ret(e) => {
288 let e = e.as_ref().map(|x| self.lower_expr(x));
289 hir::ExprKind::Ret(e)
291 ExprKind::Yeet(sub_expr) => self.lower_expr_yeet(e.span, sub_expr.as_deref()),
292 ExprKind::InlineAsm(asm) => {
293 hir::ExprKind::InlineAsm(self.lower_inline_asm(e.span, asm))
295 ExprKind::Struct(se) => {
296 let rest = match &se.rest {
297 StructRest::Base(e) => Some(self.lower_expr(e)),
298 StructRest::Rest(sp) => {
299 self.tcx.sess.emit_err(BaseExpressionDoubleDot { span: *sp });
300 Some(&*self.arena.alloc(self.expr_err(*sp)))
302 StructRest::None => None,
304 hir::ExprKind::Struct(
305 self.arena.alloc(self.lower_qpath(
310 &ImplTraitContext::Disallowed(ImplTraitPosition::Path),
313 .alloc_from_iter(se.fields.iter().map(|x| self.lower_expr_field(x))),
317 ExprKind::Yield(opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()),
318 ExprKind::Err => hir::ExprKind::Err,
319 ExprKind::Try(sub_expr) => self.lower_expr_try(e.span, sub_expr),
321 ExprKind::Paren(_) | ExprKind::ForLoop(..) => unreachable!("already handled"),
323 ExprKind::MacCall(_) => panic!("{:?} shouldn't exist here", e.span),
326 hir::Expr { hir_id, kind, span: self.lower_span(e.span) }
330 fn lower_unop(&mut self, u: UnOp) -> hir::UnOp {
332 UnOp::Deref => hir::UnOp::Deref,
333 UnOp::Not => hir::UnOp::Not,
334 UnOp::Neg => hir::UnOp::Neg,
338 fn lower_binop(&mut self, b: BinOp) -> hir::BinOp {
341 BinOpKind::Add => hir::BinOpKind::Add,
342 BinOpKind::Sub => hir::BinOpKind::Sub,
343 BinOpKind::Mul => hir::BinOpKind::Mul,
344 BinOpKind::Div => hir::BinOpKind::Div,
345 BinOpKind::Rem => hir::BinOpKind::Rem,
346 BinOpKind::And => hir::BinOpKind::And,
347 BinOpKind::Or => hir::BinOpKind::Or,
348 BinOpKind::BitXor => hir::BinOpKind::BitXor,
349 BinOpKind::BitAnd => hir::BinOpKind::BitAnd,
350 BinOpKind::BitOr => hir::BinOpKind::BitOr,
351 BinOpKind::Shl => hir::BinOpKind::Shl,
352 BinOpKind::Shr => hir::BinOpKind::Shr,
353 BinOpKind::Eq => hir::BinOpKind::Eq,
354 BinOpKind::Lt => hir::BinOpKind::Lt,
355 BinOpKind::Le => hir::BinOpKind::Le,
356 BinOpKind::Ne => hir::BinOpKind::Ne,
357 BinOpKind::Ge => hir::BinOpKind::Ge,
358 BinOpKind::Gt => hir::BinOpKind::Gt,
360 span: self.lower_span(b.span),
364 fn lower_legacy_const_generics(
367 args: Vec<AstP<Expr>>,
368 legacy_args_idx: &[usize],
369 ) -> hir::ExprKind<'hir> {
370 let ExprKind::Path(None, path) = &mut f.kind else {
374 // Split the arguments into const generics and normal arguments
375 let mut real_args = vec![];
376 let mut generic_args = vec![];
377 for (idx, arg) in args.into_iter().enumerate() {
378 if legacy_args_idx.contains(&idx) {
379 let parent_def_id = self.current_hir_id_owner;
380 let node_id = self.next_node_id();
382 // Add a definition for the in-band const def.
383 self.create_def(parent_def_id.def_id, node_id, DefPathData::AnonConst, f.span);
385 let anon_const = AnonConst { id: node_id, value: arg };
386 generic_args.push(AngleBracketedArg::Arg(GenericArg::Const(anon_const)));
392 // Add generic args to the last element of the path.
393 let last_segment = path.segments.last_mut().unwrap();
394 assert!(last_segment.args.is_none());
395 last_segment.args = Some(AstP(GenericArgs::AngleBracketed(AngleBracketedArgs {
400 // Now lower everything as normal.
401 let f = self.lower_expr(&f);
402 hir::ExprKind::Call(f, self.lower_exprs(&real_args))
409 else_opt: Option<&Expr>,
410 ) -> hir::ExprKind<'hir> {
411 let lowered_cond = self.lower_cond(cond);
412 let then_expr = self.lower_block_expr(then);
413 if let Some(rslt) = else_opt {
416 self.arena.alloc(then_expr),
417 Some(self.lower_expr(rslt)),
420 hir::ExprKind::If(lowered_cond, self.arena.alloc(then_expr), None)
424 // Lowers a condition (i.e. `cond` in `if cond` or `while cond`), wrapping it in a terminating scope
425 // so that temporaries created in the condition don't live beyond it.
426 fn lower_cond(&mut self, cond: &Expr) -> &'hir hir::Expr<'hir> {
427 fn has_let_expr(expr: &Expr) -> bool {
429 ExprKind::Binary(_, lhs, rhs) => has_let_expr(lhs) || has_let_expr(rhs),
430 ExprKind::Let(..) => true,
435 // We have to take special care for `let` exprs in the condition, e.g. in
436 // `if let pat = val` or `if foo && let pat = val`, as we _do_ want `val` to live beyond the
437 // condition in this case.
439 // In order to mantain the drop behavior for the non `let` parts of the condition,
440 // we still wrap them in terminating scopes, e.g. `if foo && let pat = val` essentially
441 // gets transformed into `if { let _t = foo; _t } && let pat = val`
443 ExprKind::Binary(op @ Spanned { node: ast::BinOpKind::And, .. }, lhs, rhs)
444 if has_let_expr(cond) =>
446 let op = self.lower_binop(*op);
447 let lhs = self.lower_cond(lhs);
448 let rhs = self.lower_cond(rhs);
450 self.arena.alloc(self.expr(cond.span, hir::ExprKind::Binary(op, lhs, rhs)))
452 ExprKind::Let(..) => self.lower_expr(cond),
454 let cond = self.lower_expr(cond);
455 let reason = DesugaringKind::CondTemporary;
456 let span_block = self.mark_span_with_reason(reason, cond.span, None);
457 self.expr_drop_temps(span_block, cond)
462 // We desugar: `'label: while $cond $body` into:
466 // if { let _t = $cond; _t } {
475 // Wrap in a construct equivalent to `{ let _t = $cond; _t }`
476 // to preserve drop semantics since `while $cond { ... }` does not
477 // let temporaries live outside of `cond`.
478 fn lower_expr_while_in_loop_scope(
483 opt_label: Option<Label>,
484 ) -> hir::ExprKind<'hir> {
485 let lowered_cond = self.with_loop_condition_scope(|t| t.lower_cond(cond));
486 let then = self.lower_block_expr(body);
487 let expr_break = self.expr_break(span);
488 let stmt_break = self.stmt_expr(span, expr_break);
489 let else_blk = self.block_all(span, arena_vec![self; stmt_break], None);
490 let else_expr = self.arena.alloc(self.expr_block(else_blk));
491 let if_kind = hir::ExprKind::If(lowered_cond, self.arena.alloc(then), Some(else_expr));
492 let if_expr = self.expr(span, if_kind);
493 let block = self.block_expr(self.arena.alloc(if_expr));
494 let span = self.lower_span(span.with_hi(cond.span.hi()));
495 let opt_label = self.lower_label(opt_label);
496 hir::ExprKind::Loop(block, opt_label, hir::LoopSource::While, span)
499 /// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_output(<expr>) }`,
500 /// `try { <stmts>; }` into `{ <stmts>; ::std::ops::Try::from_output(()) }`
501 /// and save the block id to use it as a break target for desugaring of the `?` operator.
502 fn lower_expr_try_block(&mut self, body: &Block) -> hir::ExprKind<'hir> {
503 self.with_catch_scope(body.id, |this| {
504 let mut block = this.lower_block_noalloc(body, true);
506 // Final expression of the block (if present) or `()` with span at the end of block
507 let (try_span, tail_expr) = if let Some(expr) = block.expr.take() {
509 this.mark_span_with_reason(
510 DesugaringKind::TryBlock,
512 this.allow_try_trait.clone(),
517 let try_span = this.mark_span_with_reason(
518 DesugaringKind::TryBlock,
519 this.tcx.sess.source_map().end_point(body.span),
520 this.allow_try_trait.clone(),
523 (try_span, this.expr_unit(try_span))
526 let ok_wrapped_span =
527 this.mark_span_with_reason(DesugaringKind::TryBlock, tail_expr.span, None);
529 // `::std::ops::Try::from_output($tail_expr)`
530 block.expr = Some(this.wrap_in_try_constructor(
531 hir::LangItem::TryTraitFromOutput,
537 hir::ExprKind::Block(this.arena.alloc(block), None)
541 fn wrap_in_try_constructor(
543 lang_item: hir::LangItem,
545 expr: &'hir hir::Expr<'hir>,
547 ) -> &'hir hir::Expr<'hir> {
548 let constructor = self.arena.alloc(self.expr_lang_item_path(method_span, lang_item, None));
549 self.expr_call(overall_span, constructor, std::slice::from_ref(expr))
552 fn lower_arm(&mut self, arm: &Arm) -> hir::Arm<'hir> {
553 let pat = self.lower_pat(&arm.pat);
554 let guard = arm.guard.as_ref().map(|cond| {
555 if let ExprKind::Let(pat, scrutinee, span) = &cond.kind {
556 hir::Guard::IfLet(self.arena.alloc(hir::Let {
557 hir_id: self.next_id(),
558 span: self.lower_span(*span),
559 pat: self.lower_pat(pat),
561 init: self.lower_expr(scrutinee),
564 hir::Guard::If(self.lower_expr(cond))
567 let hir_id = self.next_id();
568 self.lower_attrs(hir_id, &arm.attrs);
573 body: self.lower_expr(&arm.body),
574 span: self.lower_span(arm.span),
578 /// Lower an `async` construct to a generator that implements `Future`.
583 /// std::future::identity_future(static move? |_task_context| -> <ret_ty> {
587 pub(super) fn make_async_expr(
589 capture_clause: CaptureBy,
590 outer_hir_id: hir::HirId,
591 closure_node_id: NodeId,
592 ret_ty: Option<hir::FnRetTy<'hir>>,
594 async_gen_kind: hir::AsyncGeneratorKind,
595 body: impl FnOnce(&mut Self) -> hir::Expr<'hir>,
596 ) -> hir::ExprKind<'hir> {
597 let output = ret_ty.unwrap_or_else(|| hir::FnRetTy::DefaultReturn(self.lower_span(span)));
599 // Resume argument type, which should be `&mut Context<'_>`.
600 // NOTE: Using the `'static` lifetime here is technically cheating.
601 // The `Future::poll` argument really is `&'a mut Context<'b>`, but we cannot
602 // express the fact that we are not storing it across yield-points yet,
603 // and we would thus run into lifetime errors.
604 // See <https://github.com/rust-lang/rust/issues/68923>.
605 // Our lowering makes sure we are not mis-using the `_task_context` input type
606 // in the sense that we are indeed not using it across yield points. We
607 // get a fresh `&mut Context` for each resume / call of `Future::poll`.
608 // This "cheating" was previously done with a `ResumeTy` that contained a raw
609 // pointer, and a `get_context` accessor that pulled the `Context` lifetimes
611 let context_lifetime_ident = Ident::with_dummy_span(kw::StaticLifetime);
612 let context_lifetime = self.arena.alloc(hir::Lifetime {
613 hir_id: self.next_id(),
614 ident: context_lifetime_ident,
615 res: hir::LifetimeName::Static,
618 hir::QPath::LangItem(hir::LangItem::Context, self.lower_span(span), None);
619 let context_ty = hir::MutTy {
620 ty: self.arena.alloc(hir::Ty {
621 hir_id: self.next_id(),
622 kind: hir::TyKind::Path(context_path),
623 span: self.lower_span(span),
625 mutbl: hir::Mutability::Mut,
627 let input_ty = hir::Ty {
628 hir_id: self.next_id(),
629 kind: hir::TyKind::Rptr(context_lifetime, context_ty),
630 span: self.lower_span(span),
633 // The closure/generator `FnDecl` takes a single (resume) argument of type `input_ty`.
634 let fn_decl = self.arena.alloc(hir::FnDecl {
635 inputs: arena_vec![self; input_ty],
638 implicit_self: hir::ImplicitSelfKind::None,
639 lifetime_elision_allowed: false,
642 // Lower the argument pattern/ident. The ident is used again in the `.await` lowering.
643 let (pat, task_context_hid) = self.pat_ident_binding_mode(
645 Ident::with_dummy_span(sym::_task_context),
646 hir::BindingAnnotation::MUT,
648 let param = hir::Param {
649 hir_id: self.next_id(),
651 ty_span: self.lower_span(span),
652 span: self.lower_span(span),
654 let params = arena_vec![self; param];
656 let body = self.lower_body(move |this| {
657 this.generator_kind = Some(hir::GeneratorKind::Async(async_gen_kind));
659 let old_ctx = this.task_context;
660 this.task_context = Some(task_context_hid);
661 let res = body(this);
662 this.task_context = old_ctx;
666 // `static |_task_context| -> <ret_ty> { body }`:
667 let generator_kind = {
668 let c = self.arena.alloc(hir::Closure {
669 def_id: self.local_def_id(closure_node_id),
670 binder: hir::ClosureBinder::Default,
672 bound_generic_params: &[],
675 fn_decl_span: self.lower_span(span),
677 movability: Some(hir::Movability::Static),
680 hir::ExprKind::Closure(c)
683 let track_caller = self
685 .get(&outer_hir_id.local_id)
686 .map_or(false, |attrs| attrs.into_iter().any(|attr| attr.has_name(sym::track_caller)));
688 let hir_id = self.lower_node_id(closure_node_id);
690 self.mark_span_with_reason(DesugaringKind::Async, span, self.allow_gen_future.clone());
695 kind: AttrKind::Normal(ptr::P(NormalAttr {
697 path: Path::from_ident(Ident::new(sym::track_caller, span)),
698 args: AttrArgs::Empty,
703 id: self.tcx.sess.parse_sess.attr_id_generator.mk_attr_id(),
704 style: AttrStyle::Outer,
710 let generator = hir::Expr { hir_id, kind: generator_kind, span: self.lower_span(span) };
713 // For some reason, the async block needs to flow through *any*
714 // call (like the identity function), as otherwise type and lifetime
715 // inference have a hard time figuring things out.
716 // Without this, we would get:
717 // E0720 in src/test/ui/impl-trait/in-trait/default-body-with-rpit.rs
718 // E0700 in src/test/ui/self/self_lifetime-async.rs
720 // `future::identity_future`:
721 let identity_future =
722 self.expr_lang_item_path(unstable_span, hir::LangItem::IdentityFuture, None);
724 // `future::identity_future(generator)`:
725 hir::ExprKind::Call(self.arena.alloc(identity_future), arena_vec![self; generator])
728 /// Desugar `<expr>.await` into:
729 /// ```ignore (pseudo-rust)
730 /// match ::std::future::IntoFuture::into_future(<expr>) {
731 /// mut __awaitee => loop {
732 /// match unsafe { ::std::future::Future::poll(
733 /// <::std::pin::Pin>::new_unchecked(&mut __awaitee),
736 /// ::std::task::Poll::Ready(result) => break result,
737 /// ::std::task::Poll::Pending => {}
739 /// task_context = yield ();
743 fn lower_expr_await(&mut self, dot_await_span: Span, expr: &Expr) -> hir::ExprKind<'hir> {
744 let full_span = expr.span.to(dot_await_span);
745 match self.generator_kind {
746 Some(hir::GeneratorKind::Async(_)) => {}
747 Some(hir::GeneratorKind::Gen) | None => {
748 self.tcx.sess.emit_err(AwaitOnlyInAsyncFnAndBlocks {
750 item_span: self.current_item,
754 let span = self.mark_span_with_reason(DesugaringKind::Await, dot_await_span, None);
755 let gen_future_span = self.mark_span_with_reason(
756 DesugaringKind::Await,
758 self.allow_gen_future.clone(),
760 let expr = self.lower_expr_mut(expr);
761 let expr_hir_id = expr.hir_id;
763 // Note that the name of this binding must not be changed to something else because
764 // debuggers and debugger extensions expect it to be called `__awaitee`. They use
765 // this name to identify what is being awaited by a suspended async functions.
766 let awaitee_ident = Ident::with_dummy_span(sym::__awaitee);
767 let (awaitee_pat, awaitee_pat_hid) =
768 self.pat_ident_binding_mode(span, awaitee_ident, hir::BindingAnnotation::MUT);
770 let task_context_ident = Ident::with_dummy_span(sym::_task_context);
773 // ::std::future::Future::poll(
774 // ::std::pin::Pin::new_unchecked(&mut __awaitee),
779 let awaitee = self.expr_ident(span, awaitee_ident, awaitee_pat_hid);
780 let ref_mut_awaitee = self.expr_mut_addr_of(span, awaitee);
781 let task_context = if let Some(task_context_hid) = self.task_context {
782 self.expr_ident_mut(span, task_context_ident, task_context_hid)
784 // Use of `await` outside of an async context, we cannot use `task_context` here.
787 let new_unchecked = self.expr_call_lang_item_fn_mut(
789 hir::LangItem::PinNewUnchecked,
790 arena_vec![self; ref_mut_awaitee],
793 let call = self.expr_call_lang_item_fn(
795 hir::LangItem::FuturePoll,
796 arena_vec![self; new_unchecked, task_context],
799 self.arena.alloc(self.expr_unsafe(call))
802 // `::std::task::Poll::Ready(result) => break result`
803 let loop_node_id = self.next_node_id();
804 let loop_hir_id = self.lower_node_id(loop_node_id);
806 let x_ident = Ident::with_dummy_span(sym::result);
807 let (x_pat, x_pat_hid) = self.pat_ident(gen_future_span, x_ident);
808 let x_expr = self.expr_ident(gen_future_span, x_ident, x_pat_hid);
809 let ready_field = self.single_pat_field(gen_future_span, x_pat);
810 let ready_pat = self.pat_lang_item_variant(
812 hir::LangItem::PollReady,
816 let break_x = self.with_loop_scope(loop_node_id, move |this| {
818 hir::ExprKind::Break(this.lower_loop_destination(None), Some(x_expr));
819 this.arena.alloc(this.expr(gen_future_span, expr_break))
821 self.arm(ready_pat, break_x)
824 // `::std::task::Poll::Pending => {}`
826 let pending_pat = self.pat_lang_item_variant(
828 hir::LangItem::PollPending,
832 let empty_block = self.expr_block_empty(span);
833 self.arm(pending_pat, empty_block)
836 let inner_match_stmt = {
837 let match_expr = self.expr_match(
840 arena_vec![self; ready_arm, pending_arm],
841 hir::MatchSource::AwaitDesugar,
843 self.stmt_expr(span, match_expr)
846 // task_context = yield ();
848 let unit = self.expr_unit(span);
849 let yield_expr = self.expr(
851 hir::ExprKind::Yield(unit, hir::YieldSource::Await { expr: Some(expr_hir_id) }),
853 let yield_expr = self.arena.alloc(yield_expr);
855 if let Some(task_context_hid) = self.task_context {
856 let lhs = self.expr_ident(span, task_context_ident, task_context_hid);
858 self.expr(span, hir::ExprKind::Assign(lhs, yield_expr, self.lower_span(span)));
859 self.stmt_expr(span, assign)
861 // Use of `await` outside of an async context. Return `yield_expr` so that we can
862 // proceed with type checking.
863 self.stmt(span, hir::StmtKind::Semi(yield_expr))
867 let loop_block = self.block_all(span, arena_vec![self; inner_match_stmt, yield_stmt], None);
870 let loop_expr = self.arena.alloc(hir::Expr {
872 kind: hir::ExprKind::Loop(
875 hir::LoopSource::Loop,
876 self.lower_span(span),
878 span: self.lower_span(span),
881 // mut __awaitee => loop { ... }
882 let awaitee_arm = self.arm(awaitee_pat, loop_expr);
884 // `match ::std::future::IntoFuture::into_future(<expr>) { ... }`
885 let into_future_span = self.mark_span_with_reason(
886 DesugaringKind::Await,
888 self.allow_into_future.clone(),
890 let into_future_expr = self.expr_call_lang_item_fn(
892 hir::LangItem::IntoFutureIntoFuture,
893 arena_vec![self; expr],
897 // match <into_future_expr> {
898 // mut __awaitee => loop { .. }
900 hir::ExprKind::Match(
902 arena_vec![self; awaitee_arm],
903 hir::MatchSource::AwaitDesugar,
907 fn lower_expr_closure(
909 binder: &ClosureBinder,
910 capture_clause: CaptureBy,
912 movability: Movability,
917 ) -> hir::ExprKind<'hir> {
918 let (binder_clause, generic_params) = self.lower_closure_binder(binder);
920 let (body_id, generator_option) = self.with_new_scopes(move |this| {
921 let prev = this.current_item;
922 this.current_item = Some(fn_decl_span);
923 let mut generator_kind = None;
924 let body_id = this.lower_fn_body(decl, |this| {
925 let e = this.lower_expr_mut(body);
926 generator_kind = this.generator_kind;
929 let generator_option =
930 this.generator_movability_for_fn(&decl, fn_decl_span, generator_kind, movability);
931 this.current_item = prev;
932 (body_id, generator_option)
935 let bound_generic_params = self.lower_lifetime_binder(closure_id, generic_params);
936 // Lower outside new scope to preserve `is_in_loop_condition`.
937 let fn_decl = self.lower_fn_decl(decl, closure_id, fn_decl_span, FnDeclKind::Closure, None);
939 let c = self.arena.alloc(hir::Closure {
940 def_id: self.local_def_id(closure_id),
941 binder: binder_clause,
943 bound_generic_params,
946 fn_decl_span: self.lower_span(fn_decl_span),
947 fn_arg_span: Some(self.lower_span(fn_arg_span)),
948 movability: generator_option,
951 hir::ExprKind::Closure(c)
954 fn generator_movability_for_fn(
958 generator_kind: Option<hir::GeneratorKind>,
959 movability: Movability,
960 ) -> Option<hir::Movability> {
961 match generator_kind {
962 Some(hir::GeneratorKind::Gen) => {
963 if decl.inputs.len() > 1 {
964 self.tcx.sess.emit_err(GeneratorTooManyParameters { fn_decl_span });
968 Some(hir::GeneratorKind::Async(_)) => {
969 panic!("non-`async` closure body turned `async` during lowering");
972 if movability == Movability::Static {
973 self.tcx.sess.emit_err(ClosureCannotBeStatic { fn_decl_span });
980 fn lower_closure_binder<'c>(
982 binder: &'c ClosureBinder,
983 ) -> (hir::ClosureBinder, &'c [GenericParam]) {
984 let (binder, params) = match binder {
985 ClosureBinder::NotPresent => (hir::ClosureBinder::Default, &[][..]),
986 ClosureBinder::For { span, generic_params } => {
987 let span = self.lower_span(*span);
988 (hir::ClosureBinder::For { span }, &**generic_params)
995 fn lower_expr_async_closure(
997 binder: &ClosureBinder,
998 capture_clause: CaptureBy,
1000 closure_hir_id: hir::HirId,
1001 inner_closure_id: NodeId,
1006 ) -> hir::ExprKind<'hir> {
1007 if let &ClosureBinder::For { span, .. } = binder {
1008 self.tcx.sess.emit_err(NotSupportedForLifetimeBinderAsyncClosure { span });
1011 let (binder_clause, generic_params) = self.lower_closure_binder(binder);
1014 FnDecl { inputs: decl.inputs.clone(), output: FnRetTy::Default(fn_decl_span) };
1016 let body = self.with_new_scopes(|this| {
1017 // FIXME(cramertj): allow `async` non-`move` closures with arguments.
1018 if capture_clause == CaptureBy::Ref && !decl.inputs.is_empty() {
1019 this.tcx.sess.emit_err(AsyncNonMoveClosureNotSupported { fn_decl_span });
1022 // Transform `async |x: u8| -> X { ... }` into
1023 // `|x: u8| identity_future(|| -> X { ... })`.
1024 let body_id = this.lower_fn_body(&outer_decl, |this| {
1025 let async_ret_ty = if let FnRetTy::Ty(ty) = &decl.output {
1026 let itctx = ImplTraitContext::Disallowed(ImplTraitPosition::AsyncBlock);
1027 Some(hir::FnRetTy::Return(this.lower_ty(&ty, &itctx)))
1032 let async_body = this.make_async_expr(
1038 hir::AsyncGeneratorKind::Closure,
1039 |this| this.with_new_scopes(|this| this.lower_expr_mut(body)),
1041 this.expr(fn_decl_span, async_body)
1046 let bound_generic_params = self.lower_lifetime_binder(closure_id, generic_params);
1047 // We need to lower the declaration outside the new scope, because we
1048 // have to conserve the state of being inside a loop condition for the
1049 // closure argument types.
1051 self.lower_fn_decl(&outer_decl, closure_id, fn_decl_span, FnDeclKind::Closure, None);
1053 let c = self.arena.alloc(hir::Closure {
1054 def_id: self.local_def_id(closure_id),
1055 binder: binder_clause,
1057 bound_generic_params,
1060 fn_decl_span: self.lower_span(fn_decl_span),
1061 fn_arg_span: Some(self.lower_span(fn_arg_span)),
1064 hir::ExprKind::Closure(c)
1067 /// Destructure the LHS of complex assignments.
1068 /// For instance, lower `(a, b) = t` to `{ let (lhs1, lhs2) = t; a = lhs1; b = lhs2; }`.
1069 fn lower_expr_assign(
1075 ) -> hir::ExprKind<'hir> {
1076 // Return early in case of an ordinary assignment.
1077 fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_>, lhs: &Expr) -> bool {
1080 | ExprKind::Struct(..)
1082 | ExprKind::Underscore => false,
1083 // Check for tuple struct constructor.
1084 ExprKind::Call(callee, ..) => lower_ctx.extract_tuple_struct_path(callee).is_none(),
1085 ExprKind::Paren(e) => {
1087 // We special-case `(..)` for consistency with patterns.
1088 ExprKind::Range(None, None, RangeLimits::HalfOpen) => false,
1089 _ => is_ordinary(lower_ctx, e),
1095 if is_ordinary(self, lhs) {
1096 return hir::ExprKind::Assign(
1097 self.lower_expr(lhs),
1098 self.lower_expr(rhs),
1099 self.lower_span(eq_sign_span),
1103 let mut assignments = vec![];
1105 // The LHS becomes a pattern: `(lhs1, lhs2)`.
1106 let pat = self.destructure_assign(lhs, eq_sign_span, &mut assignments);
1107 let rhs = self.lower_expr(rhs);
1109 // Introduce a `let` for destructuring: `let (lhs1, lhs2) = t`.
1110 let destructure_let = self.stmt_let_pat(
1115 hir::LocalSource::AssignDesugar(self.lower_span(eq_sign_span)),
1118 // `a = lhs1; b = lhs2;`.
1121 .alloc_from_iter(std::iter::once(destructure_let).chain(assignments.into_iter()));
1123 // Wrap everything in a block.
1124 hir::ExprKind::Block(&self.block_all(whole_span, stmts, None), None)
1127 /// If the given expression is a path to a tuple struct, returns that path.
1128 /// It is not a complete check, but just tries to reject most paths early
1129 /// if they are not tuple structs.
1130 /// Type checking will take care of the full validation later.
1131 fn extract_tuple_struct_path<'a>(
1134 ) -> Option<(&'a Option<AstP<QSelf>>, &'a Path)> {
1135 if let ExprKind::Path(qself, path) = &expr.kind {
1136 // Does the path resolve to something disallowed in a tuple struct/variant pattern?
1137 if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
1138 if let Some(res) = partial_res.full_res() && !res.expected_in_tuple_struct_pat() {
1142 return Some((qself, path));
1147 /// If the given expression is a path to a unit struct, returns that path.
1148 /// It is not a complete check, but just tries to reject most paths early
1149 /// if they are not unit structs.
1150 /// Type checking will take care of the full validation later.
1151 fn extract_unit_struct_path<'a>(
1154 ) -> Option<(&'a Option<AstP<QSelf>>, &'a Path)> {
1155 if let ExprKind::Path(qself, path) = &expr.kind {
1156 // Does the path resolve to something disallowed in a unit struct/variant pattern?
1157 if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
1158 if let Some(res) = partial_res.full_res() && !res.expected_in_unit_struct_pat() {
1162 return Some((qself, path));
1167 /// Convert the LHS of a destructuring assignment to a pattern.
1168 /// Each sub-assignment is recorded in `assignments`.
1169 fn destructure_assign(
1173 assignments: &mut Vec<hir::Stmt<'hir>>,
1174 ) -> &'hir hir::Pat<'hir> {
1175 self.arena.alloc(self.destructure_assign_mut(lhs, eq_sign_span, assignments))
1178 fn destructure_assign_mut(
1182 assignments: &mut Vec<hir::Stmt<'hir>>,
1183 ) -> hir::Pat<'hir> {
1185 // Underscore pattern.
1186 ExprKind::Underscore => {
1187 return self.pat_without_dbm(lhs.span, hir::PatKind::Wild);
1190 ExprKind::Array(elements) => {
1192 self.destructure_sequence(elements, "slice", eq_sign_span, assignments);
1193 let slice_pat = if let Some((i, span)) = rest {
1194 let (before, after) = pats.split_at(i);
1195 hir::PatKind::Slice(
1197 Some(self.arena.alloc(self.pat_without_dbm(span, hir::PatKind::Wild))),
1201 hir::PatKind::Slice(pats, None, &[])
1203 return self.pat_without_dbm(lhs.span, slice_pat);
1206 ExprKind::Call(callee, args) => {
1207 if let Some((qself, path)) = self.extract_tuple_struct_path(callee) {
1208 let (pats, rest) = self.destructure_sequence(
1210 "tuple struct or variant",
1214 let qpath = self.lower_qpath(
1218 ParamMode::Optional,
1219 &ImplTraitContext::Disallowed(ImplTraitPosition::Path),
1221 // Destructure like a tuple struct.
1222 let tuple_struct_pat = hir::PatKind::TupleStruct(
1225 hir::DotDotPos::new(rest.map(|r| r.0)),
1227 return self.pat_without_dbm(lhs.span, tuple_struct_pat);
1230 // Unit structs and enum variants.
1231 ExprKind::Path(..) => {
1232 if let Some((qself, path)) = self.extract_unit_struct_path(lhs) {
1233 let qpath = self.lower_qpath(
1237 ParamMode::Optional,
1238 &ImplTraitContext::Disallowed(ImplTraitPosition::Path),
1240 // Destructure like a unit struct.
1241 let unit_struct_pat = hir::PatKind::Path(qpath);
1242 return self.pat_without_dbm(lhs.span, unit_struct_pat);
1246 ExprKind::Struct(se) => {
1247 let field_pats = self.arena.alloc_from_iter(se.fields.iter().map(|f| {
1248 let pat = self.destructure_assign(&f.expr, eq_sign_span, assignments);
1250 hir_id: self.next_id(),
1251 ident: self.lower_ident(f.ident),
1253 is_shorthand: f.is_shorthand,
1254 span: self.lower_span(f.span),
1257 let qpath = self.lower_qpath(
1261 ParamMode::Optional,
1262 &ImplTraitContext::Disallowed(ImplTraitPosition::Path),
1264 let fields_omitted = match &se.rest {
1265 StructRest::Base(e) => {
1266 self.tcx.sess.emit_err(FunctionalRecordUpdateDestructuringAssignemnt {
1271 StructRest::Rest(_) => true,
1272 StructRest::None => false,
1274 let struct_pat = hir::PatKind::Struct(qpath, field_pats, fields_omitted);
1275 return self.pat_without_dbm(lhs.span, struct_pat);
1278 ExprKind::Tup(elements) => {
1280 self.destructure_sequence(elements, "tuple", eq_sign_span, assignments);
1281 let tuple_pat = hir::PatKind::Tuple(pats, hir::DotDotPos::new(rest.map(|r| r.0)));
1282 return self.pat_without_dbm(lhs.span, tuple_pat);
1284 ExprKind::Paren(e) => {
1285 // We special-case `(..)` for consistency with patterns.
1286 if let ExprKind::Range(None, None, RangeLimits::HalfOpen) = e.kind {
1287 let tuple_pat = hir::PatKind::Tuple(&[], hir::DotDotPos::new(Some(0)));
1288 return self.pat_without_dbm(lhs.span, tuple_pat);
1290 return self.destructure_assign_mut(e, eq_sign_span, assignments);
1295 // Treat all other cases as normal lvalue.
1296 let ident = Ident::new(sym::lhs, self.lower_span(lhs.span));
1297 let (pat, binding) = self.pat_ident_mut(lhs.span, ident);
1298 let ident = self.expr_ident(lhs.span, ident, binding);
1300 hir::ExprKind::Assign(self.lower_expr(lhs), ident, self.lower_span(eq_sign_span));
1301 let expr = self.expr(lhs.span, assign);
1302 assignments.push(self.stmt_expr(lhs.span, expr));
1306 /// Destructure a sequence of expressions occurring on the LHS of an assignment.
1307 /// Such a sequence occurs in a tuple (struct)/slice.
1308 /// Return a sequence of corresponding patterns, and the index and the span of `..` if it
1310 /// Each sub-assignment is recorded in `assignments`.
1311 fn destructure_sequence(
1313 elements: &[AstP<Expr>],
1316 assignments: &mut Vec<hir::Stmt<'hir>>,
1317 ) -> (&'hir [hir::Pat<'hir>], Option<(usize, Span)>) {
1318 let mut rest = None;
1320 self.arena.alloc_from_iter(elements.iter().enumerate().filter_map(|(i, e)| {
1321 // Check for `..` pattern.
1322 if let ExprKind::Range(None, None, RangeLimits::HalfOpen) = e.kind {
1323 if let Some((_, prev_span)) = rest {
1324 self.ban_extra_rest_pat(e.span, prev_span, ctx);
1326 rest = Some((i, e.span));
1330 Some(self.destructure_assign_mut(e, eq_sign_span, assignments))
1336 /// Desugar `<start>..=<end>` into `std::ops::RangeInclusive::new(<start>, <end>)`.
1337 fn lower_expr_range_closed(&mut self, span: Span, e1: &Expr, e2: &Expr) -> hir::ExprKind<'hir> {
1338 let e1 = self.lower_expr_mut(e1);
1339 let e2 = self.lower_expr_mut(e2);
1341 hir::QPath::LangItem(hir::LangItem::RangeInclusiveNew, self.lower_span(span), None);
1342 let fn_expr = self.arena.alloc(self.expr(span, hir::ExprKind::Path(fn_path)));
1343 hir::ExprKind::Call(fn_expr, arena_vec![self; e1, e2])
1346 fn lower_expr_range(
1352 ) -> hir::ExprKind<'hir> {
1353 use rustc_ast::RangeLimits::*;
1355 let lang_item = match (e1, e2, lims) {
1356 (None, None, HalfOpen) => hir::LangItem::RangeFull,
1357 (Some(..), None, HalfOpen) => hir::LangItem::RangeFrom,
1358 (None, Some(..), HalfOpen) => hir::LangItem::RangeTo,
1359 (Some(..), Some(..), HalfOpen) => hir::LangItem::Range,
1360 (None, Some(..), Closed) => hir::LangItem::RangeToInclusive,
1361 (Some(..), Some(..), Closed) => unreachable!(),
1362 (start, None, Closed) => {
1363 self.tcx.sess.emit_err(InclusiveRangeWithNoEnd { span });
1365 Some(..) => hir::LangItem::RangeFrom,
1366 None => hir::LangItem::RangeFull,
1371 let fields = self.arena.alloc_from_iter(
1372 e1.iter().map(|e| (sym::start, e)).chain(e2.iter().map(|e| (sym::end, e))).map(
1374 let expr = self.lower_expr(&e);
1375 let ident = Ident::new(s, self.lower_span(e.span));
1376 self.expr_field(ident, expr, e.span)
1381 hir::ExprKind::Struct(
1382 self.arena.alloc(hir::QPath::LangItem(lang_item, self.lower_span(span), None)),
1388 fn lower_label(&self, opt_label: Option<Label>) -> Option<Label> {
1389 let label = opt_label?;
1390 Some(Label { ident: self.lower_ident(label.ident) })
1393 fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
1394 let target_id = match destination {
1396 if let Some(loop_id) = self.resolver.get_label_res(id) {
1397 Ok(self.lower_node_id(loop_id))
1399 Err(hir::LoopIdError::UnresolvedLabel)
1404 .map(|id| Ok(self.lower_node_id(id)))
1405 .unwrap_or(Err(hir::LoopIdError::OutsideLoopScope)),
1407 let label = self.lower_label(destination.map(|(_, label)| label));
1408 hir::Destination { label, target_id }
1411 fn lower_jump_destination(&mut self, id: NodeId, opt_label: Option<Label>) -> hir::Destination {
1412 if self.is_in_loop_condition && opt_label.is_none() {
1415 target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition),
1418 self.lower_loop_destination(opt_label.map(|label| (id, label)))
1422 fn with_catch_scope<T>(&mut self, catch_id: NodeId, f: impl FnOnce(&mut Self) -> T) -> T {
1423 let old_scope = self.catch_scope.replace(catch_id);
1424 let result = f(self);
1425 self.catch_scope = old_scope;
1429 fn with_loop_scope<T>(&mut self, loop_id: NodeId, f: impl FnOnce(&mut Self) -> T) -> T {
1430 // We're no longer in the base loop's condition; we're in another loop.
1431 let was_in_loop_condition = self.is_in_loop_condition;
1432 self.is_in_loop_condition = false;
1434 let old_scope = self.loop_scope.replace(loop_id);
1435 let result = f(self);
1436 self.loop_scope = old_scope;
1438 self.is_in_loop_condition = was_in_loop_condition;
1443 fn with_loop_condition_scope<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T {
1444 let was_in_loop_condition = self.is_in_loop_condition;
1445 self.is_in_loop_condition = true;
1447 let result = f(self);
1449 self.is_in_loop_condition = was_in_loop_condition;
1454 fn lower_expr_field(&mut self, f: &ExprField) -> hir::ExprField<'hir> {
1455 let hir_id = self.lower_node_id(f.id);
1456 self.lower_attrs(hir_id, &f.attrs);
1459 ident: self.lower_ident(f.ident),
1460 expr: self.lower_expr(&f.expr),
1461 span: self.lower_span(f.span),
1462 is_shorthand: f.is_shorthand,
1466 fn lower_expr_yield(&mut self, span: Span, opt_expr: Option<&Expr>) -> hir::ExprKind<'hir> {
1467 match self.generator_kind {
1468 Some(hir::GeneratorKind::Gen) => {}
1469 Some(hir::GeneratorKind::Async(_)) => {
1470 self.tcx.sess.emit_err(AsyncGeneratorsNotSupported { span });
1472 None => self.generator_kind = Some(hir::GeneratorKind::Gen),
1476 opt_expr.as_ref().map(|x| self.lower_expr(x)).unwrap_or_else(|| self.expr_unit(span));
1478 hir::ExprKind::Yield(expr, hir::YieldSource::Yield)
1481 /// Desugar `ExprForLoop` from: `[opt_ident]: for <pat> in <head> <body>` into:
1482 /// ```ignore (pseudo-rust)
1484 /// let result = match IntoIterator::into_iter(<head>) {
1486 /// [opt_ident]: loop {
1487 /// match Iterator::next(&mut iter) {
1489 /// Some(<pat>) => <body>,
1503 opt_label: Option<Label>,
1504 ) -> hir::Expr<'hir> {
1505 let head = self.lower_expr_mut(head);
1506 let pat = self.lower_pat(pat);
1508 self.mark_span_with_reason(DesugaringKind::ForLoop, self.lower_span(e.span), None);
1509 let head_span = self.mark_span_with_reason(DesugaringKind::ForLoop, head.span, None);
1510 let pat_span = self.mark_span_with_reason(DesugaringKind::ForLoop, pat.span, None);
1514 let break_expr = self.with_loop_scope(e.id, |this| this.expr_break_alloc(for_span));
1515 let pat = self.pat_none(for_span);
1516 self.arm(pat, break_expr)
1519 // Some(<pat>) => <body>,
1521 let some_pat = self.pat_some(pat_span, pat);
1522 let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body, false));
1523 let body_expr = self.arena.alloc(self.expr_block(body_block));
1524 self.arm(some_pat, body_expr)
1528 let iter = Ident::with_dummy_span(sym::iter);
1529 let (iter_pat, iter_pat_nid) =
1530 self.pat_ident_binding_mode(head_span, iter, hir::BindingAnnotation::MUT);
1532 // `match Iterator::next(&mut iter) { ... }`
1534 let iter = self.expr_ident(head_span, iter, iter_pat_nid);
1535 let ref_mut_iter = self.expr_mut_addr_of(head_span, iter);
1536 let next_expr = self.expr_call_lang_item_fn(
1538 hir::LangItem::IteratorNext,
1539 arena_vec![self; ref_mut_iter],
1542 let arms = arena_vec![self; none_arm, some_arm];
1544 self.expr_match(head_span, next_expr, arms, hir::MatchSource::ForLoopDesugar)
1546 let match_stmt = self.stmt_expr(for_span, match_expr);
1548 let loop_block = self.block_all(for_span, arena_vec![self; match_stmt], None);
1550 // `[opt_ident]: loop { ... }`
1551 let kind = hir::ExprKind::Loop(
1553 self.lower_label(opt_label),
1554 hir::LoopSource::ForLoop,
1555 self.lower_span(for_span.with_hi(head.span.hi())),
1558 self.arena.alloc(hir::Expr { hir_id: self.lower_node_id(e.id), kind, span: for_span });
1560 // `mut iter => { ... }`
1561 let iter_arm = self.arm(iter_pat, loop_expr);
1563 // `match ::std::iter::IntoIterator::into_iter(<head>) { ... }`
1564 let into_iter_expr = {
1565 self.expr_call_lang_item_fn(
1567 hir::LangItem::IntoIterIntoIter,
1568 arena_vec![self; head],
1573 let match_expr = self.arena.alloc(self.expr_match(
1576 arena_vec![self; iter_arm],
1577 hir::MatchSource::ForLoopDesugar,
1580 // This is effectively `{ let _result = ...; _result }`.
1581 // The construct was introduced in #21984 and is necessary to make sure that
1582 // temporaries in the `head` expression are dropped and do not leak to the
1583 // surrounding scope of the `match` since the `match` is not a terminating scope.
1585 // Also, add the attributes to the outer returned expr node.
1586 let expr = self.expr_drop_temps_mut(for_span, match_expr);
1587 self.lower_attrs(expr.hir_id, &e.attrs);
1591 /// Desugar `ExprKind::Try` from: `<expr>?` into:
1592 /// ```ignore (pseudo-rust)
1593 /// match Try::branch(<expr>) {
1594 /// ControlFlow::Continue(val) => #[allow(unreachable_code)] val,,
1595 /// ControlFlow::Break(residual) =>
1596 /// #[allow(unreachable_code)]
1597 /// // If there is an enclosing `try {...}`:
1598 /// break 'catch_target Try::from_residual(residual),
1600 /// return Try::from_residual(residual),
1603 fn lower_expr_try(&mut self, span: Span, sub_expr: &Expr) -> hir::ExprKind<'hir> {
1604 let unstable_span = self.mark_span_with_reason(
1605 DesugaringKind::QuestionMark,
1607 self.allow_try_trait.clone(),
1609 let try_span = self.tcx.sess.source_map().end_point(span);
1610 let try_span = self.mark_span_with_reason(
1611 DesugaringKind::QuestionMark,
1613 self.allow_try_trait.clone(),
1616 // `Try::branch(<expr>)`
1619 let sub_expr = self.lower_expr_mut(sub_expr);
1621 self.expr_call_lang_item_fn(
1623 hir::LangItem::TryTraitBranch,
1624 arena_vec![self; sub_expr],
1629 // `#[allow(unreachable_code)]`
1630 let attr = attr::mk_attr_nested_word(
1631 &self.tcx.sess.parse_sess.attr_id_generator,
1634 sym::unreachable_code,
1635 self.lower_span(span),
1637 let attrs: AttrVec = thin_vec![attr];
1639 // `ControlFlow::Continue(val) => #[allow(unreachable_code)] val,`
1640 let continue_arm = {
1641 let val_ident = Ident::with_dummy_span(sym::val);
1642 let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
1643 let val_expr = self.expr_ident(span, val_ident, val_pat_nid);
1644 self.lower_attrs(val_expr.hir_id, &attrs);
1645 let continue_pat = self.pat_cf_continue(unstable_span, val_pat);
1646 self.arm(continue_pat, val_expr)
1649 // `ControlFlow::Break(residual) =>
1650 // #[allow(unreachable_code)]
1651 // return Try::from_residual(residual),`
1653 let residual_ident = Ident::with_dummy_span(sym::residual);
1654 let (residual_local, residual_local_nid) = self.pat_ident(try_span, residual_ident);
1655 let residual_expr = self.expr_ident_mut(try_span, residual_ident, residual_local_nid);
1656 let from_residual_expr = self.wrap_in_try_constructor(
1657 hir::LangItem::TryTraitFromResidual,
1659 self.arena.alloc(residual_expr),
1662 let ret_expr = if let Some(catch_node) = self.catch_scope {
1663 let target_id = Ok(self.lower_node_id(catch_node));
1664 self.arena.alloc(self.expr(
1666 hir::ExprKind::Break(
1667 hir::Destination { label: None, target_id },
1668 Some(from_residual_expr),
1672 self.arena.alloc(self.expr(try_span, hir::ExprKind::Ret(Some(from_residual_expr))))
1674 self.lower_attrs(ret_expr.hir_id, &attrs);
1676 let break_pat = self.pat_cf_break(try_span, residual_local);
1677 self.arm(break_pat, ret_expr)
1680 hir::ExprKind::Match(
1682 arena_vec![self; break_arm, continue_arm],
1683 hir::MatchSource::TryDesugar,
1687 /// Desugar `ExprKind::Yeet` from: `do yeet <expr>` into:
1688 /// ```ignore(illustrative)
1689 /// // If there is an enclosing `try {...}`:
1690 /// break 'catch_target FromResidual::from_residual(Yeet(residual));
1692 /// return FromResidual::from_residual(Yeet(residual));
1694 /// But to simplify this, there's a `from_yeet` lang item function which
1695 /// handles the combined `FromResidual::from_residual(Yeet(residual))`.
1696 fn lower_expr_yeet(&mut self, span: Span, sub_expr: Option<&Expr>) -> hir::ExprKind<'hir> {
1697 // The expression (if present) or `()` otherwise.
1698 let (yeeted_span, yeeted_expr) = if let Some(sub_expr) = sub_expr {
1699 (sub_expr.span, self.lower_expr(sub_expr))
1701 (self.mark_span_with_reason(DesugaringKind::YeetExpr, span, None), self.expr_unit(span))
1704 let unstable_span = self.mark_span_with_reason(
1705 DesugaringKind::YeetExpr,
1707 self.allow_try_trait.clone(),
1710 let from_yeet_expr = self.wrap_in_try_constructor(
1711 hir::LangItem::TryTraitFromYeet,
1717 if let Some(catch_node) = self.catch_scope {
1718 let target_id = Ok(self.lower_node_id(catch_node));
1719 hir::ExprKind::Break(hir::Destination { label: None, target_id }, Some(from_yeet_expr))
1721 hir::ExprKind::Ret(Some(from_yeet_expr))
1725 // =========================================================================
1726 // Helper methods for building HIR.
1727 // =========================================================================
1729 /// Wrap the given `expr` in a terminating scope using `hir::ExprKind::DropTemps`.
1731 /// In terms of drop order, it has the same effect as wrapping `expr` in
1732 /// `{ let _t = $expr; _t }` but should provide better compile-time performance.
1734 /// The drop order can be important in e.g. `if expr { .. }`.
1735 pub(super) fn expr_drop_temps(
1738 expr: &'hir hir::Expr<'hir>,
1739 ) -> &'hir hir::Expr<'hir> {
1740 self.arena.alloc(self.expr_drop_temps_mut(span, expr))
1743 pub(super) fn expr_drop_temps_mut(
1746 expr: &'hir hir::Expr<'hir>,
1747 ) -> hir::Expr<'hir> {
1748 self.expr(span, hir::ExprKind::DropTemps(expr))
1754 arg: &'hir hir::Expr<'hir>,
1755 arms: &'hir [hir::Arm<'hir>],
1756 source: hir::MatchSource,
1757 ) -> hir::Expr<'hir> {
1758 self.expr(span, hir::ExprKind::Match(arg, arms, source))
1761 fn expr_break(&mut self, span: Span) -> hir::Expr<'hir> {
1762 let expr_break = hir::ExprKind::Break(self.lower_loop_destination(None), None);
1763 self.expr(span, expr_break)
1766 fn expr_break_alloc(&mut self, span: Span) -> &'hir hir::Expr<'hir> {
1767 let expr_break = self.expr_break(span);
1768 self.arena.alloc(expr_break)
1771 fn expr_mut_addr_of(&mut self, span: Span, e: &'hir hir::Expr<'hir>) -> hir::Expr<'hir> {
1772 self.expr(span, hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, e))
1775 fn expr_unit(&mut self, sp: Span) -> &'hir hir::Expr<'hir> {
1776 self.arena.alloc(self.expr(sp, hir::ExprKind::Tup(&[])))
1782 e: &'hir hir::Expr<'hir>,
1783 args: &'hir [hir::Expr<'hir>],
1784 ) -> hir::Expr<'hir> {
1785 self.expr(span, hir::ExprKind::Call(e, args))
1791 e: &'hir hir::Expr<'hir>,
1792 args: &'hir [hir::Expr<'hir>],
1793 ) -> &'hir hir::Expr<'hir> {
1794 self.arena.alloc(self.expr_call_mut(span, e, args))
1797 fn expr_call_lang_item_fn_mut(
1800 lang_item: hir::LangItem,
1801 args: &'hir [hir::Expr<'hir>],
1802 hir_id: Option<hir::HirId>,
1803 ) -> hir::Expr<'hir> {
1804 let path = self.arena.alloc(self.expr_lang_item_path(span, lang_item, hir_id));
1805 self.expr_call_mut(span, path, args)
1808 fn expr_call_lang_item_fn(
1811 lang_item: hir::LangItem,
1812 args: &'hir [hir::Expr<'hir>],
1813 hir_id: Option<hir::HirId>,
1814 ) -> &'hir hir::Expr<'hir> {
1815 self.arena.alloc(self.expr_call_lang_item_fn_mut(span, lang_item, args, hir_id))
1818 fn expr_lang_item_path(
1821 lang_item: hir::LangItem,
1822 hir_id: Option<hir::HirId>,
1823 ) -> hir::Expr<'hir> {
1826 hir::ExprKind::Path(hir::QPath::LangItem(lang_item, self.lower_span(span), hir_id)),
1830 pub(super) fn expr_ident(
1834 binding: hir::HirId,
1835 ) -> &'hir hir::Expr<'hir> {
1836 self.arena.alloc(self.expr_ident_mut(sp, ident, binding))
1839 pub(super) fn expr_ident_mut(
1843 binding: hir::HirId,
1844 ) -> hir::Expr<'hir> {
1845 let hir_id = self.next_id();
1846 let res = Res::Local(binding);
1847 let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
1849 self.arena.alloc(hir::Path {
1850 span: self.lower_span(span),
1852 segments: arena_vec![self; hir::PathSegment::new(ident, hir_id, res)],
1856 self.expr(span, expr_path)
1859 fn expr_unsafe(&mut self, expr: &'hir hir::Expr<'hir>) -> hir::Expr<'hir> {
1860 let hir_id = self.next_id();
1861 let span = expr.span;
1864 hir::ExprKind::Block(
1865 self.arena.alloc(hir::Block {
1869 rules: hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::CompilerGenerated),
1870 span: self.lower_span(span),
1871 targeted_by_break: false,
1878 fn expr_block_empty(&mut self, span: Span) -> &'hir hir::Expr<'hir> {
1879 let blk = self.block_all(span, &[], None);
1880 let expr = self.expr_block(blk);
1881 self.arena.alloc(expr)
1884 pub(super) fn expr_block(&mut self, b: &'hir hir::Block<'hir>) -> hir::Expr<'hir> {
1885 self.expr(b.span, hir::ExprKind::Block(b, None))
1888 pub(super) fn expr(&mut self, span: Span, kind: hir::ExprKind<'hir>) -> hir::Expr<'hir> {
1889 let hir_id = self.next_id();
1890 hir::Expr { hir_id, kind, span: self.lower_span(span) }
1896 expr: &'hir hir::Expr<'hir>,
1898 ) -> hir::ExprField<'hir> {
1900 hir_id: self.next_id(),
1902 span: self.lower_span(span),
1904 is_shorthand: false,
1908 fn arm(&mut self, pat: &'hir hir::Pat<'hir>, expr: &'hir hir::Expr<'hir>) -> hir::Arm<'hir> {
1910 hir_id: self.next_id(),
1913 span: self.lower_span(expr.span),