mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-28 04:45:05 +00:00
Use Crate instead of CrateId
This commit is contained in:
parent
95aa7e46fc
commit
ef7eb8c257
9 changed files with 36 additions and 45 deletions
|
@ -52,7 +52,7 @@ pub enum Def {
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct Module {
|
pub struct Module {
|
||||||
pub(crate) krate: CrateId,
|
pub(crate) krate: Crate,
|
||||||
pub(crate) module_id: ModuleId,
|
pub(crate) module_id: ModuleId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,8 +133,8 @@ impl Module {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the crate this module is part of.
|
/// Returns the crate this module is part of.
|
||||||
pub fn krate(&self, db: &impl HirDatabase) -> Option<Crate> {
|
pub fn krate(&self, _db: &impl HirDatabase) -> Option<Crate> {
|
||||||
self.krate_impl(db)
|
Some(self.krate)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Topmost parent of this module. Every module has a `crate_root`, but some
|
/// Topmost parent of this module. Every module has a `crate_root`, but some
|
||||||
|
|
|
@ -1,31 +1,28 @@
|
||||||
use ra_db::CrateId;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Crate, CrateDependency, AsName, Module,
|
Crate, CrateDependency, AsName, Module,
|
||||||
db::HirDatabase,
|
db::HirDatabase,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl Crate {
|
impl Crate {
|
||||||
pub(crate) fn new(crate_id: CrateId) -> Crate {
|
|
||||||
Crate { crate_id }
|
|
||||||
}
|
|
||||||
pub(crate) fn dependencies_impl(&self, db: &impl HirDatabase) -> Vec<CrateDependency> {
|
pub(crate) fn dependencies_impl(&self, db: &impl HirDatabase) -> Vec<CrateDependency> {
|
||||||
let crate_graph = db.crate_graph();
|
let crate_graph = db.crate_graph();
|
||||||
crate_graph
|
crate_graph
|
||||||
.dependencies(self.crate_id)
|
.dependencies(self.crate_id)
|
||||||
.map(|dep| {
|
.map(|dep| {
|
||||||
let krate = Crate::new(dep.crate_id());
|
let krate = Crate {
|
||||||
|
crate_id: dep.crate_id(),
|
||||||
|
};
|
||||||
let name = dep.as_name();
|
let name = dep.as_name();
|
||||||
CrateDependency { krate, name }
|
CrateDependency { krate, name }
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
pub(crate) fn root_module_impl(&self, db: &impl HirDatabase) -> Option<Module> {
|
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_id = module_tree.modules().next()?;
|
||||||
|
|
||||||
let module = Module {
|
let module = Module {
|
||||||
krate: self.crate_id,
|
krate: *self,
|
||||||
module_id,
|
module_id,
|
||||||
};
|
};
|
||||||
Some(module)
|
Some(module)
|
||||||
|
|
|
@ -3,7 +3,7 @@ use ra_syntax::{ast, SyntaxNode, TreeArc};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Module, ModuleSource, Problem,
|
Module, ModuleSource, Problem,
|
||||||
Crate, Name,
|
Name,
|
||||||
module_tree::ModuleId,
|
module_tree::ModuleId,
|
||||||
impl_block::ImplId,
|
impl_block::ImplId,
|
||||||
nameres::{lower::ImportId},
|
nameres::{lower::ImportId},
|
||||||
|
@ -67,10 +67,6 @@ impl Module {
|
||||||
source_map.get(&source, impl_id)
|
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 {
|
pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Module {
|
||||||
let module_tree = db.module_tree(self.krate);
|
let module_tree = db.module_tree(self.krate);
|
||||||
let module_id = self.module_id.crate_root(&module_tree);
|
let module_id = self.module_id.crate_root(&module_tree);
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use ra_syntax::{SyntaxNode, TreeArc, SourceFile};
|
use ra_syntax::{SyntaxNode, TreeArc, SourceFile};
|
||||||
use ra_db::{SourceDatabase, CrateId, salsa};
|
use ra_db::{SourceDatabase, salsa};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
MacroCallId, HirFileId,
|
MacroCallId, HirFileId,
|
||||||
|
@ -68,10 +68,10 @@ pub trait HirDatabase: SourceDatabase + AsRef<HirInterner> {
|
||||||
fn lower_module_source_map(&self, module: Module) -> Arc<ImportSourceMap>;
|
fn lower_module_source_map(&self, module: Module) -> Arc<ImportSourceMap>;
|
||||||
|
|
||||||
#[salsa::invoke(crate::nameres::ItemMap::item_map_query)]
|
#[salsa::invoke(crate::nameres::ItemMap::item_map_query)]
|
||||||
fn item_map(&self, crate_id: CrateId) -> Arc<ItemMap>;
|
fn item_map(&self, krate: Crate) -> Arc<ItemMap>;
|
||||||
|
|
||||||
#[salsa::invoke(crate::module_tree::ModuleTree::module_tree_query)]
|
#[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)]
|
#[salsa::invoke(crate::impl_block::impls_in_module_with_source_map_query)]
|
||||||
fn impls_in_module_with_source_map(
|
fn impls_in_module_with_source_map(
|
||||||
|
|
|
@ -2,7 +2,7 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use arrayvec::ArrayVec;
|
use arrayvec::ArrayVec;
|
||||||
use relative_path::RelativePathBuf;
|
use relative_path::RelativePathBuf;
|
||||||
use ra_db::{FileId, SourceRoot, CrateId};
|
use ra_db::{FileId, SourceRoot};
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
SyntaxNode, TreeArc,
|
SyntaxNode, TreeArc,
|
||||||
algo::generate,
|
algo::generate,
|
||||||
|
@ -13,6 +13,7 @@ use test_utils::tested_by;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Name, AsName, HirDatabase, SourceItemId, HirFileId, Problem, SourceFileItems, ModuleSource,
|
Name, AsName, HirDatabase, SourceItemId, HirFileId, Problem, SourceFileItems, ModuleSource,
|
||||||
|
Crate,
|
||||||
ids::SourceFileItemId,
|
ids::SourceFileItemId,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -132,10 +133,10 @@ struct LinkData {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ModuleTree {
|
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();
|
db.check_canceled();
|
||||||
let mut res = ModuleTree::default();
|
let mut res = ModuleTree::default();
|
||||||
res.init_crate(db, crate_id);
|
res.init_crate(db, krate);
|
||||||
Arc::new(res)
|
Arc::new(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,9 +156,9 @@ impl ModuleTree {
|
||||||
Some(res)
|
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 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_id = db.file_source_root(file_id);
|
||||||
|
|
||||||
let source_root = db.source_root(source_root_id);
|
let source_root = db.source_root(source_root_id);
|
||||||
|
|
|
@ -18,7 +18,6 @@ pub(crate) mod lower;
|
||||||
|
|
||||||
use std::{time, sync::Arc};
|
use std::{time, sync::Arc};
|
||||||
|
|
||||||
use ra_db::CrateId;
|
|
||||||
use ra_arena::map::ArenaMap;
|
use ra_arena::map::ArenaMap;
|
||||||
use test_utils::tested_by;
|
use test_utils::tested_by;
|
||||||
use rustc_hash::{FxHashMap, FxHashSet};
|
use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
|
@ -159,7 +158,7 @@ impl<T> PerNs<T> {
|
||||||
struct Resolver<'a, DB> {
|
struct Resolver<'a, DB> {
|
||||||
db: &'a DB,
|
db: &'a DB,
|
||||||
input: &'a FxHashMap<ModuleId, Arc<LoweredModule>>,
|
input: &'a FxHashMap<ModuleId, Arc<LoweredModule>>,
|
||||||
krate: CrateId,
|
krate: Crate,
|
||||||
module_tree: Arc<ModuleTree>,
|
module_tree: Arc<ModuleTree>,
|
||||||
processed_imports: FxHashSet<(ModuleId, ImportId)>,
|
processed_imports: FxHashSet<(ModuleId, ImportId)>,
|
||||||
result: ItemMap,
|
result: ItemMap,
|
||||||
|
@ -172,7 +171,7 @@ where
|
||||||
fn new(
|
fn new(
|
||||||
db: &'a DB,
|
db: &'a DB,
|
||||||
input: &'a FxHashMap<ModuleId, Arc<LoweredModule>>,
|
input: &'a FxHashMap<ModuleId, Arc<LoweredModule>>,
|
||||||
krate: CrateId,
|
krate: Crate,
|
||||||
) -> Resolver<'a, DB> {
|
) -> Resolver<'a, DB> {
|
||||||
let module_tree = db.module_tree(krate);
|
let module_tree = db.module_tree(krate);
|
||||||
Resolver {
|
Resolver {
|
||||||
|
@ -219,7 +218,7 @@ where
|
||||||
let crate_graph = self.db.crate_graph();
|
let crate_graph = self.db.crate_graph();
|
||||||
if let Some(crate_id) = crate_graph.crate_id_for_crate_root(file_id.as_original_file())
|
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) {
|
for dep in krate.dependencies(self.db) {
|
||||||
if let Some(module) = dep.krate.root_module(self.db) {
|
if let Some(module) = dep.krate.root_module(self.db) {
|
||||||
let def = module.into();
|
let def = module.into();
|
||||||
|
@ -331,23 +330,20 @@ enum ReachedFixedPoint {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ItemMap {
|
impl ItemMap {
|
||||||
pub(crate) fn item_map_query(db: &impl HirDatabase, crate_id: CrateId) -> Arc<ItemMap> {
|
pub(crate) fn item_map_query(db: &impl HirDatabase, krate: Crate) -> Arc<ItemMap> {
|
||||||
let start = time::Instant::now();
|
let start = time::Instant::now();
|
||||||
let module_tree = db.module_tree(crate_id);
|
let module_tree = db.module_tree(krate);
|
||||||
let input = module_tree
|
let input = module_tree
|
||||||
.modules()
|
.modules()
|
||||||
.map(|module_id| {
|
.map(|module_id| {
|
||||||
(
|
(
|
||||||
module_id,
|
module_id,
|
||||||
db.lower_module_module(Module {
|
db.lower_module_module(Module { krate, module_id }),
|
||||||
krate: crate_id,
|
|
||||||
module_id,
|
|
||||||
}),
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.collect::<FxHashMap<_, _>>();
|
.collect::<FxHashMap<_, _>>();
|
||||||
|
|
||||||
let resolver = Resolver::new(db, &input, crate_id);
|
let resolver = Resolver::new(db, &input, krate);
|
||||||
let res = resolver.resolve();
|
let res = resolver.resolve();
|
||||||
let elapsed = start.elapsed();
|
let elapsed = start.elapsed();
|
||||||
log::info!("item_map: {:?}", elapsed);
|
log::info!("item_map: {:?}", elapsed);
|
||||||
|
|
|
@ -16,7 +16,7 @@ fn item_map(fixture: &str) -> (Arc<ItemMap>, ModuleId) {
|
||||||
let module = crate::source_binder::module_from_position(&db, pos).unwrap();
|
let module = crate::source_binder::module_from_position(&db, pos).unwrap();
|
||||||
let krate = module.krate(&db).unwrap();
|
let krate = module.krate(&db).unwrap();
|
||||||
let module_id = module.module_id;
|
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
|
/// 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 module = crate::source_binder::module_from_position(&db, pos).unwrap();
|
||||||
let krate = module.krate(&db).unwrap();
|
let krate = module.krate(&db).unwrap();
|
||||||
let module_id = module.module_id;
|
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) {
|
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 module = crate::source_binder::module_from_file_id(&db, main_id).unwrap();
|
||||||
let krate = module.krate(&db).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(
|
check_module_item_map(
|
||||||
&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 module = crate::source_binder::module_from_file_id(&db, main_id).unwrap();
|
||||||
let krate = module.krate(&db).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(
|
check_module_item_map(
|
||||||
&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 module = crate::source_binder::module_from_file_id(&db, main_id).unwrap();
|
||||||
let krate = module.krate(&db).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(
|
check_module_item_map(
|
||||||
&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 krate = module.krate(&db).unwrap();
|
||||||
{
|
{
|
||||||
let events = db.log_executed(|| {
|
let events = db.log_executed(|| {
|
||||||
db.item_map(krate.crate_id);
|
db.item_map(krate);
|
||||||
});
|
});
|
||||||
assert!(format!("{:?}", events).contains("item_map"))
|
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(|| {
|
let events = db.log_executed(|| {
|
||||||
db.item_map(krate.crate_id);
|
db.item_map(krate);
|
||||||
});
|
});
|
||||||
assert!(
|
assert!(
|
||||||
!format!("{:?}", events).contains("item_map"),
|
!format!("{:?}", events).contains("item_map"),
|
||||||
|
|
|
@ -14,7 +14,7 @@ use ra_syntax::{
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
HirDatabase, Function, ModuleDef, Struct, Enum,
|
HirDatabase, Function, ModuleDef, Struct, Enum,
|
||||||
AsName, Module, HirFileId,
|
AsName, Module, HirFileId, Crate,
|
||||||
ids::{LocationCtx, SourceFileItemId},
|
ids::{LocationCtx, SourceFileItemId},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -83,7 +83,8 @@ fn module_from_source(
|
||||||
let source_root_id = db.file_source_root(file_id.as_original_file());
|
let source_root_id = db.file_source_root(file_id.as_original_file());
|
||||||
db.source_root_crates(source_root_id)
|
db.source_root_crates(source_root_id)
|
||||||
.iter()
|
.iter()
|
||||||
.find_map(|&krate| {
|
.map(|&crate_id| Crate { crate_id })
|
||||||
|
.find_map(|krate| {
|
||||||
let module_tree = db.module_tree(krate);
|
let module_tree = db.module_tree(krate);
|
||||||
let module_id = module_tree.find_module_by_source(file_id, decl_id)?;
|
let module_id = module_tree.find_module_by_source(file_id, decl_id)?;
|
||||||
Some(Module { krate, module_id })
|
Some(Module { krate, module_id })
|
||||||
|
|
|
@ -52,7 +52,7 @@ impl CrateImplBlocks {
|
||||||
.flat_map(|i| i.iter())
|
.flat_map(|i| i.iter())
|
||||||
.map(move |(module_id, impl_id)| {
|
.map(move |(module_id, impl_id)| {
|
||||||
let module = Module {
|
let module = Module {
|
||||||
krate: self.krate.crate_id,
|
krate: self.krate,
|
||||||
module_id: *module_id,
|
module_id: *module_id,
|
||||||
};
|
};
|
||||||
let module_impl_blocks = db.impls_in_module(module);
|
let module_impl_blocks = db.impls_in_module(module);
|
||||||
|
|
Loading…
Reference in a new issue