From ce29730bc773a27eaeaae7fa4122563df3b253b6 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 22 Jan 2021 16:31:40 +0100 Subject: Obtain `ModuleId`'s `DefMap` through a method --- crates/hir_def/src/attr.rs | 2 +- crates/hir_def/src/body.rs | 2 +- crates/hir_def/src/child_by_source.rs | 2 +- crates/hir_def/src/find_path.rs | 10 +++++----- crates/hir_def/src/import_map.rs | 2 +- crates/hir_def/src/lib.rs | 12 +++++++++++- crates/hir_def/src/nameres/collector.rs | 2 +- crates/hir_def/src/nameres/path_resolution.rs | 4 ++-- crates/hir_def/src/resolver.rs | 6 +++--- crates/hir_def/src/visibility.rs | 2 +- 10 files changed, 27 insertions(+), 17 deletions(-) (limited to 'crates/hir_def') diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index c72649c41..6513daec8 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs @@ -196,7 +196,7 @@ impl Attrs { pub(crate) fn attrs_query(db: &dyn DefDatabase, def: AttrDefId) -> Attrs { let raw_attrs = match def { AttrDefId::ModuleId(module) => { - let def_map = db.crate_def_map(module.krate); + let def_map = module.def_map(db); let mod_data = &def_map[module.local_id]; match mod_data.declaration_source(db) { Some(it) => { diff --git a/crates/hir_def/src/body.rs b/crates/hir_def/src/body.rs index 2c2c999dd..d0c84ab0b 100644 --- a/crates/hir_def/src/body.rs +++ b/crates/hir_def/src/body.rs @@ -86,7 +86,7 @@ impl Expander { module: ModuleId, ) -> Expander { let cfg_expander = CfgExpander::new(db, current_file_id, module.krate); - let crate_def_map = db.crate_def_map(module.krate); + let crate_def_map = module.def_map(db); let ast_id_map = db.ast_id_map(current_file_id); Expander { cfg_expander, diff --git a/crates/hir_def/src/child_by_source.rs b/crates/hir_def/src/child_by_source.rs index dcb00a1d9..65d85c86a 100644 --- a/crates/hir_def/src/child_by_source.rs +++ b/crates/hir_def/src/child_by_source.rs @@ -74,7 +74,7 @@ impl ChildBySource for ImplId { impl ChildBySource for ModuleId { fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap { - let crate_def_map = db.crate_def_map(self.krate); + let crate_def_map = self.def_map(db); let module_data = &crate_def_map[self.local_id]; module_data.scope.child_by_source(db) } diff --git a/crates/hir_def/src/find_path.rs b/crates/hir_def/src/find_path.rs index db2d125ae..c01b6daf2 100644 --- a/crates/hir_def/src/find_path.rs +++ b/crates/hir_def/src/find_path.rs @@ -110,7 +110,7 @@ fn find_path_inner( // Base cases: // - if the item is already in scope, return the name under which it is - let def_map = db.crate_def_map(from.krate); + let def_map = from.def_map(db); let from_scope: &crate::item_scope::ItemScope = &def_map[from.local_id].scope; let scope_name = if let Some((name, _)) = from_scope.name_of(item) { Some(name.clone()) } else { None }; @@ -145,7 +145,7 @@ fn find_path_inner( // - if the item is in the prelude, return the name from there if let Some(prelude_module) = def_map.prelude() { - let prelude_def_map = db.crate_def_map(prelude_module.krate); + let prelude_def_map = prelude_module.def_map(db); let prelude_scope: &crate::item_scope::ItemScope = &prelude_def_map[prelude_module.local_id].scope; if let Some((name, vis)) = prelude_scope.name_of(item) { @@ -283,7 +283,7 @@ fn find_local_import_locations( // above `from` with any visibility. That means we do not need to descend into private siblings // of `from` (and similar). - let def_map = db.crate_def_map(from.krate); + let def_map = from.def_map(db); // Compute the initial worklist. We start with all direct child modules of `from` as well as all // of its (recursive) parent modules. @@ -312,7 +312,7 @@ fn find_local_import_locations( &def_map[module.local_id] } else { // The crate might reexport a module defined in another crate. - ext_def_map = db.crate_def_map(module.krate); + ext_def_map = module.def_map(db); &ext_def_map[module.local_id] }; @@ -375,7 +375,7 @@ mod tests { parsed_path_file.syntax_node().descendants().find_map(syntax::ast::Path::cast).unwrap(); let mod_path = ModPath::from_src(ast_path, &Hygiene::new_unhygienic()).unwrap(); - let crate_def_map = db.crate_def_map(module.krate); + let crate_def_map = module.def_map(&db); let resolved = crate_def_map .resolve_path( &db, diff --git a/crates/hir_def/src/import_map.rs b/crates/hir_def/src/import_map.rs index 0251d016b..0b7830445 100644 --- a/crates/hir_def/src/import_map.rs +++ b/crates/hir_def/src/import_map.rs @@ -83,7 +83,7 @@ impl ImportMap { &def_map[module.local_id] } else { // The crate might reexport a module defined in another crate. - ext_def_map = db.crate_def_map(module.krate); + ext_def_map = module.def_map(db); &ext_def_map[module.local_id] }; diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index 08ed920c6..2f9261a7f 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs @@ -50,7 +50,10 @@ pub mod import_map; #[cfg(test)] mod test_db; -use std::hash::{Hash, Hasher}; +use std::{ + hash::{Hash, Hasher}, + sync::Arc, +}; use base_db::{impl_intern_key, salsa, CrateId}; use hir_expand::{ @@ -58,6 +61,7 @@ use hir_expand::{ MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, }; use la_arena::Idx; +use nameres::DefMap; use syntax::ast; use crate::builtin_type::BuiltinType; @@ -73,6 +77,12 @@ pub struct ModuleId { pub local_id: LocalModuleId, } +impl ModuleId { + pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc { + db.crate_def_map(self.krate) + } +} + /// An ID of a module, **local** to a specific crate pub type LocalModuleId = Idx; diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index cd68efbe6..adfcf879a 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -578,7 +578,7 @@ impl DefCollector<'_> { } else if m.krate != self.def_map.krate { mark::hit!(glob_across_crates); // glob import from other crate => we can just import everything once - let item_map = self.db.crate_def_map(m.krate); + let item_map = m.def_map(self.db); let scope = &item_map[m.local_id].scope; // Module scoped macros is included diff --git a/crates/hir_def/src/nameres/path_resolution.rs b/crates/hir_def/src/nameres/path_resolution.rs index ec90f4e65..82528b792 100644 --- a/crates/hir_def/src/nameres/path_resolution.rs +++ b/crates/hir_def/src/nameres/path_resolution.rs @@ -243,7 +243,7 @@ impl DefMap { kind: PathKind::Super(0), }; log::debug!("resolving {:?} in other crate", path); - let defp_map = db.crate_def_map(module.krate); + let defp_map = module.def_map(db); let (def, s) = defp_map.resolve_path(db, module.local_id, &path, shadow); return ResolvePathResult::with( def, @@ -356,7 +356,7 @@ impl DefMap { self } else { // Extend lifetime - keep = db.crate_def_map(prelude.krate); + keep = prelude.def_map(db); &keep }; def_map[prelude.local_id].scope.get(name) diff --git a/crates/hir_def/src/resolver.rs b/crates/hir_def/src/resolver.rs index b2f577649..130c074f0 100644 --- a/crates/hir_def/src/resolver.rs +++ b/crates/hir_def/src/resolver.rs @@ -430,7 +430,7 @@ impl Resolver { for scope in &self.scopes { if let Scope::ModuleScope(m) = scope { if let Some(prelude) = m.crate_def_map.prelude() { - let prelude_def_map = db.crate_def_map(prelude.krate); + let prelude_def_map = prelude.def_map(db); traits.extend(prelude_def_map[prelude.local_id].scope.traits()); } traits.extend(m.crate_def_map[m.module_id].scope.traits()); @@ -529,7 +529,7 @@ impl Scope { f(name.clone(), ScopeDef::PerNs(def)); }); if let Some(prelude) = m.crate_def_map.prelude() { - let prelude_def_map = db.crate_def_map(prelude.krate); + let prelude_def_map = prelude.def_map(db); prelude_def_map[prelude.local_id].scope.entries().for_each(|(name, def)| { let seen_tuple = (name.clone(), def); if !seen.contains(&seen_tuple) { @@ -633,7 +633,7 @@ pub trait HasResolver: Copy { impl HasResolver for ModuleId { fn resolver(self, db: &dyn DefDatabase) -> Resolver { - let def_map = db.crate_def_map(self.krate); + let def_map = self.def_map(db); Resolver::default().push_module_scope(def_map, self.local_id) } } diff --git a/crates/hir_def/src/visibility.rs b/crates/hir_def/src/visibility.rs index 3134fa43d..e79a91102 100644 --- a/crates/hir_def/src/visibility.rs +++ b/crates/hir_def/src/visibility.rs @@ -103,7 +103,7 @@ impl Visibility { if from_module.krate != to_module.krate { return false; } - let def_map = db.crate_def_map(from_module.krate); + let def_map = from_module.def_map(db); self.is_visible_from_def_map(&def_map, from_module.local_id) } -- cgit v1.2.3