rustc_ast_lowering/
expr.rs

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