rustc_ast_lowering/
expr.rs

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