rustc_ast_lowering/
expr.rs

1use std::mem;
2use std::ops::ControlFlow;
3use std::sync::Arc;
4
5use rustc_ast::*;
6use rustc_ast_pretty::pprust::expr_to_string;
7use rustc_data_structures::stack::ensure_sufficient_stack;
8use rustc_hir as hir;
9use rustc_hir::attrs::AttributeKind;
10use rustc_hir::def::{DefKind, Res};
11use rustc_hir::definitions::DefPathData;
12use rustc_hir::{HirId, Target, find_attr};
13use rustc_middle::span_bug;
14use rustc_middle::ty::TyCtxt;
15use rustc_session::errors::report_lit_error;
16use rustc_span::source_map::{Spanned, respan};
17use rustc_span::{ByteSymbol, DUMMY_SP, DesugaringKind, Ident, Span, Symbol, sym};
18use thin_vec::{ThinVec, thin_vec};
19use visit::{Visitor, walk_expr};
20
21use super::errors::{
22    AsyncCoroutinesNotSupported, AwaitOnlyInAsyncFnAndBlocks, ClosureCannotBeStatic,
23    CoroutineTooManyParameters, FunctionalRecordUpdateDestructuringAssignment,
24    InclusiveRangeWithNoEnd, MatchArmWithNoBody, NeverPatternWithBody, NeverPatternWithGuard,
25    UnderscoreExprLhsAssign,
26};
27use super::{
28    GenericArgsMode, ImplTraitContext, LoweringContext, ParamMode, ResolverAstLoweringExt,
29};
30use crate::errors::{InvalidLegacyConstGenericArg, UseConstGenericArg, YieldInClosure};
31use crate::{
32    AllowReturnTypeNotation, FnDeclKind, ImplTraitPosition, TryBlockScope, fluent_generated,
33};
34
35struct WillCreateDefIdsVisitor {}
36
37impl<'v> rustc_ast::visit::Visitor<'v> for WillCreateDefIdsVisitor {
38    type Result = ControlFlow<Span>;
39
40    fn visit_anon_const(&mut self, c: &'v AnonConst) -> Self::Result {
41        ControlFlow::Break(c.value.span)
42    }
43
44    fn visit_item(&mut self, item: &'v Item) -> Self::Result {
45        ControlFlow::Break(item.span)
46    }
47
48    fn visit_expr(&mut self, ex: &'v Expr) -> Self::Result {
49        match ex.kind {
50            ExprKind::Gen(..) | ExprKind::ConstBlock(..) | ExprKind::Closure(..) => {
51                ControlFlow::Break(ex.span)
52            }
53            _ => walk_expr(self, ex),
54        }
55    }
56}
57
58impl<'hir> LoweringContext<'_, 'hir> {
59    fn lower_exprs(&mut self, exprs: &[Box<Expr>]) -> &'hir [hir::Expr<'hir>] {
60        self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
61    }
62
63    pub(super) fn lower_expr(&mut self, e: &Expr) -> &'hir hir::Expr<'hir> {
64        self.arena.alloc(self.lower_expr_mut(e))
65    }
66
67    pub(super) fn lower_expr_mut(&mut self, e: &Expr) -> hir::Expr<'hir> {
68        ensure_sufficient_stack(|| {
69            let mut span = self.lower_span(e.span);
70            match &e.kind {
71                // Parenthesis expression does not have a HirId and is handled specially.
72                ExprKind::Paren(ex) => {
73                    let mut ex = self.lower_expr_mut(ex);
74                    // Include parens in span, but only if it is a super-span.
75                    if e.span.contains(ex.span) {
76                        ex.span = self.lower_span(e.span.with_ctxt(ex.span.ctxt()));
77                    }
78                    // Merge attributes into the inner expression.
79                    if !e.attrs.is_empty() {
80                        let old_attrs = self.attrs.get(&ex.hir_id.local_id).copied().unwrap_or(&[]);
81                        let new_attrs = self
82                            .lower_attrs_vec(&e.attrs, e.span, ex.hir_id, Target::from_expr(e))
83                            .into_iter()
84                            .chain(old_attrs.iter().cloned());
85                        let new_attrs = &*self.arena.alloc_from_iter(new_attrs);
86                        if new_attrs.is_empty() {
87                            return ex;
88                        }
89                        self.attrs.insert(ex.hir_id.local_id, new_attrs);
90                    }
91                    return ex;
92                }
93                // Desugar `ExprForLoop`
94                // from: `[opt_ident]: for await? <pat> in <iter> <body>`
95                //
96                // This also needs special handling because the HirId of the returned `hir::Expr` will not
97                // correspond to the `e.id`, so `lower_expr_for` handles attribute lowering itself.
98                ExprKind::ForLoop { pat, iter, body, label, kind } => {
99                    return self.lower_expr_for(e, pat, iter, body, *label, *kind);
100                }
101                _ => (),
102            }
103
104            let expr_hir_id = self.lower_node_id(e.id);
105            let attrs = self.lower_attrs(expr_hir_id, &e.attrs, e.span, Target::from_expr(e));
106
107            let kind = match &e.kind {
108                ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
109                ExprKind::ConstBlock(c) => hir::ExprKind::ConstBlock(self.lower_const_block(c)),
110                ExprKind::Repeat(expr, count) => {
111                    let expr = self.lower_expr(expr);
112                    let count = self.lower_array_length_to_const_arg(count);
113                    hir::ExprKind::Repeat(expr, count)
114                }
115                ExprKind::Tup(elts) => hir::ExprKind::Tup(self.lower_exprs(elts)),
116                ExprKind::Call(f, args) => {
117                    if let Some(legacy_args) = self.resolver.legacy_const_generic_args(f, self.tcx)
118                    {
119                        self.lower_legacy_const_generics((**f).clone(), args.clone(), &legacy_args)
120                    } else {
121                        let f = self.lower_expr(f);
122                        hir::ExprKind::Call(f, self.lower_exprs(args))
123                    }
124                }
125                ExprKind::MethodCall(box MethodCall { seg, receiver, args, span }) => {
126                    let hir_seg = self.arena.alloc(self.lower_path_segment(
127                        e.span,
128                        seg,
129                        ParamMode::Optional,
130                        GenericArgsMode::Err,
131                        ImplTraitContext::Disallowed(ImplTraitPosition::Path),
132                        // Method calls can't have bound modifiers
133                        None,
134                    ));
135                    let receiver = self.lower_expr(receiver);
136                    let args =
137                        self.arena.alloc_from_iter(args.iter().map(|x| self.lower_expr_mut(x)));
138                    hir::ExprKind::MethodCall(hir_seg, receiver, args, self.lower_span(*span))
139                }
140                ExprKind::Binary(binop, lhs, rhs) => {
141                    let binop = self.lower_binop(*binop);
142                    let lhs = self.lower_expr(lhs);
143                    let rhs = self.lower_expr(rhs);
144                    hir::ExprKind::Binary(binop, lhs, rhs)
145                }
146                ExprKind::Unary(op, ohs) => {
147                    let op = self.lower_unop(*op);
148                    let ohs = self.lower_expr(ohs);
149                    hir::ExprKind::Unary(op, ohs)
150                }
151                ExprKind::Lit(token_lit) => hir::ExprKind::Lit(self.lower_lit(token_lit, e.span)),
152                ExprKind::IncludedBytes(byte_sym) => {
153                    let lit = respan(
154                        self.lower_span(e.span),
155                        LitKind::ByteStr(*byte_sym, StrStyle::Cooked),
156                    );
157                    hir::ExprKind::Lit(lit)
158                }
159                ExprKind::Cast(expr, ty) => {
160                    let expr = self.lower_expr(expr);
161                    let ty =
162                        self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Cast));
163                    hir::ExprKind::Cast(expr, ty)
164                }
165                ExprKind::Type(expr, ty) => {
166                    let expr = self.lower_expr(expr);
167                    let ty =
168                        self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Cast));
169                    hir::ExprKind::Type(expr, ty)
170                }
171                ExprKind::AddrOf(k, m, ohs) => {
172                    let ohs = self.lower_expr(ohs);
173                    hir::ExprKind::AddrOf(*k, *m, ohs)
174                }
175                ExprKind::Let(pat, scrutinee, span, recovered) => {
176                    hir::ExprKind::Let(self.arena.alloc(hir::LetExpr {
177                        span: self.lower_span(*span),
178                        pat: self.lower_pat(pat),
179                        ty: None,
180                        init: self.lower_expr(scrutinee),
181                        recovered: *recovered,
182                    }))
183                }
184                ExprKind::If(cond, then, else_opt) => {
185                    self.lower_expr_if(cond, then, else_opt.as_deref())
186                }
187                ExprKind::While(cond, body, opt_label) => {
188                    self.with_loop_scope(expr_hir_id, |this| {
189                        let span =
190                            this.mark_span_with_reason(DesugaringKind::WhileLoop, e.span, None);
191                        let opt_label = this.lower_label(*opt_label, e.id, expr_hir_id);
192                        this.lower_expr_while_in_loop_scope(span, cond, body, opt_label)
193                    })
194                }
195                ExprKind::Loop(body, opt_label, span) => {
196                    self.with_loop_scope(expr_hir_id, |this| {
197                        let opt_label = this.lower_label(*opt_label, e.id, expr_hir_id);
198                        hir::ExprKind::Loop(
199                            this.lower_block(body, false),
200                            opt_label,
201                            hir::LoopSource::Loop,
202                            this.lower_span(*span),
203                        )
204                    })
205                }
206                ExprKind::TryBlock(body, opt_ty) => {
207                    self.lower_expr_try_block(body, opt_ty.as_deref())
208                }
209                ExprKind::Match(expr, arms, kind) => hir::ExprKind::Match(
210                    self.lower_expr(expr),
211                    self.arena.alloc_from_iter(arms.iter().map(|x| self.lower_arm(x))),
212                    match kind {
213                        MatchKind::Prefix => hir::MatchSource::Normal,
214                        MatchKind::Postfix => hir::MatchSource::Postfix,
215                    },
216                ),
217                ExprKind::Await(expr, await_kw_span) => self.lower_expr_await(*await_kw_span, expr),
218                ExprKind::Use(expr, use_kw_span) => self.lower_expr_use(*use_kw_span, expr),
219                ExprKind::Closure(box Closure {
220                    binder,
221                    capture_clause,
222                    constness,
223                    coroutine_kind,
224                    movability,
225                    fn_decl,
226                    body,
227                    fn_decl_span,
228                    fn_arg_span,
229                }) => match coroutine_kind {
230                    Some(coroutine_kind) => self.lower_expr_coroutine_closure(
231                        binder,
232                        *capture_clause,
233                        e.id,
234                        expr_hir_id,
235                        *coroutine_kind,
236                        fn_decl,
237                        body,
238                        *fn_decl_span,
239                        *fn_arg_span,
240                    ),
241                    None => self.lower_expr_closure(
242                        attrs,
243                        binder,
244                        *capture_clause,
245                        e.id,
246                        *constness,
247                        *movability,
248                        fn_decl,
249                        body,
250                        *fn_decl_span,
251                        *fn_arg_span,
252                    ),
253                },
254                ExprKind::Gen(capture_clause, block, genblock_kind, decl_span) => {
255                    let desugaring_kind = match genblock_kind {
256                        GenBlockKind::Async => hir::CoroutineDesugaring::Async,
257                        GenBlockKind::Gen => hir::CoroutineDesugaring::Gen,
258                        GenBlockKind::AsyncGen => hir::CoroutineDesugaring::AsyncGen,
259                    };
260                    self.make_desugared_coroutine_expr(
261                        *capture_clause,
262                        e.id,
263                        None,
264                        *decl_span,
265                        e.span,
266                        desugaring_kind,
267                        hir::CoroutineSource::Block,
268                        |this| this.with_new_scopes(e.span, |this| this.lower_block_expr(block)),
269                    )
270                }
271                ExprKind::Block(blk, opt_label) => {
272                    // Different from loops, label of block resolves to block id rather than
273                    // expr node id.
274                    let block_hir_id = self.lower_node_id(blk.id);
275                    let opt_label = self.lower_label(*opt_label, blk.id, block_hir_id);
276                    let hir_block = self.arena.alloc(self.lower_block_noalloc(
277                        block_hir_id,
278                        blk,
279                        opt_label.is_some(),
280                    ));
281                    hir::ExprKind::Block(hir_block, opt_label)
282                }
283                ExprKind::Assign(el, er, span) => self.lower_expr_assign(el, er, *span, e.span),
284                ExprKind::AssignOp(op, el, er) => hir::ExprKind::AssignOp(
285                    self.lower_assign_op(*op),
286                    self.lower_expr(el),
287                    self.lower_expr(er),
288                ),
289                ExprKind::Field(el, ident) => {
290                    hir::ExprKind::Field(self.lower_expr(el), self.lower_ident(*ident))
291                }
292                ExprKind::Index(el, er, brackets_span) => hir::ExprKind::Index(
293                    self.lower_expr(el),
294                    self.lower_expr(er),
295                    self.lower_span(*brackets_span),
296                ),
297                ExprKind::Range(e1, e2, lims) => {
298                    span = self.mark_span_with_reason(DesugaringKind::RangeExpr, span, None);
299                    self.lower_expr_range(span, e1.as_deref(), e2.as_deref(), *lims)
300                }
301                ExprKind::Underscore => {
302                    let guar = self.dcx().emit_err(UnderscoreExprLhsAssign { span: e.span });
303                    hir::ExprKind::Err(guar)
304                }
305                ExprKind::Path(qself, path) => {
306                    let qpath = self.lower_qpath(
307                        e.id,
308                        qself,
309                        path,
310                        ParamMode::Optional,
311                        AllowReturnTypeNotation::No,
312                        ImplTraitContext::Disallowed(ImplTraitPosition::Path),
313                        None,
314                    );
315                    hir::ExprKind::Path(qpath)
316                }
317                ExprKind::Break(opt_label, opt_expr) => {
318                    let opt_expr = opt_expr.as_ref().map(|x| self.lower_expr(x));
319                    hir::ExprKind::Break(self.lower_jump_destination(e.id, *opt_label), opt_expr)
320                }
321                ExprKind::Continue(opt_label) => {
322                    hir::ExprKind::Continue(self.lower_jump_destination(e.id, *opt_label))
323                }
324                ExprKind::Ret(e) => {
325                    let expr = e.as_ref().map(|x| self.lower_expr(x));
326                    self.checked_return(expr)
327                }
328                ExprKind::Yeet(sub_expr) => self.lower_expr_yeet(e.span, sub_expr.as_deref()),
329                ExprKind::Become(sub_expr) => {
330                    let sub_expr = self.lower_expr(sub_expr);
331                    hir::ExprKind::Become(sub_expr)
332                }
333                ExprKind::InlineAsm(asm) => {
334                    hir::ExprKind::InlineAsm(self.lower_inline_asm(e.span, asm))
335                }
336                ExprKind::FormatArgs(fmt) => self.lower_format_args(e.span, fmt),
337                ExprKind::OffsetOf(container, fields) => hir::ExprKind::OffsetOf(
338                    self.lower_ty(
339                        container,
340                        ImplTraitContext::Disallowed(ImplTraitPosition::OffsetOf),
341                    ),
342                    self.arena.alloc_from_iter(fields.iter().map(|&ident| self.lower_ident(ident))),
343                ),
344                ExprKind::Struct(se) => {
345                    let rest = match &se.rest {
346                        StructRest::Base(e) => hir::StructTailExpr::Base(self.lower_expr(e)),
347                        StructRest::Rest(sp) => {
348                            hir::StructTailExpr::DefaultFields(self.lower_span(*sp))
349                        }
350                        StructRest::None => hir::StructTailExpr::None,
351                    };
352                    hir::ExprKind::Struct(
353                        self.arena.alloc(self.lower_qpath(
354                            e.id,
355                            &se.qself,
356                            &se.path,
357                            ParamMode::Optional,
358                            AllowReturnTypeNotation::No,
359                            ImplTraitContext::Disallowed(ImplTraitPosition::Path),
360                            None,
361                        )),
362                        self.arena
363                            .alloc_from_iter(se.fields.iter().map(|x| self.lower_expr_field(x))),
364                        rest,
365                    )
366                }
367                ExprKind::Yield(kind) => self.lower_expr_yield(e.span, kind.expr().map(|x| &**x)),
368                ExprKind::Err(guar) => hir::ExprKind::Err(*guar),
369
370                ExprKind::UnsafeBinderCast(kind, expr, ty) => hir::ExprKind::UnsafeBinderCast(
371                    *kind,
372                    self.lower_expr(expr),
373                    ty.as_ref().map(|ty| {
374                        self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Cast))
375                    }),
376                ),
377
378                ExprKind::Dummy => {
379                    span_bug!(e.span, "lowered ExprKind::Dummy")
380                }
381
382                ExprKind::Try(sub_expr) => self.lower_expr_try(e.span, sub_expr),
383
384                ExprKind::Paren(_) | ExprKind::ForLoop { .. } => {
385                    unreachable!("already handled")
386                }
387
388                ExprKind::MacCall(_) => panic!("{:?} shouldn't exist here", e.span),
389            };
390
391            hir::Expr { hir_id: expr_hir_id, kind, span }
392        })
393    }
394
395    pub(crate) fn lower_const_block(&mut self, c: &AnonConst) -> hir::ConstBlock {
396        self.with_new_scopes(c.value.span, |this| {
397            let def_id = this.local_def_id(c.id);
398            hir::ConstBlock {
399                def_id,
400                hir_id: this.lower_node_id(c.id),
401                body: this.lower_const_body(c.value.span, Some(&c.value)),
402            }
403        })
404    }
405
406    pub(crate) fn lower_lit(&mut self, token_lit: &token::Lit, span: Span) -> hir::Lit {
407        let lit_kind = match LitKind::from_token_lit(*token_lit) {
408            Ok(lit_kind) => lit_kind,
409            Err(err) => {
410                let guar = report_lit_error(&self.tcx.sess.psess, err, *token_lit, span);
411                LitKind::Err(guar)
412            }
413        };
414        respan(self.lower_span(span), lit_kind)
415    }
416
417    fn lower_unop(&mut self, u: UnOp) -> hir::UnOp {
418        match u {
419            UnOp::Deref => hir::UnOp::Deref,
420            UnOp::Not => hir::UnOp::Not,
421            UnOp::Neg => hir::UnOp::Neg,
422        }
423    }
424
425    fn lower_binop(&mut self, b: BinOp) -> BinOp {
426        Spanned { node: b.node, span: self.lower_span(b.span) }
427    }
428
429    fn lower_assign_op(&mut self, a: AssignOp) -> AssignOp {
430        Spanned { node: a.node, span: self.lower_span(a.span) }
431    }
432
433    fn lower_legacy_const_generics(
434        &mut self,
435        mut f: Expr,
436        args: ThinVec<Box<Expr>>,
437        legacy_args_idx: &[usize],
438    ) -> hir::ExprKind<'hir> {
439        let ExprKind::Path(None, path) = &mut f.kind else {
440            unreachable!();
441        };
442
443        let mut error = None;
444        let mut invalid_expr_error = |tcx: TyCtxt<'_>, span| {
445            // Avoid emitting the error multiple times.
446            if error.is_none() {
447                let mut const_args = vec![];
448                let mut other_args = vec![];
449                for (idx, arg) in args.iter().enumerate() {
450                    if legacy_args_idx.contains(&idx) {
451                        const_args.push(format!("{{ {} }}", expr_to_string(arg)));
452                    } else {
453                        other_args.push(expr_to_string(arg));
454                    }
455                }
456                let suggestion = UseConstGenericArg {
457                    end_of_fn: f.span.shrink_to_hi(),
458                    const_args: const_args.join(", "),
459                    other_args: other_args.join(", "),
460                    call_args: args[0].span.to(args.last().unwrap().span),
461                };
462                error = Some(tcx.dcx().emit_err(InvalidLegacyConstGenericArg { span, suggestion }));
463            }
464            error.unwrap()
465        };
466
467        // Split the arguments into const generics and normal arguments
468        let mut real_args = vec![];
469        let mut generic_args = ThinVec::new();
470        for (idx, arg) in args.iter().cloned().enumerate() {
471            if legacy_args_idx.contains(&idx) {
472                let node_id = self.next_node_id();
473                self.create_def(
474                    node_id,
475                    None,
476                    DefKind::AnonConst,
477                    DefPathData::LateAnonConst,
478                    f.span,
479                );
480                let mut visitor = WillCreateDefIdsVisitor {};
481                let const_value = if let ControlFlow::Break(span) = visitor.visit_expr(&arg) {
482                    Box::new(Expr {
483                        id: self.next_node_id(),
484                        kind: ExprKind::Err(invalid_expr_error(self.tcx, span)),
485                        span: f.span,
486                        attrs: [].into(),
487                        tokens: None,
488                    })
489                } else {
490                    arg
491                };
492
493                let anon_const = AnonConst {
494                    id: node_id,
495                    value: const_value,
496                    mgca_disambiguation: MgcaDisambiguation::AnonConst,
497                };
498                generic_args.push(AngleBracketedArg::Arg(GenericArg::Const(anon_const)));
499            } else {
500                real_args.push(arg);
501            }
502        }
503
504        // Add generic args to the last element of the path.
505        let last_segment = path.segments.last_mut().unwrap();
506        assert!(last_segment.args.is_none());
507        last_segment.args = Some(Box::new(GenericArgs::AngleBracketed(AngleBracketedArgs {
508            span: DUMMY_SP,
509            args: generic_args,
510        })));
511
512        // Now lower everything as normal.
513        let f = self.lower_expr(&f);
514        hir::ExprKind::Call(f, self.lower_exprs(&real_args))
515    }
516
517    fn lower_expr_if(
518        &mut self,
519        cond: &Expr,
520        then: &Block,
521        else_opt: Option<&Expr>,
522    ) -> hir::ExprKind<'hir> {
523        let lowered_cond = self.lower_expr(cond);
524        let then_expr = self.lower_block_expr(then);
525        if let Some(rslt) = else_opt {
526            hir::ExprKind::If(
527                lowered_cond,
528                self.arena.alloc(then_expr),
529                Some(self.lower_expr(rslt)),
530            )
531        } else {
532            hir::ExprKind::If(lowered_cond, self.arena.alloc(then_expr), None)
533        }
534    }
535
536    // We desugar: `'label: while $cond $body` into:
537    //
538    // ```
539    // 'label: loop {
540    //   if { let _t = $cond; _t } {
541    //     $body
542    //   }
543    //   else {
544    //     break;
545    //   }
546    // }
547    // ```
548    //
549    // Wrap in a construct equivalent to `{ let _t = $cond; _t }`
550    // to preserve drop semantics since `while $cond { ... }` does not
551    // let temporaries live outside of `cond`.
552    fn lower_expr_while_in_loop_scope(
553        &mut self,
554        span: Span,
555        cond: &Expr,
556        body: &Block,
557        opt_label: Option<Label>,
558    ) -> hir::ExprKind<'hir> {
559        let lowered_cond = self.with_loop_condition_scope(|t| t.lower_expr(cond));
560        let then = self.lower_block_expr(body);
561        let expr_break = self.expr_break(span);
562        let stmt_break = self.stmt_expr(span, expr_break);
563        let else_blk = self.block_all(span, arena_vec![self; stmt_break], None);
564        let else_expr = self.arena.alloc(self.expr_block(else_blk));
565        let if_kind = hir::ExprKind::If(lowered_cond, self.arena.alloc(then), Some(else_expr));
566        let if_expr = self.expr(span, if_kind);
567        let block = self.block_expr(self.arena.alloc(if_expr));
568        let span = self.lower_span(span.with_hi(cond.span.hi()));
569        hir::ExprKind::Loop(block, opt_label, hir::LoopSource::While, span)
570    }
571
572    /// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_output(<expr>) }`,
573    /// `try { <stmts>; }` into `{ <stmts>; ::std::ops::Try::from_output(()) }`
574    /// and save the block id to use it as a break target for desugaring of the `?` operator.
575    fn lower_expr_try_block(&mut self, body: &Block, opt_ty: Option<&Ty>) -> hir::ExprKind<'hir> {
576        let body_hir_id = self.lower_node_id(body.id);
577        let new_scope = if opt_ty.is_some() {
578            TryBlockScope::Heterogeneous(body_hir_id)
579        } else {
580            TryBlockScope::Homogeneous(body_hir_id)
581        };
582        let whole_block = self.with_try_block_scope(new_scope, |this| {
583            let mut block = this.lower_block_noalloc(body_hir_id, body, true);
584
585            // Final expression of the block (if present) or `()` with span at the end of block
586            let (try_span, tail_expr) = if let Some(expr) = block.expr.take() {
587                (
588                    this.mark_span_with_reason(
589                        DesugaringKind::TryBlock,
590                        expr.span,
591                        Some(Arc::clone(&this.allow_try_trait)),
592                    ),
593                    expr,
594                )
595            } else {
596                let try_span = this.mark_span_with_reason(
597                    DesugaringKind::TryBlock,
598                    this.tcx.sess.source_map().end_point(body.span),
599                    Some(Arc::clone(&this.allow_try_trait)),
600                );
601
602                (try_span, this.expr_unit(try_span))
603            };
604
605            let ok_wrapped_span =
606                this.mark_span_with_reason(DesugaringKind::TryBlock, tail_expr.span, None);
607
608            // `::std::ops::Try::from_output($tail_expr)`
609            block.expr = Some(this.wrap_in_try_constructor(
610                hir::LangItem::TryTraitFromOutput,
611                try_span,
612                tail_expr,
613                ok_wrapped_span,
614            ));
615
616            this.arena.alloc(block)
617        });
618
619        if let Some(ty) = opt_ty {
620            let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Path));
621            let block_expr = self.arena.alloc(self.expr_block(whole_block));
622            hir::ExprKind::Type(block_expr, ty)
623        } else {
624            hir::ExprKind::Block(whole_block, None)
625        }
626    }
627
628    fn wrap_in_try_constructor(
629        &mut self,
630        lang_item: hir::LangItem,
631        method_span: Span,
632        expr: &'hir hir::Expr<'hir>,
633        overall_span: Span,
634    ) -> &'hir hir::Expr<'hir> {
635        let constructor = self.arena.alloc(self.expr_lang_item_path(method_span, lang_item));
636        self.expr_call(overall_span, constructor, std::slice::from_ref(expr))
637    }
638
639    fn lower_arm(&mut self, arm: &Arm) -> hir::Arm<'hir> {
640        let pat = self.lower_pat(&arm.pat);
641        let guard = arm.guard.as_ref().map(|cond| self.lower_expr(cond));
642        let hir_id = self.next_id();
643        let span = self.lower_span(arm.span);
644        self.lower_attrs(hir_id, &arm.attrs, arm.span, Target::Arm);
645        let is_never_pattern = pat.is_never_pattern();
646        // We need to lower the body even if it's unneeded for never pattern in match,
647        // ensure that we can get HirId for DefId if need (issue #137708).
648        let body = arm.body.as_ref().map(|x| self.lower_expr(x));
649        let body = if let Some(body) = body
650            && !is_never_pattern
651        {
652            body
653        } else {
654            // Either `body.is_none()` or `is_never_pattern` here.
655            if !is_never_pattern {
656                if self.tcx.features().never_patterns() {
657                    // If the feature is off we already emitted the error after parsing.
658                    let suggestion = span.shrink_to_hi();
659                    self.dcx().emit_err(MatchArmWithNoBody { span, suggestion });
660                }
661            } else if let Some(body) = &arm.body {
662                self.dcx().emit_err(NeverPatternWithBody { span: body.span });
663            } else if let Some(g) = &arm.guard {
664                self.dcx().emit_err(NeverPatternWithGuard { span: g.span });
665            }
666
667            // We add a fake `loop {}` arm body so that it typecks to `!`. The mir lowering of never
668            // patterns ensures this loop is not reachable.
669            let block = self.arena.alloc(hir::Block {
670                stmts: &[],
671                expr: None,
672                hir_id: self.next_id(),
673                rules: hir::BlockCheckMode::DefaultBlock,
674                span,
675                targeted_by_break: false,
676            });
677            self.arena.alloc(hir::Expr {
678                hir_id: self.next_id(),
679                kind: hir::ExprKind::Loop(block, None, hir::LoopSource::Loop, span),
680                span,
681            })
682        };
683        hir::Arm { hir_id, pat, guard, body, span }
684    }
685
686    fn lower_capture_clause(&mut self, capture_clause: CaptureBy) -> CaptureBy {
687        match capture_clause {
688            CaptureBy::Ref => CaptureBy::Ref,
689            CaptureBy::Use { use_kw } => CaptureBy::Use { use_kw: self.lower_span(use_kw) },
690            CaptureBy::Value { move_kw } => CaptureBy::Value { move_kw: self.lower_span(move_kw) },
691        }
692    }
693
694    /// Lower/desugar a coroutine construct.
695    ///
696    /// In particular, this creates the correct async resume argument and `_task_context`.
697    ///
698    /// This results in:
699    ///
700    /// ```text
701    /// static move? |<_task_context?>| -> <return_ty> {
702    ///     <body>
703    /// }
704    /// ```
705    pub(super) fn make_desugared_coroutine_expr(
706        &mut self,
707        capture_clause: CaptureBy,
708        closure_node_id: NodeId,
709        return_ty: Option<hir::FnRetTy<'hir>>,
710        fn_decl_span: Span,
711        span: Span,
712        desugaring_kind: hir::CoroutineDesugaring,
713        coroutine_source: hir::CoroutineSource,
714        body: impl FnOnce(&mut Self) -> hir::Expr<'hir>,
715    ) -> hir::ExprKind<'hir> {
716        let closure_def_id = self.local_def_id(closure_node_id);
717        let coroutine_kind = hir::CoroutineKind::Desugared(desugaring_kind, coroutine_source);
718
719        // The `async` desugaring takes a resume argument and maintains a `task_context`,
720        // whereas a generator does not.
721        let (inputs, params, task_context): (&[_], &[_], _) = match desugaring_kind {
722            hir::CoroutineDesugaring::Async | hir::CoroutineDesugaring::AsyncGen => {
723                // Resume argument type: `ResumeTy`
724                let unstable_span = self.mark_span_with_reason(
725                    DesugaringKind::Async,
726                    self.lower_span(span),
727                    Some(Arc::clone(&self.allow_gen_future)),
728                );
729                let resume_ty =
730                    self.make_lang_item_qpath(hir::LangItem::ResumeTy, unstable_span, None);
731                let input_ty = hir::Ty {
732                    hir_id: self.next_id(),
733                    kind: hir::TyKind::Path(resume_ty),
734                    span: unstable_span,
735                };
736                let inputs = arena_vec![self; input_ty];
737
738                // Lower the argument pattern/ident. The ident is used again in the `.await` lowering.
739                let (pat, task_context_hid) = self.pat_ident_binding_mode(
740                    span,
741                    Ident::with_dummy_span(sym::_task_context),
742                    hir::BindingMode::MUT,
743                );
744                let param = hir::Param {
745                    hir_id: self.next_id(),
746                    pat,
747                    ty_span: self.lower_span(span),
748                    span: self.lower_span(span),
749                };
750                let params = arena_vec![self; param];
751
752                (inputs, params, Some(task_context_hid))
753            }
754            hir::CoroutineDesugaring::Gen => (&[], &[], None),
755        };
756
757        let output =
758            return_ty.unwrap_or_else(|| hir::FnRetTy::DefaultReturn(self.lower_span(span)));
759
760        let fn_decl = self.arena.alloc(hir::FnDecl {
761            inputs,
762            output,
763            c_variadic: false,
764            implicit_self: hir::ImplicitSelfKind::None,
765            lifetime_elision_allowed: false,
766        });
767
768        let body = self.lower_body(move |this| {
769            this.coroutine_kind = Some(coroutine_kind);
770
771            let old_ctx = this.task_context;
772            if task_context.is_some() {
773                this.task_context = task_context;
774            }
775            let res = body(this);
776            this.task_context = old_ctx;
777
778            (params, res)
779        });
780
781        // `static |<_task_context?>| -> <return_ty> { <body> }`:
782        hir::ExprKind::Closure(self.arena.alloc(hir::Closure {
783            def_id: closure_def_id,
784            binder: hir::ClosureBinder::Default,
785            capture_clause: self.lower_capture_clause(capture_clause),
786            bound_generic_params: &[],
787            fn_decl,
788            body,
789            fn_decl_span: self.lower_span(fn_decl_span),
790            fn_arg_span: None,
791            kind: hir::ClosureKind::Coroutine(coroutine_kind),
792            constness: hir::Constness::NotConst,
793        }))
794    }
795
796    /// Forwards a possible `#[track_caller]` annotation from `outer_hir_id` to
797    /// `inner_hir_id` in case the `async_fn_track_caller` feature is enabled.
798    pub(super) fn maybe_forward_track_caller(
799        &mut self,
800        span: Span,
801        outer_hir_id: HirId,
802        inner_hir_id: HirId,
803    ) {
804        if self.tcx.features().async_fn_track_caller()
805            && let Some(attrs) = self.attrs.get(&outer_hir_id.local_id)
806            && find_attr!(*attrs, AttributeKind::TrackCaller(_))
807        {
808            let unstable_span = self.mark_span_with_reason(
809                DesugaringKind::Async,
810                span,
811                Some(Arc::clone(&self.allow_gen_future)),
812            );
813            self.lower_attrs(
814                inner_hir_id,
815                &[Attribute {
816                    kind: AttrKind::Normal(Box::new(NormalAttr::from_ident(Ident::new(
817                        sym::track_caller,
818                        span,
819                    )))),
820                    id: self.tcx.sess.psess.attr_id_generator.mk_attr_id(),
821                    style: AttrStyle::Outer,
822                    span: unstable_span,
823                }],
824                span,
825                Target::Fn,
826            );
827        }
828    }
829
830    /// Desugar `<expr>.await` into:
831    /// ```ignore (pseudo-rust)
832    /// match ::std::future::IntoFuture::into_future(<expr>) {
833    ///     mut __awaitee => loop {
834    ///         match unsafe { ::std::future::Future::poll(
835    ///             <::std::pin::Pin>::new_unchecked(&mut __awaitee),
836    ///             ::std::future::get_context(task_context),
837    ///         ) } {
838    ///             ::std::task::Poll::Ready(result) => break result,
839    ///             ::std::task::Poll::Pending => {}
840    ///         }
841    ///         task_context = yield ();
842    ///     }
843    /// }
844    /// ```
845    fn lower_expr_await(&mut self, await_kw_span: Span, expr: &Expr) -> hir::ExprKind<'hir> {
846        let expr = self.arena.alloc(self.lower_expr_mut(expr));
847        self.make_lowered_await(await_kw_span, expr, FutureKind::Future)
848    }
849
850    /// Takes an expr that has already been lowered and generates a desugared await loop around it
851    fn make_lowered_await(
852        &mut self,
853        await_kw_span: Span,
854        expr: &'hir hir::Expr<'hir>,
855        await_kind: FutureKind,
856    ) -> hir::ExprKind<'hir> {
857        let full_span = expr.span.to(await_kw_span);
858
859        let is_async_gen = match self.coroutine_kind {
860            Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => false,
861            Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _)) => true,
862            Some(hir::CoroutineKind::Coroutine(_))
863            | Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _))
864            | None => {
865                // Lower to a block `{ EXPR; <error> }` so that the awaited expr
866                // is not accidentally orphaned.
867                let stmt_id = self.next_id();
868                let expr_err = self.expr(
869                    expr.span,
870                    hir::ExprKind::Err(self.dcx().emit_err(AwaitOnlyInAsyncFnAndBlocks {
871                        await_kw_span,
872                        item_span: self.current_item,
873                    })),
874                );
875                return hir::ExprKind::Block(
876                    self.block_all(
877                        expr.span,
878                        arena_vec![self; hir::Stmt {
879                            hir_id: stmt_id,
880                            kind: hir::StmtKind::Semi(expr),
881                            span: expr.span,
882                        }],
883                        Some(self.arena.alloc(expr_err)),
884                    ),
885                    None,
886                );
887            }
888        };
889
890        let features = match await_kind {
891            FutureKind::Future if is_async_gen => Some(Arc::clone(&self.allow_async_gen)),
892            FutureKind::Future => None,
893            FutureKind::AsyncIterator => Some(Arc::clone(&self.allow_for_await)),
894        };
895        let span = self.mark_span_with_reason(DesugaringKind::Await, await_kw_span, features);
896        let gen_future_span = self.mark_span_with_reason(
897            DesugaringKind::Await,
898            full_span,
899            Some(Arc::clone(&self.allow_gen_future)),
900        );
901        let expr_hir_id = expr.hir_id;
902
903        // Note that the name of this binding must not be changed to something else because
904        // debuggers and debugger extensions expect it to be called `__awaitee`. They use
905        // this name to identify what is being awaited by a suspended async functions.
906        let awaitee_ident = Ident::with_dummy_span(sym::__awaitee);
907        let (awaitee_pat, awaitee_pat_hid) =
908            self.pat_ident_binding_mode(gen_future_span, awaitee_ident, hir::BindingMode::MUT);
909
910        let task_context_ident = Ident::with_dummy_span(sym::_task_context);
911
912        // unsafe {
913        //     ::std::future::Future::poll(
914        //         ::std::pin::Pin::new_unchecked(&mut __awaitee),
915        //         ::std::future::get_context(task_context),
916        //     )
917        // }
918        let poll_expr = {
919            let awaitee = self.expr_ident(span, awaitee_ident, awaitee_pat_hid);
920            let ref_mut_awaitee = self.expr_mut_addr_of(span, awaitee);
921
922            let Some(task_context_hid) = self.task_context else {
923                unreachable!("use of `await` outside of an async context.");
924            };
925
926            let task_context = self.expr_ident_mut(span, task_context_ident, task_context_hid);
927
928            let new_unchecked = self.expr_call_lang_item_fn_mut(
929                span,
930                hir::LangItem::PinNewUnchecked,
931                arena_vec![self; ref_mut_awaitee],
932            );
933            let get_context = self.expr_call_lang_item_fn_mut(
934                gen_future_span,
935                hir::LangItem::GetContext,
936                arena_vec![self; task_context],
937            );
938            let call = match await_kind {
939                FutureKind::Future => self.expr_call_lang_item_fn(
940                    span,
941                    hir::LangItem::FuturePoll,
942                    arena_vec![self; new_unchecked, get_context],
943                ),
944                FutureKind::AsyncIterator => self.expr_call_lang_item_fn(
945                    span,
946                    hir::LangItem::AsyncIteratorPollNext,
947                    arena_vec![self; new_unchecked, get_context],
948                ),
949            };
950            self.arena.alloc(self.expr_unsafe(span, call))
951        };
952
953        // `::std::task::Poll::Ready(result) => break result`
954        let loop_node_id = self.next_node_id();
955        let loop_hir_id = self.lower_node_id(loop_node_id);
956        let ready_arm = {
957            let x_ident = Ident::with_dummy_span(sym::result);
958            let (x_pat, x_pat_hid) = self.pat_ident(gen_future_span, x_ident);
959            let x_expr = self.expr_ident(gen_future_span, x_ident, x_pat_hid);
960            let ready_field = self.single_pat_field(gen_future_span, x_pat);
961            let ready_pat = self.pat_lang_item_variant(span, hir::LangItem::PollReady, ready_field);
962            let break_x = self.with_loop_scope(loop_hir_id, move |this| {
963                let expr_break =
964                    hir::ExprKind::Break(this.lower_loop_destination(None), Some(x_expr));
965                this.arena.alloc(this.expr(gen_future_span, expr_break))
966            });
967            self.arm(ready_pat, break_x)
968        };
969
970        // `::std::task::Poll::Pending => {}`
971        let pending_arm = {
972            let pending_pat = self.pat_lang_item_variant(span, hir::LangItem::PollPending, &[]);
973            let empty_block = self.expr_block_empty(span);
974            self.arm(pending_pat, empty_block)
975        };
976
977        let inner_match_stmt = {
978            let match_expr = self.expr_match(
979                span,
980                poll_expr,
981                arena_vec![self; ready_arm, pending_arm],
982                hir::MatchSource::AwaitDesugar,
983            );
984            self.stmt_expr(span, match_expr)
985        };
986
987        // Depending on `async` of `async gen`:
988        // async     - task_context = yield ();
989        // async gen - task_context = yield ASYNC_GEN_PENDING;
990        let yield_stmt = {
991            let yielded = if is_async_gen {
992                self.arena.alloc(self.expr_lang_item_path(span, hir::LangItem::AsyncGenPending))
993            } else {
994                self.expr_unit(span)
995            };
996
997            let yield_expr = self.expr(
998                span,
999                hir::ExprKind::Yield(yielded, hir::YieldSource::Await { expr: Some(expr_hir_id) }),
1000            );
1001            let yield_expr = self.arena.alloc(yield_expr);
1002
1003            let Some(task_context_hid) = self.task_context else {
1004                unreachable!("use of `await` outside of an async context.");
1005            };
1006
1007            let lhs = self.expr_ident(span, task_context_ident, task_context_hid);
1008            let assign =
1009                self.expr(span, hir::ExprKind::Assign(lhs, yield_expr, self.lower_span(span)));
1010            self.stmt_expr(span, assign)
1011        };
1012
1013        let loop_block = self.block_all(span, arena_vec![self; inner_match_stmt, yield_stmt], None);
1014
1015        // loop { .. }
1016        let loop_expr = self.arena.alloc(hir::Expr {
1017            hir_id: loop_hir_id,
1018            kind: hir::ExprKind::Loop(
1019                loop_block,
1020                None,
1021                hir::LoopSource::Loop,
1022                self.lower_span(span),
1023            ),
1024            span: self.lower_span(span),
1025        });
1026
1027        // mut __awaitee => loop { ... }
1028        let awaitee_arm = self.arm(awaitee_pat, loop_expr);
1029
1030        // `match ::std::future::IntoFuture::into_future(<expr>) { ... }`
1031        let into_future_expr = match await_kind {
1032            FutureKind::Future => self.expr_call_lang_item_fn(
1033                span,
1034                hir::LangItem::IntoFutureIntoFuture,
1035                arena_vec![self; *expr],
1036            ),
1037            // Not needed for `for await` because we expect to have already called
1038            // `IntoAsyncIterator::into_async_iter` on it.
1039            FutureKind::AsyncIterator => expr,
1040        };
1041
1042        // match <into_future_expr> {
1043        //     mut __awaitee => loop { .. }
1044        // }
1045        hir::ExprKind::Match(
1046            into_future_expr,
1047            arena_vec![self; awaitee_arm],
1048            hir::MatchSource::AwaitDesugar,
1049        )
1050    }
1051
1052    fn lower_expr_use(&mut self, use_kw_span: Span, expr: &Expr) -> hir::ExprKind<'hir> {
1053        hir::ExprKind::Use(self.lower_expr(expr), self.lower_span(use_kw_span))
1054    }
1055
1056    fn lower_expr_closure(
1057        &mut self,
1058        attrs: &[rustc_hir::Attribute],
1059        binder: &ClosureBinder,
1060        capture_clause: CaptureBy,
1061        closure_id: NodeId,
1062        constness: Const,
1063        movability: Movability,
1064        decl: &FnDecl,
1065        body: &Expr,
1066        fn_decl_span: Span,
1067        fn_arg_span: Span,
1068    ) -> hir::ExprKind<'hir> {
1069        let closure_def_id = self.local_def_id(closure_id);
1070        let (binder_clause, generic_params) = self.lower_closure_binder(binder);
1071
1072        let (body_id, closure_kind) = self.with_new_scopes(fn_decl_span, move |this| {
1073
1074            let mut coroutine_kind = find_attr!(attrs, AttributeKind::Coroutine(_) => hir::CoroutineKind::Coroutine(Movability::Movable));
1075
1076            // FIXME(contracts): Support contracts on closures?
1077            let body_id = this.lower_fn_body(decl, None, |this| {
1078                this.coroutine_kind = coroutine_kind;
1079                let e = this.lower_expr_mut(body);
1080                coroutine_kind = this.coroutine_kind;
1081                e
1082            });
1083            let coroutine_option =
1084                this.closure_movability_for_fn(decl, fn_decl_span, coroutine_kind, movability);
1085            (body_id, coroutine_option)
1086        });
1087
1088        let bound_generic_params = self.lower_lifetime_binder(closure_id, generic_params);
1089        // Lower outside new scope to preserve `is_in_loop_condition`.
1090        let fn_decl = self.lower_fn_decl(decl, closure_id, fn_decl_span, FnDeclKind::Closure, None);
1091
1092        let c = self.arena.alloc(hir::Closure {
1093            def_id: closure_def_id,
1094            binder: binder_clause,
1095            capture_clause: self.lower_capture_clause(capture_clause),
1096            bound_generic_params,
1097            fn_decl,
1098            body: body_id,
1099            fn_decl_span: self.lower_span(fn_decl_span),
1100            fn_arg_span: Some(self.lower_span(fn_arg_span)),
1101            kind: closure_kind,
1102            constness: self.lower_constness(constness),
1103        });
1104
1105        hir::ExprKind::Closure(c)
1106    }
1107
1108    fn closure_movability_for_fn(
1109        &mut self,
1110        decl: &FnDecl,
1111        fn_decl_span: Span,
1112        coroutine_kind: Option<hir::CoroutineKind>,
1113        movability: Movability,
1114    ) -> hir::ClosureKind {
1115        match coroutine_kind {
1116            Some(hir::CoroutineKind::Coroutine(_)) => {
1117                if decl.inputs.len() > 1 {
1118                    self.dcx().emit_err(CoroutineTooManyParameters { fn_decl_span });
1119                }
1120                hir::ClosureKind::Coroutine(hir::CoroutineKind::Coroutine(movability))
1121            }
1122            Some(
1123                hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _)
1124                | hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)
1125                | hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _),
1126            ) => {
1127                panic!("non-`async`/`gen` closure body turned `async`/`gen` during lowering");
1128            }
1129            None => {
1130                if movability == Movability::Static {
1131                    self.dcx().emit_err(ClosureCannotBeStatic { fn_decl_span });
1132                }
1133                hir::ClosureKind::Closure
1134            }
1135        }
1136    }
1137
1138    fn lower_closure_binder<'c>(
1139        &mut self,
1140        binder: &'c ClosureBinder,
1141    ) -> (hir::ClosureBinder, &'c [GenericParam]) {
1142        let (binder, params) = match binder {
1143            ClosureBinder::NotPresent => (hir::ClosureBinder::Default, &[][..]),
1144            ClosureBinder::For { span, generic_params } => {
1145                let span = self.lower_span(*span);
1146                (hir::ClosureBinder::For { span }, &**generic_params)
1147            }
1148        };
1149
1150        (binder, params)
1151    }
1152
1153    fn lower_expr_coroutine_closure(
1154        &mut self,
1155        binder: &ClosureBinder,
1156        capture_clause: CaptureBy,
1157        closure_id: NodeId,
1158        closure_hir_id: HirId,
1159        coroutine_kind: CoroutineKind,
1160        decl: &FnDecl,
1161        body: &Expr,
1162        fn_decl_span: Span,
1163        fn_arg_span: Span,
1164    ) -> hir::ExprKind<'hir> {
1165        let closure_def_id = self.local_def_id(closure_id);
1166        let (binder_clause, generic_params) = self.lower_closure_binder(binder);
1167
1168        let coroutine_desugaring = match coroutine_kind {
1169            CoroutineKind::Async { .. } => hir::CoroutineDesugaring::Async,
1170            CoroutineKind::Gen { .. } => hir::CoroutineDesugaring::Gen,
1171            CoroutineKind::AsyncGen { span, .. } => {
1172                span_bug!(span, "only async closures and `iter!` closures are supported currently")
1173            }
1174        };
1175
1176        let body = self.with_new_scopes(fn_decl_span, |this| {
1177            let inner_decl =
1178                FnDecl { inputs: decl.inputs.clone(), output: FnRetTy::Default(fn_decl_span) };
1179
1180            // Transform `async |x: u8| -> X { ... }` into
1181            // `|x: u8| || -> X { ... }`.
1182            let body_id = this.lower_body(|this| {
1183                let (parameters, expr) = this.lower_coroutine_body_with_moved_arguments(
1184                    &inner_decl,
1185                    |this| this.with_new_scopes(fn_decl_span, |this| this.lower_expr_mut(body)),
1186                    fn_decl_span,
1187                    body.span,
1188                    coroutine_kind,
1189                    hir::CoroutineSource::Closure,
1190                );
1191
1192                this.maybe_forward_track_caller(body.span, closure_hir_id, expr.hir_id);
1193
1194                (parameters, expr)
1195            });
1196            body_id
1197        });
1198
1199        let bound_generic_params = self.lower_lifetime_binder(closure_id, generic_params);
1200        // We need to lower the declaration outside the new scope, because we
1201        // have to conserve the state of being inside a loop condition for the
1202        // closure argument types.
1203        let fn_decl =
1204            self.lower_fn_decl(&decl, closure_id, fn_decl_span, FnDeclKind::Closure, None);
1205
1206        let c = self.arena.alloc(hir::Closure {
1207            def_id: closure_def_id,
1208            binder: binder_clause,
1209            capture_clause: self.lower_capture_clause(capture_clause),
1210            bound_generic_params,
1211            fn_decl,
1212            body,
1213            fn_decl_span: self.lower_span(fn_decl_span),
1214            fn_arg_span: Some(self.lower_span(fn_arg_span)),
1215            // Lower this as a `CoroutineClosure`. That will ensure that HIR typeck
1216            // knows that a `FnDecl` output type like `-> &str` actually means
1217            // "coroutine that returns &str", rather than directly returning a `&str`.
1218            kind: hir::ClosureKind::CoroutineClosure(coroutine_desugaring),
1219            constness: hir::Constness::NotConst,
1220        });
1221        hir::ExprKind::Closure(c)
1222    }
1223
1224    /// Destructure the LHS of complex assignments.
1225    /// For instance, lower `(a, b) = t` to `{ let (lhs1, lhs2) = t; a = lhs1; b = lhs2; }`.
1226    fn lower_expr_assign(
1227        &mut self,
1228        lhs: &Expr,
1229        rhs: &Expr,
1230        eq_sign_span: Span,
1231        whole_span: Span,
1232    ) -> hir::ExprKind<'hir> {
1233        // Return early in case of an ordinary assignment.
1234        fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_>, lhs: &Expr) -> bool {
1235            match &lhs.kind {
1236                ExprKind::Array(..)
1237                | ExprKind::Struct(..)
1238                | ExprKind::Tup(..)
1239                | ExprKind::Underscore => false,
1240                // Check for unit struct constructor.
1241                ExprKind::Path(..) => lower_ctx.extract_unit_struct_path(lhs).is_none(),
1242                // Check for tuple struct constructor.
1243                ExprKind::Call(callee, ..) => lower_ctx.extract_tuple_struct_path(callee).is_none(),
1244                ExprKind::Paren(e) => {
1245                    match e.kind {
1246                        // We special-case `(..)` for consistency with patterns.
1247                        ExprKind::Range(None, None, RangeLimits::HalfOpen) => false,
1248                        _ => is_ordinary(lower_ctx, e),
1249                    }
1250                }
1251                _ => true,
1252            }
1253        }
1254        if is_ordinary(self, lhs) {
1255            return hir::ExprKind::Assign(
1256                self.lower_expr(lhs),
1257                self.lower_expr(rhs),
1258                self.lower_span(eq_sign_span),
1259            );
1260        }
1261
1262        let mut assignments = vec![];
1263
1264        // The LHS becomes a pattern: `(lhs1, lhs2)`.
1265        let pat = self.destructure_assign(lhs, eq_sign_span, &mut assignments);
1266        let rhs = self.lower_expr(rhs);
1267
1268        // Introduce a `let` for destructuring: `let (lhs1, lhs2) = t`.
1269        let destructure_let =
1270            self.stmt_let_pat(None, whole_span, Some(rhs), pat, hir::LocalSource::AssignDesugar);
1271
1272        // `a = lhs1; b = lhs2;`.
1273        let stmts = self.arena.alloc_from_iter(std::iter::once(destructure_let).chain(assignments));
1274
1275        // Wrap everything in a block.
1276        hir::ExprKind::Block(self.block_all(whole_span, stmts, None), None)
1277    }
1278
1279    /// If the given expression is a path to a tuple struct, returns that path.
1280    /// It is not a complete check, but just tries to reject most paths early
1281    /// if they are not tuple structs.
1282    /// Type checking will take care of the full validation later.
1283    fn extract_tuple_struct_path<'a>(
1284        &mut self,
1285        expr: &'a Expr,
1286    ) -> Option<(&'a Option<Box<QSelf>>, &'a Path)> {
1287        if let ExprKind::Path(qself, path) = &expr.kind {
1288            // Does the path resolve to something disallowed in a tuple struct/variant pattern?
1289            if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
1290                if let Some(res) = partial_res.full_res()
1291                    && !res.expected_in_tuple_struct_pat()
1292                {
1293                    return None;
1294                }
1295            }
1296            return Some((qself, path));
1297        }
1298        None
1299    }
1300
1301    /// If the given expression is a path to a unit struct, returns that path.
1302    /// It is not a complete check, but just tries to reject most paths early
1303    /// if they are not unit structs.
1304    /// Type checking will take care of the full validation later.
1305    fn extract_unit_struct_path<'a>(
1306        &mut self,
1307        expr: &'a Expr,
1308    ) -> Option<(&'a Option<Box<QSelf>>, &'a Path)> {
1309        if let ExprKind::Path(qself, path) = &expr.kind {
1310            // Does the path resolve to something disallowed in a unit struct/variant pattern?
1311            if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
1312                if let Some(res) = partial_res.full_res()
1313                    && !res.expected_in_unit_struct_pat()
1314                {
1315                    return None;
1316                }
1317            }
1318            return Some((qself, path));
1319        }
1320        None
1321    }
1322
1323    /// Convert the LHS of a destructuring assignment to a pattern.
1324    /// Each sub-assignment is recorded in `assignments`.
1325    fn destructure_assign(
1326        &mut self,
1327        lhs: &Expr,
1328        eq_sign_span: Span,
1329        assignments: &mut Vec<hir::Stmt<'hir>>,
1330    ) -> &'hir hir::Pat<'hir> {
1331        self.arena.alloc(self.destructure_assign_mut(lhs, eq_sign_span, assignments))
1332    }
1333
1334    fn destructure_assign_mut(
1335        &mut self,
1336        lhs: &Expr,
1337        eq_sign_span: Span,
1338        assignments: &mut Vec<hir::Stmt<'hir>>,
1339    ) -> hir::Pat<'hir> {
1340        match &lhs.kind {
1341            // Underscore pattern.
1342            ExprKind::Underscore => {
1343                return self.pat_without_dbm(lhs.span, hir::PatKind::Wild);
1344            }
1345            // Slice patterns.
1346            ExprKind::Array(elements) => {
1347                let (pats, rest) =
1348                    self.destructure_sequence(elements, "slice", eq_sign_span, assignments);
1349                let slice_pat = if let Some((i, span)) = rest {
1350                    let (before, after) = pats.split_at(i);
1351                    hir::PatKind::Slice(
1352                        before,
1353                        Some(self.arena.alloc(self.pat_without_dbm(span, hir::PatKind::Wild))),
1354                        after,
1355                    )
1356                } else {
1357                    hir::PatKind::Slice(pats, None, &[])
1358                };
1359                return self.pat_without_dbm(lhs.span, slice_pat);
1360            }
1361            // Tuple structs.
1362            ExprKind::Call(callee, args) => {
1363                if let Some((qself, path)) = self.extract_tuple_struct_path(callee) {
1364                    let (pats, rest) = self.destructure_sequence(
1365                        args,
1366                        "tuple struct or variant",
1367                        eq_sign_span,
1368                        assignments,
1369                    );
1370                    let qpath = self.lower_qpath(
1371                        callee.id,
1372                        qself,
1373                        path,
1374                        ParamMode::Optional,
1375                        AllowReturnTypeNotation::No,
1376                        ImplTraitContext::Disallowed(ImplTraitPosition::Path),
1377                        None,
1378                    );
1379                    // Destructure like a tuple struct.
1380                    let tuple_struct_pat = hir::PatKind::TupleStruct(
1381                        qpath,
1382                        pats,
1383                        hir::DotDotPos::new(rest.map(|r| r.0)),
1384                    );
1385                    return self.pat_without_dbm(lhs.span, tuple_struct_pat);
1386                }
1387            }
1388            // Unit structs and enum variants.
1389            ExprKind::Path(..) => {
1390                if let Some((qself, path)) = self.extract_unit_struct_path(lhs) {
1391                    let qpath = self.lower_qpath(
1392                        lhs.id,
1393                        qself,
1394                        path,
1395                        ParamMode::Optional,
1396                        AllowReturnTypeNotation::No,
1397                        ImplTraitContext::Disallowed(ImplTraitPosition::Path),
1398                        None,
1399                    );
1400                    // Destructure like a unit struct.
1401                    let unit_struct_pat = hir::PatKind::Expr(self.arena.alloc(hir::PatExpr {
1402                        kind: hir::PatExprKind::Path(qpath),
1403                        hir_id: self.next_id(),
1404                        span: self.lower_span(lhs.span),
1405                    }));
1406                    return self.pat_without_dbm(lhs.span, unit_struct_pat);
1407                }
1408            }
1409            // Structs.
1410            ExprKind::Struct(se) => {
1411                let field_pats = self.arena.alloc_from_iter(se.fields.iter().map(|f| {
1412                    let pat = self.destructure_assign(&f.expr, eq_sign_span, assignments);
1413                    hir::PatField {
1414                        hir_id: self.next_id(),
1415                        ident: self.lower_ident(f.ident),
1416                        pat,
1417                        is_shorthand: f.is_shorthand,
1418                        span: self.lower_span(f.span),
1419                    }
1420                }));
1421                let qpath = self.lower_qpath(
1422                    lhs.id,
1423                    &se.qself,
1424                    &se.path,
1425                    ParamMode::Optional,
1426                    AllowReturnTypeNotation::No,
1427                    ImplTraitContext::Disallowed(ImplTraitPosition::Path),
1428                    None,
1429                );
1430                let fields_omitted = match &se.rest {
1431                    StructRest::Base(e) => {
1432                        self.dcx().emit_err(FunctionalRecordUpdateDestructuringAssignment {
1433                            span: e.span,
1434                        });
1435                        Some(self.lower_span(e.span))
1436                    }
1437                    StructRest::Rest(span) => Some(self.lower_span(*span)),
1438                    StructRest::None => None,
1439                };
1440                let struct_pat = hir::PatKind::Struct(qpath, field_pats, fields_omitted);
1441                return self.pat_without_dbm(lhs.span, struct_pat);
1442            }
1443            // Tuples.
1444            ExprKind::Tup(elements) => {
1445                let (pats, rest) =
1446                    self.destructure_sequence(elements, "tuple", eq_sign_span, assignments);
1447                let tuple_pat = hir::PatKind::Tuple(pats, hir::DotDotPos::new(rest.map(|r| r.0)));
1448                return self.pat_without_dbm(lhs.span, tuple_pat);
1449            }
1450            ExprKind::Paren(e) => {
1451                // We special-case `(..)` for consistency with patterns.
1452                if let ExprKind::Range(None, None, RangeLimits::HalfOpen) = e.kind {
1453                    let tuple_pat = hir::PatKind::Tuple(&[], hir::DotDotPos::new(Some(0)));
1454                    return self.pat_without_dbm(lhs.span, tuple_pat);
1455                } else {
1456                    return self.destructure_assign_mut(e, eq_sign_span, assignments);
1457                }
1458            }
1459            _ => {}
1460        }
1461        // Treat all other cases as normal lvalue.
1462        let ident = Ident::new(sym::lhs, self.lower_span(lhs.span));
1463        let (pat, binding) = self.pat_ident_mut(lhs.span, ident);
1464        let ident = self.expr_ident(lhs.span, ident, binding);
1465        let assign =
1466            hir::ExprKind::Assign(self.lower_expr(lhs), ident, self.lower_span(eq_sign_span));
1467        let expr = self.expr(lhs.span, assign);
1468        assignments.push(self.stmt_expr(lhs.span, expr));
1469        pat
1470    }
1471
1472    /// Destructure a sequence of expressions occurring on the LHS of an assignment.
1473    /// Such a sequence occurs in a tuple (struct)/slice.
1474    /// Return a sequence of corresponding patterns, and the index and the span of `..` if it
1475    /// exists.
1476    /// Each sub-assignment is recorded in `assignments`.
1477    fn destructure_sequence(
1478        &mut self,
1479        elements: &[Box<Expr>],
1480        ctx: &str,
1481        eq_sign_span: Span,
1482        assignments: &mut Vec<hir::Stmt<'hir>>,
1483    ) -> (&'hir [hir::Pat<'hir>], Option<(usize, Span)>) {
1484        let mut rest = None;
1485        let elements =
1486            self.arena.alloc_from_iter(elements.iter().enumerate().filter_map(|(i, e)| {
1487                // Check for `..` pattern.
1488                if let ExprKind::Range(None, None, RangeLimits::HalfOpen) = e.kind {
1489                    if let Some((_, prev_span)) = rest {
1490                        self.ban_extra_rest_pat(e.span, prev_span, ctx);
1491                    } else {
1492                        rest = Some((i, e.span));
1493                    }
1494                    None
1495                } else {
1496                    Some(self.destructure_assign_mut(e, eq_sign_span, assignments))
1497                }
1498            }));
1499        (elements, rest)
1500    }
1501
1502    /// Desugar `<start>..=<end>` into `std::ops::RangeInclusive::new(<start>, <end>)`.
1503    fn lower_expr_range_closed(&mut self, span: Span, e1: &Expr, e2: &Expr) -> hir::ExprKind<'hir> {
1504        let e1 = self.lower_expr_mut(e1);
1505        let e2 = self.lower_expr_mut(e2);
1506        let fn_path = self.make_lang_item_qpath(hir::LangItem::RangeInclusiveNew, span, None);
1507        let fn_expr = self.arena.alloc(self.expr(span, hir::ExprKind::Path(fn_path)));
1508        hir::ExprKind::Call(fn_expr, arena_vec![self; e1, e2])
1509    }
1510
1511    fn lower_expr_range(
1512        &mut self,
1513        span: Span,
1514        e1: Option<&Expr>,
1515        e2: Option<&Expr>,
1516        lims: RangeLimits,
1517    ) -> hir::ExprKind<'hir> {
1518        use rustc_ast::RangeLimits::*;
1519
1520        let lang_item = match (e1, e2, lims) {
1521            (None, None, HalfOpen) => hir::LangItem::RangeFull,
1522            (Some(..), None, HalfOpen) => {
1523                if self.tcx.features().new_range() {
1524                    hir::LangItem::RangeFromCopy
1525                } else {
1526                    hir::LangItem::RangeFrom
1527                }
1528            }
1529            (None, Some(..), HalfOpen) => hir::LangItem::RangeTo,
1530            (Some(..), Some(..), HalfOpen) => {
1531                if self.tcx.features().new_range() {
1532                    hir::LangItem::RangeCopy
1533                } else {
1534                    hir::LangItem::Range
1535                }
1536            }
1537            (None, Some(..), Closed) => {
1538                if self.tcx.features().new_range() {
1539                    hir::LangItem::RangeToInclusiveCopy
1540                } else {
1541                    hir::LangItem::RangeToInclusive
1542                }
1543            }
1544            (Some(e1), Some(e2), Closed) => {
1545                if self.tcx.features().new_range() {
1546                    hir::LangItem::RangeInclusiveCopy
1547                } else {
1548                    return self.lower_expr_range_closed(span, e1, e2);
1549                }
1550            }
1551            (start, None, Closed) => {
1552                self.dcx().emit_err(InclusiveRangeWithNoEnd { span });
1553                match start {
1554                    Some(..) => {
1555                        if self.tcx.features().new_range() {
1556                            hir::LangItem::RangeFromCopy
1557                        } else {
1558                            hir::LangItem::RangeFrom
1559                        }
1560                    }
1561                    None => hir::LangItem::RangeFull,
1562                }
1563            }
1564        };
1565
1566        let fields = self.arena.alloc_from_iter(
1567            e1.iter()
1568                .map(|e| (sym::start, e))
1569                .chain(e2.iter().map(|e| {
1570                    (
1571                        if matches!(
1572                            lang_item,
1573                            hir::LangItem::RangeInclusiveCopy | hir::LangItem::RangeToInclusiveCopy
1574                        ) {
1575                            sym::last
1576                        } else {
1577                            sym::end
1578                        },
1579                        e,
1580                    )
1581                }))
1582                .map(|(s, e)| {
1583                    let span = self.lower_span(e.span);
1584                    let span = self.mark_span_with_reason(DesugaringKind::RangeExpr, span, None);
1585                    let expr = self.lower_expr(e);
1586                    let ident = Ident::new(s, span);
1587                    self.expr_field(ident, expr, span)
1588                }),
1589        );
1590
1591        hir::ExprKind::Struct(
1592            self.arena.alloc(self.make_lang_item_qpath(lang_item, span, None)),
1593            fields,
1594            hir::StructTailExpr::None,
1595        )
1596    }
1597
1598    // Record labelled expr's HirId so that we can retrieve it in `lower_jump_destination` without
1599    // lowering node id again.
1600    fn lower_label(
1601        &mut self,
1602        opt_label: Option<Label>,
1603        dest_id: NodeId,
1604        dest_hir_id: hir::HirId,
1605    ) -> Option<Label> {
1606        let label = opt_label?;
1607        self.ident_and_label_to_local_id.insert(dest_id, dest_hir_id.local_id);
1608        Some(Label { ident: self.lower_ident(label.ident) })
1609    }
1610
1611    fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
1612        let target_id = match destination {
1613            Some((id, _)) => {
1614                if let Some(loop_id) = self.resolver.get_label_res(id) {
1615                    let local_id = self.ident_and_label_to_local_id[&loop_id];
1616                    let loop_hir_id = HirId { owner: self.current_hir_id_owner, local_id };
1617                    Ok(loop_hir_id)
1618                } else {
1619                    Err(hir::LoopIdError::UnresolvedLabel)
1620                }
1621            }
1622            None => {
1623                self.loop_scope.map(|id| Ok(id)).unwrap_or(Err(hir::LoopIdError::OutsideLoopScope))
1624            }
1625        };
1626        let label = destination
1627            .map(|(_, label)| label)
1628            .map(|label| Label { ident: self.lower_ident(label.ident) });
1629        hir::Destination { label, target_id }
1630    }
1631
1632    fn lower_jump_destination(&mut self, id: NodeId, opt_label: Option<Label>) -> hir::Destination {
1633        if self.is_in_loop_condition && opt_label.is_none() {
1634            hir::Destination {
1635                label: None,
1636                target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition),
1637            }
1638        } else {
1639            self.lower_loop_destination(opt_label.map(|label| (id, label)))
1640        }
1641    }
1642
1643    fn with_try_block_scope<T>(
1644        &mut self,
1645        scope: TryBlockScope,
1646        f: impl FnOnce(&mut Self) -> T,
1647    ) -> T {
1648        let old_scope = mem::replace(&mut self.try_block_scope, scope);
1649        let result = f(self);
1650        self.try_block_scope = old_scope;
1651        result
1652    }
1653
1654    fn with_loop_scope<T>(&mut self, loop_id: hir::HirId, f: impl FnOnce(&mut Self) -> T) -> T {
1655        // We're no longer in the base loop's condition; we're in another loop.
1656        let was_in_loop_condition = self.is_in_loop_condition;
1657        self.is_in_loop_condition = false;
1658
1659        let old_scope = self.loop_scope.replace(loop_id);
1660        let result = f(self);
1661        self.loop_scope = old_scope;
1662
1663        self.is_in_loop_condition = was_in_loop_condition;
1664
1665        result
1666    }
1667
1668    fn with_loop_condition_scope<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T {
1669        let was_in_loop_condition = self.is_in_loop_condition;
1670        self.is_in_loop_condition = true;
1671
1672        let result = f(self);
1673
1674        self.is_in_loop_condition = was_in_loop_condition;
1675
1676        result
1677    }
1678
1679    fn lower_expr_field(&mut self, f: &ExprField) -> hir::ExprField<'hir> {
1680        let hir_id = self.lower_node_id(f.id);
1681        self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField);
1682        hir::ExprField {
1683            hir_id,
1684            ident: self.lower_ident(f.ident),
1685            expr: self.lower_expr(&f.expr),
1686            span: self.lower_span(f.span),
1687            is_shorthand: f.is_shorthand,
1688        }
1689    }
1690
1691    fn lower_expr_yield(&mut self, span: Span, opt_expr: Option<&Expr>) -> hir::ExprKind<'hir> {
1692        let yielded =
1693            opt_expr.as_ref().map(|x| self.lower_expr(x)).unwrap_or_else(|| self.expr_unit(span));
1694
1695        if !self.tcx.features().yield_expr()
1696            && !self.tcx.features().coroutines()
1697            && !self.tcx.features().gen_blocks()
1698        {
1699            rustc_session::parse::feature_err(
1700                &self.tcx.sess,
1701                sym::yield_expr,
1702                span,
1703                fluent_generated::ast_lowering_yield,
1704            )
1705            .emit();
1706        }
1707
1708        let is_async_gen = match self.coroutine_kind {
1709            Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _)) => false,
1710            Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _)) => true,
1711            Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => {
1712                // Lower to a block `{ EXPR; <error> }` so that the awaited expr
1713                // is not accidentally orphaned.
1714                let stmt_id = self.next_id();
1715                let expr_err = self.expr(
1716                    yielded.span,
1717                    hir::ExprKind::Err(self.dcx().emit_err(AsyncCoroutinesNotSupported { span })),
1718                );
1719                return hir::ExprKind::Block(
1720                    self.block_all(
1721                        yielded.span,
1722                        arena_vec![self; hir::Stmt {
1723                            hir_id: stmt_id,
1724                            kind: hir::StmtKind::Semi(yielded),
1725                            span: yielded.span,
1726                        }],
1727                        Some(self.arena.alloc(expr_err)),
1728                    ),
1729                    None,
1730                );
1731            }
1732            Some(hir::CoroutineKind::Coroutine(_)) => false,
1733            None => {
1734                let suggestion = self.current_item.map(|s| s.shrink_to_lo());
1735                self.dcx().emit_err(YieldInClosure { span, suggestion });
1736                self.coroutine_kind = Some(hir::CoroutineKind::Coroutine(Movability::Movable));
1737
1738                false
1739            }
1740        };
1741
1742        if is_async_gen {
1743            // `yield $expr` is transformed into `task_context = yield async_gen_ready($expr)`.
1744            // This ensures that we store our resumed `ResumeContext` correctly, and also that
1745            // the apparent value of the `yield` expression is `()`.
1746            let desugar_span = self.mark_span_with_reason(
1747                DesugaringKind::Async,
1748                span,
1749                Some(Arc::clone(&self.allow_async_gen)),
1750            );
1751            let wrapped_yielded = self.expr_call_lang_item_fn(
1752                desugar_span,
1753                hir::LangItem::AsyncGenReady,
1754                std::slice::from_ref(yielded),
1755            );
1756            let yield_expr = self.arena.alloc(
1757                self.expr(span, hir::ExprKind::Yield(wrapped_yielded, hir::YieldSource::Yield)),
1758            );
1759
1760            let Some(task_context_hid) = self.task_context else {
1761                unreachable!("use of `await` outside of an async context.");
1762            };
1763            let task_context_ident = Ident::with_dummy_span(sym::_task_context);
1764            let lhs = self.expr_ident(desugar_span, task_context_ident, task_context_hid);
1765
1766            hir::ExprKind::Assign(lhs, yield_expr, self.lower_span(span))
1767        } else {
1768            hir::ExprKind::Yield(yielded, hir::YieldSource::Yield)
1769        }
1770    }
1771
1772    /// Desugar `ExprForLoop` from: `[opt_ident]: for <pat> in <head> <body>` into:
1773    /// ```ignore (pseudo-rust)
1774    /// {
1775    ///     let result = match IntoIterator::into_iter(<head>) {
1776    ///         mut iter => {
1777    ///             [opt_ident]: loop {
1778    ///                 match Iterator::next(&mut iter) {
1779    ///                     None => break,
1780    ///                     Some(<pat>) => <body>,
1781    ///                 };
1782    ///             }
1783    ///         }
1784    ///     };
1785    ///     result
1786    /// }
1787    /// ```
1788    fn lower_expr_for(
1789        &mut self,
1790        e: &Expr,
1791        pat: &Pat,
1792        head: &Expr,
1793        body: &Block,
1794        opt_label: Option<Label>,
1795        loop_kind: ForLoopKind,
1796    ) -> hir::Expr<'hir> {
1797        let head = self.lower_expr_mut(head);
1798        let pat = self.lower_pat(pat);
1799        let for_span =
1800            self.mark_span_with_reason(DesugaringKind::ForLoop, self.lower_span(e.span), None);
1801        let for_ctxt = for_span.ctxt();
1802
1803        // Try to point both the head and pat spans to their position in the for loop
1804        // rather than inside a macro.
1805        let head_span =
1806            head.span.find_ancestor_in_same_ctxt(e.span).unwrap_or(head.span).with_ctxt(for_ctxt);
1807        let pat_span =
1808            pat.span.find_ancestor_in_same_ctxt(e.span).unwrap_or(pat.span).with_ctxt(for_ctxt);
1809
1810        let loop_hir_id = self.lower_node_id(e.id);
1811        let label = self.lower_label(opt_label, e.id, loop_hir_id);
1812
1813        // `None => break`
1814        let none_arm = {
1815            let break_expr =
1816                self.with_loop_scope(loop_hir_id, |this| this.expr_break_alloc(for_span));
1817            let pat = self.pat_none(for_span);
1818            self.arm(pat, break_expr)
1819        };
1820
1821        // Some(<pat>) => <body>,
1822        let some_arm = {
1823            let some_pat = self.pat_some(pat_span, pat);
1824            let body_block =
1825                self.with_loop_scope(loop_hir_id, |this| this.lower_block(body, false));
1826            let body_expr = self.arena.alloc(self.expr_block(body_block));
1827            self.arm(some_pat, body_expr)
1828        };
1829
1830        // `mut iter`
1831        let iter = Ident::with_dummy_span(sym::iter);
1832        let (iter_pat, iter_pat_nid) =
1833            self.pat_ident_binding_mode(head_span, iter, hir::BindingMode::MUT);
1834
1835        let match_expr = {
1836            let iter = self.expr_ident(head_span, iter, iter_pat_nid);
1837            let next_expr = match loop_kind {
1838                ForLoopKind::For => {
1839                    // `Iterator::next(&mut iter)`
1840                    let ref_mut_iter = self.expr_mut_addr_of(head_span, iter);
1841                    self.expr_call_lang_item_fn(
1842                        head_span,
1843                        hir::LangItem::IteratorNext,
1844                        arena_vec![self; ref_mut_iter],
1845                    )
1846                }
1847                ForLoopKind::ForAwait => {
1848                    // we'll generate `unsafe { Pin::new_unchecked(&mut iter) })` and then pass this
1849                    // to make_lowered_await with `FutureKind::AsyncIterator` which will generator
1850                    // calls to `poll_next`. In user code, this would probably be a call to
1851                    // `Pin::as_mut` but here it's easy enough to do `new_unchecked`.
1852
1853                    // `&mut iter`
1854                    let iter = self.expr_mut_addr_of(head_span, iter);
1855                    // `Pin::new_unchecked(...)`
1856                    let iter = self.arena.alloc(self.expr_call_lang_item_fn_mut(
1857                        head_span,
1858                        hir::LangItem::PinNewUnchecked,
1859                        arena_vec![self; iter],
1860                    ));
1861                    // `unsafe { ... }`
1862                    let iter = self.arena.alloc(self.expr_unsafe(head_span, iter));
1863                    let kind = self.make_lowered_await(head_span, iter, FutureKind::AsyncIterator);
1864                    self.arena.alloc(hir::Expr { hir_id: self.next_id(), kind, span: head_span })
1865                }
1866            };
1867            let arms = arena_vec![self; none_arm, some_arm];
1868
1869            // `match $next_expr { ... }`
1870            self.expr_match(head_span, next_expr, arms, hir::MatchSource::ForLoopDesugar)
1871        };
1872        let match_stmt = self.stmt_expr(for_span, match_expr);
1873
1874        let loop_block = self.block_all(for_span, arena_vec![self; match_stmt], None);
1875
1876        // `[opt_ident]: loop { ... }`
1877        let kind = hir::ExprKind::Loop(
1878            loop_block,
1879            label,
1880            hir::LoopSource::ForLoop,
1881            self.lower_span(for_span.with_hi(head.span.hi())),
1882        );
1883        let loop_expr = self.arena.alloc(hir::Expr { hir_id: loop_hir_id, kind, span: for_span });
1884
1885        // `mut iter => { ... }`
1886        let iter_arm = self.arm(iter_pat, loop_expr);
1887
1888        let match_expr = match loop_kind {
1889            ForLoopKind::For => {
1890                // `::std::iter::IntoIterator::into_iter(<head>)`
1891                let into_iter_expr = self.expr_call_lang_item_fn(
1892                    head_span,
1893                    hir::LangItem::IntoIterIntoIter,
1894                    arena_vec![self; head],
1895                );
1896
1897                self.arena.alloc(self.expr_match(
1898                    for_span,
1899                    into_iter_expr,
1900                    arena_vec![self; iter_arm],
1901                    hir::MatchSource::ForLoopDesugar,
1902                ))
1903            }
1904            // `match into_async_iter(<head>) { ref mut iter => match unsafe { Pin::new_unchecked(iter) } { ... } }`
1905            ForLoopKind::ForAwait => {
1906                let iter_ident = iter;
1907                let (async_iter_pat, async_iter_pat_id) =
1908                    self.pat_ident_binding_mode(head_span, iter_ident, hir::BindingMode::REF_MUT);
1909                let iter = self.expr_ident_mut(head_span, iter_ident, async_iter_pat_id);
1910                // `Pin::new_unchecked(...)`
1911                let iter = self.arena.alloc(self.expr_call_lang_item_fn_mut(
1912                    head_span,
1913                    hir::LangItem::PinNewUnchecked,
1914                    arena_vec![self; iter],
1915                ));
1916                // `unsafe { ... }`
1917                let iter = self.arena.alloc(self.expr_unsafe(head_span, iter));
1918                let inner_match_expr = self.arena.alloc(self.expr_match(
1919                    for_span,
1920                    iter,
1921                    arena_vec![self; iter_arm],
1922                    hir::MatchSource::ForLoopDesugar,
1923                ));
1924
1925                // `::core::async_iter::IntoAsyncIterator::into_async_iter(<head>)`
1926                let iter = self.expr_call_lang_item_fn(
1927                    head_span,
1928                    hir::LangItem::IntoAsyncIterIntoIter,
1929                    arena_vec![self; head],
1930                );
1931                let iter_arm = self.arm(async_iter_pat, inner_match_expr);
1932                self.arena.alloc(self.expr_match(
1933                    for_span,
1934                    iter,
1935                    arena_vec![self; iter_arm],
1936                    hir::MatchSource::ForLoopDesugar,
1937                ))
1938            }
1939        };
1940
1941        // This is effectively `{ let _result = ...; _result }`.
1942        // The construct was introduced in #21984 and is necessary to make sure that
1943        // temporaries in the `head` expression are dropped and do not leak to the
1944        // surrounding scope of the `match` since the `match` is not a terminating scope.
1945        //
1946        // Also, add the attributes to the outer returned expr node.
1947        let expr = self.expr_drop_temps_mut(for_span, match_expr);
1948        self.lower_attrs(expr.hir_id, &e.attrs, e.span, Target::from_expr(e));
1949        expr
1950    }
1951
1952    /// Desugar `ExprKind::Try` from: `<expr>?` into:
1953    /// ```ignore (pseudo-rust)
1954    /// match Try::branch(<expr>) {
1955    ///     ControlFlow::Continue(val) => #[allow(unreachable_code)] val,,
1956    ///     ControlFlow::Break(residual) =>
1957    ///         #[allow(unreachable_code)]
1958    ///         // If there is an enclosing `try {...}`:
1959    ///         break 'catch_target Residual::into_try_type(residual),
1960    ///         // Otherwise:
1961    ///         return Try::from_residual(residual),
1962    /// }
1963    /// ```
1964    fn lower_expr_try(&mut self, span: Span, sub_expr: &Expr) -> hir::ExprKind<'hir> {
1965        let unstable_span = self.mark_span_with_reason(
1966            DesugaringKind::QuestionMark,
1967            span,
1968            Some(Arc::clone(&self.allow_try_trait)),
1969        );
1970        let try_span = self.tcx.sess.source_map().end_point(span);
1971        let try_span = self.mark_span_with_reason(
1972            DesugaringKind::QuestionMark,
1973            try_span,
1974            Some(Arc::clone(&self.allow_try_trait)),
1975        );
1976
1977        // `Try::branch(<expr>)`
1978        let scrutinee = {
1979            // expand <expr>
1980            let sub_expr = self.lower_expr_mut(sub_expr);
1981
1982            self.expr_call_lang_item_fn(
1983                unstable_span,
1984                hir::LangItem::TryTraitBranch,
1985                arena_vec![self; sub_expr],
1986            )
1987        };
1988
1989        let attrs: AttrVec = thin_vec![self.unreachable_code_attr(try_span)];
1990
1991        // `ControlFlow::Continue(val) => #[allow(unreachable_code)] val,`
1992        let continue_arm = {
1993            let val_ident = Ident::with_dummy_span(sym::val);
1994            let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
1995            let val_expr = self.expr_ident(span, val_ident, val_pat_nid);
1996            self.lower_attrs(val_expr.hir_id, &attrs, span, Target::Expression);
1997            let continue_pat = self.pat_cf_continue(unstable_span, val_pat);
1998            self.arm(continue_pat, val_expr)
1999        };
2000
2001        // `ControlFlow::Break(residual) =>
2002        //     #[allow(unreachable_code)]
2003        //     return Try::from_residual(residual),`
2004        let break_arm = {
2005            let residual_ident = Ident::with_dummy_span(sym::residual);
2006            let (residual_local, residual_local_nid) = self.pat_ident(try_span, residual_ident);
2007            let residual_expr = self.expr_ident_mut(try_span, residual_ident, residual_local_nid);
2008
2009            let (constructor_item, target_id) = match self.try_block_scope {
2010                TryBlockScope::Function => {
2011                    (hir::LangItem::TryTraitFromResidual, Err(hir::LoopIdError::OutsideLoopScope))
2012                }
2013                TryBlockScope::Homogeneous(block_id) => {
2014                    (hir::LangItem::ResidualIntoTryType, Ok(block_id))
2015                }
2016                TryBlockScope::Heterogeneous(block_id) => {
2017                    (hir::LangItem::TryTraitFromResidual, Ok(block_id))
2018                }
2019            };
2020            let from_residual_expr = self.wrap_in_try_constructor(
2021                constructor_item,
2022                try_span,
2023                self.arena.alloc(residual_expr),
2024                unstable_span,
2025            );
2026            let ret_expr = if target_id.is_ok() {
2027                self.arena.alloc(self.expr(
2028                    try_span,
2029                    hir::ExprKind::Break(
2030                        hir::Destination { label: None, target_id },
2031                        Some(from_residual_expr),
2032                    ),
2033                ))
2034            } else {
2035                let ret_expr = self.checked_return(Some(from_residual_expr));
2036                self.arena.alloc(self.expr(try_span, ret_expr))
2037            };
2038            self.lower_attrs(ret_expr.hir_id, &attrs, span, Target::Expression);
2039
2040            let break_pat = self.pat_cf_break(try_span, residual_local);
2041            self.arm(break_pat, ret_expr)
2042        };
2043
2044        hir::ExprKind::Match(
2045            scrutinee,
2046            arena_vec![self; break_arm, continue_arm],
2047            hir::MatchSource::TryDesugar(scrutinee.hir_id),
2048        )
2049    }
2050
2051    /// Desugar `ExprKind::Yeet` from: `do yeet <expr>` into:
2052    /// ```ignore(illustrative)
2053    /// // If there is an enclosing `try {...}`:
2054    /// break 'catch_target FromResidual::from_residual(Yeet(residual));
2055    /// // Otherwise:
2056    /// return FromResidual::from_residual(Yeet(residual));
2057    /// ```
2058    /// But to simplify this, there's a `from_yeet` lang item function which
2059    /// handles the combined `FromResidual::from_residual(Yeet(residual))`.
2060    fn lower_expr_yeet(&mut self, span: Span, sub_expr: Option<&Expr>) -> hir::ExprKind<'hir> {
2061        // The expression (if present) or `()` otherwise.
2062        let (yeeted_span, yeeted_expr) = if let Some(sub_expr) = sub_expr {
2063            (sub_expr.span, self.lower_expr(sub_expr))
2064        } else {
2065            (self.mark_span_with_reason(DesugaringKind::YeetExpr, span, None), self.expr_unit(span))
2066        };
2067
2068        let unstable_span = self.mark_span_with_reason(
2069            DesugaringKind::YeetExpr,
2070            span,
2071            Some(Arc::clone(&self.allow_try_trait)),
2072        );
2073
2074        let from_yeet_expr = self.wrap_in_try_constructor(
2075            hir::LangItem::TryTraitFromYeet,
2076            unstable_span,
2077            yeeted_expr,
2078            yeeted_span,
2079        );
2080
2081        match self.try_block_scope {
2082            TryBlockScope::Homogeneous(block_id) | TryBlockScope::Heterogeneous(block_id) => {
2083                hir::ExprKind::Break(
2084                    hir::Destination { label: None, target_id: Ok(block_id) },
2085                    Some(from_yeet_expr),
2086                )
2087            }
2088            TryBlockScope::Function => self.checked_return(Some(from_yeet_expr)),
2089        }
2090    }
2091
2092    // =========================================================================
2093    // Helper methods for building HIR.
2094    // =========================================================================
2095
2096    /// Wrap the given `expr` in a terminating scope using `hir::ExprKind::DropTemps`.
2097    ///
2098    /// In terms of drop order, it has the same effect as wrapping `expr` in
2099    /// `{ let _t = $expr; _t }` but should provide better compile-time performance.
2100    ///
2101    /// The drop order can be important, e.g. to drop temporaries from an `async fn`
2102    /// body before its parameters.
2103    pub(super) fn expr_drop_temps(
2104        &mut self,
2105        span: Span,
2106        expr: &'hir hir::Expr<'hir>,
2107    ) -> &'hir hir::Expr<'hir> {
2108        self.arena.alloc(self.expr_drop_temps_mut(span, expr))
2109    }
2110
2111    pub(super) fn expr_drop_temps_mut(
2112        &mut self,
2113        span: Span,
2114        expr: &'hir hir::Expr<'hir>,
2115    ) -> hir::Expr<'hir> {
2116        self.expr(span, hir::ExprKind::DropTemps(expr))
2117    }
2118
2119    pub(super) fn expr_match(
2120        &mut self,
2121        span: Span,
2122        arg: &'hir hir::Expr<'hir>,
2123        arms: &'hir [hir::Arm<'hir>],
2124        source: hir::MatchSource,
2125    ) -> hir::Expr<'hir> {
2126        self.expr(span, hir::ExprKind::Match(arg, arms, source))
2127    }
2128
2129    fn expr_break(&mut self, span: Span) -> hir::Expr<'hir> {
2130        let expr_break = hir::ExprKind::Break(self.lower_loop_destination(None), None);
2131        self.expr(span, expr_break)
2132    }
2133
2134    fn expr_break_alloc(&mut self, span: Span) -> &'hir hir::Expr<'hir> {
2135        let expr_break = self.expr_break(span);
2136        self.arena.alloc(expr_break)
2137    }
2138
2139    fn expr_mut_addr_of(&mut self, span: Span, e: &'hir hir::Expr<'hir>) -> hir::Expr<'hir> {
2140        self.expr(span, hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, e))
2141    }
2142
2143    pub(super) fn expr_unit(&mut self, sp: Span) -> &'hir hir::Expr<'hir> {
2144        self.arena.alloc(self.expr(sp, hir::ExprKind::Tup(&[])))
2145    }
2146
2147    pub(super) fn expr_str(&mut self, sp: Span, value: Symbol) -> hir::Expr<'hir> {
2148        let lit = hir::Lit {
2149            span: self.lower_span(sp),
2150            node: ast::LitKind::Str(value, ast::StrStyle::Cooked),
2151        };
2152        self.expr(sp, hir::ExprKind::Lit(lit))
2153    }
2154
2155    pub(super) fn expr_byte_str(&mut self, sp: Span, value: ByteSymbol) -> hir::Expr<'hir> {
2156        let lit = hir::Lit {
2157            span: self.lower_span(sp),
2158            node: ast::LitKind::ByteStr(value, ast::StrStyle::Cooked),
2159        };
2160        self.expr(sp, hir::ExprKind::Lit(lit))
2161    }
2162
2163    pub(super) fn expr_call_mut(
2164        &mut self,
2165        span: Span,
2166        e: &'hir hir::Expr<'hir>,
2167        args: &'hir [hir::Expr<'hir>],
2168    ) -> hir::Expr<'hir> {
2169        self.expr(span, hir::ExprKind::Call(e, args))
2170    }
2171
2172    pub(super) fn expr_struct(
2173        &mut self,
2174        span: Span,
2175        path: &'hir hir::QPath<'hir>,
2176        fields: &'hir [hir::ExprField<'hir>],
2177    ) -> hir::Expr<'hir> {
2178        self.expr(span, hir::ExprKind::Struct(path, fields, rustc_hir::StructTailExpr::None))
2179    }
2180
2181    pub(super) fn expr_enum_variant(
2182        &mut self,
2183        span: Span,
2184        path: &'hir hir::QPath<'hir>,
2185        fields: &'hir [hir::Expr<'hir>],
2186    ) -> hir::Expr<'hir> {
2187        let fields = self.arena.alloc_from_iter(fields.into_iter().enumerate().map(|(i, f)| {
2188            hir::ExprField {
2189                hir_id: self.next_id(),
2190                ident: Ident::from_str(&i.to_string()),
2191                expr: f,
2192                span: f.span,
2193                is_shorthand: false,
2194            }
2195        }));
2196        self.expr_struct(span, path, fields)
2197    }
2198
2199    pub(super) fn expr_enum_variant_lang_item(
2200        &mut self,
2201        span: Span,
2202        lang_item: hir::LangItem,
2203        fields: &'hir [hir::Expr<'hir>],
2204    ) -> hir::Expr<'hir> {
2205        let path = self.arena.alloc(self.make_lang_item_qpath(lang_item, span, None));
2206        self.expr_enum_variant(span, path, fields)
2207    }
2208
2209    pub(super) fn expr_call(
2210        &mut self,
2211        span: Span,
2212        e: &'hir hir::Expr<'hir>,
2213        args: &'hir [hir::Expr<'hir>],
2214    ) -> &'hir hir::Expr<'hir> {
2215        self.arena.alloc(self.expr_call_mut(span, e, args))
2216    }
2217
2218    pub(super) fn expr_call_lang_item_fn_mut(
2219        &mut self,
2220        span: Span,
2221        lang_item: hir::LangItem,
2222        args: &'hir [hir::Expr<'hir>],
2223    ) -> hir::Expr<'hir> {
2224        let path = self.arena.alloc(self.expr_lang_item_path(span, lang_item));
2225        self.expr_call_mut(span, path, args)
2226    }
2227
2228    pub(super) fn expr_call_lang_item_fn(
2229        &mut self,
2230        span: Span,
2231        lang_item: hir::LangItem,
2232        args: &'hir [hir::Expr<'hir>],
2233    ) -> &'hir hir::Expr<'hir> {
2234        self.arena.alloc(self.expr_call_lang_item_fn_mut(span, lang_item, args))
2235    }
2236
2237    pub(super) fn expr_lang_item_path(
2238        &mut self,
2239        span: Span,
2240        lang_item: hir::LangItem,
2241    ) -> hir::Expr<'hir> {
2242        let qpath = self.make_lang_item_qpath(lang_item, self.lower_span(span), None);
2243        self.expr(span, hir::ExprKind::Path(qpath))
2244    }
2245
2246    /// `<LangItem>::name`
2247    pub(super) fn expr_lang_item_type_relative(
2248        &mut self,
2249        span: Span,
2250        lang_item: hir::LangItem,
2251        name: Symbol,
2252    ) -> hir::Expr<'hir> {
2253        let qpath = self.make_lang_item_qpath(lang_item, self.lower_span(span), None);
2254        let path = hir::ExprKind::Path(hir::QPath::TypeRelative(
2255            self.arena.alloc(self.ty(span, hir::TyKind::Path(qpath))),
2256            self.arena.alloc(hir::PathSegment::new(
2257                Ident::new(name, self.lower_span(span)),
2258                self.next_id(),
2259                Res::Err,
2260            )),
2261        ));
2262        self.expr(span, path)
2263    }
2264
2265    pub(super) fn expr_ident(
2266        &mut self,
2267        sp: Span,
2268        ident: Ident,
2269        binding: HirId,
2270    ) -> &'hir hir::Expr<'hir> {
2271        self.arena.alloc(self.expr_ident_mut(sp, ident, binding))
2272    }
2273
2274    pub(super) fn expr_ident_mut(
2275        &mut self,
2276        span: Span,
2277        ident: Ident,
2278        binding: HirId,
2279    ) -> hir::Expr<'hir> {
2280        let hir_id = self.next_id();
2281        let res = Res::Local(binding);
2282        let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
2283            None,
2284            self.arena.alloc(hir::Path {
2285                span: self.lower_span(span),
2286                res,
2287                segments: arena_vec![self; hir::PathSegment::new(self.lower_ident(ident), hir_id, res)],
2288            }),
2289        ));
2290
2291        self.expr(span, expr_path)
2292    }
2293
2294    pub(super) fn expr_unsafe(
2295        &mut self,
2296        span: Span,
2297        expr: &'hir hir::Expr<'hir>,
2298    ) -> hir::Expr<'hir> {
2299        let hir_id = self.next_id();
2300        self.expr(
2301            span,
2302            hir::ExprKind::Block(
2303                self.arena.alloc(hir::Block {
2304                    stmts: &[],
2305                    expr: Some(expr),
2306                    hir_id,
2307                    rules: hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::CompilerGenerated),
2308                    span: self.lower_span(span),
2309                    targeted_by_break: false,
2310                }),
2311                None,
2312            ),
2313        )
2314    }
2315
2316    fn expr_block_empty(&mut self, span: Span) -> &'hir hir::Expr<'hir> {
2317        let blk = self.block_all(span, &[], None);
2318        let expr = self.expr_block(blk);
2319        self.arena.alloc(expr)
2320    }
2321
2322    pub(super) fn expr_block(&mut self, b: &'hir hir::Block<'hir>) -> hir::Expr<'hir> {
2323        self.expr(b.span, hir::ExprKind::Block(b, None))
2324    }
2325
2326    /// Wrap an expression in a block, and wrap that block in an expression again.
2327    /// Useful for constructing if-expressions, which require expressions of
2328    /// kind block.
2329    pub(super) fn block_expr_block(
2330        &mut self,
2331        expr: &'hir hir::Expr<'hir>,
2332    ) -> &'hir hir::Expr<'hir> {
2333        let b = self.block_expr(expr);
2334        self.arena.alloc(self.expr_block(b))
2335    }
2336
2337    pub(super) fn expr_ref(&mut self, span: Span, expr: &'hir hir::Expr<'hir>) -> hir::Expr<'hir> {
2338        self.expr(span, hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, expr))
2339    }
2340
2341    pub(super) fn expr_bool_literal(&mut self, span: Span, val: bool) -> hir::Expr<'hir> {
2342        self.expr(span, hir::ExprKind::Lit(Spanned { node: LitKind::Bool(val), span }))
2343    }
2344
2345    pub(super) fn expr(&mut self, span: Span, kind: hir::ExprKind<'hir>) -> hir::Expr<'hir> {
2346        let hir_id = self.next_id();
2347        hir::Expr { hir_id, kind, span: self.lower_span(span) }
2348    }
2349
2350    pub(super) fn expr_field(
2351        &mut self,
2352        ident: Ident,
2353        expr: &'hir hir::Expr<'hir>,
2354        span: Span,
2355    ) -> hir::ExprField<'hir> {
2356        hir::ExprField {
2357            hir_id: self.next_id(),
2358            ident,
2359            span: self.lower_span(span),
2360            expr,
2361            is_shorthand: false,
2362        }
2363    }
2364
2365    pub(super) fn arm(
2366        &mut self,
2367        pat: &'hir hir::Pat<'hir>,
2368        expr: &'hir hir::Expr<'hir>,
2369    ) -> hir::Arm<'hir> {
2370        hir::Arm {
2371            hir_id: self.next_id(),
2372            pat,
2373            guard: None,
2374            span: self.lower_span(expr.span),
2375            body: expr,
2376        }
2377    }
2378
2379    /// `#[allow(unreachable_code)]`
2380    pub(super) fn unreachable_code_attr(&mut self, span: Span) -> Attribute {
2381        let attr = attr::mk_attr_nested_word(
2382            &self.tcx.sess.psess.attr_id_generator,
2383            AttrStyle::Outer,
2384            Safety::Default,
2385            sym::allow,
2386            sym::unreachable_code,
2387            span,
2388        );
2389        attr
2390    }
2391}
2392
2393/// Used by [`LoweringContext::make_lowered_await`] to customize the desugaring based on what kind
2394/// of future we are awaiting.
2395#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2396enum FutureKind {
2397    /// We are awaiting a normal future
2398    Future,
2399    /// We are awaiting something that's known to be an AsyncIterator (i.e. we are in the header of
2400    /// a `for await` loop)
2401    AsyncIterator,
2402}