From 5c241b07666bc7b29e97b8206e505944775266a0 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 25 Jan 2021 15:21:33 +0100 Subject: Create all `ModuleId`s through a `DefMap` method `ModuleId` needs to be able to represent blocks, and only the associated `DefMap` will know how to construct that `ModuleId` --- crates/hir/src/code_model.rs | 27 ++++++++++----------------- crates/hir/src/semantics/source_to_def.rs | 10 ++++------ 2 files changed, 14 insertions(+), 23 deletions(-) (limited to 'crates/hir/src') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index d9b4cdfce..e9bb4f541 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -18,8 +18,8 @@ use hir_def::{ type_ref::{Mutability, TypeRef}, AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId, - LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId, TraitId, - TypeAliasId, TypeParamId, UnionId, + LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, + TypeParamId, UnionId, }; use hir_def::{find_path::PrefixKind, item_scope::ItemInNs, visibility::Visibility}; use hir_expand::{ @@ -90,8 +90,8 @@ impl Crate { } pub fn root_module(self, db: &dyn HirDatabase) -> Module { - let module_id = db.crate_def_map(self.id).root(); - Module::new(self, module_id) + let def_map = db.crate_def_map(self.id); + Module { id: def_map.module_id(def_map.root()) } } pub fn root_file(self, db: &dyn HirDatabase) -> FileId { @@ -275,10 +275,6 @@ impl ModuleDef { } impl Module { - pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module { - Module { id: ModuleId::top_level(krate.id, crate_module_id) } - } - /// Name of this module. pub fn name(self, db: &dyn HirDatabase) -> Option { let def_map = self.id.def_map(db.upcast()); @@ -302,7 +298,7 @@ impl Module { /// in the module tree of any target in `Cargo.toml`. pub fn crate_root(self, db: &dyn HirDatabase) -> Module { let def_map = db.crate_def_map(self.id.krate()); - self.with_module_id(def_map.root()) + Module { id: def_map.module_id(def_map.root()) } } /// Iterates over all child modules. @@ -311,7 +307,7 @@ impl Module { let children = def_map[self.id.local_id] .children .iter() - .map(|(_, module_id)| self.with_module_id(*module_id)) + .map(|(_, module_id)| Module { id: def_map.module_id(*module_id) }) .collect::>(); children.into_iter() } @@ -321,7 +317,7 @@ impl Module { // FIXME: handle block expressions as modules (their parent is in a different DefMap) 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)) + Some(Module { id: def_map.module_id(parent_id) }) } pub fn path_to_root(self, db: &dyn HirDatabase) -> Vec { @@ -406,10 +402,6 @@ impl Module { def_map[self.id.local_id].scope.impls().map(Impl::from).collect() } - pub(crate) fn with_module_id(self, module_id: LocalModuleId) -> Module { - Module::new(self.krate(), module_id) - } - /// Finds a path that can be used to refer to the given item from within /// this module, if possible. pub fn find_use_path(self, db: &dyn DefDatabase, item: impl Into) -> Option { @@ -1013,8 +1005,9 @@ impl MacroDef { /// early, in `hir_expand`, where modules simply do not exist yet. pub fn module(self, db: &dyn HirDatabase) -> Option { let krate = self.id.krate; - let module_id = db.crate_def_map(krate).root(); - Some(Module::new(Crate { id: krate }, module_id)) + let def_map = db.crate_def_map(krate); + let module_id = def_map.root(); + Some(Module { id: def_map.module_id(module_id) }) } /// XXX: this parses the file diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs index faede3269..6c612ee86 100644 --- a/crates/hir/src/semantics/source_to_def.rs +++ b/crates/hir/src/semantics/source_to_def.rs @@ -30,13 +30,12 @@ pub(super) struct SourceToDefCtx<'a, 'b> { 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| { + 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)) - })?; - Some(ModuleId::top_level(krate, local_id)) + Some(crate_def_map.module_id(local_id)) + }) } pub(super) fn module_to_def(&mut self, src: InFile) -> Option { @@ -63,8 +62,7 @@ impl SourceToDefCtx<'_, '_> { let child_name = src.value.name()?.as_name(); let def_map = parent_module.def_map(self.db.upcast()); let child_id = *def_map[parent_module.local_id].children.get(&child_name)?; - // FIXME: handle block expression modules - Some(ModuleId::top_level(parent_module.krate(), child_id)) + Some(def_map.module_id(child_id)) } pub(super) fn trait_to_def(&mut self, src: InFile) -> Option { -- cgit v1.2.3