From 2e3c156b0e560c4ef7075955883f30e517bdd075 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 15 Mar 2021 14:51:20 +0100 Subject: Return multiple modules in `parent_module` --- crates/hir/src/semantics/source_to_def.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'crates/hir/src/semantics/source_to_def.rs') diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs index 6c612ee86..e9d820140 100644 --- a/crates/hir/src/semantics/source_to_def.rs +++ b/crates/hir/src/semantics/source_to_def.rs @@ -12,6 +12,7 @@ use hir_def::{ }; use hir_expand::{name::AsName, AstId, MacroDefKind}; use rustc_hash::FxHashMap; +use smallvec::SmallVec; use stdx::impl_from; use syntax::{ ast::{self, NameOwner}, @@ -28,14 +29,19 @@ pub(super) struct SourceToDefCtx<'a, 'b> { } impl SourceToDefCtx<'_, '_> { - pub(super) fn file_to_def(&mut self, file: FileId) -> Option { + pub(super) fn file_to_def(&mut self, file: FileId) -> SmallVec<[ModuleId; 1]> { let _p = profile::span("SourceBinder::to_module_def"); - self.db.relevant_crates(file).iter().find_map(|&crate_id| { + let mut mods = SmallVec::new(); + for &crate_id in self.db.relevant_crates(file).iter() { // FIXME: inner items let crate_def_map = self.db.crate_def_map(crate_id); - let local_id = crate_def_map.modules_for_file(file).next()?; - Some(crate_def_map.module_id(local_id)) - }) + mods.extend( + crate_def_map + .modules_for_file(file) + .map(|local_id| crate_def_map.module_id(local_id)), + ) + } + mods } pub(super) fn module_to_def(&mut self, src: InFile) -> Option { @@ -55,7 +61,7 @@ impl SourceToDefCtx<'_, '_> { Some(parent_declaration) => self.module_to_def(parent_declaration), None => { let file_id = src.file_id.original_file(self.db.upcast()); - self.file_to_def(file_id) + self.file_to_def(file_id).get(0).copied() } }?; @@ -185,7 +191,7 @@ impl SourceToDefCtx<'_, '_> { ) -> Option { let kind = MacroDefKind::Declarative; let file_id = src.file_id.original_file(self.db.upcast()); - let krate = self.file_to_def(file_id)?.krate(); + let krate = self.file_to_def(file_id).get(0).copied()?.krate(); let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value); let ast_id = Some(AstId::new(src.file_id, file_ast_id.upcast())); Some(MacroDefId { krate, ast_id, kind, local_inner: false }) @@ -245,7 +251,7 @@ impl SourceToDefCtx<'_, '_> { return Some(res); } - let def = self.file_to_def(src.file_id.original_file(self.db.upcast()))?; + let def = self.file_to_def(src.file_id.original_file(self.db.upcast())).get(0).copied()?; Some(def.into()) } -- cgit v1.2.3