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