ra_ap_hir/semantics/
source_to_def.rs

1//! Maps *syntax* of various definitions to their semantic ids.
2//!
3//! This is a very interesting module, and, in some sense, can be considered the
4//! heart of the IDE parts of rust-analyzer.
5//!
6//! This module solves the following problem:
7//!
8//! > Given a piece of syntax, find the corresponding semantic definition (def).
9//!
10//! This problem is a part of more-or-less every IDE feature implemented. Every
11//! IDE functionality (like goto to definition), conceptually starts with a
12//! specific cursor position in a file. Starting with this text offset, we first
13//! figure out what syntactic construct are we at: is this a pattern, an
14//! expression, an item definition.
15//!
16//! Knowing only the syntax gives us relatively little info. For example,
17//! looking at the syntax of the function we can realize that it is a part of an
18//! `impl` block, but we won't be able to tell what trait function the current
19//! function overrides, and whether it does that correctly. For that, we need to
20//! go from [`ast::Fn`] to [`crate::Function`], and that's exactly what this
21//! module does.
22//!
23//! As syntax trees are values and don't know their place of origin/identity,
24//! this module also requires [`InFile`] wrappers to understand which specific
25//! real or macro-expanded file the tree comes from.
26//!
27//! The actual algorithm to resolve syntax to def is curious in two aspects:
28//!
29//! * It is recursive
30//! * It uses the inverse algorithm (what is the syntax for this def?)
31//!
32//! Specifically, the algorithm goes like this:
33//!
34//! 1. Find the syntactic container for the syntax. For example, field's
35//!    container is the struct, and structs container is a module.
36//! 2. Recursively get the def corresponding to container.
37//! 3. Ask the container def for all child defs. These child defs contain
38//!    the answer and answer's siblings.
39//! 4. For each child def, ask for it's source.
40//! 5. The child def whose source is the syntax node we've started with
41//!    is the answer.
42//!
43//! It's interesting that both Roslyn and Kotlin contain very similar code
44//! shape.
45//!
46//! Let's take a look at Roslyn:
47//!
48//!   <https://github.com/dotnet/roslyn/blob/36a0c338d6621cc5fe34b79d414074a95a6a489c/src/Compilers/CSharp/Portable/Compilation/SyntaxTreeSemanticModel.cs#L1403-L1429>
49//!   <https://sourceroslyn.io/#Microsoft.CodeAnalysis.CSharp/Compilation/SyntaxTreeSemanticModel.cs,1403>
50//!
51//! The `GetDeclaredType` takes `Syntax` as input, and returns `Symbol` as
52//! output. First, it retrieves a `Symbol` for parent `Syntax`:
53//!
54//! * <https://sourceroslyn.io/#Microsoft.CodeAnalysis.CSharp/Compilation/SyntaxTreeSemanticModel.cs,1423>
55//!
56//! Then, it iterates parent symbol's children, looking for one which has the
57//! same text span as the original node:
58//!
59//!   <https://sourceroslyn.io/#Microsoft.CodeAnalysis.CSharp/Compilation/SyntaxTreeSemanticModel.cs,1786>
60//!
61//! Now, let's look at Kotlin:
62//!
63//!   <https://github.com/JetBrains/kotlin/blob/a288b8b00e4754a1872b164999c6d3f3b8c8994a/idea/idea-frontend-fir/idea-fir-low-level-api/src/org/jetbrains/kotlin/idea/fir/low/level/api/FirModuleResolveStateImpl.kt#L93-L125>
64//!
65//! This function starts with a syntax node (`KtExpression` is syntax, like all
66//! `Kt` nodes), and returns a def. It uses
67//! `getNonLocalContainingOrThisDeclaration` to get syntactic container for a
68//! current node. Then, `findSourceNonLocalFirDeclaration` gets `Fir` for this
69//! parent. Finally, `findElementIn` function traverses `Fir` children to find
70//! one with the same source we originally started with.
71//!
72//! One question is left though -- where does the recursion stops? This happens
73//! when we get to the file syntax node, which doesn't have a syntactic parent.
74//! In that case, we loop through all the crates that might contain this file
75//! and look for a module whose source is the given file.
76//!
77//! Note that the logic in this module is somewhat fundamentally imprecise --
78//! due to conditional compilation and `#[path]` attributes, there's no
79//! injective mapping from syntax nodes to defs. This is not an edge case --
80//! more or less every item in a `lib.rs` is a part of two distinct crates: a
81//! library with `--cfg test` and a library without.
82//!
83//! At the moment, we don't really handle this well and return the first answer
84//! that works. Ideally, we should first let the caller to pick a specific
85//! active crate for a given position, and then provide an API to resolve all
86//! syntax nodes against this specific crate.
87
88use either::Either;
89use hir_def::{
90    AdtId, BlockId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, ExternBlockId,
91    ExternCrateId, FieldId, FunctionId, GenericDefId, GenericParamId, ImplId, LifetimeParamId,
92    Lookup, MacroId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId,
93    UseId, VariantId,
94    dyn_map::{
95        DynMap,
96        keys::{self, Key},
97    },
98    hir::{BindingId, Expr, LabelId},
99    nameres::{block_def_map, crate_def_map},
100};
101use hir_expand::{
102    EditionedFileId, ExpansionInfo, HirFileId, InMacroFile, MacroCallId, attrs::AttrId,
103    name::AsName,
104};
105use rustc_hash::FxHashMap;
106use smallvec::SmallVec;
107use span::FileId;
108use stdx::impl_from;
109use syntax::{
110    AstNode, AstPtr, SyntaxNode,
111    ast::{self, HasAttrs, HasName},
112};
113use tt::TextRange;
114
115use crate::{InFile, InlineAsmOperand, db::HirDatabase, semantics::child_by_source::ChildBySource};
116
117#[derive(Default)]
118pub(super) struct SourceToDefCache {
119    pub(super) dynmap_cache: FxHashMap<(ChildContainer, HirFileId), DynMap>,
120    expansion_info_cache: FxHashMap<MacroCallId, ExpansionInfo>,
121    pub(super) file_to_def_cache: FxHashMap<FileId, SmallVec<[ModuleId; 1]>>,
122    pub(super) included_file_cache: FxHashMap<EditionedFileId, Option<MacroCallId>>,
123    /// Rootnode to HirFileId cache
124    pub(super) root_to_file_cache: FxHashMap<SyntaxNode, HirFileId>,
125}
126
127impl SourceToDefCache {
128    pub(super) fn cache(
129        root_to_file_cache: &mut FxHashMap<SyntaxNode, HirFileId>,
130        root_node: SyntaxNode,
131        file_id: HirFileId,
132    ) {
133        assert!(root_node.parent().is_none());
134        let prev = root_to_file_cache.insert(root_node, file_id);
135        assert!(prev.is_none() || prev == Some(file_id));
136    }
137
138    pub(super) fn get_or_insert_include_for(
139        &mut self,
140        db: &dyn HirDatabase,
141        file: EditionedFileId,
142    ) -> Option<MacroCallId> {
143        if let Some(&m) = self.included_file_cache.get(&file) {
144            return m;
145        }
146        self.included_file_cache.insert(file, None);
147        for &crate_id in db.relevant_crates(file.file_id(db)).iter() {
148            db.include_macro_invoc(crate_id).iter().for_each(|&(macro_call_id, file_id)| {
149                self.included_file_cache.insert(file_id, Some(macro_call_id));
150            });
151        }
152        self.included_file_cache.get(&file).copied().flatten()
153    }
154
155    pub(super) fn get_or_insert_expansion(
156        &mut self,
157        db: &dyn HirDatabase,
158        macro_file: MacroCallId,
159    ) -> &ExpansionInfo {
160        self.expansion_info_cache.entry(macro_file).or_insert_with(|| {
161            let exp_info = macro_file.expansion_info(db);
162
163            let InMacroFile { file_id, value } = exp_info.expanded();
164            Self::cache(&mut self.root_to_file_cache, value, file_id.into());
165
166            exp_info
167        })
168    }
169}
170
171pub(super) struct SourceToDefCtx<'db, 'cache> {
172    pub(super) db: &'db dyn HirDatabase,
173    pub(super) cache: &'cache mut SourceToDefCache,
174}
175
176impl SourceToDefCtx<'_, '_> {
177    pub(super) fn file_to_def(&mut self, file: FileId) -> &SmallVec<[ModuleId; 1]> {
178        let _p = tracing::info_span!("SourceToDefCtx::file_to_def").entered();
179        self.cache.file_to_def_cache.entry(file).or_insert_with(|| {
180            let mut mods = SmallVec::new();
181
182            for &crate_id in self.db.relevant_crates(file).iter() {
183                // Note: `mod` declarations in block modules cannot be supported here
184                let crate_def_map = crate_def_map(self.db, crate_id);
185                let n_mods = mods.len();
186                let modules = |file| {
187                    crate_def_map
188                        .modules_for_file(self.db, file)
189                        .map(|local_id| crate_def_map.module_id(local_id))
190                };
191                mods.extend(modules(file));
192                if mods.len() == n_mods {
193                    mods.extend(
194                        self.db
195                            .include_macro_invoc(crate_id)
196                            .iter()
197                            .filter(|&&(_, file_id)| file_id.file_id(self.db) == file)
198                            .flat_map(|&(macro_call_id, file_id)| {
199                                self.cache.included_file_cache.insert(file_id, Some(macro_call_id));
200                                modules(
201                                    macro_call_id
202                                        .lookup(self.db)
203                                        .kind
204                                        .file_id()
205                                        .original_file(self.db)
206                                        .file_id(self.db),
207                                )
208                            }),
209                    );
210                }
211            }
212            if mods.is_empty() {
213                // FIXME: detached file
214            }
215            mods
216        })
217    }
218
219    pub(super) fn module_to_def(&mut self, src: InFile<&ast::Module>) -> Option<ModuleId> {
220        let _p = tracing::info_span!("module_to_def").entered();
221        let parent_declaration = self
222            .parent_ancestors_with_macros(src.syntax_ref(), |_, ancestor, _| {
223                ancestor.map(Either::<ast::Module, ast::BlockExpr>::cast).transpose()
224            })
225            .map(|it| it.transpose());
226
227        let parent_module = match parent_declaration {
228            Some(Either::Right(parent_block)) => self
229                .block_to_def(parent_block.as_ref())
230                .map(|block| block_def_map(self.db, block).root_module_id()),
231            Some(Either::Left(parent_declaration)) => {
232                self.module_to_def(parent_declaration.as_ref())
233            }
234            None => {
235                let file_id = src.file_id.original_file(self.db);
236                self.file_to_def(file_id.file_id(self.db)).first().copied()
237            }
238        }?;
239
240        let child_name = src.value.name()?.as_name();
241        let def_map = parent_module.def_map(self.db);
242        let &child_id = def_map[parent_module.local_id].children.get(&child_name)?;
243        Some(def_map.module_id(child_id))
244    }
245
246    pub(super) fn source_file_to_def(&mut self, src: InFile<&ast::SourceFile>) -> Option<ModuleId> {
247        let _p = tracing::info_span!("source_file_to_def").entered();
248        let file_id = src.file_id.original_file(self.db);
249        self.file_to_def(file_id.file_id(self.db)).first().copied()
250    }
251
252    pub(super) fn trait_to_def(&mut self, src: InFile<&ast::Trait>) -> Option<TraitId> {
253        self.to_def(src, keys::TRAIT)
254    }
255    pub(super) fn impl_to_def(&mut self, src: InFile<&ast::Impl>) -> Option<ImplId> {
256        self.to_def(src, keys::IMPL)
257    }
258    pub(super) fn fn_to_def(&mut self, src: InFile<&ast::Fn>) -> Option<FunctionId> {
259        self.to_def(src, keys::FUNCTION)
260    }
261    pub(super) fn struct_to_def(&mut self, src: InFile<&ast::Struct>) -> Option<StructId> {
262        self.to_def(src, keys::STRUCT)
263    }
264    pub(super) fn enum_to_def(&mut self, src: InFile<&ast::Enum>) -> Option<EnumId> {
265        self.to_def(src, keys::ENUM)
266    }
267    pub(super) fn union_to_def(&mut self, src: InFile<&ast::Union>) -> Option<UnionId> {
268        self.to_def(src, keys::UNION)
269    }
270    pub(super) fn static_to_def(&mut self, src: InFile<&ast::Static>) -> Option<StaticId> {
271        self.to_def(src, keys::STATIC)
272    }
273    pub(super) fn const_to_def(&mut self, src: InFile<&ast::Const>) -> Option<ConstId> {
274        self.to_def(src, keys::CONST)
275    }
276    pub(super) fn type_alias_to_def(
277        &mut self,
278        src: InFile<&ast::TypeAlias>,
279    ) -> Option<TypeAliasId> {
280        self.to_def(src, keys::TYPE_ALIAS)
281    }
282    pub(super) fn record_field_to_def(
283        &mut self,
284        src: InFile<&ast::RecordField>,
285    ) -> Option<FieldId> {
286        self.to_def(src, keys::RECORD_FIELD)
287    }
288    pub(super) fn tuple_field_to_def(&mut self, src: InFile<&ast::TupleField>) -> Option<FieldId> {
289        self.to_def(src, keys::TUPLE_FIELD)
290    }
291    pub(super) fn block_to_def(&mut self, src: InFile<&ast::BlockExpr>) -> Option<BlockId> {
292        self.to_def(src, keys::BLOCK)
293    }
294    pub(super) fn enum_variant_to_def(
295        &mut self,
296        src: InFile<&ast::Variant>,
297    ) -> Option<EnumVariantId> {
298        self.to_def(src, keys::ENUM_VARIANT)
299    }
300    pub(super) fn extern_crate_to_def(
301        &mut self,
302        src: InFile<&ast::ExternCrate>,
303    ) -> Option<ExternCrateId> {
304        self.to_def(src, keys::EXTERN_CRATE)
305    }
306    pub(super) fn extern_block_to_def(
307        &mut self,
308        src: InFile<&ast::ExternBlock>,
309    ) -> Option<ExternBlockId> {
310        self.to_def(src, keys::EXTERN_BLOCK)
311    }
312    #[allow(dead_code)]
313    pub(super) fn use_to_def(&mut self, src: InFile<&ast::Use>) -> Option<UseId> {
314        self.to_def(src, keys::USE)
315    }
316    pub(super) fn adt_to_def(
317        &mut self,
318        InFile { file_id, value }: InFile<&ast::Adt>,
319    ) -> Option<AdtId> {
320        match value {
321            ast::Adt::Enum(it) => self.enum_to_def(InFile::new(file_id, it)).map(AdtId::EnumId),
322            ast::Adt::Struct(it) => {
323                self.struct_to_def(InFile::new(file_id, it)).map(AdtId::StructId)
324            }
325            ast::Adt::Union(it) => self.union_to_def(InFile::new(file_id, it)).map(AdtId::UnionId),
326        }
327    }
328
329    pub(super) fn asm_operand_to_def(
330        &mut self,
331        src: InFile<&ast::AsmOperandNamed>,
332    ) -> Option<InlineAsmOperand> {
333        let asm = src.value.syntax().parent().and_then(ast::AsmExpr::cast)?;
334        let index = asm
335            .asm_pieces()
336            .filter_map(|it| match it {
337                ast::AsmPiece::AsmOperandNamed(it) => Some(it),
338                _ => None,
339            })
340            .position(|it| it == *src.value)?;
341        let container = self.find_pat_or_label_container(src.syntax_ref())?;
342        let source_map = self.db.body_with_source_map(container).1;
343        let expr = source_map.node_expr(src.with_value(&ast::Expr::AsmExpr(asm)))?.as_expr()?;
344        Some(InlineAsmOperand { owner: container, expr, index })
345    }
346
347    pub(super) fn bind_pat_to_def(
348        &mut self,
349        src: InFile<&ast::IdentPat>,
350    ) -> Option<(DefWithBodyId, BindingId)> {
351        let container = self.find_pat_or_label_container(src.syntax_ref())?;
352        let (body, source_map) = self.db.body_with_source_map(container);
353        let src = src.cloned().map(ast::Pat::from);
354        let pat_id = source_map.node_pat(src.as_ref())?;
355        // the pattern could resolve to a constant, verify that this is not the case
356        if let crate::Pat::Bind { id, .. } = body[pat_id.as_pat()?] {
357            Some((container, id))
358        } else {
359            None
360        }
361    }
362    pub(super) fn self_param_to_def(
363        &mut self,
364        src: InFile<&ast::SelfParam>,
365    ) -> Option<(DefWithBodyId, BindingId)> {
366        let container = self.find_pat_or_label_container(src.syntax_ref())?;
367        let body = self.db.body(container);
368        Some((container, body.self_param?))
369    }
370    pub(super) fn label_to_def(
371        &mut self,
372        src: InFile<&ast::Label>,
373    ) -> Option<(DefWithBodyId, LabelId)> {
374        let container = self.find_pat_or_label_container(src.syntax_ref())?;
375        let source_map = self.db.body_with_source_map(container).1;
376
377        let label_id = source_map.node_label(src)?;
378        Some((container, label_id))
379    }
380
381    pub(super) fn label_ref_to_def(
382        &mut self,
383        src: InFile<&ast::Lifetime>,
384    ) -> Option<(DefWithBodyId, LabelId)> {
385        let break_or_continue = ast::Expr::cast(src.value.syntax().parent()?)?;
386        let container = self.find_pat_or_label_container(src.syntax_ref())?;
387        let (body, source_map) = self.db.body_with_source_map(container);
388        let break_or_continue =
389            source_map.node_expr(src.with_value(&break_or_continue))?.as_expr()?;
390        let (Expr::Break { label, .. } | Expr::Continue { label }) = body[break_or_continue] else {
391            return None;
392        };
393        Some((container, label?))
394    }
395
396    /// (AttrId, derive attribute call id, derive call ids)
397    pub(super) fn attr_to_derive_macro_call(
398        &mut self,
399        item: InFile<&ast::Adt>,
400        src: InFile<ast::Attr>,
401    ) -> Option<(AttrId, MacroCallId, &[Option<MacroCallId>])> {
402        let map = self.dyn_map(item)?;
403        map[keys::DERIVE_MACRO_CALL]
404            .get(&AstPtr::new(&src.value))
405            .map(|&(attr_id, call_id, ref ids)| (attr_id, call_id, &**ids))
406    }
407
408    // FIXME: Make this more fine grained! This should be a `adt_has_derives`!
409    pub(super) fn file_of_adt_has_derives(&mut self, adt: InFile<&ast::Adt>) -> bool {
410        self.dyn_map(adt).as_ref().is_some_and(|map| !map[keys::DERIVE_MACRO_CALL].is_empty())
411    }
412
413    pub(super) fn derive_macro_calls<'slf>(
414        &'slf mut self,
415        adt: InFile<&ast::Adt>,
416    ) -> Option<impl Iterator<Item = (AttrId, MacroCallId, &'slf [Option<MacroCallId>])> + use<'slf>>
417    {
418        self.dyn_map(adt).as_ref().map(|&map| {
419            let dyn_map = &map[keys::DERIVE_MACRO_CALL];
420            adt.value
421                .attrs()
422                .filter_map(move |attr| dyn_map.get(&AstPtr::new(&attr)))
423                .map(|&(attr_id, call_id, ref ids)| (attr_id, call_id, &**ids))
424        })
425    }
426
427    fn to_def<Ast: AstNode + 'static, ID: Copy + 'static>(
428        &mut self,
429        src: InFile<&Ast>,
430        key: Key<Ast, ID>,
431    ) -> Option<ID> {
432        self.dyn_map(src)?[key].get(&AstPtr::new(src.value)).copied()
433    }
434
435    fn dyn_map<Ast: AstNode + 'static>(&mut self, src: InFile<&Ast>) -> Option<&DynMap> {
436        let container = self.find_container(src.map(|it| it.syntax()))?;
437        Some(self.cache_for(container, src.file_id))
438    }
439
440    fn cache_for(&mut self, container: ChildContainer, file_id: HirFileId) -> &DynMap {
441        let db = self.db;
442        self.cache
443            .dynmap_cache
444            .entry((container, file_id))
445            .or_insert_with(|| container.child_by_source(db, file_id))
446    }
447
448    pub(super) fn item_to_macro_call(&mut self, src: InFile<&ast::Item>) -> Option<MacroCallId> {
449        self.to_def(src, keys::ATTR_MACRO_CALL)
450    }
451
452    pub(super) fn macro_call_to_macro_call(
453        &mut self,
454        src: InFile<&ast::MacroCall>,
455    ) -> Option<MacroCallId> {
456        self.to_def(src, keys::MACRO_CALL)
457    }
458
459    pub(super) fn type_param_to_def(
460        &mut self,
461        src: InFile<&ast::TypeParam>,
462    ) -> Option<TypeParamId> {
463        let container: ChildContainer = self.find_generic_param_container(src.syntax_ref())?.into();
464        let dyn_map = self.cache_for(container, src.file_id);
465        dyn_map[keys::TYPE_PARAM]
466            .get(&AstPtr::new(src.value))
467            .copied()
468            .map(TypeParamId::from_unchecked)
469    }
470
471    pub(super) fn lifetime_param_to_def(
472        &mut self,
473        src: InFile<&ast::LifetimeParam>,
474    ) -> Option<LifetimeParamId> {
475        let container: ChildContainer = self.find_generic_param_container(src.syntax_ref())?.into();
476        let dyn_map = self.cache_for(container, src.file_id);
477        dyn_map[keys::LIFETIME_PARAM].get(&AstPtr::new(src.value)).copied()
478    }
479
480    pub(super) fn const_param_to_def(
481        &mut self,
482        src: InFile<&ast::ConstParam>,
483    ) -> Option<ConstParamId> {
484        let container: ChildContainer = self.find_generic_param_container(src.syntax_ref())?.into();
485        let dyn_map = self.cache_for(container, src.file_id);
486        dyn_map[keys::CONST_PARAM]
487            .get(&AstPtr::new(src.value))
488            .copied()
489            .map(ConstParamId::from_unchecked)
490    }
491
492    pub(super) fn generic_param_to_def(
493        &mut self,
494        InFile { file_id, value }: InFile<&ast::GenericParam>,
495    ) -> Option<GenericParamId> {
496        match value {
497            ast::GenericParam::ConstParam(it) => {
498                self.const_param_to_def(InFile::new(file_id, it)).map(GenericParamId::ConstParamId)
499            }
500            ast::GenericParam::LifetimeParam(it) => self
501                .lifetime_param_to_def(InFile::new(file_id, it))
502                .map(GenericParamId::LifetimeParamId),
503            ast::GenericParam::TypeParam(it) => {
504                self.type_param_to_def(InFile::new(file_id, it)).map(GenericParamId::TypeParamId)
505            }
506        }
507    }
508
509    pub(super) fn macro_to_def(&mut self, src: InFile<&ast::Macro>) -> Option<MacroId> {
510        self.dyn_map(src).and_then(|it| match src.value {
511            ast::Macro::MacroRules(value) => {
512                it[keys::MACRO_RULES].get(&AstPtr::new(value)).copied().map(MacroId::from)
513            }
514            ast::Macro::MacroDef(value) => {
515                it[keys::MACRO2].get(&AstPtr::new(value)).copied().map(MacroId::from)
516            }
517        })
518    }
519
520    pub(super) fn proc_macro_to_def(&mut self, src: InFile<&ast::Fn>) -> Option<MacroId> {
521        self.dyn_map(src).and_then(|it| {
522            it[keys::PROC_MACRO].get(&AstPtr::new(src.value)).copied().map(MacroId::from)
523        })
524    }
525
526    pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> {
527        let _p = tracing::info_span!("find_container").entered();
528        let def = self.parent_ancestors_with_macros(src, |this, container, child| {
529            this.container_to_def(container, child)
530        });
531        if let Some(def) = def {
532            return Some(def);
533        }
534
535        let def = self
536            .file_to_def(src.file_id.original_file(self.db).file_id(self.db))
537            .first()
538            .copied()?;
539        Some(def.into())
540    }
541
542    fn find_generic_param_container(&mut self, src: InFile<&SyntaxNode>) -> Option<GenericDefId> {
543        self.parent_ancestors_with_macros(src, |this, InFile { file_id, value }, _| {
544            let item = ast::Item::cast(value)?;
545            match &item {
546                ast::Item::Fn(it) => this.fn_to_def(InFile::new(file_id, it)).map(Into::into),
547                ast::Item::Struct(it) => {
548                    this.struct_to_def(InFile::new(file_id, it)).map(Into::into)
549                }
550                ast::Item::Enum(it) => this.enum_to_def(InFile::new(file_id, it)).map(Into::into),
551                ast::Item::Trait(it) => this.trait_to_def(InFile::new(file_id, it)).map(Into::into),
552                ast::Item::TypeAlias(it) => {
553                    this.type_alias_to_def(InFile::new(file_id, it)).map(Into::into)
554                }
555                ast::Item::Impl(it) => this.impl_to_def(InFile::new(file_id, it)).map(Into::into),
556                _ => None,
557            }
558        })
559    }
560
561    // FIXME: Remove this when we do inference in signatures
562    fn find_pat_or_label_container(&mut self, src: InFile<&SyntaxNode>) -> Option<DefWithBodyId> {
563        self.parent_ancestors_with_macros(src, |this, InFile { file_id, value }, _| {
564            let item = match ast::Item::cast(value.clone()) {
565                Some(it) => it,
566                None => {
567                    let variant = ast::Variant::cast(value)?;
568                    return this
569                        .enum_variant_to_def(InFile::new(file_id, &variant))
570                        .map(Into::into);
571                }
572            };
573            match &item {
574                ast::Item::Fn(it) => this.fn_to_def(InFile::new(file_id, it)).map(Into::into),
575                ast::Item::Const(it) => this.const_to_def(InFile::new(file_id, it)).map(Into::into),
576                ast::Item::Static(it) => {
577                    this.static_to_def(InFile::new(file_id, it)).map(Into::into)
578                }
579                _ => None,
580            }
581        })
582    }
583
584    /// Skips the attributed item that caused the macro invocation we are climbing up
585    fn parent_ancestors_with_macros<T>(
586        &mut self,
587        node: InFile<&SyntaxNode>,
588        mut cb: impl FnMut(
589            &mut Self,
590            /*parent: */ InFile<SyntaxNode>,
591            /*child: */ &SyntaxNode,
592        ) -> Option<T>,
593    ) -> Option<T> {
594        let parent = |this: &mut Self, node: InFile<&SyntaxNode>| match node.value.parent() {
595            Some(parent) => Some(node.with_value(parent)),
596            None => {
597                let macro_file = node.file_id.macro_file()?;
598                let expansion_info = this.cache.get_or_insert_expansion(this.db, macro_file);
599                expansion_info.arg().map(|node| node?.parent()).transpose()
600            }
601        };
602        let mut deepest_child_in_same_file = node.cloned();
603        let mut node = node.cloned();
604        while let Some(parent) = parent(self, node.as_ref()) {
605            if parent.file_id != node.file_id {
606                deepest_child_in_same_file = parent.clone();
607            }
608            if let Some(res) = cb(self, parent.clone(), &deepest_child_in_same_file.value) {
609                return Some(res);
610            }
611            node = parent;
612        }
613        None
614    }
615
616    fn container_to_def(
617        &mut self,
618        container: InFile<SyntaxNode>,
619        child: &SyntaxNode,
620    ) -> Option<ChildContainer> {
621        let cont = if let Some(item) = ast::Item::cast(container.value.clone()) {
622            match &item {
623                ast::Item::Module(it) => self.module_to_def(container.with_value(it))?.into(),
624                ast::Item::Trait(it) => self.trait_to_def(container.with_value(it))?.into(),
625                ast::Item::Impl(it) => self.impl_to_def(container.with_value(it))?.into(),
626                ast::Item::Enum(it) => self.enum_to_def(container.with_value(it))?.into(),
627                ast::Item::TypeAlias(it) => ChildContainer::GenericDefId(
628                    self.type_alias_to_def(container.with_value(it))?.into(),
629                ),
630                ast::Item::Struct(it) => {
631                    let def = self.struct_to_def(container.with_value(it))?;
632                    let is_in_body = it.field_list().is_some_and(|it| {
633                        it.syntax().text_range().contains(child.text_range().start())
634                    });
635                    if is_in_body {
636                        VariantId::from(def).into()
637                    } else {
638                        ChildContainer::GenericDefId(def.into())
639                    }
640                }
641                ast::Item::Union(it) => {
642                    let def = self.union_to_def(container.with_value(it))?;
643                    let is_in_body = it.record_field_list().is_some_and(|it| {
644                        it.syntax().text_range().contains(child.text_range().start())
645                    });
646                    if is_in_body {
647                        VariantId::from(def).into()
648                    } else {
649                        ChildContainer::GenericDefId(def.into())
650                    }
651                }
652                ast::Item::Fn(it) => {
653                    let def = self.fn_to_def(container.with_value(it))?;
654                    let child_offset = child.text_range().start();
655                    let is_in_body =
656                        it.body().is_some_and(|it| it.syntax().text_range().contains(child_offset));
657                    let in_param_pat = || {
658                        it.param_list().is_some_and(|it| {
659                            it.self_param()
660                                .and_then(|it| {
661                                    Some(TextRange::new(
662                                        it.syntax().text_range().start(),
663                                        it.name()?.syntax().text_range().end(),
664                                    ))
665                                })
666                                .is_some_and(|r| r.contains_inclusive(child_offset))
667                                || it
668                                    .params()
669                                    .filter_map(|it| it.pat())
670                                    .any(|it| it.syntax().text_range().contains(child_offset))
671                        })
672                    };
673                    if is_in_body || in_param_pat() {
674                        DefWithBodyId::from(def).into()
675                    } else {
676                        ChildContainer::GenericDefId(def.into())
677                    }
678                }
679                ast::Item::Static(it) => {
680                    let def = self.static_to_def(container.with_value(it))?;
681                    let is_in_body = it.body().is_some_and(|it| {
682                        it.syntax().text_range().contains(child.text_range().start())
683                    });
684                    if is_in_body {
685                        DefWithBodyId::from(def).into()
686                    } else {
687                        ChildContainer::GenericDefId(def.into())
688                    }
689                }
690                ast::Item::Const(it) => {
691                    let def = self.const_to_def(container.with_value(it))?;
692                    let is_in_body = it.body().is_some_and(|it| {
693                        it.syntax().text_range().contains(child.text_range().start())
694                    });
695                    if is_in_body {
696                        DefWithBodyId::from(def).into()
697                    } else {
698                        ChildContainer::GenericDefId(def.into())
699                    }
700                }
701                _ => return None,
702            }
703        } else if let Some(it) = ast::Variant::cast(container.value.clone()) {
704            let def = self.enum_variant_to_def(InFile::new(container.file_id, &it))?;
705            let is_in_body =
706                it.eq_token().is_some_and(|it| it.text_range().end() < child.text_range().start());
707            if is_in_body { DefWithBodyId::from(def).into() } else { VariantId::from(def).into() }
708        } else {
709            let it = match Either::<ast::Pat, ast::Name>::cast(container.value)? {
710                Either::Left(it) => ast::Param::cast(it.syntax().parent()?)?.syntax().parent(),
711                Either::Right(it) => ast::SelfParam::cast(it.syntax().parent()?)?.syntax().parent(),
712            }
713            .and_then(ast::ParamList::cast)?
714            .syntax()
715            .parent()
716            .and_then(ast::Fn::cast)?;
717            let def = self.fn_to_def(InFile::new(container.file_id, &it))?;
718            DefWithBodyId::from(def).into()
719        };
720        Some(cont)
721    }
722}
723
724#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
725pub(crate) enum ChildContainer {
726    DefWithBodyId(DefWithBodyId),
727    ModuleId(ModuleId),
728    TraitId(TraitId),
729    ImplId(ImplId),
730    EnumId(EnumId),
731    VariantId(VariantId),
732    /// XXX: this might be the same def as, for example an `EnumId`. However,
733    /// here the children are generic parameters, and not, eg enum variants.
734    GenericDefId(GenericDefId),
735}
736impl_from! {
737    DefWithBodyId,
738    ModuleId,
739    TraitId,
740    ImplId,
741    EnumId,
742    VariantId,
743    GenericDefId
744    for ChildContainer
745}
746
747impl ChildContainer {
748    fn child_by_source(self, db: &dyn HirDatabase, file_id: HirFileId) -> DynMap {
749        let _p = tracing::info_span!("ChildContainer::child_by_source").entered();
750        match self {
751            ChildContainer::DefWithBodyId(it) => it.child_by_source(db, file_id),
752            ChildContainer::ModuleId(it) => it.child_by_source(db, file_id),
753            ChildContainer::TraitId(it) => it.child_by_source(db, file_id),
754            ChildContainer::ImplId(it) => it.child_by_source(db, file_id),
755            ChildContainer::EnumId(it) => it.child_by_source(db, file_id),
756            ChildContainer::VariantId(it) => it.child_by_source(db, file_id),
757            ChildContainer::GenericDefId(it) => it.child_by_source(db, file_id),
758        }
759    }
760}