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