aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-03-09 10:11:59 +0000
committerAleksey Kladov <[email protected]>2020-03-09 10:11:59 +0000
commit0320ebdd101b01abe5f24e9efcef7c15005fd3a5 (patch)
tree37d5176dfabe886b1eadb2ddb16fbb6834df0a16
parent57c27f91392fdd9d72fd023f4e2fecd8b68a7d09 (diff)
Use `Index` for CrateGraph
-rw-r--r--crates/ra_db/src/input.rs17
-rw-r--r--crates/ra_hir/src/code_model.rs9
-rw-r--r--crates/ra_hir_def/src/find_path.rs2
-rw-r--r--crates/ra_hir_def/src/lang_item.rs3
-rw-r--r--crates/ra_hir_def/src/nameres.rs2
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs6
-rw-r--r--crates/ra_hir_ty/src/traits.rs2
-rw-r--r--crates/ra_ide/src/hover.rs2
-rw-r--r--crates/ra_ide/src/lib.rs4
9 files changed, 24 insertions, 23 deletions
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 @@
6//! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how 6//! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how
7//! actual IO is done and lowered to input. 7//! actual IO is done and lowered to input.
8 8
9use std::{fmt, str::FromStr}; 9use std::{fmt, ops, str::FromStr};
10 10
11use ra_cfg::CfgOptions; 11use ra_cfg::CfgOptions;
12use ra_syntax::SmolStr; 12use ra_syntax::SmolStr;
@@ -174,10 +174,6 @@ impl CrateGraph {
174 self.arena.keys().copied() 174 self.arena.keys().copied()
175 } 175 }
176 176
177 pub fn crate_data(&self, crate_id: &CrateId) -> &CrateData {
178 &self.arena[crate_id]
179 }
180
181 // FIXME: this only finds one crate with the given root; we could have multiple 177 // FIXME: this only finds one crate with the given root; we could have multiple
182 pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> { 178 pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> {
183 let (&crate_id, _) = 179 let (&crate_id, _) =
@@ -207,7 +203,7 @@ impl CrateGraph {
207 return false; 203 return false;
208 } 204 }
209 205
210 for dep in &self.crate_data(&from).dependencies { 206 for dep in &self[from].dependencies {
211 let crate_id = dep.crate_id(); 207 let crate_id = dep.crate_id();
212 if crate_id == target { 208 if crate_id == target {
213 return true; 209 return true;
@@ -221,6 +217,13 @@ impl CrateGraph {
221 } 217 }
222} 218}
223 219
220impl ops::Index<CrateId> for CrateGraph {
221 type Output = CrateData;
222 fn index(&self, crate_id: CrateId) -> &CrateData {
223 &self.arena[&crate_id]
224 }
225}
226
224impl CrateId { 227impl CrateId {
225 pub fn shift(self, amount: u32) -> CrateId { 228 pub fn shift(self, amount: u32) -> CrateId {
226 CrateId(self.0 + amount) 229 CrateId(self.0 + amount)
@@ -376,7 +379,7 @@ mod tests {
376 .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) 379 .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2)
377 .is_ok()); 380 .is_ok());
378 assert_eq!( 381 assert_eq!(
379 graph.crate_data(&crate1).dependencies, 382 graph[crate1].dependencies,
380 vec![Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }] 383 vec![Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }]
381 ); 384 );
382 } 385 }
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 {
54 54
55impl Crate { 55impl Crate {
56 pub fn dependencies(self, db: &impl DefDatabase) -> Vec<CrateDependency> { 56 pub fn dependencies(self, db: &impl DefDatabase) -> Vec<CrateDependency> {
57 db.crate_graph() 57 db.crate_graph()[self.id]
58 .crate_data(&self.id)
59 .dependencies 58 .dependencies
60 .iter() 59 .iter()
61 .map(|dep| { 60 .map(|dep| {
@@ -72,7 +71,7 @@ impl Crate {
72 crate_graph 71 crate_graph
73 .iter() 72 .iter()
74 .filter(|&krate| { 73 .filter(|&krate| {
75 crate_graph.crate_data(&krate).dependencies.iter().any(|it| it.crate_id == self.id) 74 crate_graph[krate].dependencies.iter().any(|it| it.crate_id == self.id)
76 }) 75 })
77 .map(|id| Crate { id }) 76 .map(|id| Crate { id })
78 .collect() 77 .collect()
@@ -84,11 +83,11 @@ impl Crate {
84 } 83 }
85 84
86 pub fn root_file(self, db: &impl DefDatabase) -> FileId { 85 pub fn root_file(self, db: &impl DefDatabase) -> FileId {
87 db.crate_graph().crate_data(&self.id).root_file_id 86 db.crate_graph()[self.id].root_file_id
88 } 87 }
89 88
90 pub fn edition(self, db: &impl DefDatabase) -> Edition { 89 pub fn edition(self, db: &impl DefDatabase) -> Edition {
91 db.crate_graph().crate_data(&self.id).edition 90 db.crate_graph()[self.id].edition
92 } 91 }
93 92
94 pub fn all(db: &impl DefDatabase) -> Vec<Crate> { 93 pub fn all(db: &impl DefDatabase) -> Vec<Crate> {
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(
176 // directly (only through reexports in direct dependencies). 176 // directly (only through reexports in direct dependencies).
177 for krate in Some(from.krate) 177 for krate in Some(from.krate)
178 .into_iter() 178 .into_iter()
179 .chain(crate_graph.crate_data(&from.krate).dependencies.iter().map(|dep| dep.crate_id)) 179 .chain(crate_graph[from.krate].dependencies.iter().map(|dep| dep.crate_id))
180 { 180 {
181 result.extend( 181 result.extend(
182 importable_locations_in_crate(db, item, krate) 182 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 {
116 if let Some(target) = start_crate_target { 116 if let Some(target) = start_crate_target {
117 return Some(*target); 117 return Some(*target);
118 } 118 }
119 db.crate_graph() 119 db.crate_graph()[start_crate]
120 .crate_data(&start_crate)
121 .dependencies 120 .dependencies
122 .iter() 121 .iter()
123 .find_map(|dep| db.lang_item(dep.crate_id, item.clone())) 122 .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 {
179 pub(crate) fn crate_def_map_query(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> { 179 pub(crate) fn crate_def_map_query(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> {
180 let _p = profile("crate_def_map_query"); 180 let _p = profile("crate_def_map_query");
181 let def_map = { 181 let def_map = {
182 let edition = db.crate_graph().crate_data(&krate).edition; 182 let edition = db.crate_graph()[krate].edition;
183 let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default(); 183 let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default();
184 let root = modules.alloc(ModuleData::default()); 184 let root = modules.alloc(ModuleData::default());
185 CrateDefMap { 185 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
34 let crate_graph = db.crate_graph(); 34 let crate_graph = db.crate_graph();
35 35
36 // populate external prelude 36 // populate external prelude
37 for dep in &crate_graph.crate_data(&def_map.krate).dependencies { 37 for dep in &crate_graph[def_map.krate].dependencies {
38 let dep_def_map = db.crate_def_map(dep.crate_id); 38 let dep_def_map = db.crate_def_map(dep.crate_id);
39 log::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id); 39 log::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id);
40 def_map.extern_prelude.insert( 40 def_map.extern_prelude.insert(
@@ -128,7 +128,7 @@ where
128 DB: DefDatabase, 128 DB: DefDatabase,
129{ 129{
130 fn collect(&mut self) { 130 fn collect(&mut self) {
131 let file_id = self.db.crate_graph().crate_data(&self.def_map.krate).root_file_id; 131 let file_id = self.db.crate_graph()[self.def_map.krate].root_file_id;
132 let raw_items = self.db.raw_items(file_id.into()); 132 let raw_items = self.db.raw_items(file_id.into());
133 let module_id = self.def_map.root; 133 let module_id = self.def_map.root;
134 self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id }; 134 self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id };
@@ -954,7 +954,7 @@ mod tests {
954 let krate = db.test_crate(); 954 let krate = db.test_crate();
955 955
956 let def_map = { 956 let def_map = {
957 let edition = db.crate_graph().crate_data(&krate).edition; 957 let edition = db.crate_graph()[krate].edition;
958 let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default(); 958 let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default();
959 let root = modules.alloc(ModuleData::default()); 959 let root = modules.alloc(ModuleData::default());
960 CrateDefMap { 960 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(
47 // will only ever get called for a few crates near the root of the tree (the 47 // will only ever get called for a few crates near the root of the tree (the
48 // ones the user is editing), so this may actually be a waste of memory. I'm 48 // ones the user is editing), so this may actually be a waste of memory. I'm
49 // doing it like this mainly for simplicity for now. 49 // doing it like this mainly for simplicity for now.
50 for dep in &db.crate_graph().crate_data(&krate).dependencies { 50 for dep in &db.crate_graph()[krate].dependencies {
51 impls.extend(db.impls_for_trait(dep.crate_id, trait_).iter()); 51 impls.extend(db.impls_for_trait(dep.crate_id, trait_).iter());
52 } 52 }
53 let crate_impl_defs = db.impls_in_crate(krate); 53 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<String>
121 121
122fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> { 122fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
123 let mod_path = def.module(db).map(|module| { 123 let mod_path = def.module(db).map(|module| {
124 once(db.crate_graph().crate_data(&module.krate().into()).display_name.clone()) 124 once(db.crate_graph()[module.krate().into()].display_name.clone())
125 .chain( 125 .chain(
126 module 126 module
127 .path_to_root(db) 127 .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 {
421 421
422 /// Returns the edition of the given crate. 422 /// Returns the edition of the given crate.
423 pub fn crate_edition(&self, crate_id: CrateId) -> Cancelable<Edition> { 423 pub fn crate_edition(&self, crate_id: CrateId) -> Cancelable<Edition> {
424 self.with_db(|db| db.crate_graph().crate_data(&crate_id).edition) 424 self.with_db(|db| db.crate_graph()[crate_id].edition)
425 } 425 }
426 426
427 /// Returns the root file of the given crate. 427 /// Returns the root file of the given crate.
428 pub fn crate_root(&self, crate_id: CrateId) -> Cancelable<FileId> { 428 pub fn crate_root(&self, crate_id: CrateId) -> Cancelable<FileId> {
429 self.with_db(|db| db.crate_graph().crate_data(&crate_id).root_file_id) 429 self.with_db(|db| db.crate_graph()[crate_id].root_file_id)
430 } 430 }
431 431
432 /// Returns the set of possible targets to run for the current file. 432 /// Returns the set of possible targets to run for the current file.