Repository: rust-analyzer/rust-analyzer
Pull request #709 (2019-01-30 19:29:13)

Use Crate instead of CrateId

bors[bot]: # Merge conflict

Patch:
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index 53e84429c3..c59b7fbc59 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -52,7 +52,7 @@ pub enum Def {
 
 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
 pub struct Module {
-    pub(crate) krate: CrateId,
+    pub(crate) krate: Crate,
     pub(crate) module_id: ModuleId,
 }
 
@@ -133,8 +133,8 @@ impl Module {
     }
 
     /// Returns the crate this module is part of.
-    pub fn krate(&self, db: &impl HirDatabase) -> Option<Crate> {
-        self.krate_impl(db)
+    pub fn krate(&self, _db: &impl HirDatabase) -> Option<Crate> {
+        Some(self.krate)
     }
 
     /// Topmost parent of this module. Every module has a `crate_root`, but some
diff --git a/crates/ra_hir/src/code_model_impl/krate.rs b/crates/ra_hir/src/code_model_impl/krate.rs
index cdd30b402c..86f29d959e 100644
--- a/crates/ra_hir/src/code_model_impl/krate.rs
+++ b/crates/ra_hir/src/code_model_impl/krate.rs
@@ -1,31 +1,28 @@
-use ra_db::CrateId;
-
 use crate::{
     Crate, CrateDependency, AsName, Module,
     db::HirDatabase,
 };
 
 impl Crate {
-    pub(crate) fn new(crate_id: CrateId) -> Crate {
-        Crate { crate_id }
-    }
     pub(crate) fn dependencies_impl(&self, db: &impl HirDatabase) -> Vec<CrateDependency> {
         let crate_graph = db.crate_graph();
         crate_graph
             .dependencies(self.crate_id)
             .map(|dep| {
-                let krate = Crate::new(dep.crate_id());
+                let krate = Crate {
+                    crate_id: dep.crate_id(),
+                };
                 let name = dep.as_name();
                 CrateDependency { krate, name }
             })
             .collect()
     }
     pub(crate) fn root_module_impl(&self, db: &impl HirDatabase) -> Option<Module> {
-        let module_tree = db.module_tree(self.crate_id);
+        let module_tree = db.module_tree(*self);
         let module_id = module_tree.modules().next()?;
 
         let module = Module {
-            krate: self.crate_id,
+            krate: *self,
             module_id,
         };
         Some(module)
diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs
index c6f85ac82c..4a3901b8b7 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,
-    Crate, Name,
+    Name,
     module_tree::ModuleId,
     impl_block::ImplId,
     nameres::{lower::ImportId},
@@ -67,10 +67,6 @@ impl Module {
         source_map.get(&source, impl_id)
     }
 
-    pub(crate) fn krate_impl(&self, _db: &impl HirDatabase) -> Option<Crate> {
-        Some(Crate::new(self.krate))
-    }
-
     pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Module {
         let module_tree = db.module_tree(self.krate);
         let module_id = self.module_id.crate_root(&module_tree);
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index 70d9de2121..16d5a7877b 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -1,7 +1,7 @@
 use std::sync::Arc;
 
 use ra_syntax::{SyntaxNode, TreeArc, SourceFile};
-use ra_db::{SourceDatabase, CrateId, salsa};
+use ra_db::{SourceDatabase, salsa};
 
 use crate::{
     MacroCallId, HirFileId,
@@ -67,11 +67,11 @@ pub trait HirDatabase: SourceDatabase + AsRef<HirInterner> {
     #[salsa::invoke(crate::nameres::lower::LoweredModule::lower_module_source_map_query)]
     fn lower_module_source_map(&self, module: Module) -> Arc<ImportSourceMap>;
 
-    #[salsa::invoke(query_definitions::item_map)]
-    fn item_map(&self, crate_id: CrateId) -> Arc<ItemMap>;
+    #[salsa::invoke(crate::nameres::ItemMap::item_map_query)]
+    fn item_map(&self, krate: Crate) -> Arc<ItemMap>;
 
     #[salsa::invoke(crate::module_tree::ModuleTree::module_tree_query)]
-    fn module_tree(&self, crate_id: CrateId) -> Arc<ModuleTree>;
+    fn module_tree(&self, krate: Crate) -> Arc<ModuleTree>;
 
     #[salsa::invoke(crate::impl_block::impls_in_module_with_source_map_query)]
     fn impls_in_module_with_source_map(
diff --git a/crates/ra_hir/src/module_tree.rs b/crates/ra_hir/src/module_tree.rs
index 893c375b55..1f327eeb28 100644
--- a/crates/ra_hir/src/module_tree.rs
+++ b/crates/ra_hir/src/module_tree.rs
@@ -2,7 +2,7 @@ use std::sync::Arc;
 
 use arrayvec::ArrayVec;
 use relative_path::RelativePathBuf;
-use ra_db::{FileId, SourceRoot, CrateId};
+use ra_db::{FileId, SourceRoot};
 use ra_syntax::{
     SyntaxNode, TreeArc,
     algo::generate,
@@ -13,6 +13,7 @@ use test_utils::tested_by;
 
 use crate::{
     Name, AsName, HirDatabase, SourceItemId, HirFileId, Problem, SourceFileItems, ModuleSource,
+    Crate,
     ids::SourceFileItemId,
 };
 
@@ -132,10 +133,10 @@ struct LinkData {
 }
 
 impl ModuleTree {
-    pub(crate) fn module_tree_query(db: &impl HirDatabase, crate_id: CrateId) -> Arc<ModuleTree> {
+    pub(crate) fn module_tree_query(db: &impl HirDatabase, krate: Crate) -> Arc<ModuleTree> {
         db.check_canceled();
         let mut res = ModuleTree::default();
-        res.init_crate(db, crate_id);
+        res.init_crate(db, krate);
         Arc::new(res)
     }
 
@@ -155,9 +156,9 @@ impl ModuleTree {
         Some(res)
     }
 
-    fn init_crate(&mut self, db: &impl HirDatabase, crate_id: CrateId) {
+    fn init_crate(&mut self, db: &impl HirDatabase, krate: Crate) {
         let crate_graph = db.crate_graph();
-        let file_id = crate_graph.crate_root(crate_id);
+        let file_id = crate_graph.crate_root(krate.crate_id);
         let source_root_id = db.file_source_root(file_id);
 
         let source_root = db.source_root(source_root_id);
diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs
index 97ce6c946d..4573a72ba7 100644
--- a/crates/ra_hir/src/nameres.rs
+++ b/crates/ra_hir/src/nameres.rs
@@ -16,9 +16,8 @@
 //! structure itself is modified.
 pub(crate) mod lower;
 
-use std::sync::Arc;
+use std::{time, sync::Arc};
 
-use ra_db::CrateId;
 use ra_arena::map::ArenaMap;
 use test_utils::tested_by;
 use rustc_hash::{FxHashMap, FxHashSet};
@@ -156,10 +155,10 @@ impl<T> PerNs<T> {
     }
 }
 
-pub(crate) struct Resolver<'a, DB> {
+struct Resolver<'a, DB> {
     db: &'a DB,
     input: &'a FxHashMap<ModuleId, Arc<LoweredModule>>,
-    krate: CrateId,
+    krate: Crate,
     module_tree: Arc<ModuleTree>,
     processed_imports: FxHashSet<(ModuleId, ImportId)>,
     result: ItemMap,
@@ -169,10 +168,10 @@ impl<'a, DB> Resolver<'a, DB>
 where
     DB: HirDatabase,
 {
-    pub(crate) fn new(
+    fn new(
         db: &'a DB,
         input: &'a FxHashMap<ModuleId, Arc<LoweredModule>>,
-        krate: CrateId,
+        krate: Crate,
     ) -> Resolver<'a, DB> {
         let module_tree = db.module_tree(krate);
         Resolver {
@@ -219,7 +218,7 @@ where
             let crate_graph = self.db.crate_graph();
             if let Some(crate_id) = crate_graph.crate_id_for_crate_root(file_id.as_original_file())
             {
-                let krate = Crate::new(crate_id);
+                let krate = Crate { crate_id };
                 for dep in krate.dependencies(self.db) {
                     if let Some(module) = dep.krate.root_module(self.db) {
                         let def = module.into();
@@ -331,6 +330,26 @@ enum ReachedFixedPoint {
 }
 
 impl ItemMap {
+    pub(crate) fn item_map_query(db: &impl HirDatabase, krate: Crate) -> Arc<ItemMap> {
+        let start = time::Instant::now();
+        let module_tree = db.module_tree(krate);
+        let input = module_tree
+            .modules()
+            .map(|module_id| {
+                (
+                    module_id,
+                    db.lower_module_module(Module { krate, module_id }),
+                )
+            })
+            .collect::<FxHashMap<_, _>>();
+
+        let resolver = Resolver::new(db, &input, krate);
+        let res = resolver.resolve();
+        let elapsed = start.elapsed();
+        log::info!("item_map: {:?}", elapsed);
+        Arc::new(res)
+    }
+
     pub(crate) fn resolve_path(
         &self,
         db: &impl HirDatabase,
diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs
index bf9ac0dfb4..6724649e1d 100644
--- a/crates/ra_hir/src/query_definitions.rs
+++ b/crates/ra_hir/src/query_definitions.rs
@@ -1,17 +1,11 @@
-use std::{
-    sync::Arc,
-    time::Instant,
-};
+use std::sync::Arc;
 
-use rustc_hash::FxHashMap;
 use ra_syntax::{SyntaxNode, TreeArc};
-use ra_db::{CrateId};
 
 use crate::{
     SourceFileItems, SourceItemId, HirFileId,
-    Function, FnScopes, Module,
+    Function, FnScopes,
     db::HirDatabase,
-    nameres::{ItemMap, Resolver},
 };
 
 pub(super) fn fn_scopes(db: &impl HirDatabase, func: Function) -> Arc<FnScopes> {
@@ -35,26 +29,3 @@ pub(super) fn file_item(
         .to_node(&source_file)
         .to_owned()
 }
-
-pub(super) fn item_map(db: &impl HirDatabase, crate_id: CrateId) -> Arc<ItemMap> {
-    let start = Instant::now();
-    let module_tree = db.module_tree(crate_id);
-    let input = module_tree
-        .modules()
-        .map(|module_id| {
-            (
-                module_id,
-                db.lower_module_module(Module {
-                    krate: crate_id,
-                    module_id,
-                }),
-            )
-        })
-        .collect::<FxHashMap<_, _>>();
-
-    let resolver = Resolver::new(db, &input, crate_id);
-    let res = resolver.resolve();
-    let elapsed = start.elapsed();
-    log::info!("item_map: {:?}", elapsed);
-    Arc::new(res)
-}
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 589efd0231..d1eaccf230 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -14,7 +14,7 @@ use ra_syntax::{
 
 use crate::{
     HirDatabase, Function, ModuleDef, Struct, Enum,
-    AsName, Module, HirFileId,
+    AsName, Module, HirFileId, Crate,
     ids::{LocationCtx, SourceFileItemId},
 };
 
@@ -83,7 +83,8 @@ fn module_from_source(
     let source_root_id = db.file_source_root(file_id.as_original_file());
     db.source_root_crates(source_root_id)
         .iter()
-        .find_map(|&krate| {
+        .map(|&crate_id| Crate { crate_id })
+        .find_map(|krate| {
             let module_tree = db.module_tree(krate);
             let module_id = module_tree.find_module_by_source(file_id, decl_id)?;
             Some(Module { krate, module_id })
diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs
index d70a245828..37bc3f38ce 100644
--- a/crates/ra_hir/src/ty/method_resolution.rs
+++ b/crates/ra_hir/src/ty/method_resolution.rs
@@ -52,7 +52,7 @@ impl CrateImplBlocks {
             .flat_map(|i| i.iter())
             .map(move |(module_id, impl_id)| {
                 let module = Module {
-                    krate: self.krate.crate_id,
+                    krate: self.krate,
                     module_id: *module_id,
                 };
                 let module_impl_blocks = db.impls_in_module(module);


Test patch:
diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs
index 3d420467c1..9c5ca097ea 100644
--- a/crates/ra_hir/src/nameres/tests.rs
+++ b/crates/ra_hir/src/nameres/tests.rs
@@ -16,7 +16,7 @@ fn item_map(fixture: &str) -> (Arc<ItemMap>, ModuleId) {
     let module = crate::source_binder::module_from_position(&db, pos).unwrap();
     let krate = module.krate(&db).unwrap();
     let module_id = module.module_id;
-    (db.item_map(krate.crate_id), module_id)
+    (db.item_map(krate), module_id)
 }
 
 /// Sets the crate root to the file of the cursor marker
@@ -30,7 +30,7 @@ fn item_map_custom_crate_root(fixture: &str) -> (Arc<ItemMap>, ModuleId) {
     let module = crate::source_binder::module_from_position(&db, pos).unwrap();
     let krate = module.krate(&db).unwrap();
     let module_id = module.module_id;
-    (db.item_map(krate.crate_id), module_id)
+    (db.item_map(krate), module_id)
 }
 
 fn check_module_item_map(map: &ItemMap, module_id: ModuleId, expected: &str) {
@@ -297,7 +297,7 @@ fn item_map_across_crates() {
 
     let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap();
     let krate = module.krate(&db).unwrap();
-    let item_map = db.item_map(krate.crate_id);
+    let item_map = db.item_map(krate);
 
     check_module_item_map(
         &item_map,
@@ -349,7 +349,7 @@ fn import_across_source_roots() {
 
     let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap();
     let krate = module.krate(&db).unwrap();
-    let item_map = db.item_map(krate.crate_id);
+    let item_map = db.item_map(krate);
 
     check_module_item_map(
         &item_map,
@@ -391,7 +391,7 @@ fn reexport_across_crates() {
 
     let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap();
     let krate = module.krate(&db).unwrap();
-    let item_map = db.item_map(krate.crate_id);
+    let item_map = db.item_map(krate);
 
     check_module_item_map(
         &item_map,
@@ -409,7 +409,7 @@ fn check_item_map_is_not_recomputed(initial: &str, file_change: &str) {
     let krate = module.krate(&db).unwrap();
     {
         let events = db.log_executed(|| {
-            db.item_map(krate.crate_id);
+            db.item_map(krate);
         });
         assert!(format!("{:?}", events).contains("item_map"))
     }
@@ -417,7 +417,7 @@ fn check_item_map_is_not_recomputed(initial: &str, file_change: &str) {
 
     {
         let events = db.log_executed(|| {
-            db.item_map(krate.crate_id);
+            db.item_map(krate);
         });
         assert!(
             !format!("{:?}", events).contains("item_map"),