Skip to main content

rustc_ast_lowering/
expr.rs

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