From 573f6f86561927c3694578dec6c643cc5612c148 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 13 Jan 2019 20:26:14 +0100 Subject: Refactor/simplify path resolution --- crates/ra_hir/src/code_model_impl/module.rs | 64 +++++++++++++++-------------- 1 file changed, 33 insertions(+), 31 deletions(-) (limited to 'crates/ra_hir/src/code_model_impl/module.rs') diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs index e5394d440..7215236f9 100644 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ b/crates/ra_hir/src/code_model_impl/module.rs @@ -147,41 +147,43 @@ impl Module { .def_id, ); - let segments = &path.segments; - for (idx, name) in segments.iter().enumerate() { - let curr = if let Some(r) = curr_per_ns.as_ref().take_types() { - r - } else { - return PerNs::none(); + for name in path.segments.iter() { + let curr = match curr_per_ns.as_ref().take_types() { + Some(r) => r, + None => { + // we still have path segments left, but the path so far + // didn't resolve in the types namespace => no resolution + // (don't break here because curr_per_ns might contain + // something in the value namespace, and it would be wrong + // to return that) + return PerNs::none(); + } }; - let module = match curr.resolve(db) { - Def::Module(it) => it, + // resolve segment in curr + curr_per_ns = match curr.resolve(db) { + Def::Module(m) => { + let scope = m.scope(db); + match scope.get(&name) { + Some(r) => r.def_id, + None => PerNs::none(), + } + } Def::Enum(e) => { - if segments.len() == idx + 1 { - // enum variant - let matching_variant = - e.variants(db).into_iter().find(|(n, _variant)| n == name); - - if let Some((_n, variant)) = matching_variant { - return PerNs::both(variant.def_id(), e.def_id()); - } else { - return PerNs::none(); - } - } else if segments.len() == idx { - // enum - return PerNs::types(e.def_id()); - } else { - // malformed enum? - return PerNs::none(); + // enum variant + let matching_variant = + e.variants(db).into_iter().find(|(n, _variant)| n == name); + + match matching_variant { + Some((_n, variant)) => PerNs::both(variant.def_id(), e.def_id()), + None => PerNs::none(), } } - _ => return PerNs::none(), - }; - let scope = module.scope(db); - curr_per_ns = if let Some(r) = scope.get(&name) { - r.def_id - } else { - return PerNs::none(); + _ => { + // could be an inherent method call in UFCS form + // (`Struct::method`), or some other kind of associated + // item... Which we currently don't handle (TODO) + PerNs::none() + } }; } curr_per_ns -- cgit v1.2.3