From ce29730bc773a27eaeaae7fa4122563df3b253b6 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 22 Jan 2021 16:31:40 +0100 Subject: Obtain `ModuleId`'s `DefMap` through a method --- crates/hir/src/code_model.rs | 16 ++++++++-------- crates/hir/src/has_source.rs | 6 +++--- crates/hir/src/semantics/source_to_def.rs | 3 ++- 3 files changed, 13 insertions(+), 12 deletions(-) (limited to 'crates/hir') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index aaa7013b6..c34a99d90 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -281,7 +281,7 @@ impl Module { /// Name of this module. pub fn name(self, db: &dyn HirDatabase) -> Option { - let def_map = db.crate_def_map(self.id.krate); + let def_map = self.id.def_map(db.upcast()); let parent = def_map[self.id.local_id].parent?; def_map[parent].children.iter().find_map(|(name, module_id)| { if *module_id == self.id.local_id { @@ -307,7 +307,7 @@ impl Module { /// Iterates over all child modules. pub fn children(self, db: &dyn HirDatabase) -> impl Iterator { - let def_map = db.crate_def_map(self.id.krate); + let def_map = self.id.def_map(db.upcast()); let children = def_map[self.id.local_id] .children .iter() @@ -318,7 +318,7 @@ impl Module { /// Finds a parent module. pub fn parent(self, db: &dyn HirDatabase) -> Option { - let def_map = db.crate_def_map(self.id.krate); + let def_map = self.id.def_map(db.upcast()); let parent_id = def_map[self.id.local_id].parent?; Some(self.with_module_id(parent_id)) } @@ -339,7 +339,7 @@ impl Module { db: &dyn HirDatabase, visible_from: Option, ) -> Vec<(Name, ScopeDef)> { - db.crate_def_map(self.id.krate)[self.id.local_id] + self.id.def_map(db.upcast())[self.id.local_id] .scope .entries() .filter_map(|(name, def)| { @@ -362,14 +362,14 @@ impl Module { } pub fn visibility_of(self, db: &dyn HirDatabase, def: &ModuleDef) -> Option { - db.crate_def_map(self.id.krate)[self.id.local_id].scope.visibility_of(def.clone().into()) + self.id.def_map(db.upcast())[self.id.local_id].scope.visibility_of(def.clone().into()) } pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { let _p = profile::span("Module::diagnostics").detail(|| { format!("{:?}", self.name(db).map_or("".into(), |name| name.to_string())) }); - let crate_def_map = db.crate_def_map(self.id.krate); + let crate_def_map = self.id.def_map(db.upcast()); crate_def_map.add_diagnostics(db.upcast(), self.id.local_id, sink); for decl in self.declarations(db) { match decl { @@ -396,12 +396,12 @@ impl Module { } pub fn declarations(self, db: &dyn HirDatabase) -> Vec { - let def_map = db.crate_def_map(self.id.krate); + let def_map = self.id.def_map(db.upcast()); def_map[self.id.local_id].scope.declarations().map(ModuleDef::from).collect() } pub fn impl_defs(self, db: &dyn HirDatabase) -> Vec { - let def_map = db.crate_def_map(self.id.krate); + let def_map = self.id.def_map(db.upcast()); def_map[self.id.local_id].scope.impls().map(Impl::from).collect() } diff --git a/crates/hir/src/has_source.rs b/crates/hir/src/has_source.rs index 7c57d8378..262002671 100644 --- a/crates/hir/src/has_source.rs +++ b/crates/hir/src/has_source.rs @@ -24,12 +24,12 @@ pub trait HasSource { impl Module { /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. pub fn definition_source(self, db: &dyn HirDatabase) -> InFile { - let def_map = db.crate_def_map(self.id.krate); + let def_map = self.id.def_map(db.upcast()); def_map[self.id.local_id].definition_source(db.upcast()) } pub fn is_mod_rs(self, db: &dyn HirDatabase) -> bool { - let def_map = db.crate_def_map(self.id.krate); + let def_map = self.id.def_map(db.upcast()); match def_map[self.id.local_id].origin { ModuleOrigin::File { is_mod_rs, .. } => is_mod_rs, _ => false, @@ -39,7 +39,7 @@ impl Module { /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. /// `None` for the crate root. pub fn declaration_source(self, db: &dyn HirDatabase) -> Option> { - let def_map = db.crate_def_map(self.id.krate); + let def_map = self.id.def_map(db.upcast()); def_map[self.id.local_id].declaration_source(db.upcast()) } } diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs index 9bf60c72a..775f7ec8b 100644 --- a/crates/hir/src/semantics/source_to_def.rs +++ b/crates/hir/src/semantics/source_to_def.rs @@ -31,6 +31,7 @@ impl SourceToDefCtx<'_, '_> { pub(super) fn file_to_def(&mut self, file: FileId) -> Option { let _p = profile::span("SourceBinder::to_module_def"); let (krate, local_id) = self.db.relevant_crates(file).iter().find_map(|&crate_id| { + // 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_id, local_id)) @@ -60,7 +61,7 @@ impl SourceToDefCtx<'_, '_> { }?; let child_name = src.value.name()?.as_name(); - let def_map = self.db.crate_def_map(parent_module.krate); + let def_map = parent_module.def_map(self.db.upcast()); let child_id = *def_map[parent_module.local_id].children.get(&child_name)?; Some(ModuleId { krate: parent_module.krate, local_id: child_id }) } -- cgit v1.2.3