From 857c35ddb03ee5db97bbb4743dfeedeb3df350ec Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 25 Jan 2019 10:08:21 +0300 Subject: refactor import resolution extract path resolution use enums instead of bools --- crates/ra_hir/src/marks.rs | 7 +- crates/ra_hir/src/nameres.rs | 211 ++++++++++++++++++++----------------- crates/ra_hir/src/nameres/tests.rs | 21 ++++ crates/test_utils/src/marks.rs | 6 +- 4 files changed, 146 insertions(+), 99 deletions(-) diff --git a/crates/ra_hir/src/marks.rs b/crates/ra_hir/src/marks.rs index f4d0c3e59..338ed0516 100644 --- a/crates/ra_hir/src/marks.rs +++ b/crates/ra_hir/src/marks.rs @@ -1,3 +1,4 @@ -use test_utils::mark; - -mark!(name_res_works_for_broken_modules); +test_utils::marks!( + name_res_works_for_broken_modules + item_map_enum_importing +); diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index a3bc98958..8c8494b46 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -19,6 +19,7 @@ pub(crate) mod lower; use std::sync::Arc; use ra_db::CrateId; +use test_utils::tested_by; use rustc_hash::{FxHashMap, FxHashSet}; use crate::{ @@ -273,7 +274,7 @@ where // already done continue; } - if self.resolve_import(module_id, import_id, import_data) { + if self.resolve_import(module_id, import_id, import_data) == ReachedFixedPoint::Yes { log::debug!("import {:?} resolved (or definite error)", import_id); self.processed_imports.insert((module_id, import_id)); } @@ -285,116 +286,138 @@ where module_id: ModuleId, import_id: ImportId, import: &ImportData, - ) -> bool { + ) -> ReachedFixedPoint { log::debug!("resolving import: {:?}", import); if import.is_glob { - return false; + return ReachedFixedPoint::Yes; }; + let original_module = Module { + krate: self.krate, + module_id, + }; + let (def_id, reached_fixedpoint) = + self.result + .resolve_path(self.db, original_module, &import.path); + + if reached_fixedpoint == ReachedFixedPoint::Yes { + let last_segment = import.path.segments.last().unwrap(); + self.update(module_id, |items| { + let res = Resolution { + def_id, + import: Some(import_id), + }; + items.items.insert(last_segment.name.clone(), res); + }); + log::debug!( + "resolved import {:?} ({:?}) cross-source root to {:?}", + last_segment.name, + import, + def_id, + ); + } + reached_fixedpoint + } + + fn update(&mut self, module_id: ModuleId, f: impl FnOnce(&mut ModuleScope)) { + let module_items = self.result.per_module.get_mut(&module_id).unwrap(); + f(module_items) + } +} - let mut curr: ModuleId = match import.path.kind { - PathKind::Plain | PathKind::Self_ => module_id, +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum ReachedFixedPoint { + Yes, + No, +} + +impl ItemMap { + // returns true if we are sure that additions to `ItemMap` wouldn't change + // the result. That is, if we've reached fixed point at this particular + // import. + fn resolve_path( + &self, + db: &impl HirDatabase, + original_module: Module, + path: &Path, + ) -> (PerNs, ReachedFixedPoint) { + let mut curr_per_ns: PerNs = PerNs::types(match path.kind { + PathKind::Crate => original_module.crate_root(db).into(), + PathKind::Self_ | PathKind::Plain => original_module.into(), PathKind::Super => { - match module_id.parent(&self.module_tree) { - Some(it) => it, - None => { - // TODO: error - log::debug!("super path in root module"); - return true; // this can't suddenly resolve if we just resolve some other imports - } + if let Some(p) = original_module.parent(db) { + p.into() + } else { + log::debug!("super path in root module"); + return (PerNs::none(), ReachedFixedPoint::Yes); } } - PathKind::Crate => module_id.crate_root(&self.module_tree), PathKind::Abs => { - // TODO: absolute use is not supported for now - return false; + // TODO: absolute use is not supported + return (PerNs::none(), ReachedFixedPoint::Yes); } - }; - - for (i, segment) in import.path.segments.iter().enumerate() { - let is_last = i == import.path.segments.len() - 1; - - let def_id = match self.result.per_module[&curr].items.get(&segment.name) { - Some(res) if !res.def_id.is_none() => res.def_id, - _ => { - log::debug!("path segment {:?} not found", segment.name); - return false; + }); + + for (i, segment) in path.segments.iter().enumerate() { + 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(), ReachedFixedPoint::No); } }; + // resolve segment in curr + + curr_per_ns = match curr { + ModuleDef::Module(module) => { + if module.krate != original_module.krate { + let path = Path { + segments: path.segments[i..].iter().cloned().collect(), + kind: PathKind::Crate, + }; + log::debug!("resolving {:?} in other crate", path); + let def_id = module.resolve_path(db, &path); + return (def_id, ReachedFixedPoint::Yes); + } - if !is_last { - let type_def_id = if let Some(d) = def_id.take(Namespace::Types) { - d - } else { - log::debug!( - "path segment {:?} resolved to value only, but is not last", - segment.name - ); - return false; - }; - curr = match type_def_id { - ModuleDef::Module(module) => { - if module.krate == self.krate { - module.module_id - } else { - let path = Path { - segments: import.path.segments[i + 1..].iter().cloned().collect(), - kind: PathKind::Crate, - }; - log::debug!("resolving {:?} in other source root", path); - let def_id = module.resolve_path(self.db, &path); - if !def_id.is_none() { - let last_segment = path.segments.last().unwrap(); - self.update(module_id, |items| { - let res = Resolution { - def_id, - import: Some(import_id), - }; - items.items.insert(last_segment.name.clone(), res); - }); - log::debug!( - "resolved import {:?} ({:?}) cross-source root to {:?}", - last_segment.name, - import, - def_id, - ); - return true; - } else { - log::debug!("rest of path did not resolve in other source root"); - return true; - } + match self.per_module[&module.module_id].items.get(&segment.name) { + Some(res) if !res.def_id.is_none() => res.def_id, + _ => { + log::debug!("path segment {:?} not found", segment.name); + return (PerNs::none(), ReachedFixedPoint::No); } } - _ => { - log::debug!( - "path segment {:?} resolved to non-module {:?}, but is not last", - segment.name, - type_def_id, - ); - return true; // this resolved to a non-module, so the path won't ever resolve + } + ModuleDef::Enum(e) => { + // enum variant + tested_by!(item_map_enum_importing); + let matching_variant = e + .variants(db) + .into_iter() + .find(|(n, _variant)| n == &segment.name); + + match matching_variant { + Some((_n, variant)) => PerNs::both(variant.into(), (*e).into()), + None => PerNs::none(), } } - } else { - log::debug!( - "resolved import {:?} ({:?}) within source root to {:?}", - segment.name, - import, - def_id, - ); - self.update(module_id, |items| { - let res = Resolution { - def_id, - import: Some(import_id), - }; - items.items.insert(segment.name.clone(), res); - }) - } + _ => { + // 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) + log::debug!( + "path segment {:?} resolved to non-module {:?}, but is not last", + segment.name, + curr, + ); + return (PerNs::none(), ReachedFixedPoint::Yes); + } + }; } - true - } - - fn update(&mut self, module_id: ModuleId, f: impl FnOnce(&mut ModuleScope)) { - let module_items = self.result.per_module.get_mut(&module_id).unwrap(); - f(module_items) + (curr_per_ns, ReachedFixedPoint::Yes) } } diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 9322bf08c..430d16a2e 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -215,6 +215,27 @@ fn item_map_using_self() { ); } +#[test] +fn item_map_enum_importing() { + covers!(item_map_enum_importing); + let (item_map, module_id) = item_map( + " + //- /lib.rs + enum E { V } + use self::E::V; + <|> + ", + ); + check_module_item_map( + &item_map, + module_id, + " + E: t + V: t v + ", + ); +} + #[test] fn item_map_across_crates() { let (mut db, sr) = MockDatabase::with_files( diff --git a/crates/test_utils/src/marks.rs b/crates/test_utils/src/marks.rs index 79ffedf69..ee47b5219 100644 --- a/crates/test_utils/src/marks.rs +++ b/crates/test_utils/src/marks.rs @@ -46,11 +46,13 @@ macro_rules! covers { } #[macro_export] -macro_rules! mark { - ($ident:ident) => { +macro_rules! marks { + ($($ident:ident)*) => { + $( #[allow(bad_style)] pub(crate) static $ident: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + )* }; } -- cgit v1.2.3 From e884ab05c216fc4a4a35d6267d08519dc5dda41d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 25 Jan 2019 10:15:10 +0300 Subject: write path resolution code only once --- crates/ra_hir/src/code_model_impl/module.rs | 63 ++--------------------------- crates/ra_hir/src/nameres.rs | 13 +++++- crates/ra_ide_api/src/marks.rs | 4 +- 3 files changed, 15 insertions(+), 65 deletions(-) diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs index 6419d3934..a5b190384 100644 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ b/crates/ra_hir/src/code_model_impl/module.rs @@ -3,7 +3,7 @@ use ra_syntax::{ast, SyntaxNode, TreeArc}; use crate::{ Module, ModuleSource, Problem, ModuleDef, - Crate, Name, Path, PathKind, PerNs, + Crate, Name, Path, PerNs, module_tree::ModuleId, nameres::{ModuleScope, lower::ImportId}, db::HirDatabase, @@ -97,65 +97,8 @@ impl Module { } pub(crate) fn resolve_path_impl(&self, db: &impl HirDatabase, path: &Path) -> PerNs { - let mut curr_per_ns: PerNs = PerNs::types(match path.kind { - PathKind::Crate => self.crate_root(db).into(), - PathKind::Self_ | PathKind::Plain => self.clone().into(), - PathKind::Super => { - if let Some(p) = self.parent(db) { - p.into() - } else { - return PerNs::none(); - } - } - PathKind::Abs => { - // TODO: absolute use is not supported - return PerNs::none(); - } - }); - - for segment 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(); - } - }; - // resolve segment in curr - - curr_per_ns = match curr { - ModuleDef::Module(m) => { - let scope = m.scope(db); - match scope.get(&segment.name) { - Some(r) => r.def_id.clone(), - None => PerNs::none(), - } - } - ModuleDef::Enum(e) => { - // enum variant - let matching_variant = e - .variants(db) - .into_iter() - .find(|(n, _variant)| n == &segment.name); - - match matching_variant { - Some((_n, variant)) => PerNs::both(variant.into(), (*e).into()), - None => 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 + let item_map = db.item_map(self.krate); + item_map.resolve_path(db, *self, path) } pub(crate) fn problems_impl( diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index 8c8494b46..0046dfebf 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -297,7 +297,7 @@ where }; let (def_id, reached_fixedpoint) = self.result - .resolve_path(self.db, original_module, &import.path); + .resolve_path_fp(self.db, original_module, &import.path); if reached_fixedpoint == ReachedFixedPoint::Yes { let last_segment = import.path.segments.last().unwrap(); @@ -331,10 +331,19 @@ enum ReachedFixedPoint { } impl ItemMap { + pub(crate) fn resolve_path( + &self, + db: &impl HirDatabase, + original_module: Module, + path: &Path, + ) -> PerNs { + self.resolve_path_fp(db, original_module, path).0 + } + // returns true if we are sure that additions to `ItemMap` wouldn't change // the result. That is, if we've reached fixed point at this particular // import. - fn resolve_path( + fn resolve_path_fp( &self, db: &impl HirDatabase, original_module: Module, diff --git a/crates/ra_ide_api/src/marks.rs b/crates/ra_ide_api/src/marks.rs index b4a726ef0..dc5b2702a 100644 --- a/crates/ra_ide_api/src/marks.rs +++ b/crates/ra_ide_api/src/marks.rs @@ -1,3 +1 @@ -use test_utils::mark; - -mark!(inserts_parens_for_function_calls); +test_utils::marks!(inserts_parens_for_function_calls); -- cgit v1.2.3 From bc77f91cf6bec95176b32d0794775e5f00c25367 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 25 Jan 2019 10:16:28 +0300 Subject: rename def_id -> def --- crates/ra_hir/src/nameres.rs | 39 ++++++++-------------- crates/ra_hir/src/nameres/tests.rs | 4 +-- .../ra_ide_api/src/completion/completion_item.rs | 5 +-- 3 files changed, 17 insertions(+), 31 deletions(-) diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index 0046dfebf..bd5fbfadf 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -59,7 +59,7 @@ impl ModuleScope { #[derive(Debug, Clone, PartialEq, Eq)] pub struct Resolution { /// None for unresolved - pub def_id: PerNs, + pub def: PerNs, /// ident by which this is imported into local scope. pub import: Option, } @@ -211,11 +211,11 @@ where let krate = Crate::new(crate_id); for dep in krate.dependencies(self.db) { if let Some(module) = dep.krate.root_module(self.db) { - let def_id = module.into(); + let def = module.into(); self.add_module_item( &mut module_items, dep.name.clone(), - PerNs::types(def_id), + PerNs::types(def), ); } } @@ -227,7 +227,7 @@ where module_items.items.insert( segment.name.clone(), Resolution { - def_id: PerNs::none(), + def: PerNs::none(), import: Some(import_id), }, ); @@ -235,11 +235,8 @@ where } } // Populate explicitly declared items, except modules - for (name, &def_id) in input.declarations.iter() { - let resolution = Resolution { - def_id, - import: None, - }; + for (name, &def) in input.declarations.iter() { + let resolution = Resolution { def, import: None }; module_items.items.insert(name.clone(), resolution); } @@ -255,16 +252,8 @@ where self.result.per_module.insert(module_id, module_items); } - fn add_module_item( - &self, - module_items: &mut ModuleScope, - name: Name, - def_id: PerNs, - ) { - let resolution = Resolution { - def_id, - import: None, - }; + fn add_module_item(&self, module_items: &mut ModuleScope, name: Name, def: PerNs) { + let resolution = Resolution { def, import: None }; module_items.items.insert(name, resolution); } @@ -295,7 +284,7 @@ where krate: self.krate, module_id, }; - let (def_id, reached_fixedpoint) = + let (def, reached_fixedpoint) = self.result .resolve_path_fp(self.db, original_module, &import.path); @@ -303,7 +292,7 @@ where let last_segment = import.path.segments.last().unwrap(); self.update(module_id, |items| { let res = Resolution { - def_id, + def, import: Some(import_id), }; items.items.insert(last_segment.name.clone(), res); @@ -312,7 +301,7 @@ where "resolved import {:?} ({:?}) cross-source root to {:?}", last_segment.name, import, - def_id, + def, ); } reached_fixedpoint @@ -388,12 +377,12 @@ impl ItemMap { kind: PathKind::Crate, }; log::debug!("resolving {:?} in other crate", path); - let def_id = module.resolve_path(db, &path); - return (def_id, ReachedFixedPoint::Yes); + let def = module.resolve_path(db, &path); + return (def, ReachedFixedPoint::Yes); } match self.per_module[&module.module_id].items.get(&segment.name) { - Some(res) if !res.def_id.is_none() => res.def_id, + Some(res) if !res.def.is_none() => res.def, _ => { log::debug!("path segment {:?} not found", segment.name); return (PerNs::none(), ReachedFixedPoint::No); diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 430d16a2e..7e35c016f 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -37,8 +37,8 @@ fn check_module_item_map(map: &ItemMap, module_id: ModuleId, expected: &str) { fn dump_resolution(resolution: &Resolution) -> &'static str { match ( - resolution.def_id.types.is_some(), - resolution.def_id.values.is_some(), + resolution.def.types.is_some(), + resolution.def.values.is_some(), ) { (true, true) => "t v", (true, false) => "t", diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index 3ba6c33ee..b2b047766 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs @@ -209,10 +209,7 @@ impl Builder { ctx: &CompletionContext, resolution: &hir::Resolution, ) -> Builder { - let def = resolution - .def_id - .take_types() - .or(resolution.def_id.take_values()); + let def = resolution.def.take_types().or(resolution.def.take_values()); let def = match def { None => return self, Some(it) => it, -- cgit v1.2.3 From b90b90cb9518e8fac3cf93ab1d7565cc2cf94ce0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 25 Jan 2019 10:17:50 +0300 Subject: fix comment --- crates/ra_hir/src/nameres.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index bd5fbfadf..32afd8e0a 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -329,9 +329,8 @@ impl ItemMap { self.resolve_path_fp(db, original_module, path).0 } - // returns true if we are sure that additions to `ItemMap` wouldn't change - // the result. That is, if we've reached fixed point at this particular - // import. + // Returns Yes if we are sure that additions to `ItemMap` wouldn't change + // the result. fn resolve_path_fp( &self, db: &impl HirDatabase, -- cgit v1.2.3 From ed27bd8d776e7efd4394d6cdda02aa7f93c48273 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 25 Jan 2019 10:20:35 +0300 Subject: remove useless wrapper --- crates/ra_hir/src/code_model_api.rs | 2 +- crates/ra_hir/src/code_model_impl/module.rs | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 6739627b4..e469c782e 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -169,7 +169,7 @@ impl Module { } pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs { - self.resolve_path_impl(db, path) + db.item_map(self.krate).resolve_path(db, *self, path) } pub fn problems(&self, db: &impl HirDatabase) -> Vec<(TreeArc, Problem)> { diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs index a5b190384..bf0e212ca 100644 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ b/crates/ra_hir/src/code_model_impl/module.rs @@ -96,11 +96,6 @@ impl Module { item_map.per_module[&self.module_id].clone() } - pub(crate) fn resolve_path_impl(&self, db: &impl HirDatabase, path: &Path) -> PerNs { - let item_map = db.item_map(self.krate); - item_map.resolve_path(db, *self, path) - } - pub(crate) fn problems_impl( &self, db: &impl HirDatabase, -- cgit v1.2.3 From 74beb5bfcbc33d6232a9b08b4df4bcfd781790e9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 25 Jan 2019 10:29:00 +0300 Subject: simplify --- crates/ra_arena/src/map.rs | 4 ++++ crates/ra_hir/src/code_model_api.rs | 2 +- crates/ra_hir/src/code_model_impl/module.rs | 12 +++--------- crates/ra_hir/src/nameres.rs | 14 +++++++++++--- crates/ra_hir/src/nameres/tests.rs | 2 +- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/crates/ra_arena/src/map.rs b/crates/ra_arena/src/map.rs index 2f09d677f..be80edaf3 100644 --- a/crates/ra_arena/src/map.rs +++ b/crates/ra_arena/src/map.rs @@ -29,6 +29,10 @@ impl ArenaMap { self.v.get(Self::to_idx(id)).and_then(|it| it.as_ref()) } + pub fn get_mut(&mut self, id: ID) -> Option<&mut T> { + self.v.get_mut(Self::to_idx(id)).and_then(|it| it.as_mut()) + } + pub fn values(&self) -> impl Iterator { self.v.iter().filter_map(|o| o.as_ref()) } diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index e469c782e..a469ad477 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -165,7 +165,7 @@ impl Module { /// Returns a `ModuleScope`: a set of items, visible in this module. pub fn scope(&self, db: &impl HirDatabase) -> ModuleScope { - self.scope_impl(db) + db.item_map(self.krate)[self.module_id].clone() } pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs { diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs index bf0e212ca..480ec27bf 100644 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ b/crates/ra_hir/src/code_model_impl/module.rs @@ -2,10 +2,10 @@ use ra_db::FileId; use ra_syntax::{ast, SyntaxNode, TreeArc}; use crate::{ - Module, ModuleSource, Problem, ModuleDef, - Crate, Name, Path, PerNs, + Module, ModuleSource, Problem, + Crate, Name, module_tree::ModuleId, - nameres::{ModuleScope, lower::ImportId}, + nameres::{lower::ImportId}, db::HirDatabase, }; @@ -90,12 +90,6 @@ impl Module { Some(self.with_module_id(parent_id)) } - /// Returns a `ModuleScope`: a set of items, visible in this module. - pub(crate) fn scope_impl(&self, db: &impl HirDatabase) -> ModuleScope { - let item_map = db.item_map(self.krate); - item_map.per_module[&self.module_id].clone() - } - pub(crate) fn problems_impl( &self, db: &impl HirDatabase, diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index 32afd8e0a..88ad0a44d 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -19,6 +19,7 @@ pub(crate) mod lower; use std::sync::Arc; use ra_db::CrateId; +use ra_arena::map::ArenaMap; use test_utils::tested_by; use rustc_hash::{FxHashMap, FxHashSet}; @@ -37,7 +38,14 @@ use crate::{ // FIXME: currenty we compute item map per source-root. We should do it per crate instead. #[derive(Default, Debug, PartialEq, Eq)] pub struct ItemMap { - pub per_module: FxHashMap, + per_module: ArenaMap, +} + +impl std::ops::Index for ItemMap { + type Output = ModuleScope; + fn index(&self, id: ModuleId) -> &ModuleScope { + &self.per_module[id] + } } #[derive(Debug, Default, PartialEq, Eq, Clone)] @@ -308,7 +316,7 @@ where } fn update(&mut self, module_id: ModuleId, f: impl FnOnce(&mut ModuleScope)) { - let module_items = self.result.per_module.get_mut(&module_id).unwrap(); + let module_items = self.result.per_module.get_mut(module_id).unwrap(); f(module_items) } } @@ -380,7 +388,7 @@ impl ItemMap { return (def, ReachedFixedPoint::Yes); } - match self.per_module[&module.module_id].items.get(&segment.name) { + match self[module.module_id].items.get(&segment.name) { Some(res) if !res.def.is_none() => res.def, _ => { log::debug!("path segment {:?} not found", segment.name); diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 7e35c016f..c033bebe8 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -20,7 +20,7 @@ fn item_map(fixture: &str) -> (Arc, ModuleId) { } fn check_module_item_map(map: &ItemMap, module_id: ModuleId, expected: &str) { - let mut lines = map.per_module[&module_id] + let mut lines = map[module_id] .items .iter() .map(|(name, res)| format!("{}: {}", name, dump_resolution(res))) -- cgit v1.2.3 From 3b45ddd98453a39a9a630571bfb6ec0b56a03d25 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 25 Jan 2019 10:29:55 +0300 Subject: deglobify --- crates/ra_hir/src/nameres.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index 88ad0a44d..5fcbabe9d 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -29,8 +29,7 @@ use crate::{ HirDatabase, Crate, Name, module_tree::{ModuleId, ModuleTree}, -//FIXME: deglobify - nameres::lower::*, + nameres::lower::{ImportId, LoweredModule, ImportData}, }; /// `ItemMap` is the result of name resolution. It contains, for each -- cgit v1.2.3 From 0707f65806961028f2be64812869c66020e2a1a6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 25 Jan 2019 10:31:45 +0300 Subject: drop obsolete fixme --- crates/ra_hir/src/nameres.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index 5fcbabe9d..639726b5e 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -34,7 +34,6 @@ use crate::{ /// `ItemMap` is the result of name resolution. It contains, for each /// module, the set of visible items. -// FIXME: currenty we compute item map per source-root. We should do it per crate instead. #[derive(Default, Debug, PartialEq, Eq)] pub struct ItemMap { per_module: ArenaMap, -- cgit v1.2.3