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