Skip to main content

rustc_ast_lowering/
expr.rs

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