rustc_ast_lowering/
expr.rs

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