rustc_ast_lowering/
expr.rs

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