Skip to main content

hir_def/expr_store/
lower.rs

1//! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
2//! representation.
3
4mod asm;
5mod format_args;
6mod generics;
7mod path;
8
9use std::{cell::OnceCell, mem};
10
11use base_db::{FxIndexSet, SourceDatabase};
12use cfg::CfgOptions;
13use either::Either;
14use hir_expand::{
15    HirFileId, InFile, MacroDefId,
16    mod_path::ModPath,
17    name::{AsName, Name},
18    span_map::SpanMap,
19};
20use intern::{Symbol, sym};
21use itertools::Itertools;
22use rustc_abi::ExternAbi;
23use rustc_hash::FxHashMap;
24use smallvec::SmallVec;
25use stdx::never;
26use syntax::{
27    AstNode, AstPtr, SyntaxNodePtr,
28    ast::{
29        self, ArrayExprKind, AstChildren, BlockExpr, ForBinder, HasArgList, HasGenericArgs,
30        HasGenericParams, HasLoopBody, HasName, HasTypeBounds, IsString, RangeItem,
31        SlicePatComponents,
32    },
33};
34use thin_vec::ThinVec;
35use tt::TextRange;
36
37use crate::{
38    AdtId, BlockId, BlockIdLt, ConstId, DefWithBodyId, FunctionId, GenericDefId, HasModule, ImplId,
39    ItemContainerId, LoweringMode, MacroId, ModuleDefId, ModuleId, TraitId, TypeAliasId,
40    UnresolvedMacro,
41    attrs::AttrFlags,
42    expr_store::{
43        Body, BodySourceMap, ExprPtr, ExprRoot, ExpressionStore, ExpressionStoreBuilder,
44        ExpressionStoreDiagnostics, ExpressionStoreSourceMap, HygieneId, LabelPtr, LifetimePtr,
45        MissingBodyItemKind, PatPtr, StoreVisitor, TypePtr,
46        body::Param,
47        expander::Expander,
48        lower::generics::ImplTraitLowerFn,
49        path::{AssociatedTypeBinding, GenericArg, GenericArgs, GenericArgsParentheses, Path},
50    },
51    hir::{
52        Array, Binding, BindingAnnotation, BindingId, BindingProblems, CaptureBy, ClosureKind,
53        CoroutineKind, CoroutineSource, Expr, ExprId, Item, Label, LabelId, Literal, LoopSource,
54        MatchArm, Movability, OffsetOf, Pat, PatId, RecordFieldPat, RecordLitField, RecordSpread,
55        Statement, Unsafe, generics::GenericParams,
56    },
57    item_scope::BuiltinShadowMode,
58    lang_item::{LangItemTarget, LangItems},
59    nameres::{DefMap, LocalDefMap, MacroSubNs, block_def_map},
60    signatures::TypeAliasSignature,
61    type_ref::{
62        ArrayType, ConstRef, FnType, LifetimeRef, LifetimeRefId, Mutability, PathId, Rawness,
63        RefType, TraitBoundModifier, TraitRef, TypeBound, TypeRef, TypeRefId, UseArgRef,
64    },
65    unstable_features::UnstableFeatures,
66};
67
68pub use self::path::hir_segment_to_ast_segment;
69
70pub(super) fn lower_body(
71    db: &dyn SourceDatabase,
72    owner: DefWithBodyId,
73    syntax_node: SyntaxNodePtr,
74    current_file_id: HirFileId,
75    module: ModuleId,
76    parameters: Option<ast::ParamList>,
77    body: Option<ast::Expr>,
78    is_async_fn: bool,
79    is_gen_fn: bool,
80) -> (Body, BodySourceMap) {
81    // We cannot leave the root span map empty and let any identifier from it be treated as root,
82    // because when inside nested macros `SyntaxContextId`s from the outer macro will be interleaved
83    // with the inner macro, and that will cause confusion because they won't be the same as `ROOT`
84    // even though they should be the same. Also, when the body comes from multiple expansions, their
85    // hygiene is different.
86
87    let mut self_param = None;
88    let mut source_map_self_param = None;
89    let mut params = vec![];
90    let mut collector = ExprCollector::new(db, module, current_file_id, LoweringMode::Analysis);
91
92    let skip_body = AttrFlags::query(
93        db,
94        match owner {
95            DefWithBodyId::FunctionId(it) => it.into(),
96            DefWithBodyId::StaticId(it) => it.into(),
97            DefWithBodyId::ConstId(it) => it.into(),
98            DefWithBodyId::VariantId(it) => it.into(),
99        },
100    )
101    .contains(AttrFlags::RUST_ANALYZER_SKIP);
102    // If #[rust_analyzer::skip] annotated, only construct enough information for the signature
103    // and skip the body.
104    if skip_body {
105        collector.with_expr_root(|collector| {
106            if let Some(param_list) = parameters {
107                if let Some(self_param_syn) =
108                    param_list.self_param().filter(|self_param| collector.check_cfg(self_param))
109                {
110                    let is_mutable = self_param_syn.mut_token().is_some()
111                        && self_param_syn.amp_token().is_none();
112                    let hygiene = self_param_syn
113                        .name()
114                        .map(|name| collector.hygiene_id_for(name.syntax().text_range()))
115                        .unwrap_or(HygieneId::ROOT);
116                    let binding_id: la_arena::Idx<Binding> = collector.alloc_binding(
117                        Name::new_symbol_root(sym::self_),
118                        BindingAnnotation::new(is_mutable, false),
119                        hygiene,
120                    );
121                    self_param = Some(Param::new(binding_id));
122                    source_map_self_param =
123                        Some(collector.expander.in_file(AstPtr::new(&self_param_syn)));
124                }
125                let count = param_list.params().filter(|it| collector.check_cfg(it)).count();
126                params = (0..count).map(|_| Param::new(collector.missing_pat())).collect();
127            };
128
129            collector.missing_expr()
130        });
131        let (store, source_map) = collector.store.finish();
132        return (
133            Body { store, params: params.into_boxed_slice(), self_param },
134            BodySourceMap { self_param: source_map_self_param, store: source_map },
135        );
136    }
137    validate_required_body(db, owner, current_file_id, syntax_node, body.as_ref(), &mut collector);
138    collector.with_expr_root(|collector| {
139        if let DefWithBodyId::FunctionId(func) = owner
140            && let Some(param_list) = parameters
141        {
142            if let Some(self_param_syn) =
143                param_list.self_param().filter(|it| collector.check_cfg(it))
144            {
145                let is_mutable =
146                    self_param_syn.mut_token().is_some() && self_param_syn.amp_token().is_none();
147                let hygiene = self_param_syn
148                    .name()
149                    .map(|name| collector.hygiene_id_for(name.syntax().text_range()))
150                    .unwrap_or(HygieneId::ROOT);
151                let binding_id: la_arena::Idx<Binding> = collector.alloc_binding(
152                    Name::new_symbol_root(sym::self_),
153                    BindingAnnotation::new(is_mutable, false),
154                    hygiene,
155                );
156                self_param = Some(Param::new(binding_id));
157                source_map_self_param =
158                    Some(collector.expander.in_file(AstPtr::new(&self_param_syn)));
159            }
160
161            let params_are_bare_idents = match func.loc(db).container {
162                ItemContainerId::ExternBlockId(_) => true,
163                ItemContainerId::TraitId(_) => body.is_none(),
164                ItemContainerId::ModuleId(_) | ItemContainerId::ImplId(_) => false,
165            };
166
167            for param in param_list.params() {
168                if collector.check_cfg(&param) {
169                    let param_pat = match param.pat() {
170                        Some(pat) => {
171                            if params_are_bare_idents {
172                                collector.collect_param_as_ident(pat)
173                            } else {
174                                collector.collect_pat_top(Some(pat))
175                            }
176                        }
177                        None => collector.missing_pat(),
178                    };
179                    params.push(Param::new(param_pat));
180                }
181            }
182        }
183
184        collector.collect(
185            &mut self_param,
186            &mut params,
187            body,
188            if is_async_fn {
189                Awaitable::Yes
190            } else {
191                match owner {
192                    DefWithBodyId::FunctionId(..) => Awaitable::No("non-async function"),
193                    DefWithBodyId::StaticId(..) => Awaitable::No("static"),
194                    DefWithBodyId::ConstId(..) => Awaitable::No("constant"),
195                    DefWithBodyId::VariantId(..) => Awaitable::No("enum variant"),
196                }
197            },
198            is_async_fn,
199            is_gen_fn,
200        )
201    });
202
203    let (store, source_map) = collector.store.finish();
204    (
205        Body { store, params: params.into_boxed_slice(), self_param },
206        BodySourceMap { self_param: source_map_self_param, store: source_map },
207    )
208}
209
210fn validate_required_body(
211    db: &(dyn SourceDatabase + 'static),
212    owner: DefWithBodyId,
213    current_file_id: HirFileId,
214    syntax_node: SyntaxNodePtr,
215    body: Option<&ast::Expr>,
216    collector: &mut ExprCollector<'_>,
217) {
218    if body.is_some() {
219        return;
220    }
221    let diagnostic_kind = match owner {
222        // FIXME: add diagnostic for missing body
223        // rustc says: if body.is_none() && !is_intrinsic && !self.is_sdylib_interface
224        DefWithBodyId::FunctionId(_function_id) => None,
225        DefWithBodyId::StaticId(id) => match id.loc(db).container {
226            ItemContainerId::ModuleId(_) => Some(MissingBodyItemKind::Static),
227            ItemContainerId::ExternBlockId(_)
228            | ItemContainerId::ImplId(_)
229            | ItemContainerId::TraitId(_) => None,
230        },
231        DefWithBodyId::ConstId(id) => match id.loc(db).container {
232            ItemContainerId::ModuleId(_) => Some(MissingBodyItemKind::Const),
233            ItemContainerId::ImplId(_) => Some(MissingBodyItemKind::AssocConst),
234            ItemContainerId::ExternBlockId(_) | ItemContainerId::TraitId(_) => None,
235        },
236        DefWithBodyId::VariantId(_) => None,
237    };
238    if let Some(kind) = diagnostic_kind {
239        let node = InFile::new(current_file_id, syntax_node);
240        collector.store.diagnostics.push(ExpressionStoreDiagnostics::MissingBody { node, kind });
241    }
242}
243
244pub(crate) fn lower_type_ref(
245    db: &dyn SourceDatabase,
246    module: ModuleId,
247    type_ref: InFile<Option<ast::Type>>,
248) -> (ExpressionStore, ExpressionStoreSourceMap, TypeRefId) {
249    let mut expr_collector =
250        ExprCollector::new(db, module, type_ref.file_id, LoweringMode::Analysis);
251    let type_ref =
252        expr_collector.lower_type_ref_opt(type_ref.value, &mut ExprCollector::impl_trait_allocator);
253    let (store, source_map) = expr_collector.store.finish();
254    (store, source_map, type_ref)
255}
256
257pub fn lower_generic_params(
258    db: &dyn SourceDatabase,
259    module: ModuleId,
260    def: GenericDefId,
261    file_id: HirFileId,
262    param_list: Option<ast::GenericParamList>,
263    where_clause: Option<ast::WhereClause>,
264    mode: LoweringMode,
265) -> (ExpressionStore, GenericParams, ExpressionStoreSourceMap) {
266    let mut expr_collector = ExprCollector::new(db, module, file_id, mode);
267    let mut collector = generics::GenericParamsCollector::new(def);
268    collector.lower(&mut expr_collector, param_list, where_clause);
269    let params = collector.finish();
270    let (store, source_map) = expr_collector.store.finish();
271    (store, params, source_map)
272}
273
274pub(crate) fn lower_impl(
275    db: &dyn SourceDatabase,
276    module: ModuleId,
277    impl_syntax: InFile<ast::Impl>,
278    impl_id: ImplId,
279) -> (ExpressionStore, ExpressionStoreSourceMap, TypeRefId, Option<TraitRef>, GenericParams) {
280    let mut expr_collector =
281        ExprCollector::new(db, module, impl_syntax.file_id, LoweringMode::Analysis);
282    let self_ty =
283        expr_collector.lower_type_ref_opt_disallow_impl_trait(impl_syntax.value.self_ty());
284    let trait_ = impl_syntax.value.trait_().and_then(|it| match &it {
285        ast::Type::PathType(path_type) => {
286            let path = expr_collector
287                .lower_path_type(path_type, &mut ExprCollector::impl_trait_allocator)?;
288            Some(TraitRef { path: expr_collector.alloc_path(path, AstPtr::new(&it)) })
289        }
290        _ => None,
291    });
292    let mut collector = generics::GenericParamsCollector::new(impl_id.into());
293    collector.lower(
294        &mut expr_collector,
295        impl_syntax.value.generic_param_list(),
296        impl_syntax.value.where_clause(),
297    );
298    let params = collector.finish();
299    let (store, source_map) = expr_collector.store.finish();
300    (store, source_map, self_ty, trait_, params)
301}
302
303pub(crate) fn lower_trait(
304    db: &dyn SourceDatabase,
305    module: ModuleId,
306    trait_syntax: InFile<ast::Trait>,
307    trait_id: TraitId,
308) -> (ExpressionStore, ExpressionStoreSourceMap, GenericParams) {
309    let mut expr_collector =
310        ExprCollector::new(db, module, trait_syntax.file_id, LoweringMode::Analysis);
311    let mut collector = generics::GenericParamsCollector::with_self_param(
312        &mut expr_collector,
313        trait_id.into(),
314        trait_syntax.value.type_bound_list(),
315    );
316    collector.lower(
317        &mut expr_collector,
318        trait_syntax.value.generic_param_list(),
319        trait_syntax.value.where_clause(),
320    );
321    let params = collector.finish();
322    let (store, source_map) = expr_collector.store.finish();
323    (store, source_map, params)
324}
325
326pub(crate) fn lower_type_alias(
327    db: &dyn SourceDatabase,
328    container: ItemContainerId,
329    alias: InFile<ast::TypeAlias>,
330    type_alias_id: TypeAliasId,
331) -> (ExpressionStore, ExpressionStoreSourceMap, GenericParams, Box<[TypeBound]>, Option<TypeRefId>)
332{
333    let mut expr_collector =
334        ExprCollector::new(db, container.module(db), alias.file_id, LoweringMode::Analysis);
335    let bounds = alias
336        .value
337        .type_bound_list()
338        .map(|bounds| {
339            bounds
340                .bounds()
341                .map(|bound| {
342                    expr_collector.lower_type_bound(bound, &mut ExprCollector::impl_trait_allocator)
343                })
344                .collect()
345        })
346        .unwrap_or_default();
347    let mut collector = generics::GenericParamsCollector::new(type_alias_id.into());
348    collector.lower(
349        &mut expr_collector,
350        alias.value.generic_param_list(),
351        alias.value.where_clause(),
352    );
353    let params = collector.finish();
354    let type_ref = alias
355        .value
356        .ty()
357        .map(|ty| expr_collector.lower_type_ref(ty, &mut ExprCollector::impl_trait_allocator));
358    if alias.value.ty().is_none() {
359        let diagnostic_kind = match container {
360            ItemContainerId::ModuleId(_) => Some(MissingBodyItemKind::TypeAlias),
361            ItemContainerId::ImplId(_) => Some(MissingBodyItemKind::AssocType),
362            ItemContainerId::ExternBlockId(_) => None,
363            ItemContainerId::TraitId(_) => None,
364        };
365        if let Some(kind) = diagnostic_kind {
366            let node = InFile::new(alias.file_id, SyntaxNodePtr::new(alias.value.syntax()));
367            expr_collector
368                .store
369                .diagnostics
370                .push(ExpressionStoreDiagnostics::MissingBody { node, kind });
371        }
372    };
373    let (store, source_map) = expr_collector.store.finish();
374    (store, source_map, params, bounds, type_ref)
375}
376
377pub(crate) fn lower_function(
378    db: &dyn SourceDatabase,
379    module: ModuleId,
380    fn_: InFile<ast::Fn>,
381    function_id: FunctionId,
382) -> (
383    ExpressionStore,
384    ExpressionStoreSourceMap,
385    GenericParams,
386    Box<[TypeRefId]>,
387    Option<TypeRefId>,
388    bool,
389    bool,
390) {
391    let mut expr_collector = ExprCollector::new(db, module, fn_.file_id, LoweringMode::Analysis);
392    let mut collector = generics::GenericParamsCollector::new(function_id.into());
393    collector.lower(&mut expr_collector, fn_.value.generic_param_list(), fn_.value.where_clause());
394    let mut params = vec![];
395    let mut has_self_param = false;
396    let mut has_variadic = false;
397    collector.collect_impl_trait(&mut expr_collector, |collector, mut impl_trait_lower_fn| {
398        collector.with_lifetime_bound_scope(LifetimeBoundScope::Argument, |collector| {
399            if let Some(param_list) = fn_.value.param_list() {
400                if let Some(param) = param_list.self_param() {
401                    let enabled = collector.check_cfg(&param);
402                    if enabled {
403                        has_self_param = true;
404                        params.push(match param.ty() {
405                            Some(ty) => collector.lower_type_ref(ty, &mut impl_trait_lower_fn),
406                            None => {
407                                let self_type = collector.alloc_type_ref_desugared(TypeRef::Path(
408                                    Name::new_symbol_root(sym::Self_).into(),
409                                ));
410                                let lifetime = param
411                                    .lifetime()
412                                    .map(|lifetime| collector.lower_lifetime_ref(lifetime));
413                                match param.kind() {
414                                    ast::SelfParamKind::Owned => self_type,
415                                    ast::SelfParamKind::Ref => collector.alloc_type_ref_desugared(
416                                        TypeRef::Reference(Box::new(RefType {
417                                            ty: self_type,
418                                            lifetime,
419                                            mutability: Mutability::Shared,
420                                        })),
421                                    ),
422                                    ast::SelfParamKind::MutRef => collector
423                                        .alloc_type_ref_desugared(TypeRef::Reference(Box::new(
424                                            RefType {
425                                                ty: self_type,
426                                                lifetime,
427                                                mutability: Mutability::Mut,
428                                            },
429                                        ))),
430                                }
431                            }
432                        });
433                    }
434                }
435                let p = param_list
436                    .params()
437                    .filter(|param| collector.check_cfg(param))
438                    .filter(|param| {
439                        let is_variadic = param.dotdotdot_token().is_some();
440                        has_variadic |= is_variadic;
441                        !is_variadic
442                    })
443                    .map(|param| param.ty())
444                    // FIXME
445                    .collect::<Vec<_>>();
446                for p in p {
447                    params.push(collector.lower_type_ref_opt(p, &mut impl_trait_lower_fn));
448                }
449            }
450        })
451    });
452    let return_type = fn_.value.ret_type().map(|ret_type| {
453        expr_collector.with_lifetime_bound_scope(LifetimeBoundScope::Return, |this| {
454            this.lower_type_ref_opt(ret_type.ty(), &mut ExprCollector::impl_trait_allocator)
455        })
456    });
457    collector.update_to_late_bound_lifetimes(&expr_collector.named_lifetime_store);
458    let generics = collector.finish();
459
460    let return_type = if fn_.value.async_token().is_some() || fn_.value.gen_token().is_some() {
461        let (path, assoc_name) =
462            match (fn_.value.async_token().is_some(), fn_.value.gen_token().is_some()) {
463                (true, true) => {
464                    (hir_expand::mod_path::path![core::async_iter::AsyncIterator], sym::Item)
465                }
466                (true, false) => (hir_expand::mod_path::path![core::future::Future], sym::Output),
467                (false, true) => (hir_expand::mod_path::path![core::iter::Iterator], sym::Item),
468                (false, false) => unreachable!(),
469            };
470        let mut generic_args: Vec<_> =
471            std::iter::repeat_n(None, path.segments().len() - 1).collect();
472        let binding = AssociatedTypeBinding {
473            name: Name::new_symbol_root(assoc_name),
474            args: None,
475            type_ref: Some(
476                return_type
477                    .unwrap_or_else(|| expr_collector.alloc_type_ref_desugared(TypeRef::unit())),
478            ),
479            bounds: Box::default(),
480        };
481        generic_args
482            .push(Some(GenericArgs { bindings: Box::new([binding]), ..GenericArgs::empty() }));
483
484        let path = Path::from_known_path(path, generic_args);
485        let path = PathId::from_type_ref_unchecked(
486            expr_collector.alloc_type_ref_desugared(TypeRef::Path(path)),
487        );
488        let ty_bound = TypeBound::Path(path, TraitBoundModifier::None);
489        Some(
490            expr_collector
491                .alloc_type_ref_desugared(TypeRef::ImplTrait(ThinVec::from_iter([ty_bound]))),
492        )
493    } else {
494        return_type
495    };
496    let (store, source_map) = expr_collector.store.finish();
497    (
498        store,
499        source_map,
500        generics,
501        params.into_boxed_slice(),
502        return_type,
503        has_self_param,
504        has_variadic,
505    )
506}
507
508pub struct ExprCollector<'db> {
509    db: &'db dyn SourceDatabase,
510    cfg_options: &'db CfgOptions,
511    expander: Expander<'db>,
512    def_map: &'db DefMap,
513    local_def_map: &'db LocalDefMap,
514    module: ModuleId,
515    lowering_mode: LoweringMode,
516    lang_items: OnceCell<&'db LangItems>,
517    pub store: ExpressionStoreBuilder,
518    pub named_lifetime_store: NamedLifetimeStore,
519
520    // state stuff
521    // Prevent nested impl traits like `impl Foo<impl Bar>`.
522    outer_impl_trait: bool,
523
524    is_lowering_coroutine: bool,
525
526    for_type_binder: Option<ThinVec<Name>>,
527
528    /// Legacy (`macro_rules!`) macros can have multiple definitions and shadow each other,
529    /// and we need to find the current definition. So we track the number of definitions we saw.
530    current_block_legacy_macro_defs_count: FxHashMap<Name, usize>,
531
532    current_try_block: Option<TryBlock>,
533
534    label_ribs: Vec<LabelRib>,
535    unowned_bindings: Vec<BindingId>,
536
537    awaitable_context: Option<Awaitable>,
538    krate: base_db::Crate,
539
540    name_generator_index: usize,
541}
542
543#[derive(Clone, Debug)]
544struct LabelRib {
545    kind: RibKind,
546}
547
548impl LabelRib {
549    fn new(kind: RibKind) -> Self {
550        LabelRib { kind }
551    }
552}
553
554#[derive(Clone, Debug, PartialEq, Eq)]
555enum RibKind {
556    Normal(Name, LabelId, HygieneId),
557    Closure,
558    Constant,
559    MacroDef(Box<MacroDefId>),
560}
561
562impl RibKind {
563    /// This rib forbids referring to labels defined in upwards ribs.
564    fn is_label_barrier(&self) -> bool {
565        match self {
566            RibKind::Normal(..) | RibKind::MacroDef(_) => false,
567            RibKind::Closure | RibKind::Constant => true,
568        }
569    }
570}
571
572#[derive(PartialEq, Eq, Debug, Copy, Clone)]
573enum Awaitable {
574    Yes,
575    No(&'static str),
576}
577
578enum TryBlock {
579    // `try { ... }`
580    Homogeneous { label: LabelId },
581    // `try bikeshed Ty { ... }`
582    Heterogeneous { label: LabelId },
583}
584
585#[derive(Debug, Default)]
586struct BindingList {
587    map: FxHashMap<(Name, HygieneId), BindingId>,
588    is_used: FxHashMap<BindingId, bool>,
589    reject_new: bool,
590}
591
592impl BindingList {
593    fn find(
594        &mut self,
595        ec: &mut ExprCollector<'_>,
596        name: Name,
597        hygiene: HygieneId,
598        mode: BindingAnnotation,
599    ) -> BindingId {
600        let id = *self
601            .map
602            .entry((name, hygiene))
603            .or_insert_with_key(|(name, hygiene)| ec.alloc_binding(name.clone(), mode, *hygiene));
604        if ec.store.bindings[id].mode != mode {
605            ec.store.bindings[id].problems = Some(BindingProblems::BoundInconsistently);
606        }
607        self.check_is_used(ec, id);
608        id
609    }
610
611    fn check_is_used(&mut self, ec: &mut ExprCollector<'_>, id: BindingId) {
612        match self.is_used.get(&id) {
613            None => {
614                if self.reject_new {
615                    ec.store.bindings[id].problems = Some(BindingProblems::NotBoundAcrossAll);
616                }
617            }
618            Some(true) => {
619                ec.store.bindings[id].problems = Some(BindingProblems::BoundMoreThanOnce);
620            }
621            Some(false) => {}
622        }
623        self.is_used.insert(id, true);
624    }
625}
626
627#[derive(Debug, Default)]
628pub struct NamedLifetimeStore {
629    lifetime_bound_scope: Option<LifetimeBoundScope>,
630    lifetimes_in_where_clause: FxIndexSet<Name>,
631    lifetimes_constrained_by_input: FxIndexSet<Name>,
632    lifetimes_in_output: FxIndexSet<Name>,
633}
634
635#[derive(Debug)]
636enum LifetimeBoundScope {
637    Argument,
638    Return,
639    WhereClause,
640    ImplTrait { is_argument_scope: bool },
641}
642
643impl NamedLifetimeStore {
644    /// Adds in the lifetime one of three lists fields if
645    /// `lifetime_bound_context` is `Some` and based on enum variant.
646    pub(crate) fn push_named_lifetime(&mut self, lifetime: Name) {
647        match self.lifetime_bound_scope {
648            Some(LifetimeBoundScope::Argument) => {
649                self.lifetimes_constrained_by_input.insert(lifetime);
650            }
651            Some(LifetimeBoundScope::Return) => {
652                self.lifetimes_in_output.insert(lifetime);
653            }
654            Some(LifetimeBoundScope::WhereClause) => {
655                self.lifetimes_in_where_clause.insert(lifetime);
656            }
657            Some(LifetimeBoundScope::ImplTrait { is_argument_scope }) => {
658                if is_argument_scope {
659                    self.lifetimes_in_where_clause.insert(lifetime);
660                } else {
661                    self.lifetimes_in_output.insert(lifetime);
662                }
663            }
664            None => (),
665        };
666    }
667}
668
669impl<'db> ExprCollector<'db> {
670    pub fn new(
671        db: &dyn SourceDatabase,
672        module: ModuleId,
673        current_file_id: HirFileId,
674        lowering_mode: LoweringMode,
675    ) -> ExprCollector<'_> {
676        let (def_map, local_def_map) = module.local_def_map(db);
677        let expander = Expander::new(db, current_file_id, def_map);
678        let krate = module.krate(db);
679        let mut result = ExprCollector {
680            db,
681            cfg_options: krate.cfg_options(db),
682            module,
683            lowering_mode,
684            def_map,
685            local_def_map,
686            lang_items: OnceCell::new(),
687            store: ExpressionStoreBuilder::default(),
688            expander,
689            current_try_block: None,
690            is_lowering_coroutine: false,
691            label_ribs: Vec::new(),
692            unowned_bindings: Vec::new(),
693            awaitable_context: None,
694            current_block_legacy_macro_defs_count: FxHashMap::default(),
695            outer_impl_trait: false,
696            krate,
697            name_generator_index: 0,
698            named_lifetime_store: NamedLifetimeStore::default(),
699            for_type_binder: None,
700        };
701        result.store.inference_roots = Some(SmallVec::new());
702        result
703    }
704
705    fn generate_new_name(&mut self) -> Name {
706        let index = self.name_generator_index;
707        self.name_generator_index += 1;
708        Name::generate_new_name(index)
709    }
710
711    #[inline]
712    pub(crate) fn lang_items(&self) -> &'db LangItems {
713        self.lang_items.get_or_init(|| crate::lang_item::lang_items(self.db, self.def_map.krate()))
714    }
715
716    #[inline]
717    pub(crate) fn span_map(&self) -> SpanMap<'_> {
718        self.expander.span_map()
719    }
720
721    pub(in crate::expr_store) fn lower_lifetime_ref(
722        &mut self,
723        lifetime: ast::Lifetime,
724    ) -> LifetimeRefId {
725        // FIXME: Keyword check?
726        let lifetime_ref = match lifetime.text() {
727            "" | "'" => LifetimeRef::Error,
728            "'static" => LifetimeRef::Static,
729            "'_" => LifetimeRef::Placeholder,
730            text => LifetimeRef::Named(Name::new_lifetime(text)),
731        };
732        self.alloc_lifetime_ref(lifetime_ref, AstPtr::new(&lifetime))
733    }
734
735    pub(in crate::expr_store) fn lower_lifetime_ref_opt(
736        &mut self,
737        lifetime: Option<ast::Lifetime>,
738    ) -> LifetimeRefId {
739        match lifetime {
740            Some(lifetime) => self.lower_lifetime_ref(lifetime),
741            None => self.alloc_lifetime_ref_desugared(LifetimeRef::Placeholder),
742        }
743    }
744
745    /// Converts an `ast::TypeRef` to a `hir::TypeRef`.
746    pub(in crate::expr_store) fn lower_type_ref(
747        &mut self,
748        node: ast::Type,
749        impl_trait_lower_fn: ImplTraitLowerFn<'_>,
750    ) -> TypeRefId {
751        let ty = match &node {
752            ast::Type::ParenType(inner) => {
753                return self.lower_type_ref_opt(inner.ty(), impl_trait_lower_fn);
754            }
755            ast::Type::TupleType(inner) => TypeRef::Tuple(ThinVec::from_iter(Vec::from_iter(
756                inner.fields().map(|it| self.lower_type_ref(it, impl_trait_lower_fn)),
757            ))),
758            ast::Type::NeverType(..) => TypeRef::Never,
759            ast::Type::PathType(inner) => inner
760                .path()
761                .and_then(|it| self.lower_path(it, impl_trait_lower_fn))
762                .map(TypeRef::Path)
763                .unwrap_or(TypeRef::Error),
764            ast::Type::PtrType(inner) => {
765                let inner_ty = self.lower_type_ref_opt(inner.ty(), impl_trait_lower_fn);
766                let mutability = Mutability::from_mutable(inner.mut_token().is_some());
767                TypeRef::RawPtr(inner_ty, mutability)
768            }
769            ast::Type::ArrayType(inner) => {
770                let len = self.lower_const_arg_opt(inner.const_arg());
771                TypeRef::Array(ArrayType {
772                    ty: self.lower_type_ref_opt(inner.ty(), impl_trait_lower_fn),
773                    len,
774                })
775            }
776            ast::Type::SliceType(inner) => {
777                TypeRef::Slice(self.lower_type_ref_opt(inner.ty(), impl_trait_lower_fn))
778            }
779            ast::Type::RefType(inner) => {
780                let inner_ty = self.lower_type_ref_opt(inner.ty(), impl_trait_lower_fn);
781                let lifetime = inner.lifetime().map(|lt| self.lower_lifetime_ref(lt));
782                let mutability = Mutability::from_mutable(inner.mut_token().is_some());
783                TypeRef::Reference(Box::new(RefType { ty: inner_ty, lifetime, mutability }))
784            }
785            ast::Type::InferType(_inner) => TypeRef::Placeholder,
786            ast::Type::FnPtrType(inner) => {
787                let ret_ty = inner
788                    .ret_type()
789                    .and_then(|rt| rt.ty())
790                    .map(|it| self.lower_type_ref(it, impl_trait_lower_fn))
791                    .unwrap_or_else(|| self.alloc_type_ref_desugared(TypeRef::unit()));
792                let mut is_varargs = false;
793                let mut params = if let Some(pl) = inner.param_list() {
794                    if let Some(param) = pl.params().last() {
795                        is_varargs = param.dotdotdot_token().is_some();
796                    }
797
798                    pl.params()
799                        .filter(|it| it.dotdotdot_token().is_none())
800                        .map(|it| {
801                            let type_ref = self.lower_type_ref_opt(it.ty(), impl_trait_lower_fn);
802                            let name = match it.pat() {
803                                Some(ast::Pat::IdentPat(it)) => Some(
804                                    it.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing),
805                                ),
806                                _ => None,
807                            };
808                            (name, type_ref)
809                        })
810                        .collect()
811                } else {
812                    Vec::with_capacity(1)
813                };
814                fn lower_abi(abi: ast::Abi) -> ExternAbi {
815                    abi.abi_string()
816                        .and_then(|abi| abi.text_without_quotes().parse().ok())
817                        .unwrap_or(ExternAbi::FALLBACK)
818                }
819
820                let abi = inner.abi().map(lower_abi).unwrap_or(ExternAbi::Rust);
821                params.push((None, ret_ty));
822
823                let binder = self.for_type_binder.take().map(|b| b.into());
824                TypeRef::Fn(Box::new(FnType {
825                    is_varargs,
826                    is_unsafe: inner.unsafe_token().is_some(),
827                    abi,
828                    params: params.into_boxed_slice(),
829                    binder,
830                }))
831            }
832            // for types are close enough for our purposes to the inner type for now...
833            ast::Type::ForType(inner) => {
834                let binder = self.lower_for_binder_opt(inner.for_binder());
835                let old_for_binder = self.for_type_binder.replace(binder);
836                let ty = self.lower_type_ref_opt(inner.ty(), impl_trait_lower_fn);
837                self.for_type_binder = old_for_binder;
838                return ty;
839            }
840            ast::Type::ImplTraitType(inner) => {
841                if self.outer_impl_trait {
842                    // Disallow nested impl traits
843                    TypeRef::Error
844                } else {
845                    return self.with_outer_impl_trait_scope(true, |this| {
846                        let is_argument_scope = this.is_argument_lt_bound_scope();
847                        let type_bounds = this.with_lifetime_bound_scope(
848                            LifetimeBoundScope::ImplTrait { is_argument_scope },
849                            |this| {
850                                this.type_bounds_from_ast(
851                                    inner.type_bound_list(),
852                                    impl_trait_lower_fn,
853                                )
854                            },
855                        );
856                        impl_trait_lower_fn(this, AstPtr::new(&node), type_bounds)
857                    });
858                }
859            }
860            ast::Type::DynTraitType(inner) => TypeRef::DynTrait(
861                self.type_bounds_from_ast(inner.type_bound_list(), impl_trait_lower_fn),
862            ),
863            ast::Type::PatternType(inner) => TypeRef::PatternType(
864                self.lower_type_ref_opt(inner.ty(), impl_trait_lower_fn),
865                self.collect_ty_pat_opt(inner.pat()),
866            ),
867            ast::Type::MacroType(mt) => match mt.macro_call() {
868                Some(mcall) => {
869                    let macro_ptr = AstPtr::new(&mcall);
870                    let src = self.expander.in_file(AstPtr::new(&node));
871                    let id = self.collect_macro_call(mcall, macro_ptr, true, |this, expansion| {
872                        this.lower_type_ref_opt(expansion, impl_trait_lower_fn)
873                    });
874                    self.store.types_map.insert(src, id);
875                    return id;
876                }
877                None => TypeRef::Error,
878            },
879        };
880        self.alloc_type_ref(ty, AstPtr::new(&node))
881    }
882
883    pub(crate) fn lower_type_ref_disallow_impl_trait(&mut self, node: ast::Type) -> TypeRefId {
884        self.lower_type_ref(node, &mut Self::impl_trait_error_allocator)
885    }
886
887    pub(crate) fn lower_type_ref_opt(
888        &mut self,
889        node: Option<ast::Type>,
890        impl_trait_lower_fn: ImplTraitLowerFn<'_>,
891    ) -> TypeRefId {
892        match node {
893            Some(node) => self.lower_type_ref(node, impl_trait_lower_fn),
894            None => self.alloc_error_type(),
895        }
896    }
897
898    pub(crate) fn lower_type_ref_opt_disallow_impl_trait(
899        &mut self,
900        node: Option<ast::Type>,
901    ) -> TypeRefId {
902        self.lower_type_ref_opt(node, &mut Self::impl_trait_error_allocator)
903    }
904
905    fn alloc_type_ref(&mut self, type_ref: TypeRef, node: TypePtr) -> TypeRefId {
906        let id = self.store.types.alloc(type_ref);
907        let ptr = self.expander.in_file(node);
908        self.store.types_map_back.insert(id, ptr);
909        self.store.types_map.insert(ptr, id);
910        id
911    }
912
913    fn alloc_lifetime_ref(
914        &mut self,
915        lifetime_ref: LifetimeRef,
916        node: LifetimePtr,
917    ) -> LifetimeRefId {
918        if let LifetimeRef::Named(name) = &lifetime_ref {
919            self.named_lifetime_store.push_named_lifetime(name.clone());
920        }
921
922        let id = self.store.lifetimes.alloc(lifetime_ref);
923        let ptr = self.expander.in_file(node);
924        self.store.lifetime_map_back.insert(id, ptr);
925        self.store.lifetime_map.insert(ptr, id);
926        id
927    }
928
929    fn alloc_type_ref_desugared(&mut self, type_ref: TypeRef) -> TypeRefId {
930        self.store.types.alloc(type_ref)
931    }
932
933    fn alloc_lifetime_ref_desugared(&mut self, lifetime_ref: LifetimeRef) -> LifetimeRefId {
934        self.store.lifetimes.alloc(lifetime_ref)
935    }
936
937    fn alloc_error_type(&mut self) -> TypeRefId {
938        self.store.types.alloc(TypeRef::Error)
939    }
940
941    pub fn lower_path(
942        &mut self,
943        ast: ast::Path,
944        impl_trait_lower_fn: ImplTraitLowerFn<'_>,
945    ) -> Option<Path> {
946        super::lower::path::lower_path(self, ast, impl_trait_lower_fn)
947    }
948
949    fn with_outer_impl_trait_scope<R>(
950        &mut self,
951        impl_trait: bool,
952        f: impl FnOnce(&mut Self) -> R,
953    ) -> R {
954        let old = mem::replace(&mut self.outer_impl_trait, impl_trait);
955        let result = f(self);
956        self.outer_impl_trait = old;
957        result
958    }
959
960    pub fn impl_trait_error_allocator(
961        ec: &mut ExprCollector<'_>,
962        ptr: TypePtr,
963        _: ThinVec<TypeBound>,
964    ) -> TypeRefId {
965        ec.alloc_type_ref(TypeRef::Error, ptr)
966    }
967
968    fn impl_trait_allocator(
969        ec: &mut ExprCollector<'_>,
970        ptr: TypePtr,
971        bounds: ThinVec<TypeBound>,
972    ) -> TypeRefId {
973        ec.alloc_type_ref(TypeRef::ImplTrait(bounds), ptr)
974    }
975
976    fn alloc_path(&mut self, path: Path, node: TypePtr) -> PathId {
977        PathId::from_type_ref_unchecked(self.alloc_type_ref(TypeRef::Path(path), node))
978    }
979
980    /// Collect `GenericArgs` from the parts of a fn-like path, i.e. `Fn(X, Y)
981    /// -> Z` (which desugars to `Fn<(X, Y), Output=Z>`).
982    pub(in crate::expr_store) fn lower_generic_args_from_fn_path(
983        &mut self,
984        args: Option<ast::ParenthesizedArgList>,
985        ret_type: Option<ast::RetType>,
986        impl_trait_lower_fn: ImplTraitLowerFn<'_>,
987    ) -> Option<GenericArgs> {
988        let params = args?;
989        let mut param_types = Vec::new();
990        for param in params.type_args() {
991            let type_ref = self.lower_type_ref_opt(param.ty(), impl_trait_lower_fn);
992            param_types.push(type_ref);
993        }
994        let args = Box::new([GenericArg::Type(
995            self.alloc_type_ref_desugared(TypeRef::Tuple(ThinVec::from_iter(param_types))),
996        )]);
997        let bindings = if let Some(ret_type) = ret_type {
998            let type_ref = self.lower_type_ref_opt(ret_type.ty(), impl_trait_lower_fn);
999            Box::new([AssociatedTypeBinding {
1000                name: Name::new_symbol_root(sym::Output),
1001                args: None,
1002                type_ref: Some(type_ref),
1003                bounds: Box::default(),
1004            }])
1005        } else {
1006            // -> ()
1007            let type_ref = self.alloc_type_ref_desugared(TypeRef::unit());
1008            Box::new([AssociatedTypeBinding {
1009                name: Name::new_symbol_root(sym::Output),
1010                args: None,
1011                type_ref: Some(type_ref),
1012                bounds: Box::default(),
1013            }])
1014        };
1015        Some(GenericArgs {
1016            args,
1017            has_self_type: false,
1018            bindings,
1019            parenthesized: GenericArgsParentheses::ParenSugar,
1020        })
1021    }
1022
1023    pub(super) fn lower_generic_args(
1024        &mut self,
1025        node: ast::GenericArgList,
1026        impl_trait_lower_fn: ImplTraitLowerFn<'_>,
1027    ) -> Option<GenericArgs> {
1028        // This needs to be kept in sync with `hir_generic_arg_to_ast()`.
1029        let mut args = Vec::new();
1030        let mut bindings = Vec::new();
1031        for generic_arg in node.generic_args() {
1032            match generic_arg {
1033                ast::GenericArg::TypeArg(type_arg) => {
1034                    let type_ref = self.lower_type_ref_opt(type_arg.ty(), impl_trait_lower_fn);
1035                    args.push(GenericArg::Type(type_ref));
1036                }
1037                ast::GenericArg::AssocTypeArg(assoc_type_arg) => {
1038                    // This needs to be kept in sync with `hir_assoc_type_binding_to_ast()`.
1039                    if assoc_type_arg.param_list().is_some() {
1040                        // We currently ignore associated return type bounds.
1041                        continue;
1042                    }
1043                    if let Some(name_ref) = assoc_type_arg.name_ref() {
1044                        // Nested impl traits like `impl Foo<Assoc = impl Bar>` are allowed
1045                        self.with_outer_impl_trait_scope(false, |this| {
1046                            let name = name_ref.as_name();
1047                            let args = assoc_type_arg
1048                                .generic_arg_list()
1049                                .and_then(|args| this.lower_generic_args(args, impl_trait_lower_fn))
1050                                .or_else(|| {
1051                                    assoc_type_arg
1052                                        .return_type_syntax()
1053                                        .map(|_| GenericArgs::return_type_notation())
1054                                });
1055                            let type_ref = assoc_type_arg
1056                                .ty()
1057                                .map(|it| this.lower_type_ref(it, impl_trait_lower_fn));
1058                            let bounds = if let Some(l) = assoc_type_arg.type_bound_list() {
1059                                l.bounds()
1060                                    .map(|it| this.lower_type_bound(it, impl_trait_lower_fn))
1061                                    .collect()
1062                            } else {
1063                                Box::default()
1064                            };
1065                            bindings.push(AssociatedTypeBinding { name, args, type_ref, bounds });
1066                        });
1067                    }
1068                }
1069                ast::GenericArg::LifetimeArg(lifetime_arg) => {
1070                    if let Some(lifetime) = lifetime_arg.lifetime() {
1071                        let lifetime_ref = self.lower_lifetime_ref(lifetime);
1072                        args.push(GenericArg::Lifetime(lifetime_ref))
1073                    }
1074                }
1075                ast::GenericArg::ConstArg(arg) => {
1076                    let arg = self.lower_const_arg(arg);
1077                    args.push(GenericArg::Const(arg))
1078                }
1079            }
1080        }
1081
1082        if args.is_empty() && bindings.is_empty() {
1083            return None;
1084        }
1085        Some(GenericArgs {
1086            args: args.into_boxed_slice(),
1087            has_self_type: false,
1088            bindings: bindings.into_boxed_slice(),
1089            parenthesized: GenericArgsParentheses::No,
1090        })
1091    }
1092
1093    /// Lowers a desugared coroutine body after moving all of the arguments
1094    /// into the body. This is to make sure that the future actually owns the
1095    /// arguments that are passed to the function, and to ensure things like
1096    /// drop order are stable.
1097    fn lower_coroutine_body_with_moved_arguments(
1098        &mut self,
1099        self_param: &mut Option<Param<BindingId>>,
1100        params: &mut [Param<PatId>],
1101        body: ExprId,
1102        kind: CoroutineKind,
1103        coroutine_source: CoroutineSource,
1104    ) -> ExprId {
1105        // Async function parameters are lowered into the closure body so that they are
1106        // captured and so that the drop order matches the equivalent non-async functions.
1107        //
1108        // from:
1109        //
1110        //     async fn foo(<pattern>: <ty>, <pattern>: <ty>, <pattern>: <ty>) {
1111        //         <body>
1112        //     }
1113        //
1114        // into:
1115        //
1116        //     fn foo(__arg0: <ty>, __arg1: <ty>, __arg2: <ty>) {
1117        //       async move {
1118        //         let __arg2 = __arg2;
1119        //         let <pattern> = __arg2;
1120        //         let __arg1 = __arg1;
1121        //         let <pattern> = __arg1;
1122        //         let __arg0 = __arg0;
1123        //         let <pattern> = __arg0;
1124        //         drop-temps { <body> } // see comments later in fn for details
1125        //       }
1126        //     }
1127        //
1128        // If `<pattern>` is a simple ident, then it is lowered to a single
1129        // `let <pattern> = <pattern>;` statement as an optimization.
1130
1131        let mut statements =
1132            Vec::with_capacity(usize::from(self_param.is_some()) + (params.len() * 2));
1133
1134        if let Some(self_param) = self_param {
1135            let Binding { ref name, mode, hygiene, .. } = self.store.bindings[self_param.formal];
1136            let name = name.clone();
1137            let child_binding_id = self.alloc_binding(name.clone(), mode, hygiene);
1138            let child_pat_id =
1139                self.alloc_pat_desugared(Pat::Bind { id: child_binding_id, subpat: None });
1140            self.add_definition_to_binding(child_binding_id, child_pat_id);
1141            let expr = self.alloc_expr_desugared(Expr::Path(name.into()));
1142            if !hygiene.is_root() {
1143                self.store.ident_hygiene.insert(expr.into(), hygiene);
1144            }
1145            statements.push(Statement::Let {
1146                pat: child_pat_id,
1147                type_ref: None,
1148                initializer: Some(expr),
1149                else_branch: None,
1150            });
1151            self_param.user_written = child_binding_id;
1152        }
1153
1154        for param in params {
1155            let (name, hygiene, is_simple_parameter) = match self.store.pats[param.formal] {
1156                // Check if this is a binding pattern, if so, we can optimize and avoid adding a
1157                // `let <pat> = __argN;` statement. In this case, we do not rename the parameter.
1158                Pat::Bind { id, subpat: None, .. }
1159                    if matches!(
1160                        self.store.bindings[id].mode,
1161                        BindingAnnotation::Unannotated | BindingAnnotation::Mutable
1162                    ) =>
1163                {
1164                    let binding = &self.store.bindings[id];
1165                    (binding.name.clone(), binding.hygiene, true)
1166                }
1167                Pat::Bind { id, .. } => {
1168                    // If this is a `ref` binding, we can't leave it as is but we can at least reuse the name, for better display.
1169                    let binding = &self.store.bindings[id];
1170                    (binding.name.clone(), binding.hygiene, false)
1171                }
1172                _ => (self.generate_new_name(), HygieneId::ROOT, false),
1173            };
1174            let pat_syntax = self.store.pat_map_back.get(param.formal).copied();
1175            if !is_simple_parameter {
1176                // It needs to be mutable so the inner patterns can borrow it mutably (`ref mut`).
1177                let child_binding_id =
1178                    self.alloc_binding(name.clone(), BindingAnnotation::Mutable, hygiene);
1179                let child_pat_id =
1180                    self.alloc_pat_desugared(Pat::Bind { id: child_binding_id, subpat: None });
1181                self.add_definition_to_binding(child_binding_id, child_pat_id);
1182                if let Some(pat_syntax) = pat_syntax {
1183                    self.store.pat_map_back.insert(child_pat_id, pat_syntax);
1184                }
1185                let expr = self.alloc_expr_desugared(Expr::Path(name.clone().into()));
1186                if !hygiene.is_root() {
1187                    self.store.ident_hygiene.insert(expr.into(), hygiene);
1188                }
1189                statements.push(Statement::Let {
1190                    pat: child_pat_id,
1191                    type_ref: None,
1192                    initializer: Some(expr),
1193                    else_branch: None,
1194                });
1195            }
1196            let expr = self.alloc_expr_desugared(Expr::Path(name.clone().into()));
1197            if !hygiene.is_root() {
1198                self.store.ident_hygiene.insert(expr.into(), hygiene);
1199            }
1200            statements.push(Statement::Let {
1201                pat: param.formal,
1202                type_ref: None,
1203                initializer: Some(expr),
1204                else_branch: None,
1205            });
1206
1207            let parent_binding_id = self.alloc_binding(name, BindingAnnotation::Mutable, hygiene);
1208            let parent_pat_id =
1209                self.alloc_pat_desugared(Pat::Bind { id: parent_binding_id, subpat: None });
1210            self.add_definition_to_binding(parent_binding_id, parent_pat_id);
1211            if let Some(pat_syntax) = pat_syntax {
1212                self.store.pat_map_back.insert(parent_pat_id, pat_syntax);
1213            }
1214            *param = Param { formal: parent_pat_id, user_written: param.formal };
1215        }
1216
1217        let coroutine = self.desugared_coroutine_expr(
1218            kind,
1219            coroutine_source,
1220            // The default capture mode here is by-ref. Later on during upvar analysis,
1221            // we will force the captured arguments to by-move, but for async closures,
1222            // we want to make sure that we avoid unnecessarily moving captures, or else
1223            // all async closures would default to `FnOnce` as their calling mode.
1224            CaptureBy::Ref,
1225            None,
1226            statements.into_boxed_slice(),
1227            Some(body),
1228        );
1229        // It's important that this comes last, see the lowering of async closures for why.
1230        self.alloc_expr_desugared(coroutine)
1231    }
1232
1233    fn desugared_coroutine_expr(
1234        &mut self,
1235        kind: CoroutineKind,
1236        source: CoroutineSource,
1237        capture_by: CaptureBy,
1238        id: Option<BlockId>,
1239        statements: Box<[Statement]>,
1240        tail: Option<ExprId>,
1241    ) -> Expr {
1242        let block = self.alloc_expr_desugared(Expr::Block {
1243            label: None,
1244            id,
1245            statements,
1246            tail,
1247            unsafe_: Unsafe::No,
1248        });
1249        Expr::Closure {
1250            args: Box::default(),
1251            arg_types: Box::default(),
1252            ret_type: None,
1253            body: block,
1254            closure_kind: ClosureKind::Coroutine { kind, source },
1255            capture_by,
1256        }
1257    }
1258
1259    fn collect(
1260        &mut self,
1261        self_param: &mut Option<Param<BindingId>>,
1262        params: &mut [Param<PatId>],
1263        expr: Option<ast::Expr>,
1264        awaitable: Awaitable,
1265        is_async_fn: bool,
1266        is_gen_fn: bool,
1267    ) -> ExprId {
1268        self.awaitable_context.replace(awaitable);
1269        self.with_label_rib(RibKind::Closure, |this| {
1270            let body = this.collect_expr_opt(expr);
1271            if is_async_fn || is_gen_fn {
1272                let kind = match (is_async_fn, is_gen_fn) {
1273                    (true, true) => CoroutineKind::AsyncGen,
1274                    (true, false) => CoroutineKind::Async,
1275                    (false, true) => CoroutineKind::Gen,
1276                    (false, false) => unreachable!(),
1277                };
1278                let coroutine = this.lower_coroutine_body_with_moved_arguments(
1279                    self_param,
1280                    params,
1281                    body,
1282                    kind,
1283                    CoroutineSource::Fn,
1284                );
1285                // *All* locals belong to the inner coroutine...
1286                this.associate_unowned_bindings_with(0, coroutine);
1287                // ...except the formal parameters, which are *not* necessarily what was passed as parameters into `collect()`,
1288                // since `lower_coroutine_body_with_moved_arguments()` might have changed them.
1289                params
1290                    .iter()
1291                    .filter_map(|param| {
1292                        if let Pat::Bind { id, .. } = this.store.pats[param.formal] {
1293                            Some(id)
1294                        } else {
1295                            never!(
1296                                "`lower_coroutine_body_with_moved_arguments()` should make sure \
1297                                the coroutine closure only have simple bind args"
1298                            );
1299                            None
1300                        }
1301                    })
1302                    .chain(self_param.map(|param| param.formal))
1303                    .for_each(|param| {
1304                        // They are owned by the top-level function, so should not be present in `bindings_owner`.
1305                        this.store.binding_owners.remove(&param);
1306                    });
1307                coroutine
1308            } else {
1309                body
1310            }
1311        })
1312    }
1313
1314    fn type_bounds_from_ast(
1315        &mut self,
1316        type_bounds_opt: Option<ast::TypeBoundList>,
1317        impl_trait_lower_fn: ImplTraitLowerFn<'_>,
1318    ) -> ThinVec<TypeBound> {
1319        if let Some(type_bounds) = type_bounds_opt {
1320            ThinVec::from_iter(Vec::from_iter(
1321                type_bounds.bounds().map(|it| self.lower_type_bound(it, impl_trait_lower_fn)),
1322            ))
1323        } else {
1324            ThinVec::from_iter([])
1325        }
1326    }
1327
1328    fn lower_path_type(
1329        &mut self,
1330        path_type: &ast::PathType,
1331        impl_trait_lower_fn: ImplTraitLowerFn<'_>,
1332    ) -> Option<Path> {
1333        let path = self.lower_path(path_type.path()?, impl_trait_lower_fn)?;
1334        Some(path)
1335    }
1336
1337    fn lower_type_bound(
1338        &mut self,
1339        node: ast::TypeBound,
1340        impl_trait_lower_fn: ImplTraitLowerFn<'_>,
1341    ) -> TypeBound {
1342        let Some(kind) = node.kind() else { return TypeBound::Error };
1343        match kind {
1344            ast::TypeBoundKind::PathType(binder, path_type) => {
1345                let binder = self.lower_for_binder_opt(binder);
1346                let m = match node.question_mark_token() {
1347                    Some(_) => TraitBoundModifier::Maybe,
1348                    None => TraitBoundModifier::None,
1349                };
1350                self.lower_path_type(&path_type, impl_trait_lower_fn)
1351                    .map(|p| {
1352                        let path = self.alloc_path(p, AstPtr::new(&path_type).upcast());
1353                        if binder.is_empty() {
1354                            TypeBound::Path(path, m)
1355                        } else {
1356                            TypeBound::ForLifetime(binder, path)
1357                        }
1358                    })
1359                    .unwrap_or(TypeBound::Error)
1360            }
1361            ast::TypeBoundKind::Use(gal) => TypeBound::Use(
1362                gal.use_bound_generic_args()
1363                    .map(|p| match p {
1364                        ast::UseBoundGenericArg::Lifetime(l) => {
1365                            UseArgRef::Lifetime(self.lower_lifetime_ref(l))
1366                        }
1367                        ast::UseBoundGenericArg::NameRef(n) => UseArgRef::Name(n.as_name()),
1368                    })
1369                    .collect(),
1370            ),
1371            ast::TypeBoundKind::Lifetime(lifetime) => {
1372                TypeBound::Lifetime(self.lower_lifetime_ref(lifetime))
1373            }
1374        }
1375    }
1376
1377    fn lower_for_binder_opt(&mut self, binder: Option<ForBinder>) -> ThinVec<Name> {
1378        binder.map(|b| self.lower_for_binder(b)).unwrap_or_default()
1379    }
1380
1381    fn lower_for_binder(&mut self, binder: ForBinder) -> ThinVec<Name> {
1382        match binder.generic_param_list() {
1383            Some(gpl) => gpl
1384                .lifetime_params()
1385                .flat_map(|lp| lp.lifetime().map(|lt| Name::new_lifetime(lt.text())))
1386                .collect(),
1387            None => ThinVec::default(),
1388        }
1389    }
1390
1391    fn lower_const_arg_opt(&mut self, arg: Option<ast::ConstArg>) -> ConstRef {
1392        ConstRef {
1393            expr: self.with_fresh_binding_expr_root(|this| {
1394                this.collect_expr_opt(arg.and_then(|arg| arg.expr()))
1395            }),
1396        }
1397    }
1398
1399    pub fn lower_const_arg(&mut self, arg: ast::ConstArg) -> ConstRef {
1400        ConstRef {
1401            expr: self.with_fresh_binding_expr_root(|this| this.collect_expr_opt(arg.expr())),
1402        }
1403    }
1404
1405    fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
1406        self.maybe_collect_expr(expr).unwrap_or_else(|| self.missing_expr())
1407    }
1408
1409    pub(in crate::expr_store) fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId {
1410        match expr {
1411            Some(expr) => self.collect_expr(expr),
1412            None => self.missing_expr(),
1413        }
1414    }
1415
1416    /// Returns `None` if and only if the expression is `#[cfg]`d out.
1417    fn maybe_collect_expr(&mut self, expr: ast::Expr) -> Option<ExprId> {
1418        if !self.check_cfg(&expr) {
1419            return None;
1420        }
1421        let syntax_ptr = AstPtr::new(&expr);
1422
1423        // FIXME: Move some of these arms out into separate methods for clarity
1424        Some(match expr {
1425            ast::Expr::IfExpr(e) => {
1426                let then_branch = self.collect_block_opt(e.then_branch());
1427
1428                let else_branch = e.else_branch().map(|b| match b {
1429                    ast::ElseBranch::Block(it) => self.collect_block(it),
1430                    ast::ElseBranch::IfExpr(elif) => {
1431                        let expr: ast::Expr = ast::Expr::cast(elif.syntax().clone()).unwrap();
1432                        self.collect_expr(expr)
1433                    }
1434                });
1435
1436                let condition = self.collect_expr_opt(e.condition());
1437
1438                self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr)
1439            }
1440            ast::Expr::LetExpr(e) => {
1441                let pat = self.collect_pat_top(e.pat());
1442                let expr = self.collect_expr_opt(e.expr());
1443                self.alloc_expr(Expr::Let { pat, expr }, syntax_ptr)
1444            }
1445            ast::Expr::BlockExpr(e) => match e.modifier() {
1446                Some(ast::BlockModifier::Try { try_token: _, bikeshed_token: _, result_type }) => {
1447                    self.desugar_try_block(e, result_type)
1448                }
1449                Some(ast::BlockModifier::Unsafe(_)) => {
1450                    self.collect_block_(e, |_, id, statements, tail| Expr::Block {
1451                        id,
1452                        statements,
1453                        tail,
1454                        label: None,
1455                        unsafe_: Unsafe::Yes,
1456                    })
1457                }
1458                Some(ast::BlockModifier::Label(label)) => {
1459                    let label_hygiene = self.hygiene_id_for(label.syntax().text_range());
1460                    let label_id = self.collect_label(label);
1461                    self.with_labeled_rib(label_id, label_hygiene, |this| {
1462                        this.collect_block_(e, |_, id, statements, tail| Expr::Block {
1463                            id,
1464                            statements,
1465                            tail,
1466                            label: Some(label_id),
1467                            unsafe_: Unsafe::No,
1468                        })
1469                    })
1470                }
1471                Some(ast::BlockModifier::Async(_)) => {
1472                    let capture_by =
1473                        if e.move_token().is_some() { CaptureBy::Value } else { CaptureBy::Ref };
1474                    self.with_label_rib(RibKind::Closure, |this| {
1475                        this.with_binding_owner(|this| {
1476                            this.with_awaitable_block(Awaitable::Yes, |this| {
1477                                this.collect_block_(e, |this, id, statements, tail| {
1478                                    this.desugared_coroutine_expr(
1479                                        CoroutineKind::Async,
1480                                        CoroutineSource::Block,
1481                                        capture_by,
1482                                        id,
1483                                        statements,
1484                                        tail,
1485                                    )
1486                                })
1487                            })
1488                        })
1489                    })
1490                }
1491                Some(ast::BlockModifier::Gen(_)) => {
1492                    let capture_by =
1493                        if e.move_token().is_some() { CaptureBy::Value } else { CaptureBy::Ref };
1494                    self.with_label_rib(RibKind::Closure, |this| {
1495                        this.with_binding_owner(|this| {
1496                            this.with_awaitable_block(Awaitable::No("non-async gen block"), |this| {
1497                                this.collect_block_(e, |this, id, statements, tail| {
1498                                    this.desugared_coroutine_expr(
1499                                        CoroutineKind::Gen,
1500                                        CoroutineSource::Block,
1501                                        capture_by,
1502                                        id,
1503                                        statements,
1504                                        tail,
1505                                    )
1506                                })
1507                            })
1508                        })
1509                    })
1510                }
1511                Some(ast::BlockModifier::AsyncGen(_)) => {
1512                    let capture_by =
1513                        if e.move_token().is_some() { CaptureBy::Value } else { CaptureBy::Ref };
1514                    self.with_label_rib(RibKind::Closure, |this| {
1515                        this.with_binding_owner(|this| {
1516                            this.with_awaitable_block(Awaitable::Yes, |this| {
1517                                this.collect_block_(e, |this, id, statements, tail| {
1518                                    this.desugared_coroutine_expr(
1519                                        CoroutineKind::AsyncGen,
1520                                        CoroutineSource::Block,
1521                                        capture_by,
1522                                        id,
1523                                        statements,
1524                                        tail,
1525                                    )
1526                                })
1527                            })
1528                        })
1529                    })
1530                }
1531                Some(ast::BlockModifier::Const(_)) => {
1532                    self.with_label_rib(RibKind::Constant, |this| {
1533                        this.with_awaitable_block(Awaitable::No("constant block"), |this| {
1534                            this.with_binding_owner(|this| {
1535                                let inner_expr = this.collect_block(e);
1536                                this.alloc_expr(Expr::Const(inner_expr), syntax_ptr)
1537                            })
1538                        })
1539                    })
1540                }
1541                None => self.collect_block(e),
1542            },
1543            ast::Expr::LoopExpr(e) => {
1544                let label = e.label().map(|label| {
1545                    (self.hygiene_id_for(label.syntax().text_range()), self.collect_label(label))
1546                });
1547                let body = self.collect_labelled_block_opt(label, e.loop_body());
1548                self.alloc_expr(Expr::Loop { body, label: label.map(|it| it.1), source: LoopSource::Loop }, syntax_ptr)
1549            }
1550            ast::Expr::WhileExpr(e) => self.collect_while_loop(syntax_ptr, e),
1551            ast::Expr::ForExpr(e) => self.collect_for_loop(syntax_ptr, e),
1552            ast::Expr::CallExpr(e) => {
1553                let callee = self.collect_expr_opt(e.expr());
1554                let args = if let Some(arg_list) = e.arg_list() {
1555                    arg_list.args().filter_map(|e| self.maybe_collect_expr(e)).collect()
1556                } else {
1557                    Box::default()
1558                };
1559                self.alloc_expr(Expr::Call { callee, args }, syntax_ptr)
1560            }
1561            ast::Expr::MethodCallExpr(e) => {
1562                let receiver = self.collect_expr_opt(e.receiver());
1563                let args = if let Some(arg_list) = e.arg_list() {
1564                    arg_list.args().filter_map(|e| self.maybe_collect_expr(e)).collect()
1565                } else {
1566                    Box::default()
1567                };
1568                let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
1569                let generic_args = e
1570                    .generic_arg_list()
1571                    .and_then(|it| {
1572                        self.lower_generic_args(it, &mut Self::impl_trait_error_allocator)
1573                    })
1574                    .map(Box::new);
1575                self.alloc_expr(
1576                    Expr::MethodCall { receiver, method_name, args, generic_args },
1577                    syntax_ptr,
1578                )
1579            }
1580            ast::Expr::MatchExpr(e) => {
1581                let expr = self.collect_expr_opt(e.expr());
1582                let arms = if let Some(match_arm_list) = e.match_arm_list() {
1583                    match_arm_list
1584                        .arms()
1585                        .filter_map(|arm| {
1586                            if self.check_cfg(&arm) {
1587                                Some(MatchArm {
1588                                    pat: self.collect_pat_top(arm.pat()),
1589                                    expr: self.collect_expr_opt(arm.expr()),
1590                                    guard: arm
1591                                        .guard()
1592                                        .map(|guard| self.collect_expr_opt(guard.condition())),
1593                                })
1594                            } else {
1595                                None
1596                            }
1597                        })
1598                        .collect()
1599                } else {
1600                    Box::default()
1601                };
1602                self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
1603            }
1604            ast::Expr::PathExpr(e) => {
1605                let (path, hygiene) = self
1606                    .collect_expr_path(e)
1607                    .map(|(path, hygiene)| (Expr::Path(path), hygiene))
1608                    .unwrap_or((Expr::Missing, HygieneId::ROOT));
1609                let expr_id = self.alloc_expr(path, syntax_ptr);
1610                if !hygiene.is_root() {
1611                    self.store.ident_hygiene.insert(expr_id.into(), hygiene);
1612                }
1613                expr_id
1614            }
1615            ast::Expr::ContinueExpr(e) => {
1616                let label = self.resolve_label(e.lifetime()).unwrap_or_else(|e| {
1617                    self.store.diagnostics.push(e);
1618                    None
1619                });
1620                self.alloc_expr(Expr::Continue { label }, syntax_ptr)
1621            }
1622            ast::Expr::BreakExpr(e) => {
1623                let label = self.resolve_label(e.lifetime()).unwrap_or_else(|e| {
1624                    self.store.diagnostics.push(e);
1625                    None
1626                });
1627                let expr = e.expr().map(|e| self.collect_expr(e));
1628                self.alloc_expr(Expr::Break { expr, label }, syntax_ptr)
1629            }
1630            ast::Expr::ParenExpr(e) => {
1631                let inner = self.collect_expr_opt(e.expr());
1632                // make the paren expr point to the inner expression as well for IDE resolution
1633                let src = self.expander.in_file(syntax_ptr);
1634                self.store.expr_map.insert(src, inner.into());
1635                inner
1636            }
1637            ast::Expr::ReturnExpr(e) => {
1638                let expr = e.expr().map(|e| self.collect_expr(e));
1639                self.alloc_expr(Expr::Return { expr }, syntax_ptr)
1640            }
1641            ast::Expr::BecomeExpr(e) => {
1642                let expr =
1643                    e.expr().map(|e| self.collect_expr(e)).unwrap_or_else(|| self.missing_expr());
1644                self.alloc_expr(Expr::Become { expr }, syntax_ptr)
1645            }
1646            ast::Expr::YieldExpr(e) => {
1647                self.is_lowering_coroutine = true;
1648                let expr = e.expr().map(|e| self.collect_expr(e));
1649                self.alloc_expr(Expr::Yield { expr }, syntax_ptr)
1650            }
1651            ast::Expr::YeetExpr(e) => {
1652                let expr = e.expr().map(|e| self.collect_expr(e));
1653                self.alloc_expr(Expr::Yeet { expr }, syntax_ptr)
1654            }
1655            ast::Expr::RecordExpr(e) => {
1656                let path = e
1657                    .path()
1658                    .and_then(|path| self.lower_path(path, &mut Self::impl_trait_error_allocator));
1659                let Some(path) = path else {
1660                    return Some(self.missing_expr());
1661                };
1662                let record_lit = if let Some(nfl) = e.record_expr_field_list() {
1663                    let fields = nfl
1664                        .fields()
1665                        .filter_map(|field| {
1666                            if !self.check_cfg(&field) {
1667                                return None;
1668                            }
1669
1670                            let name = field.field_name()?.as_name();
1671
1672                            let expr = match field.expr() {
1673                                Some(e) => self.collect_expr(e),
1674                                None => self.missing_expr(),
1675                            };
1676                            let src = self.expander.in_file(AstPtr::new(&field));
1677                            self.store.field_map_back.insert(expr, src);
1678                            Some(RecordLitField { name, expr })
1679                        })
1680                        .collect();
1681                    let spread_expr = nfl.spread().map(|s| self.collect_expr(s));
1682                    let has_spread_syntax = nfl.dotdot_token().is_some();
1683                    let spread = match (spread_expr, has_spread_syntax) {
1684                        (None, false) => RecordSpread::None,
1685                        (None, true) => RecordSpread::FieldDefaults,
1686                        (Some(expr), _) => RecordSpread::Expr(expr),
1687                    };
1688                    Expr::RecordLit { path, fields, spread }
1689                } else {
1690                    Expr::RecordLit { path, fields: ThinVec::default(), spread: RecordSpread::None }
1691                };
1692
1693                self.alloc_expr(record_lit, syntax_ptr)
1694            }
1695            ast::Expr::FieldExpr(e) => {
1696                let expr = self.collect_expr_opt(e.expr());
1697                let name = match e.field_access() {
1698                    Some(kind) => kind.as_name(),
1699                    _ => Name::missing(),
1700                };
1701                self.alloc_expr(Expr::Field { expr, name }, syntax_ptr)
1702            }
1703            ast::Expr::AwaitExpr(e) => {
1704                let expr = self.collect_expr_opt(e.expr());
1705                if let Awaitable::No(location) = self.is_lowering_awaitable_block() {
1706                    self.store.diagnostics.push(ExpressionStoreDiagnostics::AwaitOutsideOfAsync {
1707                        node: self.expander.in_file(AstPtr::new(&e)),
1708                        location: location.to_string(),
1709                    });
1710                }
1711                self.alloc_expr(Expr::Await { expr }, syntax_ptr)
1712            }
1713            ast::Expr::TryExpr(e) => self.collect_try_operator(syntax_ptr, e),
1714            ast::Expr::CastExpr(e) => {
1715                let expr = self.collect_expr_opt(e.expr());
1716                let type_ref = self.lower_type_ref_opt_disallow_impl_trait(e.ty());
1717                self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
1718            }
1719            ast::Expr::RefExpr(e) => {
1720                let expr = self.collect_expr_opt(e.expr());
1721                let raw_tok = e.raw_token().is_some();
1722                let mutability = if raw_tok {
1723                    if e.mut_token().is_some() { Mutability::Mut } else { Mutability::Shared }
1724                } else {
1725                    Mutability::from_mutable(e.mut_token().is_some())
1726                };
1727                let rawness = Rawness::from_raw(raw_tok);
1728                self.alloc_expr(Expr::Ref { expr, rawness, mutability }, syntax_ptr)
1729            }
1730            ast::Expr::PrefixExpr(e) => {
1731                let expr = self.collect_expr_opt(e.expr());
1732                match e.op_kind() {
1733                    Some(op) => self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr),
1734                    None => self.alloc_expr(Expr::Missing, syntax_ptr),
1735                }
1736            }
1737            ast::Expr::ClosureExpr(e) => self.with_label_rib(RibKind::Closure, |this| {
1738                let mut is_coroutine_closure = false;
1739                let closure = this.with_binding_owner_and_return(|this| {
1740                    let mut args = Vec::new();
1741                    let mut arg_types = Vec::new();
1742                    // For coroutine closures, the body, aka. the coroutine is the bindings owner, and not the closure.
1743                    if let Some(pl) = e.param_list() {
1744                        let num_params = pl.params().count();
1745                        args.reserve_exact(num_params);
1746                        arg_types.reserve_exact(num_params);
1747                        for param in pl.params() {
1748                            if !this.check_cfg(&param) {
1749                                continue;
1750                            }
1751
1752                            let pat = this.collect_pat_top(param.pat());
1753                            let type_ref =
1754                                param.ty().map(|it| this.lower_type_ref_disallow_impl_trait(it));
1755                            args.push(pat);
1756                            arg_types.push(type_ref);
1757                        }
1758                    }
1759                    let ret_type = e
1760                        .ret_type()
1761                        .and_then(|r| r.ty())
1762                        .map(|it| this.lower_type_ref_disallow_impl_trait(it));
1763
1764                    let prev_is_lowering_coroutine = mem::take(&mut this.is_lowering_coroutine);
1765                    let prev_try_block = this.current_try_block.take();
1766
1767                    let awaitable = if e.async_token().is_some() {
1768                        Awaitable::Yes
1769                    } else {
1770                        Awaitable::No("non-async closure")
1771                    };
1772                    let mut body = this
1773                        .with_awaitable_block(awaitable, |this| this.collect_expr_opt(e.body()));
1774                    let kind = {
1775                        if e.async_token().is_some() && e.gen_token().is_some() {
1776                            Some(CoroutineKind::AsyncGen)
1777                        } else if e.async_token().is_some() {
1778                            Some(CoroutineKind::Async)
1779                        } else if e.gen_token().is_some() {
1780                            Some(CoroutineKind::Gen)
1781                        } else {
1782                            None
1783                        }
1784                    };
1785
1786                    let closure_kind = if let Some(kind) = kind {
1787                        // It's important that this expr is allocated immediately before the closure.
1788                        // We rely on it for `coroutine_for_closure()`.
1789                        let mut args_with_user_written = args.iter().copied().map(Param::new).collect::<Vec<_>>();
1790                        body = this.lower_coroutine_body_with_moved_arguments(
1791                            &mut None,
1792                            &mut args_with_user_written,
1793                            body,
1794                            kind,
1795                            CoroutineSource::Closure,
1796                        );
1797                        args.iter_mut().set_from(args_with_user_written.iter().map(|arg| arg.formal));
1798                        is_coroutine_closure = true;
1799
1800                        ClosureKind::CoroutineClosure(kind)
1801                    } else if this.is_lowering_coroutine {
1802                        let movability = if e.static_token().is_some() {
1803                            Movability::Static
1804                        } else {
1805                            Movability::Movable
1806                        };
1807                        ClosureKind::OldCoroutine(movability)
1808                    } else {
1809                        ClosureKind::Closure
1810                    };
1811                    let capture_by =
1812                        if e.move_token().is_some() { CaptureBy::Value } else { CaptureBy::Ref };
1813                    this.is_lowering_coroutine = prev_is_lowering_coroutine;
1814                    this.current_try_block = prev_try_block;
1815                    let closure = this.alloc_expr(
1816                        Expr::Closure {
1817                            args: args.into(),
1818                            arg_types: arg_types.into(),
1819                            ret_type,
1820                            body,
1821                            closure_kind,
1822                            capture_by,
1823                        },
1824                        syntax_ptr,
1825                    );
1826
1827                    (if is_coroutine_closure { body } else { closure }, closure)
1828                });
1829
1830                if is_coroutine_closure {
1831                    let Expr::Closure { args, .. } = &this.store.exprs[closure] else {
1832                        unreachable!()
1833                    };
1834                    for &arg in args {
1835                        let Pat::Bind { id, .. } = this.store.pats[arg] else {
1836                            never!("`lower_coroutine_body_with_moved_arguments()` should make sure the coroutine closure only have simple bind args");
1837                            continue;
1838                        };
1839                        this.store.binding_owners.insert(id, closure);
1840                    }
1841                }
1842
1843                closure
1844            }),
1845            ast::Expr::BinExpr(e) => {
1846                let op = e.op_kind();
1847                if let Some(ast::BinaryOp::Assignment { op: None }) = op {
1848                    let target = self.collect_expr_as_pat_opt(e.lhs());
1849                    let value = self.collect_expr_opt(e.rhs());
1850                    self.alloc_expr(Expr::Assignment { target, value }, syntax_ptr)
1851                } else {
1852                    let lhs = self.collect_expr_opt(e.lhs());
1853                    let rhs = self.collect_expr_opt(e.rhs());
1854                    self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr)
1855                }
1856            }
1857            ast::Expr::TupleExpr(e) => {
1858                let mut exprs: Vec<_> = e.fields().filter_map(|expr| self.maybe_collect_expr(expr)).collect();
1859                // if there is a leading comma, the user is most likely to type out a leading expression
1860                // so we insert a missing expression at the beginning for IDE features
1861                if comma_follows_token(e.l_paren_token()) {
1862                    exprs.insert(0, self.missing_expr());
1863                }
1864
1865                self.alloc_expr(Expr::Tuple { exprs: exprs.into_boxed_slice() }, syntax_ptr)
1866            }
1867            ast::Expr::ArrayExpr(e) => {
1868                let kind = e.kind();
1869
1870                match kind {
1871                    ArrayExprKind::ElementList(e) => {
1872                        let elements = e
1873                            .filter_map(|expr| self.maybe_collect_expr(expr))
1874                            .collect();
1875                        self.alloc_expr(Expr::Array(Array::ElementList { elements }), syntax_ptr)
1876                    }
1877                    ArrayExprKind::Repeat { initializer, repeat } => {
1878                        let initializer = self.collect_expr_opt(initializer);
1879                        let repeat = self.with_label_rib(RibKind::Constant, |this| {
1880                            if let Some(repeat) = repeat {
1881                                this.with_binding_owner(|this| this.collect_expr(repeat))
1882                            } else {
1883                                this.missing_expr()
1884                            }
1885                        });
1886                        self.alloc_expr(
1887                            Expr::Array(Array::Repeat { initializer, repeat }),
1888                            syntax_ptr,
1889                        )
1890                    }
1891                }
1892            }
1893
1894            ast::Expr::Literal(e) => self.alloc_expr(Expr::Literal(e.kind().into()), syntax_ptr),
1895            ast::Expr::IndexExpr(e) => {
1896                let base = self.collect_expr_opt(e.base());
1897                let index = self.collect_expr_opt(e.index());
1898                self.alloc_expr(Expr::Index { base, index }, syntax_ptr)
1899            }
1900            ast::Expr::RangeExpr(e) => self.collect_range_expr(e, syntax_ptr),
1901            ast::Expr::MacroExpr(e) => {
1902                let e = e.macro_call()?;
1903                let macro_ptr = AstPtr::new(&e);
1904                let id = self.collect_macro_call(e, macro_ptr, true, |this, expansion| {
1905                    expansion.map(|it| this.maybe_collect_expr(it))
1906                });
1907                match id {
1908                    Some(Some(id)) => {
1909                        // Make the macro-call point to its expanded expression so we can query
1910                        // semantics on syntax pointers to the macro
1911                        let src = self.expander.in_file(syntax_ptr);
1912                        self.store.expr_map.insert(src, id.into());
1913                        id
1914                    }
1915                    // Macro expanded into a disabled cfg (yes, there is such thing, see the weird_cfgs test).
1916                    Some(None) => return None,
1917                    None => self.alloc_expr(Expr::Missing, syntax_ptr),
1918                }
1919            }
1920            ast::Expr::UnderscoreExpr(_) => self.alloc_expr(Expr::Underscore, syntax_ptr),
1921            ast::Expr::AsmExpr(e) => self.lower_inline_asm(e, syntax_ptr),
1922            ast::Expr::OffsetOfExpr(e) => {
1923                let container = self.lower_type_ref_opt_disallow_impl_trait(e.ty());
1924                let fields = e.fields().map(|it| it.as_name()).collect();
1925                self.alloc_expr(Expr::OffsetOf(OffsetOf { container, fields }), syntax_ptr)
1926            }
1927            ast::Expr::FormatArgsExpr(f) => self.collect_format_args(f, syntax_ptr),
1928            ast::Expr::IncludeBytesExpr(_) => self.alloc_expr(Expr::IncludeBytes, syntax_ptr),
1929            ast::Expr::CfgPredExpr(e) => {
1930                let enabled = Expr::Literal(Literal::Bool(self.check_cfg_pred(e.cfg_predicate())));
1931                self.alloc_expr(enabled, syntax_ptr)
1932            },
1933        })
1934    }
1935
1936    fn collect_range_expr(&mut self, e: ast::RangeExpr, syntax_ptr: AstPtr<ast::Expr>) -> ExprId {
1937        let lhs = e.start().map(|lhs| self.collect_expr(lhs));
1938        let rhs = e.end().map(|rhs| self.collect_expr(rhs));
1939        let kind = e.op_kind().unwrap_or(ast::RangeOp::Exclusive);
1940        let new_range = self.features().new_range;
1941        let lang_items = self.lang_items();
1942        let lang_item = match (lhs, rhs, kind) {
1943            (None, None, _) => lang_items.RangeFull,
1944            (Some(..), None, ast::RangeOp::Exclusive) => {
1945                if new_range {
1946                    lang_items.RangeFromCopy
1947                } else {
1948                    lang_items.RangeFrom
1949                }
1950            }
1951            (None, Some(..), ast::RangeOp::Exclusive) => lang_items.RangeTo,
1952            (Some(..), Some(..), ast::RangeOp::Exclusive) => {
1953                if new_range {
1954                    lang_items.RangeCopy
1955                } else {
1956                    lang_items.Range
1957                }
1958            }
1959            (None, Some(..), ast::RangeOp::Inclusive) => {
1960                if new_range {
1961                    lang_items.RangeToInclusiveCopy
1962                } else {
1963                    lang_items.RangeToInclusive
1964                }
1965            }
1966            (Some(lhs), Some(rhs), ast::RangeOp::Inclusive) => {
1967                if new_range {
1968                    lang_items.RangeInclusiveCopy
1969                } else {
1970                    return self.collect_inclusive_range(syntax_ptr, lang_items, lhs, rhs);
1971                }
1972            }
1973            (Some(..), None, ast::RangeOp::Inclusive) => {
1974                if new_range {
1975                    lang_items.RangeFromCopy
1976                } else {
1977                    lang_items.RangeFrom
1978                }
1979            }
1980        };
1981        let Some(struct_path) = self.lang_path(lang_item) else {
1982            return self.alloc_expr(Expr::Missing, syntax_ptr);
1983        };
1984        let lhs = lhs.map(|lhs| (lhs, sym::start));
1985        let rhs = rhs.map(|rhs| {
1986            (
1987                rhs,
1988                if lang_item == lang_items.RangeInclusiveCopy
1989                    || lang_item == lang_items.RangeToInclusiveCopy
1990                {
1991                    sym::last
1992                } else {
1993                    sym::end
1994                },
1995            )
1996        });
1997        let fields = std::iter::chain(lhs, rhs)
1998            .map(|(expr, name)| RecordLitField { name: Name::new_symbol_root(name), expr })
1999            .collect();
2000        self.alloc_expr(
2001            Expr::RecordLit { path: struct_path, fields, spread: RecordSpread::None },
2002            syntax_ptr,
2003        )
2004    }
2005
2006    fn collect_inclusive_range(
2007        &mut self,
2008        syntax_ptr: AstPtr<ast::Expr>,
2009        lang_items: &LangItems,
2010        lhs: ExprId,
2011        rhs: ExprId,
2012    ) -> ExprId {
2013        let fn_path = self.alloc_expr_desugared(self.lang_path_expr(lang_items.RangeInclusiveNew));
2014        self.alloc_expr(Expr::Call { callee: fn_path, args: Box::new([lhs, rhs]) }, syntax_ptr)
2015    }
2016
2017    fn collect_expr_path(&mut self, e: ast::PathExpr) -> Option<(Path, HygieneId)> {
2018        e.path().and_then(|path| {
2019            let path = self.lower_path(path, &mut Self::impl_trait_error_allocator)?;
2020            // Need to enable `mod_path.len() < 1` for `self`.
2021            let may_be_variable = matches!(&path, Path::BarePath(mod_path) if mod_path.len() <= 1);
2022            let hygiene = if may_be_variable {
2023                self.hygiene_id_for(e.syntax().text_range())
2024            } else {
2025                HygieneId::ROOT
2026            };
2027            Some((path, hygiene))
2028        })
2029    }
2030
2031    /// Whether this path should be lowered as destructuring assignment, or as a normal assignment.
2032    fn path_is_destructuring_assignment(&self, path: &ModPath) -> bool {
2033        // rustc has access to a full resolver here, including local variables and generic params, and it checks the following
2034        // criteria: a path not lowered as destructuring assignment if it can *fully resolve* to something that is *not*
2035        // a const, a unit struct or a variant.
2036        // We don't have access to a full resolver here. So we should do the same as rustc, but assuming that local variables
2037        // could be resolved to nothing (fortunately, there cannot be a local variable shadowing a unit struct/variant/const,
2038        // as that is an error). We don't need to consider const params as it's an error to refer to these in patterns.
2039        let (resolution, unresolved_idx, _) = self.def_map.resolve_path_locally(
2040            self.local_def_map,
2041            self.db,
2042            self.module,
2043            path,
2044            BuiltinShadowMode::Other,
2045        );
2046        match unresolved_idx {
2047            Some(_) => {
2048                // If `Some(_)`, path could be resolved to unit struct/variant/const with type information, i.e. an assoc type or const.
2049                // If `None`, path could be a local variable.
2050                resolution.take_types().is_some()
2051            }
2052            None => match resolution.take_values() {
2053                // We don't need to consider non-unit structs/variants, as those are not value types.
2054                Some(ModuleDefId::EnumVariantId(_))
2055                | Some(ModuleDefId::AdtId(_))
2056                | Some(ModuleDefId::ConstId(_)) => true,
2057                _ => false,
2058            },
2059        }
2060    }
2061
2062    fn collect_expr_as_pat_opt(&mut self, expr: Option<ast::Expr>) -> PatId {
2063        match expr {
2064            Some(expr) => self.collect_expr_as_pat(expr),
2065            _ => self.missing_pat(),
2066        }
2067    }
2068
2069    fn collect_expr_as_pat(&mut self, expr: ast::Expr) -> PatId {
2070        self.maybe_collect_expr_as_pat(&expr).unwrap_or_else(|| {
2071            let src = self.expander.in_file(AstPtr::new(&expr).wrap_left());
2072            let expr = self.collect_expr(expr);
2073            // Do not use `alloc_pat_from_expr()` here, it will override the entry in `expr_map`.
2074            let id = self.store.pats.alloc(Pat::Expr(expr));
2075            self.store.pat_map_back.insert(id, src);
2076            id
2077        })
2078    }
2079
2080    fn maybe_collect_expr_as_pat(&mut self, expr: &ast::Expr) -> Option<PatId> {
2081        if !self.check_cfg(expr) {
2082            return None;
2083        }
2084        let syntax_ptr = AstPtr::new(expr);
2085
2086        let result = match expr {
2087            ast::Expr::UnderscoreExpr(_) => self.alloc_pat_from_expr(Pat::Wild, syntax_ptr),
2088            ast::Expr::ParenExpr(e) => {
2089                // We special-case `(..)` for consistency with patterns.
2090                if let Some(ast::Expr::RangeExpr(range)) = e.expr()
2091                    && range.is_range_full()
2092                {
2093                    return Some(self.alloc_pat_from_expr(
2094                        Pat::Tuple { args: Box::default(), ellipsis: Some(0) },
2095                        syntax_ptr,
2096                    ));
2097                }
2098                return e.expr().and_then(|expr| self.maybe_collect_expr_as_pat(&expr));
2099            }
2100            ast::Expr::TupleExpr(e) => {
2101                let (ellipsis, args) = collect_tuple(self, e.fields());
2102                self.alloc_pat_from_expr(Pat::Tuple { args, ellipsis }, syntax_ptr)
2103            }
2104            ast::Expr::ArrayExpr(e) => {
2105                if e.semicolon_token().is_some() {
2106                    return None;
2107                }
2108
2109                let mut elements = e.exprs();
2110                let mut rest_ptr = None;
2111                let prefix = elements
2112                    .by_ref()
2113                    .map_while(|elem| match collect_possibly_rest(self, elem.clone()) {
2114                        Either::Left(elem) => Some(elem),
2115                        Either::Right(()) => {
2116                            rest_ptr = Some(AstPtr::new(&elem));
2117                            None
2118                        }
2119                    })
2120                    .collect();
2121                let suffix = elements.map(|elem| self.collect_expr_as_pat(elem)).collect();
2122                let slice = rest_ptr.map(|ptr| self.alloc_pat_from_expr(Pat::Rest, ptr));
2123                self.alloc_pat_from_expr(Pat::Slice { prefix, slice, suffix }, syntax_ptr)
2124            }
2125            ast::Expr::CallExpr(e) => {
2126                let path = collect_path(self, e.expr()?)?;
2127                let path = path
2128                    .path()
2129                    .and_then(|path| self.lower_path(path, &mut Self::impl_trait_error_allocator));
2130                let Some(path) = path else {
2131                    return Some(self.missing_pat());
2132                };
2133                let (ellipsis, args) = collect_tuple(self, e.arg_list()?.args());
2134                self.alloc_pat_from_expr(Pat::TupleStruct { path, args, ellipsis }, syntax_ptr)
2135            }
2136            ast::Expr::PathExpr(e) => {
2137                let (path, hygiene) = self.collect_expr_path(e.clone())?;
2138                let mod_path = path.mod_path().expect("should not lower to lang path");
2139                if self.path_is_destructuring_assignment(mod_path) {
2140                    let pat_id = self.alloc_pat_from_expr(Pat::Path(path), syntax_ptr);
2141                    if !hygiene.is_root() {
2142                        self.store.ident_hygiene.insert(pat_id.into(), hygiene);
2143                    }
2144                    pat_id
2145                } else {
2146                    return None;
2147                }
2148            }
2149            ast::Expr::MacroExpr(e) => {
2150                let e = e.macro_call()?;
2151                let macro_ptr = AstPtr::new(&e);
2152                let src = self.expander.in_file(AstPtr::new(expr));
2153                let id = self.collect_macro_call(e, macro_ptr, true, |this, expansion| {
2154                    this.collect_expr_as_pat_opt(expansion)
2155                });
2156                self.store.expr_map.insert(src, id.into());
2157                id
2158            }
2159            ast::Expr::RecordExpr(e) => {
2160                let path = e
2161                    .path()
2162                    .and_then(|path| self.lower_path(path, &mut Self::impl_trait_error_allocator));
2163                let Some(path) = path else {
2164                    return Some(self.missing_pat());
2165                };
2166                let record_field_list = e.record_expr_field_list()?;
2167                let ellipsis = record_field_list.dotdot_token().is_some();
2168                if let Some(spread) = record_field_list.spread() {
2169                    self.store.diagnostics.push(
2170                        ExpressionStoreDiagnostics::FruInDestructuringAssignment {
2171                            node: self.expander.in_file(AstPtr::new(&spread)),
2172                        },
2173                    );
2174                }
2175                let args = record_field_list
2176                    .fields()
2177                    .filter_map(|f| {
2178                        if !self.check_cfg(&f) {
2179                            return None;
2180                        }
2181                        let field_expr = f.expr()?;
2182                        let pat = self.collect_expr_as_pat(field_expr);
2183                        let name = f.field_name()?.as_name();
2184                        let src = self.expander.in_file(AstPtr::new(&f).wrap_left());
2185                        self.store.pat_field_map_back.insert(pat, src);
2186                        Some(RecordFieldPat { name, pat })
2187                    })
2188                    .collect();
2189                self.alloc_pat_from_expr(Pat::Record { path, args, ellipsis }, syntax_ptr)
2190            }
2191            _ => return None,
2192        };
2193        return Some(result);
2194
2195        fn collect_path(this: &mut ExprCollector<'_>, expr: ast::Expr) -> Option<ast::PathExpr> {
2196            match expr {
2197                ast::Expr::PathExpr(e) => Some(e),
2198                ast::Expr::MacroExpr(mac) => {
2199                    let call = mac.macro_call()?;
2200                    {
2201                        let macro_ptr = AstPtr::new(&call);
2202                        this.collect_macro_call(call, macro_ptr, true, |this, expanded_path| {
2203                            collect_path(this, expanded_path?)
2204                        })
2205                    }
2206                }
2207                _ => None,
2208            }
2209        }
2210
2211        fn collect_possibly_rest(
2212            this: &mut ExprCollector<'_>,
2213            expr: ast::Expr,
2214        ) -> Either<PatId, ()> {
2215            match &expr {
2216                ast::Expr::RangeExpr(e) if e.is_range_full() => Either::Right(()),
2217                ast::Expr::MacroExpr(mac) => match mac.macro_call() {
2218                    Some(call) => {
2219                        let macro_ptr = AstPtr::new(&call);
2220                        let pat = this.collect_macro_call(
2221                            call,
2222                            macro_ptr,
2223                            true,
2224                            |this, expanded_expr| match expanded_expr {
2225                                Some(expanded_pat) => collect_possibly_rest(this, expanded_pat),
2226                                None => Either::Left(this.missing_pat()),
2227                            },
2228                        );
2229                        if let Either::Left(pat) = pat {
2230                            let src = this.expander.in_file(AstPtr::new(&expr).wrap_left());
2231                            this.store.pat_map_back.insert(pat, src);
2232                        }
2233                        pat
2234                    }
2235                    None => {
2236                        let ptr = AstPtr::new(&expr);
2237                        Either::Left(this.alloc_pat_from_expr(Pat::Missing, ptr))
2238                    }
2239                },
2240                _ => Either::Left(this.collect_expr_as_pat(expr)),
2241            }
2242        }
2243
2244        fn collect_tuple(
2245            this: &mut ExprCollector<'_>,
2246            fields: ast::AstChildren<ast::Expr>,
2247        ) -> (Option<u32>, Box<[la_arena::Idx<Pat>]>) {
2248            let mut ellipsis = None;
2249            let args = fields
2250                .enumerate()
2251                .filter_map(|(idx, elem)| {
2252                    match collect_possibly_rest(this, elem) {
2253                        Either::Left(pat) => Some(pat),
2254                        Either::Right(()) => {
2255                            if ellipsis.is_none() {
2256                                ellipsis = Some(idx as u32);
2257                            }
2258                            // FIXME: Report an error here otherwise.
2259                            None
2260                        }
2261                    }
2262                })
2263                .collect();
2264            (ellipsis, args)
2265        }
2266    }
2267
2268    /// The callback should return two exprs: the first is the bindings owner, the second is the expr to return.
2269    fn with_binding_owner_and_return(
2270        &mut self,
2271        create_expr: impl FnOnce(&mut Self) -> (ExprId, ExprId),
2272    ) -> ExprId {
2273        let prev_unowned_bindings_len = self.unowned_bindings.len();
2274        let (bindings_owner, expr_to_return) = create_expr(self);
2275        self.associate_unowned_bindings_with(prev_unowned_bindings_len, bindings_owner);
2276        expr_to_return
2277    }
2278
2279    fn associate_unowned_bindings_with(
2280        &mut self,
2281        prev_unowned_bindings_len: usize,
2282        bindings_owner: ExprId,
2283    ) {
2284        for binding in self.unowned_bindings.drain(prev_unowned_bindings_len..) {
2285            self.store.binding_owners.insert(binding, bindings_owner);
2286        }
2287    }
2288
2289    fn with_binding_owner(&mut self, create_expr: impl FnOnce(&mut Self) -> ExprId) -> ExprId {
2290        self.with_binding_owner_and_return(move |this| {
2291            let expr = create_expr(this);
2292            (expr, expr)
2293        })
2294    }
2295
2296    /// Desugar `try { <stmts>; <expr> }` into `'<new_label>: { <stmts>; ::std::ops::Try::from_output(<expr>) }`,
2297    /// `try { <stmts>; }` into `'<new_label>: { <stmts>; ::std::ops::Try::from_output(()) }`
2298    /// and save the `<new_label>` to use it as a break target for desugaring of the `?` operator.
2299    fn desugar_try_block(&mut self, e: BlockExpr, result_type: Option<ast::Type>) -> ExprId {
2300        let try_from_output = self.lang_path(self.lang_items().TryTraitFromOutput);
2301        let label = self.generate_new_name();
2302        let label = self.alloc_label_desugared(Label { name: label }, AstPtr::new(&e).wrap_right());
2303        let try_block_info = match result_type {
2304            Some(_) => TryBlock::Heterogeneous { label },
2305            None => TryBlock::Homogeneous { label },
2306        };
2307        let old_try_block = self.current_try_block.replace(try_block_info);
2308
2309        let ptr = AstPtr::new(&e).upcast();
2310        let (btail, expr_id) = self.with_labeled_rib(label, HygieneId::ROOT, |this| {
2311            let mut btail = None;
2312            let block = this.collect_block_(e, |_, id, statements, tail| {
2313                btail = tail;
2314                Expr::Block { id, statements, tail, label: Some(label), unsafe_: Unsafe::No }
2315            });
2316            (btail, block)
2317        });
2318
2319        let callee = self
2320            .alloc_expr_desugared_with_ptr(try_from_output.map_or(Expr::Missing, Expr::Path), ptr);
2321        let next_tail = match btail {
2322            Some(tail) => self
2323                .alloc_expr_desugared_with_ptr(Expr::Call { callee, args: Box::new([tail]) }, ptr),
2324            None => {
2325                let unit =
2326                    self.alloc_expr_desugared_with_ptr(Expr::Tuple { exprs: Box::new([]) }, ptr);
2327                self.alloc_expr_desugared_with_ptr(
2328                    Expr::Call { callee, args: Box::new([unit]) },
2329                    ptr,
2330                )
2331            }
2332        };
2333        let Expr::Block { tail, .. } = &mut self.store.exprs[expr_id] else {
2334            unreachable!("block was lowered to non-block");
2335        };
2336        *tail = Some(next_tail);
2337        self.current_try_block = old_try_block;
2338        match result_type {
2339            Some(ty) => {
2340                // `{ let <name>: <ty> = <expr>; <name> }`
2341                let name = self.generate_new_name();
2342                let type_ref = self.lower_type_ref_disallow_impl_trait(ty);
2343                let binding = self.alloc_binding(
2344                    name.clone(),
2345                    BindingAnnotation::Unannotated,
2346                    HygieneId::ROOT,
2347                );
2348                let pat = self.alloc_pat_desugared(Pat::Bind { id: binding, subpat: None });
2349                self.add_definition_to_binding(binding, pat);
2350                let tail_expr =
2351                    self.alloc_expr_desugared_with_ptr(Expr::Path(Path::from(name)), ptr);
2352                self.alloc_expr_desugared_with_ptr(
2353                    Expr::Block {
2354                        id: None,
2355                        statements: Box::new([Statement::Let {
2356                            pat,
2357                            type_ref: Some(type_ref),
2358                            initializer: Some(expr_id),
2359                            else_branch: None,
2360                        }]),
2361                        tail: Some(tail_expr),
2362                        label: None,
2363                        unsafe_: Unsafe::No,
2364                    },
2365                    ptr,
2366                )
2367            }
2368            None => expr_id,
2369        }
2370    }
2371
2372    /// Desugar `ast::WhileExpr` from: `[opt_ident]: while <cond> <body>` into:
2373    /// ```ignore (pseudo-rust)
2374    /// [opt_ident]: loop {
2375    ///   if <cond> {
2376    ///     <body>
2377    ///   }
2378    ///   else {
2379    ///     break;
2380    ///   }
2381    /// }
2382    /// ```
2383    /// FIXME: Rustc wraps the condition in [`DropTemps`] -- a construct equivalent to
2384    /// `{ let _t = <cond>; _t }` -- to preserve drop semantics.
2385    /// We should probably do the same in future.
2386    ///
2387    /// [`DropTemps`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.ExprKind.html#variant.DropTemps
2388    fn collect_while_loop(&mut self, syntax_ptr: AstPtr<ast::Expr>, e: ast::WhileExpr) -> ExprId {
2389        let label = e.label().map(|label| {
2390            (self.hygiene_id_for(label.syntax().text_range()), self.collect_label(label))
2391        });
2392        let body = self.collect_labelled_block_opt(label, e.loop_body());
2393
2394        // Labels can also be used in the condition expression, like this:
2395        // ```
2396        // fn main() {
2397        //     let mut optional = Some(0);
2398        //     'my_label: while let Some(a) = match optional {
2399        //         None => break 'my_label,
2400        //         Some(val) => Some(val),
2401        //     } {
2402        //         println!("{}", a);
2403        //         optional = None;
2404        //     }
2405        // }
2406        // ```
2407        let condition = match label {
2408            Some((label_hygiene, label)) => self.with_labeled_rib(label, label_hygiene, |this| {
2409                this.collect_expr_opt(e.condition())
2410            }),
2411            None => self.collect_expr_opt(e.condition()),
2412        };
2413
2414        let break_expr = self.alloc_expr(Expr::Break { expr: None, label: None }, syntax_ptr);
2415        let if_expr = self.alloc_expr(
2416            Expr::If { condition, then_branch: body, else_branch: Some(break_expr) },
2417            syntax_ptr,
2418        );
2419        self.alloc_expr(
2420            Expr::Loop { body: if_expr, label: label.map(|it| it.1), source: LoopSource::While },
2421            syntax_ptr,
2422        )
2423    }
2424
2425    /// Desugar `ast::ForExpr` from: `[opt_ident]: for <pat> in <head> <body>` into:
2426    /// ```ignore (pseudo-rust)
2427    /// match IntoIterator::into_iter(<head>) {
2428    ///     mut iter => {
2429    ///         [opt_ident]: loop {
2430    ///             match Iterator::next(&mut iter) {
2431    ///                 None => break,
2432    ///                 Some(<pat>) => <body>,
2433    ///             };
2434    ///         }
2435    ///     }
2436    /// }
2437    /// ```
2438    fn collect_for_loop(&mut self, syntax_ptr: AstPtr<ast::Expr>, e: ast::ForExpr) -> ExprId {
2439        let lang_items = self.lang_items();
2440        let (Some(into_iter_fn), Some(iter_next_fn), Some(option_some), Some(option_none)) = (
2441            self.lang_path(lang_items.IntoIterIntoIter),
2442            self.lang_path(lang_items.IteratorNext),
2443            self.lang_path(lang_items.OptionSome),
2444            self.lang_path(lang_items.OptionNone),
2445        ) else {
2446            return self.missing_expr();
2447        };
2448        let iterable = e.iterable();
2449        let syntax_ptr_iterable = iterable.as_ref().map_or(syntax_ptr, AstPtr::new);
2450        let loop_body = e.loop_body().map(|it| it.into());
2451        let head = self.collect_expr_opt(iterable);
2452        let into_iter_fn_expr =
2453            self.alloc_expr_desugared_with_ptr(Expr::Path(into_iter_fn), syntax_ptr_iterable);
2454        let iterator = self.alloc_expr_desugared_with_ptr(
2455            Expr::Call { callee: into_iter_fn_expr, args: Box::new([head]) },
2456            syntax_ptr_iterable,
2457        );
2458        let none_arm = MatchArm {
2459            pat: self.alloc_pat_desugared(Pat::Path(option_none)),
2460            guard: None,
2461            expr: self
2462                .alloc_expr_desugared_with_ptr(Expr::Break { expr: None, label: None }, syntax_ptr),
2463        };
2464        let some_pat = Pat::TupleStruct {
2465            path: option_some,
2466            args: Box::new([self.collect_pat_top(e.pat())]),
2467            ellipsis: None,
2468        };
2469        let label = e.label().map(|label| {
2470            (self.hygiene_id_for(label.syntax().text_range()), self.collect_label(label))
2471        });
2472        let some_arm = MatchArm {
2473            pat: self.alloc_pat_desugared(some_pat),
2474            guard: None,
2475            expr: self.with_opt_labeled_rib(label, |this| this.collect_expr_opt(loop_body)),
2476        };
2477        let iter_name = self.generate_new_name();
2478        let iter_expr = self
2479            .alloc_expr_desugared_with_ptr(Expr::Path(Path::from(iter_name.clone())), syntax_ptr);
2480        let iter_expr_mut = self.alloc_expr_desugared_with_ptr(
2481            Expr::Ref { expr: iter_expr, rawness: Rawness::Ref, mutability: Mutability::Mut },
2482            syntax_ptr,
2483        );
2484        let iter_next_fn_expr =
2485            self.alloc_expr_desugared_with_ptr(Expr::Path(iter_next_fn), syntax_ptr);
2486        let iter_next_expr = self.alloc_expr_desugared_with_ptr(
2487            Expr::Call { callee: iter_next_fn_expr, args: Box::new([iter_expr_mut]) },
2488            syntax_ptr,
2489        );
2490        let loop_inner = self.alloc_expr_desugared_with_ptr(
2491            Expr::Match { expr: iter_next_expr, arms: Box::new([none_arm, some_arm]) },
2492            syntax_ptr,
2493        );
2494        let loop_inner = self.alloc_expr_desugared_with_ptr(
2495            Expr::Block {
2496                id: None,
2497                statements: Box::default(),
2498                tail: Some(loop_inner),
2499                label: None,
2500                unsafe_: Unsafe::No,
2501            },
2502            syntax_ptr,
2503        );
2504        let loop_outer = self.alloc_expr_desugared_with_ptr(
2505            Expr::Loop {
2506                body: loop_inner,
2507                label: label.map(|it| it.1),
2508                source: LoopSource::ForLoop,
2509            },
2510            syntax_ptr,
2511        );
2512        let iter_binding =
2513            self.alloc_binding(iter_name, BindingAnnotation::Mutable, HygieneId::ROOT);
2514        let iter_pat = self.alloc_pat_desugared(Pat::Bind { id: iter_binding, subpat: None });
2515        self.add_definition_to_binding(iter_binding, iter_pat);
2516        self.alloc_expr(
2517            Expr::Match {
2518                expr: iterator,
2519                arms: Box::new([MatchArm { pat: iter_pat, guard: None, expr: loop_outer }]),
2520            },
2521            syntax_ptr,
2522        )
2523    }
2524
2525    /// Desugar `ast::TryExpr` from: `<expr>?` into:
2526    /// ```ignore (pseudo-rust)
2527    /// match Try::branch(<expr>) {
2528    ///     ControlFlow::Continue(val) => val,
2529    ///     ControlFlow::Break(residual) =>
2530    ///         // If there is an enclosing `try {...}`:
2531    ///         break 'catch_target Residual::into_try_type(residual),
2532    ///         // If there is an enclosing `try bikeshed Ty {...}`:
2533    ///         break 'catch_target Try::from_residual(residual),
2534    ///         // Otherwise:
2535    ///         return Try::from_residual(residual),
2536    /// }
2537    /// ```
2538    fn collect_try_operator(&mut self, syntax_ptr: AstPtr<ast::Expr>, e: ast::TryExpr) -> ExprId {
2539        let lang_items = self.lang_items();
2540        let (Some(try_branch), Some(cf_continue), Some(cf_break)) = (
2541            self.lang_path(lang_items.TryTraitBranch),
2542            self.lang_path(lang_items.ControlFlowContinue),
2543            self.lang_path(lang_items.ControlFlowBreak),
2544        ) else {
2545            return self.missing_expr();
2546        };
2547        let operand = self.collect_expr_opt(e.expr());
2548        let try_branch = self.alloc_expr(Expr::Path(try_branch), syntax_ptr);
2549        let expr = self
2550            .alloc_expr(Expr::Call { callee: try_branch, args: Box::new([operand]) }, syntax_ptr);
2551        let continue_name = self.generate_new_name();
2552        let continue_binding = self.alloc_binding(
2553            continue_name.clone(),
2554            BindingAnnotation::Unannotated,
2555            HygieneId::ROOT,
2556        );
2557        let continue_bpat =
2558            self.alloc_pat_desugared(Pat::Bind { id: continue_binding, subpat: None });
2559        self.add_definition_to_binding(continue_binding, continue_bpat);
2560        let continue_arm = MatchArm {
2561            pat: self.alloc_pat_desugared(Pat::TupleStruct {
2562                path: cf_continue,
2563                args: Box::new([continue_bpat]),
2564                ellipsis: None,
2565            }),
2566            guard: None,
2567            expr: self.alloc_expr(Expr::Path(Path::from(continue_name)), syntax_ptr),
2568        };
2569        let break_name = self.generate_new_name();
2570        let break_binding =
2571            self.alloc_binding(break_name.clone(), BindingAnnotation::Unannotated, HygieneId::ROOT);
2572        let break_bpat = self.alloc_pat_desugared(Pat::Bind { id: break_binding, subpat: None });
2573        self.add_definition_to_binding(break_binding, break_bpat);
2574        let break_arm = MatchArm {
2575            pat: self.alloc_pat_desugared(Pat::TupleStruct {
2576                path: cf_break,
2577                args: Box::new([break_bpat]),
2578                ellipsis: None,
2579            }),
2580            guard: None,
2581            expr: {
2582                let it = self.alloc_expr(Expr::Path(Path::from(break_name)), syntax_ptr);
2583                let convert_fn = match self.current_try_block {
2584                    Some(TryBlock::Homogeneous { .. }) => {
2585                        self.lang_path(lang_items.ResidualIntoTryType)
2586                    }
2587                    Some(TryBlock::Heterogeneous { .. }) | None => {
2588                        self.lang_path(lang_items.TryTraitFromResidual)
2589                    }
2590                };
2591                let callee =
2592                    self.alloc_expr(convert_fn.map_or(Expr::Missing, Expr::Path), syntax_ptr);
2593                let result =
2594                    self.alloc_expr(Expr::Call { callee, args: Box::new([it]) }, syntax_ptr);
2595                self.alloc_expr(
2596                    match self.current_try_block {
2597                        Some(
2598                            TryBlock::Heterogeneous { label } | TryBlock::Homogeneous { label },
2599                        ) => Expr::Break { expr: Some(result), label: Some(label) },
2600                        None => Expr::Return { expr: Some(result) },
2601                    },
2602                    syntax_ptr,
2603                )
2604            },
2605        };
2606        let arms = Box::new([continue_arm, break_arm]);
2607        self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
2608    }
2609
2610    fn collect_macro_call<T, U>(
2611        &mut self,
2612        mcall: ast::MacroCall,
2613        syntax_ptr: AstPtr<ast::MacroCall>,
2614        record_diagnostics: bool,
2615        collector: impl FnOnce(&mut Self, Option<T>) -> U,
2616    ) -> U
2617    where
2618        T: ast::AstNode,
2619    {
2620        let macro_call_ptr = self.expander.in_file(syntax_ptr);
2621
2622        let block_call = self.def_map.modules[self.module].scope.macro_invoc(
2623            self.expander.in_file(self.expander.ast_id_map().ast_id_for_ptr(syntax_ptr)),
2624        );
2625        let res = match block_call {
2626            // fast path, macro call is in a block module
2627            Some(call) => Ok(self.expander.enter_expand_id(self.db, call)),
2628            None => {
2629                let resolver = |path: &_| {
2630                    self.def_map
2631                        .resolve_path(
2632                            self.local_def_map,
2633                            self.db,
2634                            self.module,
2635                            path,
2636                            crate::item_scope::BuiltinShadowMode::Other,
2637                            Some(MacroSubNs::Bang),
2638                        )
2639                        .0
2640                        .take_macros()
2641                };
2642                self.expander.enter_expand(
2643                    self.db,
2644                    mcall,
2645                    self.krate,
2646                    resolver,
2647                    &mut |ptr, call| {
2648                        _ = self.store.expansions.insert(ptr.map(|(it, _)| it), call);
2649                    },
2650                )
2651            }
2652        };
2653
2654        let res = match res {
2655            Ok(res) => res,
2656            Err(UnresolvedMacro { path }) => {
2657                if record_diagnostics {
2658                    self.store.diagnostics.push(ExpressionStoreDiagnostics::UnresolvedMacroCall {
2659                        node: self.expander.in_file(syntax_ptr),
2660                        path,
2661                    });
2662                }
2663                return collector(self, None);
2664            }
2665        };
2666        // No need to push macro and parsing errors as they'll be recreated from `macro_calls()`.
2667
2668        match res.value {
2669            Some((mark, expansion)) => {
2670                // Keep collecting even with expansion errors so we can provide completions and
2671                // other services in incomplete macro expressions.
2672                if let Some(macro_file) = self.expander.current_file_id().macro_file() {
2673                    self.store.expansions.insert(macro_call_ptr, macro_file);
2674                }
2675
2676                let id = collector(self, expansion.map(|it| it.tree()));
2677                self.expander.exit(mark);
2678                id
2679            }
2680            None => collector(self, None),
2681        }
2682    }
2683
2684    fn collect_macro_as_stmt(
2685        &mut self,
2686        statements: &mut Vec<Statement>,
2687        mac: ast::MacroExpr,
2688    ) -> Option<ExprId> {
2689        if !self.check_cfg(&ast::Expr::MacroExpr(mac.clone())) {
2690            return None;
2691        }
2692
2693        let mac_call = mac.macro_call()?;
2694        let syntax_ptr = AstPtr::new(&ast::Expr::from(mac));
2695        let macro_ptr = AstPtr::new(&mac_call);
2696        let expansion = self.collect_macro_call(
2697            mac_call,
2698            macro_ptr,
2699            false,
2700            |this, expansion: Option<ast::MacroStmts>| match expansion {
2701                Some(expansion) => {
2702                    expansion.statements().for_each(|stmt| this.collect_stmt(statements, stmt));
2703                    expansion.expr().and_then(|expr| match expr {
2704                        ast::Expr::MacroExpr(mac) => this.collect_macro_as_stmt(statements, mac),
2705                        expr => this.maybe_collect_expr(expr),
2706                    })
2707                }
2708                None => None,
2709            },
2710        );
2711        expansion.inspect(|&tail| {
2712            // Make the macro-call point to its expanded expression so we can query
2713            // semantics on syntax pointers to the macro
2714            let src = self.expander.in_file(syntax_ptr);
2715            self.store.expr_map.insert(src, tail.into());
2716        })
2717    }
2718
2719    fn collect_stmt(&mut self, statements: &mut Vec<Statement>, s: ast::Stmt) {
2720        match s {
2721            ast::Stmt::LetStmt(stmt) => {
2722                if !self.check_cfg(&stmt) {
2723                    return;
2724                }
2725                let pat = self.collect_pat_top(stmt.pat());
2726                let type_ref = stmt.ty().map(|it| self.lower_type_ref_disallow_impl_trait(it));
2727                let initializer = stmt.initializer().map(|e| self.collect_expr(e));
2728                let else_branch = stmt
2729                    .let_else()
2730                    .and_then(|let_else| let_else.block_expr())
2731                    .map(|block| self.collect_block(block));
2732                statements.push(Statement::Let { pat, type_ref, initializer, else_branch });
2733            }
2734            ast::Stmt::ExprStmt(stmt) => {
2735                let expr = stmt.expr();
2736                let has_semi = stmt.semicolon_token().is_some();
2737                // Note that macro could be expanded to multiple statements
2738                if let Some(ast::Expr::MacroExpr(mac)) = expr {
2739                    if let Some(expr) = self.collect_macro_as_stmt(statements, mac) {
2740                        statements.push(Statement::Expr { expr, has_semi })
2741                    }
2742                } else if let Some(expr) = expr.and_then(|expr| self.maybe_collect_expr(expr)) {
2743                    statements.push(Statement::Expr { expr, has_semi });
2744                }
2745            }
2746            ast::Stmt::Item(ast::Item::MacroDef(macro_)) => {
2747                if !self.check_cfg(&macro_) {
2748                    return;
2749                }
2750                let Some(name) = macro_.name() else {
2751                    statements.push(Statement::Item(Item::Other));
2752                    return;
2753                };
2754                let name = name.as_name();
2755                let macro_id =
2756                    self.def_map.modules[self.def_map.root].scope.get(&name).take_macros();
2757                self.collect_macro_def(statements, macro_id);
2758            }
2759            ast::Stmt::Item(ast::Item::MacroRules(macro_)) => {
2760                if !self.check_cfg(&macro_) {
2761                    return;
2762                }
2763                let Some(name) = macro_.name() else {
2764                    statements.push(Statement::Item(Item::Other));
2765                    return;
2766                };
2767                let name = name.as_name();
2768                let macro_defs_count =
2769                    self.current_block_legacy_macro_defs_count.entry(name.clone()).or_insert(0);
2770                let macro_id = self.def_map.modules[self.def_map.root]
2771                    .scope
2772                    .get_legacy_macro(&name)
2773                    .and_then(|it| it.get(*macro_defs_count))
2774                    .copied();
2775                *macro_defs_count += 1;
2776                self.collect_macro_def(statements, macro_id);
2777            }
2778            ast::Stmt::Item(_item) => statements.push(Statement::Item(Item::Other)),
2779        }
2780    }
2781
2782    fn collect_macro_def(&mut self, statements: &mut Vec<Statement>, macro_id: Option<MacroId>) {
2783        let Some(macro_id) = macro_id else {
2784            never!("def map should have macro definition, but it doesn't");
2785            statements.push(Statement::Item(Item::Other));
2786            return;
2787        };
2788        let macro_id = macro_id.definition(self.db);
2789        statements.push(Statement::Item(Item::MacroDef(Box::new(macro_id))));
2790        self.label_ribs.push(LabelRib::new(RibKind::MacroDef(Box::new(macro_id))));
2791    }
2792
2793    fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId {
2794        self.collect_block_(block, |_, id, statements, tail| Expr::Block {
2795            id,
2796            statements,
2797            tail,
2798            label: None,
2799            unsafe_: Unsafe::No,
2800        })
2801    }
2802
2803    fn collect_block_(
2804        &mut self,
2805        block: ast::BlockExpr,
2806        mk_block: impl FnOnce(&mut Self, Option<BlockId>, Box<[Statement]>, Option<ExprId>) -> Expr,
2807    ) -> ExprId {
2808        let block_id = (|| {
2809            let token = self.lowering_mode.allow_tracked_structs()?;
2810            let file_local_id = self.expander.ast_id_map().ast_id_for_block(&block)?;
2811            let ast_id = self.expander.in_file(file_local_id);
2812            Some(unsafe { BlockIdLt::new(self.db, ast_id, self.module, token).to_static() })
2813        })();
2814
2815        let (module, def_map) =
2816            match block_id.map(|block_id| (block_def_map(self.db, block_id), block_id)) {
2817                Some((def_map, block_id)) => {
2818                    self.store.block_scopes.push(block_id);
2819                    (def_map.root_module_id(), def_map)
2820                }
2821                None => (self.module, self.def_map),
2822            };
2823        let prev_def_map = mem::replace(&mut self.def_map, def_map);
2824        let prev_local_module = mem::replace(&mut self.module, module);
2825        let prev_legacy_macros_count = mem::take(&mut self.current_block_legacy_macro_defs_count);
2826
2827        let mut statements = Vec::new();
2828        block.statements().for_each(|s| self.collect_stmt(&mut statements, s));
2829        let tail = block.tail_expr().and_then(|e| match e {
2830            ast::Expr::MacroExpr(mac) => self.collect_macro_as_stmt(&mut statements, mac),
2831            expr => self.maybe_collect_expr(expr),
2832        });
2833        let tail = tail.or_else(|| {
2834            let stmt = statements.pop()?;
2835            if let Statement::Expr { expr, has_semi: false } = stmt {
2836                return Some(expr);
2837            }
2838            statements.push(stmt);
2839            None
2840        });
2841
2842        let syntax_node_ptr = AstPtr::new(&block.into());
2843        let expr = mk_block(self, block_id, statements.into_boxed_slice(), tail);
2844        let expr_id = self.alloc_expr(expr, syntax_node_ptr);
2845
2846        self.def_map = prev_def_map;
2847        self.module = prev_local_module;
2848        self.current_block_legacy_macro_defs_count = prev_legacy_macros_count;
2849        expr_id
2850    }
2851
2852    fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId {
2853        match expr {
2854            Some(block) => self.collect_block(block),
2855            None => self.missing_expr(),
2856        }
2857    }
2858
2859    fn collect_labelled_block_opt(
2860        &mut self,
2861        label: Option<(HygieneId, LabelId)>,
2862        expr: Option<ast::BlockExpr>,
2863    ) -> ExprId {
2864        match label {
2865            Some((hygiene, label)) => {
2866                self.with_labeled_rib(label, hygiene, |this| this.collect_block_opt(expr))
2867            }
2868            None => self.collect_block_opt(expr),
2869        }
2870    }
2871
2872    fn collect_param_as_ident(&mut self, pat: ast::Pat) -> PatId {
2873        // parameters of functions in `extern` blocks and associated trait functions without a body
2874        // can only be simple identifiers and wildcards.
2875        // Furthermore, the identifiers in their parameters are always interpreted as bindings, even
2876        // if in a normal function they won't be, because they would refer to a path pattern.
2877
2878        match &pat {
2879            ast::Pat::IdentPat(bp) if bp.is_simple_ident() => {
2880                let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
2881                let hygiene = bp
2882                    .name()
2883                    .map(|name| self.hygiene_id_for(name.syntax().text_range()))
2884                    .unwrap_or(HygieneId::ROOT);
2885                let binding = self.alloc_binding(name, BindingAnnotation::Unannotated, hygiene);
2886                let pat =
2887                    self.alloc_pat(Pat::Bind { id: binding, subpat: None }, AstPtr::new(&pat));
2888                self.add_definition_to_binding(binding, pat);
2889                pat
2890            }
2891            ast::Pat::WildcardPat(_) => self.alloc_pat(Pat::Wild, AstPtr::new(&pat)),
2892            ast::Pat::MacroPat(mac) => self
2893                .collect_macro_pat_with(mac.clone(), |this, pat| this.collect_param_as_ident(pat)),
2894            _ => {
2895                self.store.diagnostics.push(ExpressionStoreDiagnostics::PatternArgInExternFn {
2896                    node: self.expander.in_file(AstPtr::new(&pat)),
2897                });
2898                self.missing_pat()
2899            }
2900        }
2901    }
2902
2903    // region: patterns
2904
2905    fn collect_pat_top(&mut self, pat: Option<ast::Pat>) -> PatId {
2906        match pat {
2907            Some(pat) => self.collect_pat(pat, &mut BindingList::default()),
2908            None => self.missing_pat(),
2909        }
2910    }
2911
2912    fn collect_pat(&mut self, pat: ast::Pat, binding_list: &mut BindingList) -> PatId {
2913        let pattern = match &pat {
2914            ast::Pat::IdentPat(bp) => {
2915                let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
2916                let hygiene = bp
2917                    .name()
2918                    .map(|name| self.hygiene_id_for(name.syntax().text_range()))
2919                    .unwrap_or(HygieneId::ROOT);
2920
2921                let annotation =
2922                    BindingAnnotation::new(bp.mut_token().is_some(), bp.ref_token().is_some());
2923                let subpat = bp.pat().map(|subpat| self.collect_pat(subpat, binding_list));
2924
2925                let is_simple_ident_pat =
2926                    annotation == BindingAnnotation::Unannotated && subpat.is_none();
2927                let (binding, pattern) = if is_simple_ident_pat {
2928                    // This could also be a single-segment path pattern. To
2929                    // decide that, we need to try resolving the name.
2930                    let (resolved, _) = self.def_map.resolve_path(
2931                        self.local_def_map,
2932                        self.db,
2933                        self.module,
2934                        &name.clone().into(),
2935                        BuiltinShadowMode::Other,
2936                        None,
2937                    );
2938                    // Funnily enough, record structs/variants *can* be shadowed
2939                    // by pattern bindings (but unit or tuple structs/variants
2940                    // can't).
2941                    match resolved.take_values() {
2942                        Some(ModuleDefId::ConstId(_)) => (None, Pat::Path(name.into())),
2943                        Some(ModuleDefId::EnumVariantId(_)) => (None, Pat::Path(name.into())),
2944                        Some(ModuleDefId::AdtId(AdtId::StructId(_))) => {
2945                            (None, Pat::Path(name.into()))
2946                        }
2947                        // shadowing statics is an error as well, so we just ignore that case here
2948                        _ => {
2949                            let id = binding_list.find(self, name, hygiene, annotation);
2950                            (Some(id), Pat::Bind { id, subpat })
2951                        }
2952                    }
2953                } else {
2954                    let id = binding_list.find(self, name, hygiene, annotation);
2955                    (Some(id), Pat::Bind { id, subpat })
2956                };
2957
2958                let ptr = AstPtr::new(&pat);
2959                let pat = self.alloc_pat(pattern, ptr);
2960                if let Some(binding_id) = binding {
2961                    self.add_definition_to_binding(binding_id, pat);
2962                }
2963                return pat;
2964            }
2965            ast::Pat::TupleStructPat(p) => {
2966                let path = p
2967                    .path()
2968                    .and_then(|path| self.lower_path(path, &mut Self::impl_trait_error_allocator));
2969                let Some(path) = path else {
2970                    return self.missing_pat();
2971                };
2972                let (args, ellipsis) = self.collect_tuple_pat(
2973                    p.fields(),
2974                    comma_follows_token(p.l_paren_token()),
2975                    binding_list,
2976                );
2977                Pat::TupleStruct { path, args, ellipsis }
2978            }
2979            ast::Pat::RefPat(p) => {
2980                let pat = self.collect_pat_opt(p.pat(), binding_list);
2981                let mutability = Mutability::from_mutable(p.mut_token().is_some());
2982                Pat::Ref { pat, mutability }
2983            }
2984            ast::Pat::PathPat(p) => {
2985                let path = p
2986                    .path()
2987                    .and_then(|path| self.lower_path(path, &mut Self::impl_trait_error_allocator));
2988                path.map(Pat::Path).unwrap_or(Pat::Missing)
2989            }
2990            ast::Pat::OrPat(p) => 'b: {
2991                let prev_is_used = mem::take(&mut binding_list.is_used);
2992                let prev_reject_new = mem::take(&mut binding_list.reject_new);
2993                let mut pats = Vec::with_capacity(p.pats().count());
2994                let mut it = p.pats();
2995                let Some(first) = it.next() else {
2996                    break 'b Pat::Or(Box::new([]));
2997                };
2998                pats.push(self.collect_pat(first, binding_list));
2999                binding_list.reject_new = true;
3000                for rest in it {
3001                    for it in binding_list.is_used.values_mut() {
3002                        *it = false;
3003                    }
3004                    pats.push(self.collect_pat(rest, binding_list));
3005                    for (&id, &is_used) in binding_list.is_used.iter() {
3006                        if !is_used {
3007                            self.store.bindings[id].problems =
3008                                Some(BindingProblems::NotBoundAcrossAll);
3009                        }
3010                    }
3011                }
3012                binding_list.reject_new = prev_reject_new;
3013                let current_is_used = mem::replace(&mut binding_list.is_used, prev_is_used);
3014                for (id, _) in current_is_used.into_iter() {
3015                    binding_list.check_is_used(self, id);
3016                }
3017                if let &[pat] = &*pats {
3018                    // Leading pipe without real OR pattern. Leaving an one-item OR pattern may confuse later stages.
3019                    return pat;
3020                }
3021                Pat::Or(pats.into())
3022            }
3023            ast::Pat::ParenPat(p) => return self.collect_pat_opt(p.pat(), binding_list),
3024            ast::Pat::TuplePat(p) => {
3025                let (args, ellipsis) = self.collect_tuple_pat(
3026                    p.fields(),
3027                    comma_follows_token(p.l_paren_token()),
3028                    binding_list,
3029                );
3030                Pat::Tuple { args, ellipsis }
3031            }
3032            ast::Pat::WildcardPat(_) => Pat::Wild,
3033            ast::Pat::RecordPat(p) => {
3034                let path = p
3035                    .path()
3036                    .and_then(|path| self.lower_path(path, &mut Self::impl_trait_error_allocator));
3037                let Some(path) = path else {
3038                    return self.missing_pat();
3039                };
3040                let record_pat_field_list =
3041                    &p.record_pat_field_list().expect("every struct should have a field list");
3042                let args = record_pat_field_list
3043                    .fields()
3044                    .filter_map(|f| {
3045                        if !self.check_cfg(&f) {
3046                            return None;
3047                        }
3048                        let ast_pat = f.pat()?;
3049                        let pat = self.collect_pat(ast_pat, binding_list);
3050                        let name = f.field_name()?.as_name();
3051                        let src = self.expander.in_file(AstPtr::new(&f).wrap_right());
3052                        self.store.pat_field_map_back.insert(pat, src);
3053                        Some(RecordFieldPat { name, pat })
3054                    })
3055                    .collect();
3056
3057                let ellipsis = record_pat_field_list.rest_pat().is_some();
3058
3059                Pat::Record { path, args, ellipsis }
3060            }
3061            ast::Pat::SlicePat(p) => {
3062                let SlicePatComponents { prefix, slice, suffix } = p.components();
3063
3064                Pat::Slice {
3065                    prefix: prefix.into_iter().map(|p| self.collect_pat(p, binding_list)).collect(),
3066                    slice: slice.map(|p| self.collect_pat(p, binding_list)),
3067                    suffix: suffix.into_iter().map(|p| self.collect_pat(p, binding_list)).collect(),
3068                }
3069            }
3070            ast::Pat::LiteralPat(lit) => 'b: {
3071                let Some((hir_lit, ast_lit)) = pat_literal_to_hir(lit) else {
3072                    break 'b Pat::Missing;
3073                };
3074                let expr = Expr::Literal(hir_lit);
3075                let expr_ptr = AstPtr::new(&ast::Expr::Literal(ast_lit));
3076                let expr_id = self.alloc_expr(expr, expr_ptr);
3077                Pat::Lit(expr_id)
3078            }
3079            ast::Pat::RestPat(_) => Pat::Rest,
3080            ast::Pat::BoxPat(boxpat) => {
3081                let inner = self.collect_pat_opt(boxpat.pat(), binding_list);
3082                Pat::Box { inner }
3083            }
3084            ast::Pat::DerefPat(inner) => {
3085                let inner = self.collect_pat_opt(inner.pat(), binding_list);
3086                Pat::Deref { inner }
3087            }
3088            ast::Pat::NotNull(_) => Pat::NotNull,
3089            ast::Pat::ConstBlockPat(_) => Pat::Missing,
3090            ast::Pat::CfgPredPat(p) => {
3091                let enabled = Expr::Literal(Literal::Bool(self.check_cfg_pred(p.cfg_predicate())));
3092                let expr_id = self.alloc_expr_from_pat(enabled, AstPtr::new(&pat));
3093                Pat::Lit(expr_id)
3094            }
3095            ast::Pat::MacroPat(mac) => {
3096                return self.collect_macro_pat_with(mac.clone(), |this, expanded_pat| {
3097                    this.collect_pat(expanded_pat, binding_list)
3098                });
3099            }
3100            ast::Pat::RangePat(p) => {
3101                let mut range_part_lower = |p: Option<ast::Pat>| -> Option<ExprId> {
3102                    p.and_then(|it| {
3103                        let ptr = PatPtr::new(&it);
3104                        match &it {
3105                            ast::Pat::LiteralPat(it) => Some(self.alloc_expr_from_pat(
3106                                Expr::Literal(pat_literal_to_hir(it)?.0),
3107                                ptr,
3108                            )),
3109                            ast::Pat::IdentPat(ident) if ident.is_simple_ident() => ident
3110                                .name()
3111                                .map(|name| name.as_name())
3112                                .map(Path::from)
3113                                .map(|path| self.alloc_expr_from_pat(Expr::Path(path), ptr)),
3114                            ast::Pat::PathPat(p) => p
3115                                .path()
3116                                .and_then(|path| {
3117                                    self.lower_path(path, &mut Self::impl_trait_error_allocator)
3118                                })
3119                                .map(|parsed| self.alloc_expr_from_pat(Expr::Path(parsed), ptr)),
3120                            // We only need to handle literal, ident (if bare) and path patterns here,
3121                            // as any other pattern as a range pattern operand is semantically invalid.
3122                            _ => None,
3123                        }
3124                    })
3125                };
3126                let start = range_part_lower(p.start());
3127                let end = range_part_lower(p.end());
3128                match p.op_kind() {
3129                    Some(range_type) => Pat::Range { start, end, range_type },
3130                    None => Pat::Missing,
3131                }
3132            }
3133        };
3134        let ptr = AstPtr::new(&pat);
3135        self.alloc_pat(pattern, ptr)
3136    }
3137
3138    fn collect_macro_pat_with(
3139        &mut self,
3140        mac: ast::MacroPat,
3141        callback: impl FnOnce(&mut Self, ast::Pat) -> PatId,
3142    ) -> PatId {
3143        match mac.macro_call() {
3144            Some(call) => {
3145                let macro_ptr = AstPtr::new(&call);
3146                let src = self.expander.in_file(AstPtr::new(&mac.into()));
3147                let pat = self.collect_macro_call(call, macro_ptr, true, |this, expanded_pat| {
3148                    match expanded_pat {
3149                        Some(pat) => callback(this, pat),
3150                        None => this.missing_pat(),
3151                    }
3152                });
3153                self.store.pat_map.insert(src, pat.into());
3154                pat
3155            }
3156            None => self.missing_pat(),
3157        }
3158    }
3159
3160    fn collect_pat_opt(&mut self, pat: Option<ast::Pat>, binding_list: &mut BindingList) -> PatId {
3161        match pat {
3162            Some(pat) => self.collect_pat(pat, binding_list),
3163            None => self.missing_pat(),
3164        }
3165    }
3166
3167    fn collect_tuple_pat(
3168        &mut self,
3169        args: AstChildren<ast::Pat>,
3170        has_leading_comma: bool,
3171        binding_list: &mut BindingList,
3172    ) -> (Box<[PatId]>, Option<u32>) {
3173        let args: Vec<_> = args.map(|p| self.collect_pat_possibly_rest(p, binding_list)).collect();
3174        // Find the location of the `..`, if there is one. Note that we do not
3175        // consider the possibility of there being multiple `..` here.
3176        let ellipsis = args.iter().position(|p| p.is_right()).map(|it| it as u32);
3177
3178        // We want to skip the `..` pattern here, since we account for it above.
3179        let mut args: Vec<_> = args.into_iter().filter_map(Either::left).collect();
3180        // if there is a leading comma, the user is most likely to type out a leading pattern
3181        // so we insert a missing pattern at the beginning for IDE features
3182        if has_leading_comma {
3183            args.insert(0, self.missing_pat());
3184        }
3185
3186        (args.into_boxed_slice(), ellipsis)
3187    }
3188
3189    // `collect_pat` rejects `ast::Pat::RestPat`, but it should be handled in some cases that
3190    // it is the macro expansion result of an arg sub-pattern in a slice or tuple pattern.
3191    fn collect_pat_possibly_rest(
3192        &mut self,
3193        pat: ast::Pat,
3194        binding_list: &mut BindingList,
3195    ) -> Either<PatId, ()> {
3196        match &pat {
3197            ast::Pat::RestPat(_) => Either::Right(()),
3198            ast::Pat::MacroPat(mac) => match mac.macro_call() {
3199                Some(call) => {
3200                    let macro_ptr = AstPtr::new(&call);
3201                    let src = self.expander.in_file(AstPtr::new(&pat));
3202                    let pat =
3203                        self.collect_macro_call(call, macro_ptr, true, |this, expanded_pat| {
3204                            if let Some(expanded_pat) = expanded_pat {
3205                                this.collect_pat_possibly_rest(expanded_pat, binding_list)
3206                            } else {
3207                                Either::Left(this.missing_pat())
3208                            }
3209                        });
3210                    if let Some(pat) = pat.left() {
3211                        self.store.pat_map.insert(src, pat.into());
3212                    }
3213                    pat
3214                }
3215                None => {
3216                    let ptr = AstPtr::new(&pat);
3217                    Either::Left(self.alloc_pat(Pat::Missing, ptr))
3218                }
3219            },
3220            _ => Either::Left(self.collect_pat(pat, binding_list)),
3221        }
3222    }
3223
3224    fn collect_ty_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId {
3225        match pat {
3226            Some(pat) => self.collect_ty_pat(pat),
3227            None => self.missing_pat(),
3228        }
3229    }
3230
3231    fn collect_ty_pat(&mut self, pat: ast::Pat) -> PatId {
3232        let ptr = AstPtr::new(&pat);
3233        match pat {
3234            ast::Pat::NotNull(_) => self.alloc_pat(Pat::NotNull, ptr),
3235            ast::Pat::OrPat(pat) => {
3236                let pat = pat.pats().map(|pat| self.collect_ty_pat(pat)).collect();
3237                self.alloc_pat(Pat::Or(pat), ptr)
3238            }
3239            ast::Pat::RangePat(range_pat) => {
3240                let start = range_pat
3241                    .start()
3242                    .map(|pat| {
3243                        self.with_fresh_binding_expr_root(|this| this.lower_ty_pat_range_side(pat))
3244                    })
3245                    .unwrap_or_else(|| self.lower_ty_pat_range_end(self.lang_items().RangeMin));
3246                let end = range_pat
3247                    .end()
3248                    .map(|pat| match range_pat.op_kind() {
3249                        Some(ast::RangeOp::Inclusive) | None => self
3250                            .with_fresh_binding_expr_root(|this| this.lower_ty_pat_range_side(pat)),
3251                        Some(ast::RangeOp::Exclusive) => self.lower_excluded_range_end(pat),
3252                    })
3253                    .unwrap_or_else(|| self.lower_ty_pat_range_end(self.lang_items().RangeMax));
3254                self.alloc_pat(
3255                    Pat::Range {
3256                        start: Some(start),
3257                        end: Some(end),
3258                        range_type: ast::RangeOp::Inclusive,
3259                    },
3260                    ptr,
3261                )
3262            }
3263            ast::Pat::MacroPat(pat) => {
3264                self.collect_macro_pat_with(pat, |this, pat| this.collect_ty_pat(pat))
3265            }
3266            _ => {
3267                // FIXME: Emit an error.
3268                self.alloc_pat(Pat::Missing, ptr)
3269            }
3270        }
3271    }
3272
3273    fn lower_ty_pat_range_side(&mut self, pat: ast::Pat) -> ExprId {
3274        let ptr = AstPtr::new(&pat);
3275        match &pat {
3276            ast::Pat::LiteralPat(it) => {
3277                let Some((literal, _)) = pat_literal_to_hir(it) else { return self.missing_expr() };
3278                self.alloc_expr_from_pat(Expr::Literal(literal), ptr)
3279            }
3280            ast::Pat::PathPat(it) => {
3281                let path = it
3282                    .path()
3283                    .and_then(|path| self.lower_path(path, &mut Self::impl_trait_error_allocator));
3284                self.alloc_expr_from_pat(path.map(Expr::Path).unwrap_or(Expr::Missing), ptr)
3285            }
3286            ast::Pat::IdentPat(it) if it.is_simple_ident() => {
3287                let name = it.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
3288                self.alloc_expr_from_pat(Expr::Path(name.into()), ptr)
3289            }
3290            _ => self.missing_expr(), // FIXME: Emit an error.
3291        }
3292    }
3293
3294    /// When a range has no end specified (`1..` or `1..=`) or no start specified (`..5` or `..=5`),
3295    /// we instead use a constant of the MAX/MIN of the type.
3296    /// This way the type system does not have to handle the lack of a start/end.
3297    fn lower_ty_pat_range_end(&mut self, lang_item: Option<ConstId>) -> ExprId {
3298        self.with_fresh_binding_expr_root(|this| {
3299            this.alloc_expr_desugared(
3300                this.lang_path(lang_item).map(Expr::Path).unwrap_or(Expr::Missing),
3301            )
3302        })
3303    }
3304
3305    /// Lowers the range end of an exclusive range (`2..5`) to an inclusive range 2..=(5 - 1).
3306    /// This way the type system doesn't have to handle the distinction between inclusive/exclusive ranges.
3307    fn lower_excluded_range_end(&mut self, pat: ast::Pat) -> ExprId {
3308        self.with_fresh_binding_expr_root(|this| {
3309            let excluded_end = this.lower_ty_pat_range_side(pat);
3310            let range_sub_path =
3311                this.lang_path(this.lang_items().RangeSub).map(Expr::Path).unwrap_or(Expr::Missing);
3312            let range_sub_path = this.alloc_expr_desugared(range_sub_path);
3313            this.alloc_expr_desugared(Expr::Call {
3314                callee: range_sub_path,
3315                args: Box::new([excluded_end]),
3316            })
3317        })
3318    }
3319
3320    // endregion: patterns
3321
3322    /// Returns `false` (and emits diagnostics) when `owner` if `#[cfg]`d out, and `true` when
3323    /// not.
3324    fn check_cfg(&mut self, owner: &dyn ast::HasAttrs) -> bool {
3325        let enabled = self.expander.is_cfg_enabled(owner, self.cfg_options);
3326        match enabled {
3327            Ok(()) => true,
3328            Err(cfg) => {
3329                self.store.diagnostics.push(ExpressionStoreDiagnostics::InactiveCode {
3330                    node: self.expander.in_file(SyntaxNodePtr::new(owner.syntax())),
3331                    cfg,
3332                    opts: self.cfg_options.clone(),
3333                });
3334                false
3335            }
3336        }
3337    }
3338
3339    /// Used for `cfg!()`, conducting imprecise checks
3340    fn check_cfg_pred(&self, pred: Option<ast::CfgPredicate>) -> bool {
3341        let expr = pred.map_or(cfg::CfgExpr::Invalid, cfg::CfgExpr::parse_from_ast);
3342        self.cfg_options.check(&expr) != Some(false)
3343    }
3344
3345    fn add_definition_to_binding(&mut self, binding_id: BindingId, pat_id: PatId) {
3346        self.store.binding_definitions.entry(binding_id).or_default().push(pat_id);
3347    }
3348
3349    // region: labels
3350
3351    fn collect_label(&mut self, ast_label: ast::Label) -> LabelId {
3352        let label = Label {
3353            name: ast_label
3354                .lifetime()
3355                .as_ref()
3356                .map_or_else(Name::missing, |lt| Name::new_lifetime(lt.text())),
3357        };
3358        self.alloc_label(label, AstPtr::new(&ast_label))
3359    }
3360
3361    fn resolve_label(
3362        &self,
3363        lifetime: Option<ast::Lifetime>,
3364    ) -> Result<Option<LabelId>, ExpressionStoreDiagnostics> {
3365        let Some(lifetime) = lifetime else { return Ok(None) };
3366        let mut hygiene_id =
3367            self.expander.hygiene_for_range(self.db, lifetime.syntax().text_range());
3368        let mut hygiene_info = if hygiene_id.is_root() {
3369            None
3370        } else {
3371            hygiene_id.syntax_context().outer_expn(self.db).map(|expansion| {
3372                let expansion = hir_expand::MacroCallId::from(expansion).loc(self.db);
3373                (hygiene_id.syntax_context().parent(self.db), expansion.def)
3374            })
3375        };
3376        let name = Name::new_lifetime(lifetime.text());
3377
3378        for (rib_idx, rib) in self.label_ribs.iter().enumerate().rev() {
3379            match &rib.kind {
3380                RibKind::Normal(label_name, id, label_hygiene)
3381                    if *label_name == name && *label_hygiene == hygiene_id =>
3382                {
3383                    return if self.is_label_valid_from_rib(rib_idx) {
3384                        Ok(Some(*id))
3385                    } else {
3386                        Err(ExpressionStoreDiagnostics::UnreachableLabel {
3387                            name,
3388                            node: self.expander.in_file(AstPtr::new(&lifetime)),
3389                        })
3390                    };
3391                }
3392                RibKind::MacroDef(macro_id) => {
3393                    if let Some((parent_ctx, label_macro_id)) = hygiene_info
3394                        && label_macro_id == **macro_id
3395                    {
3396                        // A macro is allowed to refer to labels from before its declaration.
3397                        // Therefore, if we got to the rib of its declaration, give up its hygiene
3398                        // and use its parent expansion.
3399
3400                        hygiene_id = HygieneId::new(parent_ctx.opaque_and_semiopaque(self.db));
3401                        hygiene_info = parent_ctx.outer_expn(self.db).map(|expansion| {
3402                            let expansion = hir_expand::MacroCallId::from(expansion).loc(self.db);
3403                            (parent_ctx.parent(self.db), expansion.def)
3404                        });
3405                    }
3406                }
3407                _ => {}
3408            }
3409        }
3410
3411        Err(ExpressionStoreDiagnostics::UndeclaredLabel {
3412            name,
3413            node: self.expander.in_file(AstPtr::new(&lifetime)),
3414        })
3415    }
3416
3417    fn is_label_valid_from_rib(&self, rib_index: usize) -> bool {
3418        !self.label_ribs[rib_index + 1..].iter().any(|rib| rib.kind.is_label_barrier())
3419    }
3420
3421    fn pop_label_rib(&mut self) {
3422        // We need to pop all macro defs, plus one rib.
3423        while let Some(LabelRib { kind: RibKind::MacroDef(_) }) = self.label_ribs.pop() {
3424            // Do nothing.
3425        }
3426    }
3427
3428    fn with_label_rib<T>(&mut self, kind: RibKind, f: impl FnOnce(&mut Self) -> T) -> T {
3429        self.label_ribs.push(LabelRib::new(kind));
3430        let res = f(self);
3431        self.pop_label_rib();
3432        res
3433    }
3434
3435    fn with_labeled_rib<T>(
3436        &mut self,
3437        label: LabelId,
3438        hygiene: HygieneId,
3439        f: impl FnOnce(&mut Self) -> T,
3440    ) -> T {
3441        self.label_ribs.push(LabelRib::new(RibKind::Normal(
3442            self.store.labels[label].name.clone(),
3443            label,
3444            hygiene,
3445        )));
3446        let res = f(self);
3447        self.pop_label_rib();
3448        res
3449    }
3450
3451    fn with_opt_labeled_rib<T>(
3452        &mut self,
3453        label: Option<(HygieneId, LabelId)>,
3454        f: impl FnOnce(&mut Self) -> T,
3455    ) -> T {
3456        match label {
3457            None => f(self),
3458            Some((hygiene, label)) => self.with_labeled_rib(label, hygiene, f),
3459        }
3460    }
3461    // endregion: labels
3462
3463    /// While `expr` is a macro call, repeatedly expand it. If the end result is a string literal,
3464    /// return that, along with a boolean for whether it was a direct string literal, i.e. no macro
3465    /// calls were involved. In all other cases, return `None`.
3466    fn expand_macros_to_string(&mut self, expr: ast::Expr) -> Option<(ast::String, bool)> {
3467        let m = match expr {
3468            ast::Expr::MacroExpr(m) => m,
3469            ast::Expr::Literal(l) => {
3470                return match l.kind() {
3471                    ast::LiteralKind::String(s) => Some((s, true)),
3472                    _ => None,
3473                };
3474            }
3475            _ => return None,
3476        };
3477        let e = m.macro_call()?;
3478        let macro_ptr = AstPtr::new(&e);
3479        let (exp, _) = self.collect_macro_call(e, macro_ptr, true, |this, expansion| {
3480            expansion.and_then(|it| this.expand_macros_to_string(it))
3481        })?;
3482        Some((exp, false))
3483    }
3484
3485    fn lang_path(&self, lang: Option<impl Into<LangItemTarget>>) -> Option<Path> {
3486        Some(Path::LangItem(lang?.into(), None))
3487    }
3488
3489    fn lang_path_expr(&self, lang: Option<impl Into<LangItemTarget>>) -> Expr {
3490        self.lang_path(lang).map_or(Expr::Missing, Expr::Path)
3491    }
3492
3493    fn ty_rel_lang_path(
3494        &self,
3495        lang: Option<impl Into<LangItemTarget>>,
3496        relative_name: Symbol,
3497    ) -> Option<Path> {
3498        Some(Path::LangItem(lang?.into(), Some(Name::new_symbol_root(relative_name))))
3499    }
3500
3501    fn ty_rel_lang_path_expr(
3502        &self,
3503        lang: Option<impl Into<LangItemTarget>>,
3504        relative_name: Symbol,
3505    ) -> Expr {
3506        self.ty_rel_lang_path(lang, relative_name).map_or(Expr::Missing, Expr::Path)
3507    }
3508}
3509
3510fn pat_literal_to_hir(lit: &ast::LiteralPat) -> Option<(Literal, ast::Literal)> {
3511    let ast_lit = lit.literal()?;
3512    let mut hir_lit: Literal = ast_lit.kind().into();
3513    if lit.minus_token().is_some() {
3514        hir_lit = hir_lit.negate()?;
3515    }
3516    Some((hir_lit, ast_lit))
3517}
3518
3519impl<'db> ExprCollector<'db> {
3520    fn features(&self) -> &'db UnstableFeatures {
3521        self.def_map.features()
3522    }
3523
3524    fn with_fresh_binding_expr_root(&mut self, f: impl FnOnce(&mut Self) -> ExprId) -> ExprId {
3525        self.with_expr_root(|this| this.with_binding_owner(f))
3526    }
3527
3528    fn with_expr_root(&mut self, f: impl FnOnce(&mut Self) -> ExprId) -> ExprId {
3529        let inference_roots = self.store.inference_roots.take();
3530        let root = f(self);
3531        self.store.inference_roots = inference_roots;
3532
3533        if let Some(inference_roots) = &mut self.store.inference_roots {
3534            inference_roots.push(ExprRoot {
3535                root,
3536                exprs_end: end(&self.store.exprs),
3537                pats_end: end(&self.store.pats),
3538                bindings_end: end(&self.store.bindings),
3539            });
3540        }
3541
3542        return root;
3543
3544        fn end<T>(arena: &la_arena::Arena<T>) -> la_arena::Idx<T> {
3545            la_arena::Idx::from_raw(la_arena::RawIdx::from_u32(arena.len() as u32))
3546        }
3547    }
3548
3549    fn alloc_expr(&mut self, expr: Expr, ptr: ExprPtr) -> ExprId {
3550        let src = self.expander.in_file(ptr);
3551        let id = self.store.exprs.alloc(expr);
3552        self.store.expr_map_back.insert(id, src.map(AstPtr::wrap_left));
3553        self.store.expr_map.insert(src, id.into());
3554        id
3555    }
3556    // FIXME: desugared exprs don't have ptr, that's wrong and should be fixed.
3557    // Migrate to alloc_expr_desugared_with_ptr and then rename back
3558    fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId {
3559        self.store.exprs.alloc(expr)
3560    }
3561    fn alloc_expr_desugared_with_ptr(&mut self, expr: Expr, ptr: ExprPtr) -> ExprId {
3562        let src = self.expander.in_file(ptr);
3563        let id = self.store.exprs.alloc(expr);
3564        self.store.expr_map_back.insert(id, src.map(AstPtr::wrap_left));
3565        // We intentionally don't fill this as it could overwrite a non-desugared entry
3566        // self.store.expr_map.insert(src, id);
3567        id
3568    }
3569    fn missing_expr(&mut self) -> ExprId {
3570        self.alloc_expr_desugared(Expr::Missing)
3571    }
3572
3573    fn alloc_binding(
3574        &mut self,
3575        name: Name,
3576        mode: BindingAnnotation,
3577        hygiene: HygieneId,
3578    ) -> BindingId {
3579        let binding = self.store.bindings.alloc(Binding { name, mode, problems: None, hygiene });
3580        self.unowned_bindings.push(binding);
3581        binding
3582    }
3583
3584    fn alloc_pat_from_expr(&mut self, pat: Pat, ptr: ExprPtr) -> PatId {
3585        let src = self.expander.in_file(ptr);
3586        let id = self.store.pats.alloc(pat);
3587        self.store.expr_map.insert(src, id.into());
3588        self.store.pat_map_back.insert(id, src.map(AstPtr::wrap_left));
3589        id
3590    }
3591
3592    fn alloc_expr_from_pat(&mut self, expr: Expr, ptr: PatPtr) -> ExprId {
3593        let src = self.expander.in_file(ptr);
3594        let id = self.store.exprs.alloc(expr);
3595        self.store.pat_map.insert(src, id.into());
3596        self.store.expr_map_back.insert(id, src.map(AstPtr::wrap_right));
3597        id
3598    }
3599
3600    fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId {
3601        let src = self.expander.in_file(ptr);
3602        let id = self.store.pats.alloc(pat);
3603        self.store.pat_map_back.insert(id, src.map(AstPtr::wrap_right));
3604        self.store.pat_map.insert(src, id.into());
3605        id
3606    }
3607    // FIXME: desugared pats don't have ptr, that's wrong and should be fixed somehow.
3608    fn alloc_pat_desugared(&mut self, pat: Pat) -> PatId {
3609        self.store.pats.alloc(pat)
3610    }
3611    fn missing_pat(&mut self) -> PatId {
3612        self.store.pats.alloc(Pat::Missing)
3613    }
3614
3615    fn alloc_label(&mut self, label: Label, ptr: AstPtr<ast::Label>) -> LabelId {
3616        self.alloc_label_desugared(label, ptr.wrap_left())
3617    }
3618
3619    fn alloc_label_desugared(&mut self, label: Label, ptr: LabelPtr) -> LabelId {
3620        let src = self.expander.in_file(ptr);
3621        let id = self.store.labels.alloc(label);
3622        self.store.label_map_back.insert(id, src);
3623        self.store.label_map.insert(src, id);
3624        id
3625    }
3626
3627    fn is_lowering_awaitable_block(&self) -> &Awaitable {
3628        self.awaitable_context.as_ref().unwrap_or(&Awaitable::No("unknown"))
3629    }
3630
3631    fn with_awaitable_block<T>(
3632        &mut self,
3633        awaitable: Awaitable,
3634        f: impl FnOnce(&mut Self) -> T,
3635    ) -> T {
3636        let orig = self.awaitable_context.replace(awaitable);
3637        let res = f(self);
3638        self.awaitable_context = orig;
3639        res
3640    }
3641
3642    fn hygiene_id_for(&self, range: TextRange) -> HygieneId {
3643        self.expander.hygiene_for_range(self.db, range)
3644    }
3645
3646    fn with_lifetime_bound_scope<T>(
3647        &mut self,
3648        bound_scope: LifetimeBoundScope,
3649        f: impl FnOnce(&mut Self) -> T,
3650    ) -> T {
3651        let old = self.named_lifetime_store.lifetime_bound_scope.replace(bound_scope);
3652        let res = f(self);
3653        self.named_lifetime_store.lifetime_bound_scope = old;
3654        res
3655    }
3656
3657    fn for_path_type_projection<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T {
3658        if self.is_argument_lt_bound_scope() {
3659            let old = self.named_lifetime_store.lifetime_bound_scope.take();
3660            let res = f(self);
3661            self.named_lifetime_store.lifetime_bound_scope = old;
3662            res
3663        } else {
3664            f(self)
3665        }
3666    }
3667
3668    fn push_named_target_lifetime(&mut self, id: LifetimeRefId) {
3669        if let LifetimeRef::Named(name) = &self.store.lifetimes[id] {
3670            self.named_lifetime_store.push_named_lifetime(name.clone());
3671        }
3672    }
3673
3674    fn is_argument_lt_bound_scope(&mut self) -> bool {
3675        matches!(self.named_lifetime_store.lifetime_bound_scope, Some(LifetimeBoundScope::Argument))
3676    }
3677
3678    fn get_constrained_lifetimes_if_type_alias<'a>(
3679        &mut self,
3680        mod_path: &'a ModPath,
3681        generic_args: Option<&'a GenericArgs>,
3682    ) -> Option<impl Iterator<Item = LifetimeRefId> + use<'a, 'db>> {
3683        let generic_args = generic_args?;
3684        let r_path = self.def_map.resolve_path(
3685            self.local_def_map,
3686            self.db,
3687            self.module,
3688            mod_path,
3689            BuiltinShadowMode::Module,
3690            None,
3691        );
3692        let ModuleDefId::TypeAliasId(id) = r_path.0.take_types()? else { return None };
3693
3694        let constrained_lt_indices = get_constrained_lifetimes(self.db, id);
3695        let res = constrained_lt_indices.iter().filter_map(|&idx| {
3696            generic_args
3697                .args
3698                .iter()
3699                .filter_map(|arg| match arg {
3700                    &GenericArg::Lifetime(lt_ref) => Some(lt_ref),
3701                    GenericArg::Type(_) | GenericArg::Const(_) => None,
3702                })
3703                .nth(idx as usize)
3704        });
3705        return Some(res);
3706
3707        #[salsa::tracked(returns(deref), cycle_result = get_constrained_lifetimes_cycle_result)]
3708        fn get_constrained_lifetimes(
3709            db: &dyn SourceDatabase,
3710            type_alias_id: TypeAliasId,
3711        ) -> Box<[u32]> {
3712            let TypeAliasSignature { generic_params, store, ty, .. } =
3713                TypeAliasSignature::of(db, type_alias_id);
3714            let &Some(ty) = ty else { return Default::default() };
3715
3716            let mut visitor = Visitor {
3717                store,
3718                generic_params,
3719                parent: type_alias_id,
3720                constrained_lt_indices: Vec::new(),
3721            };
3722            store.visit_type_ref_children(ty, &mut visitor);
3723
3724            return visitor.constrained_lt_indices.into_boxed_slice();
3725
3726            struct Visitor<'a> {
3727                store: &'a ExpressionStore,
3728                generic_params: &'a GenericParams,
3729                parent: TypeAliasId,
3730                constrained_lt_indices: Vec<u32>,
3731            }
3732
3733            impl StoreVisitor for Visitor<'_> {
3734                fn on_lifetime(&mut self, lifetime: LifetimeRefId) {
3735                    if let LifetimeRef::Named(lifetime_name) = &self.store[lifetime]
3736                        && let Some(param_id) = self
3737                            .generic_params
3738                            .find_lifetime_by_name(lifetime_name, self.parent.into())
3739                    {
3740                        self.constrained_lt_indices.push(param_id.local_id.into_raw().into_u32());
3741                    }
3742                }
3743
3744                fn on_generic_args(&mut self, args: &GenericArgs) {
3745                    if !args.has_self_type {
3746                        crate::expr_store::visit_generic_args(self, args);
3747                    }
3748                }
3749            }
3750        }
3751
3752        fn get_constrained_lifetimes_cycle_result(
3753            _db: &dyn SourceDatabase,
3754            _: salsa::Id,
3755            _id: TypeAliasId,
3756        ) -> Box<[u32]> {
3757            Default::default()
3758        }
3759    }
3760}
3761
3762fn comma_follows_token(t: Option<syntax::SyntaxToken>) -> bool {
3763    (|| syntax::algo::skip_trivia_token(t?.next_token()?, syntax::Direction::Next))()
3764        .is_some_and(|it| it.kind() == syntax::T![,])
3765}
3766
3767/// This function find the AST fragment that corresponds to an `AssociatedTypeBinding` in the HIR.
3768pub fn hir_assoc_type_binding_to_ast(
3769    segment_args: &ast::GenericArgList,
3770    binding_idx: u32,
3771) -> Option<ast::AssocTypeArg> {
3772    segment_args
3773        .generic_args()
3774        .filter_map(|arg| match arg {
3775            ast::GenericArg::AssocTypeArg(it) => Some(it),
3776            _ => None,
3777        })
3778        .filter(|binding| binding.param_list().is_none() && binding.name_ref().is_some())
3779        .nth(binding_idx as usize)
3780}
3781
3782/// This function find the AST generic argument from the one in the HIR. Does not support the `Self` argument.
3783pub fn hir_generic_arg_to_ast(
3784    args: &ast::GenericArgList,
3785    arg_idx: u32,
3786    has_self_arg: bool,
3787) -> Option<ast::GenericArg> {
3788    args.generic_args()
3789        .filter(|arg| match arg {
3790            ast::GenericArg::AssocTypeArg(_) => false,
3791            ast::GenericArg::LifetimeArg(arg) => arg.lifetime().is_some(),
3792            ast::GenericArg::ConstArg(_) | ast::GenericArg::TypeArg(_) => true,
3793        })
3794        .nth(arg_idx as usize - has_self_arg as usize)
3795}