From e69af8596262931f8e55b7f9203f65d14827e2d8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 22 Dec 2019 15:08:57 +0100 Subject: Refactor PerNs construction --- crates/ra_hir_def/src/item_scope.rs | 20 +++++++- crates/ra_hir_def/src/nameres/collector.rs | 81 ++++++++++++------------------ 2 files changed, 52 insertions(+), 49 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs index eab3e2fff..a96b5cfd2 100644 --- a/crates/ra_hir_def/src/item_scope.rs +++ b/crates/ra_hir_def/src/item_scope.rs @@ -5,7 +5,7 @@ use hir_expand::name::Name; use once_cell::sync::Lazy; use rustc_hash::FxHashMap; -use crate::{per_ns::PerNs, BuiltinType, ImplId, MacroDefId, ModuleDefId, TraitId}; +use crate::{per_ns::PerNs, AdtId, BuiltinType, ImplId, MacroDefId, ModuleDefId, TraitId}; #[derive(Debug, Default, PartialEq, Eq)] pub struct ItemScope { @@ -153,3 +153,21 @@ pub struct Resolution { pub def: PerNs, pub(crate) import: bool, } + +impl From for PerNs { + fn from(def: ModuleDefId) -> PerNs { + match def { + ModuleDefId::ModuleId(_) => PerNs::types(def), + ModuleDefId::FunctionId(_) => PerNs::values(def), + ModuleDefId::AdtId(adt) => match adt { + AdtId::StructId(_) | AdtId::UnionId(_) => PerNs::both(def, def), + AdtId::EnumId(_) => PerNs::types(def), + }, + ModuleDefId::EnumVariantId(_) => PerNs::both(def, def), + ModuleDefId::ConstId(_) | ModuleDefId::StaticId(_) => PerNs::values(def), + ModuleDefId::TraitId(_) => PerNs::types(def), + ModuleDefId::TypeAliasId(_) => PerNs::types(def), + ModuleDefId::BuiltinType(_) => PerNs::types(def), + } + } +} diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 2b194f488..b4e438257 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -714,12 +714,9 @@ where modules[res].scope.define_legacy_macro(name, mac) } modules[self.module_id].children.insert(name.clone(), res); - let resolution = Resolution { - def: PerNs::types( - ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(), - ), - import: false, - }; + let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res }; + let def: ModuleDefId = module.into(); + let resolution = Resolution { def: def.into(), import: false }; self.def_collector.update(self.module_id, None, &[(name, resolution)]); res } @@ -734,63 +731,51 @@ where let name = def.name.clone(); let container = ContainerId::ModuleId(module); - let def: PerNs = match def.kind { - raw::DefKind::Function(ast_id) => { - let def = FunctionLoc { - container: container.into(), - ast_id: AstId::new(self.file_id, ast_id), - } - .intern(self.def_collector.db); - - PerNs::values(def.into()) + let def: ModuleDefId = match def.kind { + raw::DefKind::Function(ast_id) => FunctionLoc { + container: container.into(), + ast_id: AstId::new(self.file_id, ast_id), } + .intern(self.def_collector.db) + .into(), raw::DefKind::Struct(ast_id) => { - let def = StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) } - .intern(self.def_collector.db); - PerNs::both(def.into(), def.into()) + StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) } + .intern(self.def_collector.db) + .into() } raw::DefKind::Union(ast_id) => { - let def = UnionLoc { container, ast_id: AstId::new(self.file_id, ast_id) } - .intern(self.def_collector.db); - PerNs::both(def.into(), def.into()) + UnionLoc { container, ast_id: AstId::new(self.file_id, ast_id) } + .intern(self.def_collector.db) + .into() } raw::DefKind::Enum(ast_id) => { - let def = EnumLoc { container, ast_id: AstId::new(self.file_id, ast_id) } - .intern(self.def_collector.db); - PerNs::types(def.into()) + EnumLoc { container, ast_id: AstId::new(self.file_id, ast_id) } + .intern(self.def_collector.db) + .into() } raw::DefKind::Const(ast_id) => { - let def = ConstLoc { - container: container.into(), - ast_id: AstId::new(self.file_id, ast_id), - } - .intern(self.def_collector.db); - - PerNs::values(def.into()) + ConstLoc { container: container.into(), ast_id: AstId::new(self.file_id, ast_id) } + .intern(self.def_collector.db) + .into() } raw::DefKind::Static(ast_id) => { - let def = StaticLoc { container, ast_id: AstId::new(self.file_id, ast_id) } - .intern(self.def_collector.db); - - PerNs::values(def.into()) + StaticLoc { container, ast_id: AstId::new(self.file_id, ast_id) } + .intern(self.def_collector.db) + .into() } raw::DefKind::Trait(ast_id) => { - let def = TraitLoc { container, ast_id: AstId::new(self.file_id, ast_id) } - .intern(self.def_collector.db); - - PerNs::types(def.into()) + TraitLoc { container, ast_id: AstId::new(self.file_id, ast_id) } + .intern(self.def_collector.db) + .into() } - raw::DefKind::TypeAlias(ast_id) => { - let def = TypeAliasLoc { - container: container.into(), - ast_id: AstId::new(self.file_id, ast_id), - } - .intern(self.def_collector.db); - - PerNs::types(def.into()) + raw::DefKind::TypeAlias(ast_id) => TypeAliasLoc { + container: container.into(), + ast_id: AstId::new(self.file_id, ast_id), } + .intern(self.def_collector.db) + .into(), }; - let resolution = Resolution { def, import: false }; + let resolution = Resolution { def: def.into(), import: false }; self.def_collector.update(self.module_id, None, &[(name, resolution)]) } -- cgit v1.2.3