aboutsummaryrefslogtreecommitdiff
path: root/crates/hir
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir')
-rw-r--r--crates/hir/Cargo.toml1
-rw-r--r--crates/hir/src/semantics.rs8
-rw-r--r--crates/hir/src/semantics/source_to_def.rs22
3 files changed, 21 insertions, 10 deletions
diff --git a/crates/hir/Cargo.toml b/crates/hir/Cargo.toml
index d4ea7327e..55e9c3f0c 100644
--- a/crates/hir/Cargo.toml
+++ b/crates/hir/Cargo.toml
@@ -15,6 +15,7 @@ rustc-hash = "1.1.0"
15either = "1.5.3" 15either = "1.5.3"
16arrayvec = "0.5.1" 16arrayvec = "0.5.1"
17itertools = "0.10.0" 17itertools = "0.10.0"
18smallvec = "1.4.0"
18 19
19stdx = { path = "../stdx", version = "0.0.0" } 20stdx = { path = "../stdx", version = "0.0.0" }
20syntax = { path = "../syntax", version = "0.0.0" } 21syntax = { path = "../syntax", version = "0.0.0" }
diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs
index 945638cc5..519339c0c 100644
--- a/crates/hir/src/semantics.rs
+++ b/crates/hir/src/semantics.rs
@@ -259,6 +259,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
259 } 259 }
260 260
261 pub fn to_module_def(&self, file: FileId) -> Option<Module> { 261 pub fn to_module_def(&self, file: FileId) -> Option<Module> {
262 self.imp.to_module_def(file).next()
263 }
264
265 pub fn to_module_defs(&self, file: FileId) -> impl Iterator<Item = Module> {
262 self.imp.to_module_def(file) 266 self.imp.to_module_def(file)
263 } 267 }
264 268
@@ -537,8 +541,8 @@ impl<'db> SemanticsImpl<'db> {
537 f(&mut ctx) 541 f(&mut ctx)
538 } 542 }
539 543
540 fn to_module_def(&self, file: FileId) -> Option<Module> { 544 fn to_module_def(&self, file: FileId) -> impl Iterator<Item = Module> {
541 self.with_ctx(|ctx| ctx.file_to_def(file)).map(Module::from) 545 self.with_ctx(|ctx| ctx.file_to_def(file)).into_iter().map(Module::from)
542 } 546 }
543 547
544 fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> { 548 fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> {
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::{
12}; 12};
13use hir_expand::{name::AsName, AstId, MacroDefKind}; 13use hir_expand::{name::AsName, AstId, MacroDefKind};
14use rustc_hash::FxHashMap; 14use rustc_hash::FxHashMap;
15use smallvec::SmallVec;
15use stdx::impl_from; 16use stdx::impl_from;
16use syntax::{ 17use syntax::{
17 ast::{self, NameOwner}, 18 ast::{self, NameOwner},
@@ -28,14 +29,19 @@ pub(super) struct SourceToDefCtx<'a, 'b> {
28} 29}
29 30
30impl SourceToDefCtx<'_, '_> { 31impl SourceToDefCtx<'_, '_> {
31 pub(super) fn file_to_def(&mut self, file: FileId) -> Option<ModuleId> { 32 pub(super) fn file_to_def(&mut self, file: FileId) -> SmallVec<[ModuleId; 1]> {
32 let _p = profile::span("SourceBinder::to_module_def"); 33 let _p = profile::span("SourceBinder::to_module_def");
33 self.db.relevant_crates(file).iter().find_map(|&crate_id| { 34 let mut mods = SmallVec::new();
35 for &crate_id in self.db.relevant_crates(file).iter() {
34 // FIXME: inner items 36 // FIXME: inner items
35 let crate_def_map = self.db.crate_def_map(crate_id); 37 let crate_def_map = self.db.crate_def_map(crate_id);
36 let local_id = crate_def_map.modules_for_file(file).next()?; 38 mods.extend(
37 Some(crate_def_map.module_id(local_id)) 39 crate_def_map
38 }) 40 .modules_for_file(file)
41 .map(|local_id| crate_def_map.module_id(local_id)),
42 )
43 }
44 mods
39 } 45 }
40 46
41 pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> { 47 pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> {
@@ -55,7 +61,7 @@ impl SourceToDefCtx<'_, '_> {
55 Some(parent_declaration) => self.module_to_def(parent_declaration), 61 Some(parent_declaration) => self.module_to_def(parent_declaration),
56 None => { 62 None => {
57 let file_id = src.file_id.original_file(self.db.upcast()); 63 let file_id = src.file_id.original_file(self.db.upcast());
58 self.file_to_def(file_id) 64 self.file_to_def(file_id).get(0).copied()
59 } 65 }
60 }?; 66 }?;
61 67
@@ -185,7 +191,7 @@ impl SourceToDefCtx<'_, '_> {
185 ) -> Option<MacroDefId> { 191 ) -> Option<MacroDefId> {
186 let kind = MacroDefKind::Declarative; 192 let kind = MacroDefKind::Declarative;
187 let file_id = src.file_id.original_file(self.db.upcast()); 193 let file_id = src.file_id.original_file(self.db.upcast());
188 let krate = self.file_to_def(file_id)?.krate(); 194 let krate = self.file_to_def(file_id).get(0).copied()?.krate();
189 let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value); 195 let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value);
190 let ast_id = Some(AstId::new(src.file_id, file_ast_id.upcast())); 196 let ast_id = Some(AstId::new(src.file_id, file_ast_id.upcast()));
191 Some(MacroDefId { krate, ast_id, kind, local_inner: false }) 197 Some(MacroDefId { krate, ast_id, kind, local_inner: false })
@@ -245,7 +251,7 @@ impl SourceToDefCtx<'_, '_> {
245 return Some(res); 251 return Some(res);
246 } 252 }
247 253
248 let def = self.file_to_def(src.file_id.original_file(self.db.upcast()))?; 254 let def = self.file_to_def(src.file_id.original_file(self.db.upcast())).get(0).copied()?;
249 Some(def.into()) 255 Some(def.into())
250 } 256 }
251 257