From 0320ebdd101b01abe5f24e9efcef7c15005fd3a5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 9 Mar 2020 11:11:59 +0100 Subject: Use `Index` for CrateGraph --- crates/ra_db/src/input.rs | 17 ++++++++++------- crates/ra_hir/src/code_model.rs | 9 ++++----- crates/ra_hir_def/src/find_path.rs | 2 +- crates/ra_hir_def/src/lang_item.rs | 3 +-- crates/ra_hir_def/src/nameres.rs | 2 +- crates/ra_hir_def/src/nameres/collector.rs | 6 +++--- crates/ra_hir_ty/src/traits.rs | 2 +- crates/ra_ide/src/hover.rs | 2 +- crates/ra_ide/src/lib.rs | 4 ++-- 9 files changed, 24 insertions(+), 23 deletions(-) (limited to 'crates') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 4069c0fed..cd078e43a 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -6,7 +6,7 @@ //! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how //! actual IO is done and lowered to input. -use std::{fmt, str::FromStr}; +use std::{fmt, ops, str::FromStr}; use ra_cfg::CfgOptions; use ra_syntax::SmolStr; @@ -174,10 +174,6 @@ impl CrateGraph { self.arena.keys().copied() } - pub fn crate_data(&self, crate_id: &CrateId) -> &CrateData { - &self.arena[crate_id] - } - // FIXME: this only finds one crate with the given root; we could have multiple pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option { let (&crate_id, _) = @@ -207,7 +203,7 @@ impl CrateGraph { return false; } - for dep in &self.crate_data(&from).dependencies { + for dep in &self[from].dependencies { let crate_id = dep.crate_id(); if crate_id == target { return true; @@ -221,6 +217,13 @@ impl CrateGraph { } } +impl ops::Index for CrateGraph { + type Output = CrateData; + fn index(&self, crate_id: CrateId) -> &CrateData { + &self.arena[&crate_id] + } +} + impl CrateId { pub fn shift(self, amount: u32) -> CrateId { CrateId(self.0 + amount) @@ -376,7 +379,7 @@ mod tests { .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) .is_ok()); assert_eq!( - graph.crate_data(&crate1).dependencies, + graph[crate1].dependencies, vec![Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }] ); } diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 78c444037..a53c4961e 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -54,8 +54,7 @@ pub struct CrateDependency { impl Crate { pub fn dependencies(self, db: &impl DefDatabase) -> Vec { - db.crate_graph() - .crate_data(&self.id) + db.crate_graph()[self.id] .dependencies .iter() .map(|dep| { @@ -72,7 +71,7 @@ impl Crate { crate_graph .iter() .filter(|&krate| { - crate_graph.crate_data(&krate).dependencies.iter().any(|it| it.crate_id == self.id) + crate_graph[krate].dependencies.iter().any(|it| it.crate_id == self.id) }) .map(|id| Crate { id }) .collect() @@ -84,11 +83,11 @@ impl Crate { } pub fn root_file(self, db: &impl DefDatabase) -> FileId { - db.crate_graph().crate_data(&self.id).root_file_id + db.crate_graph()[self.id].root_file_id } pub fn edition(self, db: &impl DefDatabase) -> Edition { - db.crate_graph().crate_data(&self.id).edition + db.crate_graph()[self.id].edition } pub fn all(db: &impl DefDatabase) -> Vec { diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs index 217e19b01..07ca74ec3 100644 --- a/crates/ra_hir_def/src/find_path.rs +++ b/crates/ra_hir_def/src/find_path.rs @@ -176,7 +176,7 @@ fn find_importable_locations( // directly (only through reexports in direct dependencies). for krate in Some(from.krate) .into_iter() - .chain(crate_graph.crate_data(&from.krate).dependencies.iter().map(|dep| dep.crate_id)) + .chain(crate_graph[from.krate].dependencies.iter().map(|dep| dep.crate_id)) { result.extend( importable_locations_in_crate(db, item, krate) diff --git a/crates/ra_hir_def/src/lang_item.rs b/crates/ra_hir_def/src/lang_item.rs index 79e8d8038..6de49730e 100644 --- a/crates/ra_hir_def/src/lang_item.rs +++ b/crates/ra_hir_def/src/lang_item.rs @@ -116,8 +116,7 @@ impl LangItems { if let Some(target) = start_crate_target { return Some(*target); } - db.crate_graph() - .crate_data(&start_crate) + db.crate_graph()[start_crate] .dependencies .iter() .find_map(|dep| db.lang_item(dep.crate_id, item.clone())) diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 6af0f4a8e..066b43ef9 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -179,7 +179,7 @@ impl CrateDefMap { pub(crate) fn crate_def_map_query(db: &impl DefDatabase, krate: CrateId) -> Arc { let _p = profile("crate_def_map_query"); let def_map = { - let edition = db.crate_graph().crate_data(&krate).edition; + let edition = db.crate_graph()[krate].edition; let mut modules: Arena = Arena::default(); let root = modules.alloc(ModuleData::default()); CrateDefMap { diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index e69f89b80..bfa3db47a 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -34,7 +34,7 @@ pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> C let crate_graph = db.crate_graph(); // populate external prelude - for dep in &crate_graph.crate_data(&def_map.krate).dependencies { + for dep in &crate_graph[def_map.krate].dependencies { let dep_def_map = db.crate_def_map(dep.crate_id); log::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id); def_map.extern_prelude.insert( @@ -128,7 +128,7 @@ where DB: DefDatabase, { fn collect(&mut self) { - let file_id = self.db.crate_graph().crate_data(&self.def_map.krate).root_file_id; + let file_id = self.db.crate_graph()[self.def_map.krate].root_file_id; let raw_items = self.db.raw_items(file_id.into()); let module_id = self.def_map.root; self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id }; @@ -954,7 +954,7 @@ mod tests { let krate = db.test_crate(); let def_map = { - let edition = db.crate_graph().crate_data(&krate).edition; + let edition = db.crate_graph()[krate].edition; let mut modules: Arena = Arena::default(); let root = modules.alloc(ModuleData::default()); CrateDefMap { diff --git a/crates/ra_hir_ty/src/traits.rs b/crates/ra_hir_ty/src/traits.rs index 6c653c4f5..6e1c8e42a 100644 --- a/crates/ra_hir_ty/src/traits.rs +++ b/crates/ra_hir_ty/src/traits.rs @@ -47,7 +47,7 @@ pub(crate) fn impls_for_trait_query( // will only ever get called for a few crates near the root of the tree (the // ones the user is editing), so this may actually be a waste of memory. I'm // doing it like this mainly for simplicity for now. - for dep in &db.crate_graph().crate_data(&krate).dependencies { + for dep in &db.crate_graph()[krate].dependencies { impls.extend(db.impls_for_trait(dep.crate_id, trait_).iter()); } let crate_impl_defs = db.impls_in_crate(krate); diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index 8b8af35fc..25e038a55 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -121,7 +121,7 @@ fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option { let mod_path = def.module(db).map(|module| { - once(db.crate_graph().crate_data(&module.krate().into()).display_name.clone()) + once(db.crate_graph()[module.krate().into()].display_name.clone()) .chain( module .path_to_root(db) diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index 903624381..c60e86aea 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -421,12 +421,12 @@ impl Analysis { /// Returns the edition of the given crate. pub fn crate_edition(&self, crate_id: CrateId) -> Cancelable { - self.with_db(|db| db.crate_graph().crate_data(&crate_id).edition) + self.with_db(|db| db.crate_graph()[crate_id].edition) } /// Returns the root file of the given crate. pub fn crate_root(&self, crate_id: CrateId) -> Cancelable { - self.with_db(|db| db.crate_graph().crate_data(&crate_id).root_file_id) + self.with_db(|db| db.crate_graph()[crate_id].root_file_id) } /// Returns the set of possible targets to run for the current file. -- cgit v1.2.3 From 254ef1860b4b5a27bfcba2d20f29761c14a9ab2e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 9 Mar 2020 11:14:51 +0100 Subject: Minimize API --- crates/ra_db/src/input.rs | 8 ++------ crates/ra_hir_def/src/nameres/collector.rs | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) (limited to 'crates') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index cd078e43a..3da28b435 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -111,8 +111,8 @@ pub struct CrateData { /// This actual crate name can be different in a particular dependent crate /// or may even be missing for some cases, such as a dummy crate for the code snippet. pub display_name: Option, - cfg_options: CfgOptions, - env: Env, + pub cfg_options: CfgOptions, + pub env: Env, pub dependencies: Vec, } @@ -149,10 +149,6 @@ impl CrateGraph { crate_id } - pub fn cfg_options(&self, crate_id: CrateId) -> &CfgOptions { - &self.arena[&crate_id].cfg_options - } - pub fn add_dep( &mut self, from: CrateId, diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index bfa3db47a..d0459d9b0 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -51,7 +51,7 @@ pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> C } } - let cfg_options = crate_graph.cfg_options(def_map.krate); + let cfg_options = &crate_graph[def_map.krate].cfg_options; let mut collector = DefCollector { db, -- cgit v1.2.3 From d0d5aa935b8b5ceb339fb2afabf449032559766a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 9 Mar 2020 11:17:39 +0100 Subject: Simplify --- crates/ra_db/src/input.rs | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) (limited to 'crates') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 3da28b435..2912eb9ba 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -142,7 +142,14 @@ impl CrateGraph { cfg_options: CfgOptions, env: Env, ) -> CrateId { - let data = CrateData::new(file_id, edition, display_name, cfg_options, env); + let data = CrateData { + root_file_id: file_id, + edition, + display_name, + cfg_options, + env, + dependencies: Vec::new(), + }; let crate_id = CrateId(self.arena.len() as u32); let prev = self.arena.insert(crate_id, data); assert!(prev.is_none()); @@ -227,23 +234,6 @@ impl CrateId { } impl CrateData { - fn new( - root_file_id: FileId, - edition: Edition, - display_name: Option, - cfg_options: CfgOptions, - env: Env, - ) -> CrateData { - CrateData { - root_file_id, - edition, - display_name, - dependencies: Vec::new(), - cfg_options, - env, - } - } - fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { self.dependencies.push(Dependency { name, crate_id }) } -- cgit v1.2.3 From 100cbc57ce2bd903ecab7d8bfb0abf7777076510 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 9 Mar 2020 11:18:41 +0100 Subject: Simplify --- crates/ra_db/src/input.rs | 8 +------- crates/ra_hir/src/code_model.rs | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) (limited to 'crates') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 2912eb9ba..b77640b2b 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -207,7 +207,7 @@ impl CrateGraph { } for dep in &self[from].dependencies { - let crate_id = dep.crate_id(); + let crate_id = dep.crate_id; if crate_id == target { return true; } @@ -261,12 +261,6 @@ impl fmt::Display for Edition { } } -impl Dependency { - pub fn crate_id(&self) -> CrateId { - self.crate_id - } -} - #[derive(Debug)] pub struct ParseEditionError { invalid_input: String, diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index a53c4961e..41d4e2ed3 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -58,7 +58,7 @@ impl Crate { .dependencies .iter() .map(|dep| { - let krate = Crate { id: dep.crate_id() }; + let krate = Crate { id: dep.crate_id }; let name = dep.as_name(); CrateDependency { krate, name } }) -- cgit v1.2.3