rustc_ast_lowering/
expr.rs

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