aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/resolve.rs
diff options
context:
space:
mode:
authoruHOOCCOOHu <[email protected]>2019-09-11 19:01:07 +0100
committeruHOOCCOOHu <[email protected]>2019-09-15 12:40:32 +0100
commit4926bed42680d329f906be93450bec6b2ba0e99b (patch)
tree455c0bc9d839a18fffda6d018bf41d1c58ebfa52 /crates/ra_hir/src/resolve.rs
parent2d79a1ad83cc39075c7c9e3230973013c8c58b17 (diff)
Support path starting with a type
Diffstat (limited to 'crates/ra_hir/src/resolve.rs')
-rw-r--r--crates/ra_hir/src/resolve.rs28
1 files changed, 17 insertions, 11 deletions
diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs
index 3207b6626..e357c74e3 100644
--- a/crates/ra_hir/src/resolve.rs
+++ b/crates/ra_hir/src/resolve.rs
@@ -15,6 +15,7 @@ use crate::{
15 name::{Name, SELF_PARAM, SELF_TYPE}, 15 name::{Name, SELF_PARAM, SELF_TYPE},
16 nameres::{CrateDefMap, CrateModuleId, PerNs}, 16 nameres::{CrateDefMap, CrateModuleId, PerNs},
17 path::{Path, PathKind}, 17 path::{Path, PathKind},
18 type_ref::TypeRef,
18 Adt, BuiltinType, Const, Enum, EnumVariant, Function, MacroDef, ModuleDef, Static, Struct, 19 Adt, BuiltinType, Const, Enum, EnumVariant, Function, MacroDef, ModuleDef, Static, Struct,
19 Trait, TypeAlias, 20 Trait, TypeAlias,
20}; 21};
@@ -64,9 +65,10 @@ pub enum TypeNs {
64} 65}
65 66
66#[derive(Debug)] 67#[derive(Debug)]
67pub enum ValueOrPartial { 68pub enum ResolveValueResult<'a> {
68 ValueNs(ValueNs), 69 ValueNs(ValueNs),
69 Partial(TypeNs, usize), 70 Partial(TypeNs, usize),
71 TypeRef(&'a TypeRef),
70} 72}
71 73
72#[derive(Debug)] 74#[derive(Debug)]
@@ -183,11 +185,15 @@ impl Resolver {
183 Some(res) 185 Some(res)
184 } 186 }
185 187
186 pub(crate) fn resolve_path_in_value_ns( 188 pub(crate) fn resolve_path_in_value_ns<'p>(
187 &self, 189 &self,
188 db: &impl HirDatabase, 190 db: &impl HirDatabase,
189 path: &Path, 191 path: &'p Path,
190 ) -> Option<ValueOrPartial> { 192 ) -> Option<ResolveValueResult<'p>> {
193 if let Some(type_ref) = &path.type_ref {
194 return Some(ResolveValueResult::TypeRef(type_ref));
195 }
196
191 let n_segments = path.segments.len(); 197 let n_segments = path.segments.len();
192 let tmp = SELF_PARAM; 198 let tmp = SELF_PARAM;
193 let first_name = if path.is_self() { &tmp } else { &path.segments.first()?.name }; 199 let first_name = if path.is_self() { &tmp } else { &path.segments.first()?.name };
@@ -208,7 +214,7 @@ impl Resolver {
208 .find(|entry| entry.name() == first_name); 214 .find(|entry| entry.name() == first_name);
209 215
210 if let Some(e) = entry { 216 if let Some(e) = entry {
211 return Some(ValueOrPartial::ValueNs(ValueNs::LocalBinding(e.pat()))); 217 return Some(ResolveValueResult::ValueNs(ValueNs::LocalBinding(e.pat())));
212 } 218 }
213 } 219 }
214 Scope::ExprScope(_) => continue, 220 Scope::ExprScope(_) => continue,
@@ -216,7 +222,7 @@ impl Resolver {
216 Scope::GenericParams(params) if n_segments > 1 => { 222 Scope::GenericParams(params) if n_segments > 1 => {
217 if let Some(param) = params.find_by_name(first_name) { 223 if let Some(param) = params.find_by_name(first_name) {
218 let ty = TypeNs::GenericParam(param.idx); 224 let ty = TypeNs::GenericParam(param.idx);
219 return Some(ValueOrPartial::Partial(ty, 1)); 225 return Some(ResolveValueResult::Partial(ty, 1));
220 } 226 }
221 } 227 }
222 Scope::GenericParams(_) => continue, 228 Scope::GenericParams(_) => continue,
@@ -224,7 +230,7 @@ impl Resolver {
224 Scope::ImplBlockScope(impl_) if n_segments > 1 => { 230 Scope::ImplBlockScope(impl_) if n_segments > 1 => {
225 if first_name == &SELF_TYPE { 231 if first_name == &SELF_TYPE {
226 let ty = TypeNs::SelfType(*impl_); 232 let ty = TypeNs::SelfType(*impl_);
227 return Some(ValueOrPartial::Partial(ty, 1)); 233 return Some(ResolveValueResult::Partial(ty, 1));
228 } 234 }
229 } 235 }
230 Scope::ImplBlockScope(_) => continue, 236 Scope::ImplBlockScope(_) => continue,
@@ -247,7 +253,7 @@ impl Resolver {
247 | ModuleDef::BuiltinType(_) 253 | ModuleDef::BuiltinType(_)
248 | ModuleDef::Module(_) => return None, 254 | ModuleDef::Module(_) => return None,
249 }; 255 };
250 Some(ValueOrPartial::ValueNs(value)) 256 Some(ResolveValueResult::ValueNs(value))
251 } 257 }
252 Some(idx) => { 258 Some(idx) => {
253 let ty = match module_def.take_types()? { 259 let ty = match module_def.take_types()? {
@@ -262,7 +268,7 @@ impl Resolver {
262 | ModuleDef::Const(_) 268 | ModuleDef::Const(_)
263 | ModuleDef::Static(_) => return None, 269 | ModuleDef::Static(_) => return None,
264 }; 270 };
265 Some(ValueOrPartial::Partial(ty, idx)) 271 Some(ResolveValueResult::Partial(ty, idx))
266 } 272 }
267 }; 273 };
268 } 274 }
@@ -277,8 +283,8 @@ impl Resolver {
277 path: &Path, 283 path: &Path,
278 ) -> Option<ValueNs> { 284 ) -> Option<ValueNs> {
279 match self.resolve_path_in_value_ns(db, path)? { 285 match self.resolve_path_in_value_ns(db, path)? {
280 ValueOrPartial::ValueNs(it) => Some(it), 286 ResolveValueResult::ValueNs(it) => Some(it),
281 ValueOrPartial::Partial(..) => None, 287 ResolveValueResult::Partial(..) | ResolveValueResult::TypeRef(_) => None,
282 } 288 }
283 } 289 }
284 290