rustc_ast_lowering/
expr.rs

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