aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/nameres.rs18
-rw-r--r--crates/ra_hir/src/nameres/collector.rs6
-rw-r--r--crates/ra_hir/src/nameres/per_ns.rs30
-rw-r--r--crates/ra_hir/src/resolve.rs10
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 {
200#[derive(Debug, Clone, PartialEq, Eq, Default)] 200#[derive(Debug, Clone, PartialEq, Eq, Default)]
201pub struct Resolution { 201pub struct Resolution {
202 /// None for unresolved 202 /// None for unresolved
203 pub def: PerNs<ModuleDef>, 203 pub def: PerNs,
204 /// ident by which this is imported into local scope. 204 /// ident by which this is imported into local scope.
205 pub import: Option<ImportId>, 205 pub import: Option<ImportId>,
206} 206}
@@ -213,7 +213,7 @@ impl Resolution {
213 213
214#[derive(Debug, Clone)] 214#[derive(Debug, Clone)]
215struct ResolvePathResult { 215struct ResolvePathResult {
216 resolved_def: PerNs<ModuleDef>, 216 resolved_def: PerNs,
217 segment_index: Option<usize>, 217 segment_index: Option<usize>,
218 reached_fixedpoint: ReachedFixedPoint, 218 reached_fixedpoint: ReachedFixedPoint,
219} 219}
@@ -224,7 +224,7 @@ impl ResolvePathResult {
224 } 224 }
225 225
226 fn with( 226 fn with(
227 resolved_def: PerNs<ModuleDef>, 227 resolved_def: PerNs,
228 reached_fixedpoint: ReachedFixedPoint, 228 reached_fixedpoint: ReachedFixedPoint,
229 segment_index: Option<usize>, 229 segment_index: Option<usize>,
230 ) -> ResolvePathResult { 230 ) -> ResolvePathResult {
@@ -316,7 +316,7 @@ impl CrateDefMap {
316 db: &impl DefDatabase, 316 db: &impl DefDatabase,
317 original_module: CrateModuleId, 317 original_module: CrateModuleId,
318 path: &Path, 318 path: &Path,
319 ) -> (PerNs<ModuleDef>, Option<usize>) { 319 ) -> (PerNs, Option<usize>) {
320 let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path); 320 let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path);
321 (res.resolved_def, res.segment_index) 321 (res.resolved_def, res.segment_index)
322 } 322 }
@@ -331,7 +331,7 @@ impl CrateDefMap {
331 path: &Path, 331 path: &Path,
332 ) -> ResolvePathResult { 332 ) -> ResolvePathResult {
333 let mut segments = path.segments.iter().enumerate(); 333 let mut segments = path.segments.iter().enumerate();
334 let mut curr_per_ns: PerNs<ModuleDef> = match path.kind { 334 let mut curr_per_ns: PerNs = match path.kind {
335 PathKind::Crate => { 335 PathKind::Crate => {
336 PerNs::types(Module { krate: self.krate, module_id: self.root }.into()) 336 PerNs::types(Module { krate: self.krate, module_id: self.root }.into())
337 } 337 }
@@ -456,7 +456,7 @@ impl CrateDefMap {
456 ResolvePathResult::with(curr_per_ns, ReachedFixedPoint::Yes, None) 456 ResolvePathResult::with(curr_per_ns, ReachedFixedPoint::Yes, None)
457 } 457 }
458 458
459 fn resolve_name_in_crate_root_or_extern_prelude(&self, name: &Name) -> PerNs<ModuleDef> { 459 fn resolve_name_in_crate_root_or_extern_prelude(&self, name: &Name) -> PerNs {
460 let from_crate_root = 460 let from_crate_root =
461 self[self.root].scope.get(name).map_or_else(PerNs::none, |res| res.def); 461 self[self.root].scope.get(name).map_or_else(PerNs::none, |res| res.def);
462 let from_extern_prelude = self.resolve_name_in_extern_prelude(name); 462 let from_extern_prelude = self.resolve_name_in_extern_prelude(name);
@@ -469,7 +469,7 @@ impl CrateDefMap {
469 db: &impl DefDatabase, 469 db: &impl DefDatabase,
470 module: CrateModuleId, 470 module: CrateModuleId,
471 name: &Name, 471 name: &Name,
472 ) -> PerNs<ModuleDef> { 472 ) -> PerNs {
473 // Resolve in: 473 // Resolve in:
474 // - legacy scope of macro 474 // - legacy scope of macro
475 // - current module / scope 475 // - current module / scope
@@ -485,11 +485,11 @@ impl CrateDefMap {
485 from_legacy_macro.or(from_scope).or(from_extern_prelude).or(from_prelude) 485 from_legacy_macro.or(from_scope).or(from_extern_prelude).or(from_prelude)
486 } 486 }
487 487
488 fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs<ModuleDef> { 488 fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs {
489 self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)) 489 self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it))
490 } 490 }
491 491
492 fn resolve_in_prelude(&self, db: &impl DefDatabase, name: &Name) -> PerNs<ModuleDef> { 492 fn resolve_in_prelude(&self, db: &impl DefDatabase, name: &Name) -> PerNs {
493 if let Some(prelude) = self.prelude { 493 if let Some(prelude) = self.prelude {
494 let keep; 494 let keep;
495 let def_map = if prelude.krate == self.krate { 495 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
243 &self, 243 &self,
244 module_id: CrateModuleId, 244 module_id: CrateModuleId,
245 import: &raw::ImportData, 245 import: &raw::ImportData,
246 ) -> (PerNs<ModuleDef>, ReachedFixedPoint) { 246 ) -> (PerNs, ReachedFixedPoint) {
247 log::debug!("resolving import: {:?} ({:?})", import, self.def_map.edition); 247 log::debug!("resolving import: {:?} ({:?})", import, self.def_map.edition);
248 if import.is_extern_crate { 248 if import.is_extern_crate {
249 let res = self.def_map.resolve_name_in_extern_prelude( 249 let res = self.def_map.resolve_name_in_extern_prelude(
@@ -268,7 +268,7 @@ where
268 fn record_resolved_import( 268 fn record_resolved_import(
269 &mut self, 269 &mut self,
270 module_id: CrateModuleId, 270 module_id: CrateModuleId,
271 def: PerNs<ModuleDef>, 271 def: PerNs,
272 import_id: raw::ImportId, 272 import_id: raw::ImportId,
273 import: &raw::ImportData, 273 import: &raw::ImportData,
274 ) { 274 ) {
@@ -662,7 +662,7 @@ where
662 }; 662 };
663 } 663 }
664 let name = def.name.clone(); 664 let name = def.name.clone();
665 let def: PerNs<ModuleDef> = match def.kind { 665 let def: PerNs = match def.kind {
666 raw::DefKind::Function(ast_id) => PerNs::values(def!(Function, ast_id)), 666 raw::DefKind::Function(ast_id) => PerNs::values(def!(Function, ast_id)),
667 raw::DefKind::Struct(ast_id) => { 667 raw::DefKind::Struct(ast_id) => {
668 let s = def!(Struct, ast_id); 668 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 @@
1use crate::MacroDef; 1use crate::{MacroDef, ModuleDef};
2 2
3#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] 3#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
4pub enum Namespace { 4pub enum Namespace {
@@ -9,38 +9,38 @@ pub enum Namespace {
9} 9}
10 10
11#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] 11#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
12pub struct PerNs<T> { 12pub struct PerNs {
13 pub types: Option<T>, 13 pub types: Option<ModuleDef>,
14 pub values: Option<T>, 14 pub values: Option<ModuleDef>,
15 /// Since macros has different type, many methods simply ignore it. 15 /// Since macros has different type, many methods simply ignore it.
16 /// We can only use special method like `get_macros` to access it. 16 /// We can only use special method like `get_macros` to access it.
17 pub macros: Option<MacroDef>, 17 pub macros: Option<MacroDef>,
18} 18}
19 19
20impl<T> Default for PerNs<T> { 20impl Default for PerNs {
21 fn default() -> Self { 21 fn default() -> Self {
22 PerNs { types: None, values: None, macros: None } 22 PerNs { types: None, values: None, macros: None }
23 } 23 }
24} 24}
25 25
26impl<T> PerNs<T> { 26impl PerNs {
27 pub fn none() -> PerNs<T> { 27 pub fn none() -> PerNs {
28 PerNs { types: None, values: None, macros: None } 28 PerNs { types: None, values: None, macros: None }
29 } 29 }
30 30
31 pub fn values(t: T) -> PerNs<T> { 31 pub fn values(t: ModuleDef) -> PerNs {
32 PerNs { types: None, values: Some(t), macros: None } 32 PerNs { types: None, values: Some(t), macros: None }
33 } 33 }
34 34
35 pub fn types(t: T) -> PerNs<T> { 35 pub fn types(t: ModuleDef) -> PerNs {
36 PerNs { types: Some(t), values: None, macros: None } 36 PerNs { types: Some(t), values: None, macros: None }
37 } 37 }
38 38
39 pub fn both(types: T, values: T) -> PerNs<T> { 39 pub fn both(types: ModuleDef, values: ModuleDef) -> PerNs {
40 PerNs { types: Some(types), values: Some(values), macros: None } 40 PerNs { types: Some(types), values: Some(values), macros: None }
41 } 41 }
42 42
43 pub fn macros(macro_: MacroDef) -> PerNs<T> { 43 pub fn macros(macro_: MacroDef) -> PerNs {
44 PerNs { types: None, values: None, macros: Some(macro_) } 44 PerNs { types: None, values: None, macros: Some(macro_) }
45 } 45 }
46 46
@@ -52,11 +52,11 @@ impl<T> PerNs<T> {
52 self.types.is_some() && self.values.is_some() && self.macros.is_some() 52 self.types.is_some() && self.values.is_some() && self.macros.is_some()
53 } 53 }
54 54
55 pub fn take_types(self) -> Option<T> { 55 pub fn take_types(self) -> Option<ModuleDef> {
56 self.types 56 self.types
57 } 57 }
58 58
59 pub fn take_values(self) -> Option<T> { 59 pub fn take_values(self) -> Option<ModuleDef> {
60 self.values 60 self.values
61 } 61 }
62 62
@@ -64,11 +64,11 @@ impl<T> PerNs<T> {
64 self.macros 64 self.macros
65 } 65 }
66 66
67 pub fn only_macros(&self) -> PerNs<T> { 67 pub fn only_macros(&self) -> PerNs {
68 PerNs { types: None, values: None, macros: self.macros } 68 PerNs { types: None, values: None, macros: self.macros }
69 } 69 }
70 70
71 pub fn or(self, other: PerNs<T>) -> PerNs<T> { 71 pub fn or(self, other: PerNs) -> PerNs {
72 PerNs { 72 PerNs {
73 types: self.types.or(other.types), 73 types: self.types.or(other.types),
74 values: self.values.or(other.values), 74 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 {
112 } 112 }
113 113
114 /// pub only for source-binder 114 /// pub only for source-binder
115 pub(crate) fn resolve_module_path( 115 pub(crate) fn resolve_module_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs {
116 &self,
117 db: &impl HirDatabase,
118 path: &Path,
119 ) -> PerNs<ModuleDef> {
120 let (item_map, module) = match self.module() { 116 let (item_map, module) = match self.module() {
121 Some(it) => it, 117 Some(it) => it,
122 None => return PerNs::none(), 118 None => return PerNs::none(),
@@ -385,8 +381,8 @@ pub enum ScopeDef {
385 Unknown, 381 Unknown,
386} 382}
387 383
388impl From<PerNs<ModuleDef>> for ScopeDef { 384impl From<PerNs> for ScopeDef {
389 fn from(def: PerNs<ModuleDef>) -> Self { 385 fn from(def: PerNs) -> Self {
390 def.take_types() 386 def.take_types()
391 .or_else(|| def.take_values()) 387 .or_else(|| def.take_values())
392 .map(ScopeDef::ModuleDef) 388 .map(ScopeDef::ModuleDef)