diff options
author | Jonas Schievink <[email protected]> | 2021-01-22 15:31:40 +0000 |
---|---|---|
committer | Jonas Schievink <[email protected]> | 2021-01-22 15:33:58 +0000 |
commit | ce29730bc773a27eaeaae7fa4122563df3b253b6 (patch) | |
tree | 57818d6bc1e176a421f63c01a6561fb5e6a291b2 /crates/hir_def | |
parent | a5322e3d5b813e4bce7a73762c14bebbd9a36e01 (diff) |
Obtain `ModuleId`'s `DefMap` through a method
Diffstat (limited to 'crates/hir_def')
-rw-r--r-- | crates/hir_def/src/attr.rs | 2 | ||||
-rw-r--r-- | crates/hir_def/src/body.rs | 2 | ||||
-rw-r--r-- | crates/hir_def/src/child_by_source.rs | 2 | ||||
-rw-r--r-- | crates/hir_def/src/find_path.rs | 10 | ||||
-rw-r--r-- | crates/hir_def/src/import_map.rs | 2 | ||||
-rw-r--r-- | crates/hir_def/src/lib.rs | 12 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/collector.rs | 2 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/path_resolution.rs | 4 | ||||
-rw-r--r-- | crates/hir_def/src/resolver.rs | 6 | ||||
-rw-r--r-- | crates/hir_def/src/visibility.rs | 2 |
10 files changed, 27 insertions, 17 deletions
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 { | |||
196 | pub(crate) fn attrs_query(db: &dyn DefDatabase, def: AttrDefId) -> Attrs { | 196 | pub(crate) fn attrs_query(db: &dyn DefDatabase, def: AttrDefId) -> Attrs { |
197 | let raw_attrs = match def { | 197 | let raw_attrs = match def { |
198 | AttrDefId::ModuleId(module) => { | 198 | AttrDefId::ModuleId(module) => { |
199 | let def_map = db.crate_def_map(module.krate); | 199 | let def_map = module.def_map(db); |
200 | let mod_data = &def_map[module.local_id]; | 200 | let mod_data = &def_map[module.local_id]; |
201 | match mod_data.declaration_source(db) { | 201 | match mod_data.declaration_source(db) { |
202 | Some(it) => { | 202 | 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 { | |||
86 | module: ModuleId, | 86 | module: ModuleId, |
87 | ) -> Expander { | 87 | ) -> Expander { |
88 | let cfg_expander = CfgExpander::new(db, current_file_id, module.krate); | 88 | let cfg_expander = CfgExpander::new(db, current_file_id, module.krate); |
89 | let crate_def_map = db.crate_def_map(module.krate); | 89 | let crate_def_map = module.def_map(db); |
90 | let ast_id_map = db.ast_id_map(current_file_id); | 90 | let ast_id_map = db.ast_id_map(current_file_id); |
91 | Expander { | 91 | Expander { |
92 | cfg_expander, | 92 | 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 { | |||
74 | 74 | ||
75 | impl ChildBySource for ModuleId { | 75 | impl ChildBySource for ModuleId { |
76 | fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap { | 76 | fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap { |
77 | let crate_def_map = db.crate_def_map(self.krate); | 77 | let crate_def_map = self.def_map(db); |
78 | let module_data = &crate_def_map[self.local_id]; | 78 | let module_data = &crate_def_map[self.local_id]; |
79 | module_data.scope.child_by_source(db) | 79 | module_data.scope.child_by_source(db) |
80 | } | 80 | } |
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( | |||
110 | // Base cases: | 110 | // Base cases: |
111 | 111 | ||
112 | // - if the item is already in scope, return the name under which it is | 112 | // - if the item is already in scope, return the name under which it is |
113 | let def_map = db.crate_def_map(from.krate); | 113 | let def_map = from.def_map(db); |
114 | let from_scope: &crate::item_scope::ItemScope = &def_map[from.local_id].scope; | 114 | let from_scope: &crate::item_scope::ItemScope = &def_map[from.local_id].scope; |
115 | let scope_name = | 115 | let scope_name = |
116 | if let Some((name, _)) = from_scope.name_of(item) { Some(name.clone()) } else { None }; | 116 | if let Some((name, _)) = from_scope.name_of(item) { Some(name.clone()) } else { None }; |
@@ -145,7 +145,7 @@ fn find_path_inner( | |||
145 | 145 | ||
146 | // - if the item is in the prelude, return the name from there | 146 | // - if the item is in the prelude, return the name from there |
147 | if let Some(prelude_module) = def_map.prelude() { | 147 | if let Some(prelude_module) = def_map.prelude() { |
148 | let prelude_def_map = db.crate_def_map(prelude_module.krate); | 148 | let prelude_def_map = prelude_module.def_map(db); |
149 | let prelude_scope: &crate::item_scope::ItemScope = | 149 | let prelude_scope: &crate::item_scope::ItemScope = |
150 | &prelude_def_map[prelude_module.local_id].scope; | 150 | &prelude_def_map[prelude_module.local_id].scope; |
151 | if let Some((name, vis)) = prelude_scope.name_of(item) { | 151 | if let Some((name, vis)) = prelude_scope.name_of(item) { |
@@ -283,7 +283,7 @@ fn find_local_import_locations( | |||
283 | // above `from` with any visibility. That means we do not need to descend into private siblings | 283 | // above `from` with any visibility. That means we do not need to descend into private siblings |
284 | // of `from` (and similar). | 284 | // of `from` (and similar). |
285 | 285 | ||
286 | let def_map = db.crate_def_map(from.krate); | 286 | let def_map = from.def_map(db); |
287 | 287 | ||
288 | // Compute the initial worklist. We start with all direct child modules of `from` as well as all | 288 | // Compute the initial worklist. We start with all direct child modules of `from` as well as all |
289 | // of its (recursive) parent modules. | 289 | // of its (recursive) parent modules. |
@@ -312,7 +312,7 @@ fn find_local_import_locations( | |||
312 | &def_map[module.local_id] | 312 | &def_map[module.local_id] |
313 | } else { | 313 | } else { |
314 | // The crate might reexport a module defined in another crate. | 314 | // The crate might reexport a module defined in another crate. |
315 | ext_def_map = db.crate_def_map(module.krate); | 315 | ext_def_map = module.def_map(db); |
316 | &ext_def_map[module.local_id] | 316 | &ext_def_map[module.local_id] |
317 | }; | 317 | }; |
318 | 318 | ||
@@ -375,7 +375,7 @@ mod tests { | |||
375 | parsed_path_file.syntax_node().descendants().find_map(syntax::ast::Path::cast).unwrap(); | 375 | parsed_path_file.syntax_node().descendants().find_map(syntax::ast::Path::cast).unwrap(); |
376 | let mod_path = ModPath::from_src(ast_path, &Hygiene::new_unhygienic()).unwrap(); | 376 | let mod_path = ModPath::from_src(ast_path, &Hygiene::new_unhygienic()).unwrap(); |
377 | 377 | ||
378 | let crate_def_map = db.crate_def_map(module.krate); | 378 | let crate_def_map = module.def_map(&db); |
379 | let resolved = crate_def_map | 379 | let resolved = crate_def_map |
380 | .resolve_path( | 380 | .resolve_path( |
381 | &db, | 381 | &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 { | |||
83 | &def_map[module.local_id] | 83 | &def_map[module.local_id] |
84 | } else { | 84 | } else { |
85 | // The crate might reexport a module defined in another crate. | 85 | // The crate might reexport a module defined in another crate. |
86 | ext_def_map = db.crate_def_map(module.krate); | 86 | ext_def_map = module.def_map(db); |
87 | &ext_def_map[module.local_id] | 87 | &ext_def_map[module.local_id] |
88 | }; | 88 | }; |
89 | 89 | ||
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; | |||
50 | #[cfg(test)] | 50 | #[cfg(test)] |
51 | mod test_db; | 51 | mod test_db; |
52 | 52 | ||
53 | use std::hash::{Hash, Hasher}; | 53 | use std::{ |
54 | hash::{Hash, Hasher}, | ||
55 | sync::Arc, | ||
56 | }; | ||
54 | 57 | ||
55 | use base_db::{impl_intern_key, salsa, CrateId}; | 58 | use base_db::{impl_intern_key, salsa, CrateId}; |
56 | use hir_expand::{ | 59 | use hir_expand::{ |
@@ -58,6 +61,7 @@ use hir_expand::{ | |||
58 | MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, | 61 | MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, |
59 | }; | 62 | }; |
60 | use la_arena::Idx; | 63 | use la_arena::Idx; |
64 | use nameres::DefMap; | ||
61 | use syntax::ast; | 65 | use syntax::ast; |
62 | 66 | ||
63 | use crate::builtin_type::BuiltinType; | 67 | use crate::builtin_type::BuiltinType; |
@@ -73,6 +77,12 @@ pub struct ModuleId { | |||
73 | pub local_id: LocalModuleId, | 77 | pub local_id: LocalModuleId, |
74 | } | 78 | } |
75 | 79 | ||
80 | impl ModuleId { | ||
81 | pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> { | ||
82 | db.crate_def_map(self.krate) | ||
83 | } | ||
84 | } | ||
85 | |||
76 | /// An ID of a module, **local** to a specific crate | 86 | /// An ID of a module, **local** to a specific crate |
77 | pub type LocalModuleId = Idx<nameres::ModuleData>; | 87 | pub type LocalModuleId = Idx<nameres::ModuleData>; |
78 | 88 | ||
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<'_> { | |||
578 | } else if m.krate != self.def_map.krate { | 578 | } else if m.krate != self.def_map.krate { |
579 | mark::hit!(glob_across_crates); | 579 | mark::hit!(glob_across_crates); |
580 | // glob import from other crate => we can just import everything once | 580 | // glob import from other crate => we can just import everything once |
581 | let item_map = self.db.crate_def_map(m.krate); | 581 | let item_map = m.def_map(self.db); |
582 | let scope = &item_map[m.local_id].scope; | 582 | let scope = &item_map[m.local_id].scope; |
583 | 583 | ||
584 | // Module scoped macros is included | 584 | // 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 { | |||
243 | kind: PathKind::Super(0), | 243 | kind: PathKind::Super(0), |
244 | }; | 244 | }; |
245 | log::debug!("resolving {:?} in other crate", path); | 245 | log::debug!("resolving {:?} in other crate", path); |
246 | let defp_map = db.crate_def_map(module.krate); | 246 | let defp_map = module.def_map(db); |
247 | let (def, s) = defp_map.resolve_path(db, module.local_id, &path, shadow); | 247 | let (def, s) = defp_map.resolve_path(db, module.local_id, &path, shadow); |
248 | return ResolvePathResult::with( | 248 | return ResolvePathResult::with( |
249 | def, | 249 | def, |
@@ -356,7 +356,7 @@ impl DefMap { | |||
356 | self | 356 | self |
357 | } else { | 357 | } else { |
358 | // Extend lifetime | 358 | // Extend lifetime |
359 | keep = db.crate_def_map(prelude.krate); | 359 | keep = prelude.def_map(db); |
360 | &keep | 360 | &keep |
361 | }; | 361 | }; |
362 | def_map[prelude.local_id].scope.get(name) | 362 | 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 { | |||
430 | for scope in &self.scopes { | 430 | for scope in &self.scopes { |
431 | if let Scope::ModuleScope(m) = scope { | 431 | if let Scope::ModuleScope(m) = scope { |
432 | if let Some(prelude) = m.crate_def_map.prelude() { | 432 | if let Some(prelude) = m.crate_def_map.prelude() { |
433 | let prelude_def_map = db.crate_def_map(prelude.krate); | 433 | let prelude_def_map = prelude.def_map(db); |
434 | traits.extend(prelude_def_map[prelude.local_id].scope.traits()); | 434 | traits.extend(prelude_def_map[prelude.local_id].scope.traits()); |
435 | } | 435 | } |
436 | traits.extend(m.crate_def_map[m.module_id].scope.traits()); | 436 | traits.extend(m.crate_def_map[m.module_id].scope.traits()); |
@@ -529,7 +529,7 @@ impl Scope { | |||
529 | f(name.clone(), ScopeDef::PerNs(def)); | 529 | f(name.clone(), ScopeDef::PerNs(def)); |
530 | }); | 530 | }); |
531 | if let Some(prelude) = m.crate_def_map.prelude() { | 531 | if let Some(prelude) = m.crate_def_map.prelude() { |
532 | let prelude_def_map = db.crate_def_map(prelude.krate); | 532 | let prelude_def_map = prelude.def_map(db); |
533 | prelude_def_map[prelude.local_id].scope.entries().for_each(|(name, def)| { | 533 | prelude_def_map[prelude.local_id].scope.entries().for_each(|(name, def)| { |
534 | let seen_tuple = (name.clone(), def); | 534 | let seen_tuple = (name.clone(), def); |
535 | if !seen.contains(&seen_tuple) { | 535 | if !seen.contains(&seen_tuple) { |
@@ -633,7 +633,7 @@ pub trait HasResolver: Copy { | |||
633 | 633 | ||
634 | impl HasResolver for ModuleId { | 634 | impl HasResolver for ModuleId { |
635 | fn resolver(self, db: &dyn DefDatabase) -> Resolver { | 635 | fn resolver(self, db: &dyn DefDatabase) -> Resolver { |
636 | let def_map = db.crate_def_map(self.krate); | 636 | let def_map = self.def_map(db); |
637 | Resolver::default().push_module_scope(def_map, self.local_id) | 637 | Resolver::default().push_module_scope(def_map, self.local_id) |
638 | } | 638 | } |
639 | } | 639 | } |
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 { | |||
103 | if from_module.krate != to_module.krate { | 103 | if from_module.krate != to_module.krate { |
104 | return false; | 104 | return false; |
105 | } | 105 | } |
106 | let def_map = db.crate_def_map(from_module.krate); | 106 | let def_map = from_module.def_map(db); |
107 | self.is_visible_from_def_map(&def_map, from_module.local_id) | 107 | self.is_visible_from_def_map(&def_map, from_module.local_id) |
108 | } | 108 | } |
109 | 109 | ||