aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/nameres.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-02-01 22:37:59 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-02-01 22:37:59 +0000
commit4447019f4b5f24728bb7b91b161755ddb373c74c (patch)
treec53ff3531cbbad182e821eb92fa9ad201d2bff0c /crates/ra_hir/src/nameres.rs
parent2b5c226e86892113bcab478cdf4c9adaf1e7b2f6 (diff)
parentc5852f422ff45adaa21815c1a15e03b067a56a82 (diff)
Merge #693
693: Name resolution refactoring r=matklad a=flodiebold This is still very WIP, but it's becoming quite big and I want to make sure this isn't going in a completely bad direction :sweat_smile:. I'm not really happy with how the path resolution looks, and I'm not sure `PerNs<Resolution>` is the best return type -- there are 'this cannot happen in the (types/values) namespace' cases everywhere. I also want to unify the `resolver` and `nameres` namespaces once I'm done switching everything to `Resolver`. Also, `Resolver` only has a lifetime because it needs to have a reference to the `ItemMap` during import resolution :confused: The differences in the completion snapshots are almost completely just ordering (except it completes `Self` as well now), so I changed it to sort the completions before snapshotting. Co-authored-by: Florian Diebold <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/nameres.rs')
-rw-r--r--crates/ra_hir/src/nameres.rs33
1 files changed, 27 insertions, 6 deletions
diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs
index f8627acbe..7ec6512b6 100644
--- a/crates/ra_hir/src/nameres.rs
+++ b/crates/ra_hir/src/nameres.rs
@@ -24,13 +24,13 @@ use rustc_hash::{FxHashMap, FxHashSet};
24 24
25use crate::{ 25use crate::{
26 Module, ModuleDef, 26 Module, ModuleDef,
27 Path, PathKind, Crate, 27 Path, PathKind, PersistentHirDatabase,
28 Name, PersistentHirDatabase, 28 Crate, Name,
29 module_tree::{ModuleId, ModuleTree}, 29 module_tree::{ModuleId, ModuleTree},
30 nameres::lower::{ImportId, LoweredModule, ImportData}, 30 nameres::lower::{ImportId, LoweredModule, ImportData},
31}; 31};
32 32
33/// `ItemMap` is the result of name resolution. It contains, for each 33/// `ItemMap` is the result of module name resolution. It contains, for each
34/// module, the set of visible items. 34/// module, the set of visible items.
35#[derive(Default, Debug, PartialEq, Eq)] 35#[derive(Default, Debug, PartialEq, Eq)]
36pub struct ItemMap { 36pub struct ItemMap {
@@ -46,7 +46,7 @@ impl std::ops::Index<ModuleId> for ItemMap {
46 46
47#[derive(Debug, Default, PartialEq, Eq, Clone)] 47#[derive(Debug, Default, PartialEq, Eq, Clone)]
48pub struct ModuleScope { 48pub struct ModuleScope {
49 items: FxHashMap<Name, Resolution>, 49 pub(crate) items: FxHashMap<Name, Resolution>,
50} 50}
51 51
52impl ModuleScope { 52impl ModuleScope {
@@ -80,6 +80,15 @@ pub struct PerNs<T> {
80 pub values: Option<T>, 80 pub values: Option<T>,
81} 81}
82 82
83impl<T> Default for PerNs<T> {
84 fn default() -> Self {
85 PerNs {
86 types: None,
87 values: None,
88 }
89 }
90}
91
83impl<T> PerNs<T> { 92impl<T> PerNs<T> {
84 pub fn none() -> PerNs<T> { 93 pub fn none() -> PerNs<T> {
85 PerNs { 94 PerNs {
@@ -113,6 +122,10 @@ impl<T> PerNs<T> {
113 self.types.is_none() && self.values.is_none() 122 self.types.is_none() && self.values.is_none()
114 } 123 }
115 124
125 pub fn is_both(&self) -> bool {
126 self.types.is_some() && self.values.is_some()
127 }
128
116 pub fn take(self, namespace: Namespace) -> Option<T> { 129 pub fn take(self, namespace: Namespace) -> Option<T> {
117 match namespace { 130 match namespace {
118 Namespace::Types => self.types, 131 Namespace::Types => self.types,
@@ -139,6 +152,13 @@ impl<T> PerNs<T> {
139 } 152 }
140 } 153 }
141 154
155 pub fn combine(self, other: PerNs<T>) -> PerNs<T> {
156 PerNs {
157 types: self.types.or(other.types),
158 values: self.values.or(other.values),
159 }
160 }
161
142 pub fn and_then<U>(self, f: impl Fn(T) -> Option<U>) -> PerNs<U> { 162 pub fn and_then<U>(self, f: impl Fn(T) -> Option<U>) -> PerNs<U> {
143 PerNs { 163 PerNs {
144 types: self.types.and_then(&f), 164 types: self.types.and_then(&f),
@@ -402,10 +422,11 @@ impl ItemMap {
402 if module.krate != original_module.krate { 422 if module.krate != original_module.krate {
403 let path = Path { 423 let path = Path {
404 segments: path.segments[i..].iter().cloned().collect(), 424 segments: path.segments[i..].iter().cloned().collect(),
405 kind: PathKind::Crate, 425 kind: PathKind::Self_,
406 }; 426 };
407 log::debug!("resolving {:?} in other crate", path); 427 log::debug!("resolving {:?} in other crate", path);
408 let def = module.resolve_path(db, &path); 428 let item_map = db.item_map(module.krate);
429 let def = item_map.resolve_path(db, *module, &path);
409 return (def, ReachedFixedPoint::Yes); 430 return (def, ReachedFixedPoint::Yes);
410 } 431 }
411 432