Skip to main content

rustc_ast_lowering/
expr.rs

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