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