From 2fbe79ed9adaeb93e0c9b8bc1d7f67dff075b8dd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 13 Sep 2019 16:38:59 +0300 Subject: make PerNs non-generic --- crates/ra_hir/src/nameres.rs | 18 +++++++++--------- crates/ra_hir/src/nameres/collector.rs | 6 +++--- crates/ra_hir/src/nameres/per_ns.rs | 30 +++++++++++++++--------------- crates/ra_hir/src/resolve.rs | 10 +++------- 4 files changed, 30 insertions(+), 34 deletions(-) diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index 44a4ddba0..7922e3f7e 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -200,7 +200,7 @@ impl ModuleScope { #[derive(Debug, Clone, PartialEq, Eq, Default)] pub struct Resolution { /// None for unresolved - pub def: PerNs, + pub def: PerNs, /// ident by which this is imported into local scope. pub import: Option, } @@ -213,7 +213,7 @@ impl Resolution { #[derive(Debug, Clone)] struct ResolvePathResult { - resolved_def: PerNs, + resolved_def: PerNs, segment_index: Option, reached_fixedpoint: ReachedFixedPoint, } @@ -224,7 +224,7 @@ impl ResolvePathResult { } fn with( - resolved_def: PerNs, + resolved_def: PerNs, reached_fixedpoint: ReachedFixedPoint, segment_index: Option, ) -> ResolvePathResult { @@ -316,7 +316,7 @@ impl CrateDefMap { db: &impl DefDatabase, original_module: CrateModuleId, path: &Path, - ) -> (PerNs, Option) { + ) -> (PerNs, Option) { let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path); (res.resolved_def, res.segment_index) } @@ -331,7 +331,7 @@ impl CrateDefMap { path: &Path, ) -> ResolvePathResult { let mut segments = path.segments.iter().enumerate(); - let mut curr_per_ns: PerNs = match path.kind { + let mut curr_per_ns: PerNs = match path.kind { PathKind::Crate => { PerNs::types(Module { krate: self.krate, module_id: self.root }.into()) } @@ -456,7 +456,7 @@ impl CrateDefMap { ResolvePathResult::with(curr_per_ns, ReachedFixedPoint::Yes, None) } - fn resolve_name_in_crate_root_or_extern_prelude(&self, name: &Name) -> PerNs { + fn resolve_name_in_crate_root_or_extern_prelude(&self, name: &Name) -> PerNs { let from_crate_root = self[self.root].scope.get(name).map_or_else(PerNs::none, |res| res.def); let from_extern_prelude = self.resolve_name_in_extern_prelude(name); @@ -469,7 +469,7 @@ impl CrateDefMap { db: &impl DefDatabase, module: CrateModuleId, name: &Name, - ) -> PerNs { + ) -> PerNs { // Resolve in: // - legacy scope of macro // - current module / scope @@ -485,11 +485,11 @@ impl CrateDefMap { from_legacy_macro.or(from_scope).or(from_extern_prelude).or(from_prelude) } - fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs { + fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs { self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)) } - fn resolve_in_prelude(&self, db: &impl DefDatabase, name: &Name) -> PerNs { + fn resolve_in_prelude(&self, db: &impl DefDatabase, name: &Name) -> PerNs { if let Some(prelude) = self.prelude { let keep; let def_map = if prelude.krate == self.krate { diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index 7d2f313c1..1446c49a0 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -243,7 +243,7 @@ where &self, module_id: CrateModuleId, import: &raw::ImportData, - ) -> (PerNs, ReachedFixedPoint) { + ) -> (PerNs, ReachedFixedPoint) { log::debug!("resolving import: {:?} ({:?})", import, self.def_map.edition); if import.is_extern_crate { let res = self.def_map.resolve_name_in_extern_prelude( @@ -268,7 +268,7 @@ where fn record_resolved_import( &mut self, module_id: CrateModuleId, - def: PerNs, + def: PerNs, import_id: raw::ImportId, import: &raw::ImportData, ) { @@ -662,7 +662,7 @@ where }; } let name = def.name.clone(); - let def: PerNs = match def.kind { + let def: PerNs = match def.kind { raw::DefKind::Function(ast_id) => PerNs::values(def!(Function, ast_id)), raw::DefKind::Struct(ast_id) => { let s = def!(Struct, ast_id); diff --git a/crates/ra_hir/src/nameres/per_ns.rs b/crates/ra_hir/src/nameres/per_ns.rs index b0f4dff33..964da2794 100644 --- a/crates/ra_hir/src/nameres/per_ns.rs +++ b/crates/ra_hir/src/nameres/per_ns.rs @@ -1,4 +1,4 @@ -use crate::MacroDef; +use crate::{MacroDef, ModuleDef}; #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Namespace { @@ -9,38 +9,38 @@ pub enum Namespace { } #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub struct PerNs { - pub types: Option, - pub values: Option, +pub struct PerNs { + pub types: Option, + pub values: Option, /// Since macros has different type, many methods simply ignore it. /// We can only use special method like `get_macros` to access it. pub macros: Option, } -impl Default for PerNs { +impl Default for PerNs { fn default() -> Self { PerNs { types: None, values: None, macros: None } } } -impl PerNs { - pub fn none() -> PerNs { +impl PerNs { + pub fn none() -> PerNs { PerNs { types: None, values: None, macros: None } } - pub fn values(t: T) -> PerNs { + pub fn values(t: ModuleDef) -> PerNs { PerNs { types: None, values: Some(t), macros: None } } - pub fn types(t: T) -> PerNs { + pub fn types(t: ModuleDef) -> PerNs { PerNs { types: Some(t), values: None, macros: None } } - pub fn both(types: T, values: T) -> PerNs { + pub fn both(types: ModuleDef, values: ModuleDef) -> PerNs { PerNs { types: Some(types), values: Some(values), macros: None } } - pub fn macros(macro_: MacroDef) -> PerNs { + pub fn macros(macro_: MacroDef) -> PerNs { PerNs { types: None, values: None, macros: Some(macro_) } } @@ -52,11 +52,11 @@ impl PerNs { self.types.is_some() && self.values.is_some() && self.macros.is_some() } - pub fn take_types(self) -> Option { + pub fn take_types(self) -> Option { self.types } - pub fn take_values(self) -> Option { + pub fn take_values(self) -> Option { self.values } @@ -64,11 +64,11 @@ impl PerNs { self.macros } - pub fn only_macros(&self) -> PerNs { + pub fn only_macros(&self) -> PerNs { PerNs { types: None, values: None, macros: self.macros } } - pub fn or(self, other: PerNs) -> PerNs { + pub fn or(self, other: PerNs) -> PerNs { PerNs { types: self.types.or(other.types), values: self.values.or(other.values), diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index d841593f8..3207b6626 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -112,11 +112,7 @@ impl Resolver { } /// pub only for source-binder - pub(crate) fn resolve_module_path( - &self, - db: &impl HirDatabase, - path: &Path, - ) -> PerNs { + pub(crate) fn resolve_module_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs { let (item_map, module) = match self.module() { Some(it) => it, None => return PerNs::none(), @@ -385,8 +381,8 @@ pub enum ScopeDef { Unknown, } -impl From> for ScopeDef { - fn from(def: PerNs) -> Self { +impl From for ScopeDef { + fn from(def: PerNs) -> Self { def.take_types() .or_else(|| def.take_values()) .map(ScopeDef::ModuleDef) -- cgit v1.2.3