aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-01-18 20:12:11 +0000
committerGitHub <[email protected]>2021-01-18 20:12:11 +0000
commit0791c8e44c6fd1e7519df003dcb8c9e156916f46 (patch)
tree28468414f1ba4183859ebdf78c31f23b87ebda96 /crates/hir_def
parent8164e2ea31ed0c169bc295acb6e13650576e5366 (diff)
parent57a82fb05b46fd028c0b971dc6ce317db3682146 (diff)
Merge #7336
7336: Rename `CrateDefMap` to `DefMap` r=matklad a=jonas-schievink I propose handling local items by computing a `DefMap` for every block expression, using the regular (early) name resolution algorithm. The result of that will be a `DefMap` that has a reference to the parent `DefMap`, which is either the one computed for the containing block expression, or the crate's root `DefMap`. Name resolution will fall back to a name in the parent `DefMap` if it cannot be resolved in the inner block. The `DefMap`s computed for block expressions will go through a separate query that can be garbage-collected much more aggressively, since these `DefMap`s should be cheap to compute and are never part of a crate's public API. The first step towards that is to make `CrateDefMap` not specific to crates anymore, hence this rename (if this plans sounds reasonable). cc https://github.com/rust-analyzer/rust-analyzer/issues/7325 and https://github.com/rust-analyzer/rust-analyzer/issues/1165 Co-authored-by: Jonas Schievink <[email protected]>
Diffstat (limited to 'crates/hir_def')
-rw-r--r--crates/hir_def/src/body.rs4
-rw-r--r--crates/hir_def/src/db.rs10
-rw-r--r--crates/hir_def/src/find_path.rs4
-rw-r--r--crates/hir_def/src/nameres.rs12
-rw-r--r--crates/hir_def/src/nameres/collector.rs14
-rw-r--r--crates/hir_def/src/nameres/path_resolution.rs4
-rw-r--r--crates/hir_def/src/nameres/tests.rs2
-rw-r--r--crates/hir_def/src/resolver.rs12
-rw-r--r--crates/hir_def/src/visibility.rs6
9 files changed, 32 insertions, 36 deletions
diff --git a/crates/hir_def/src/body.rs b/crates/hir_def/src/body.rs
index 344f0b6c0..3b2dd0f6e 100644
--- a/crates/hir_def/src/body.rs
+++ b/crates/hir_def/src/body.rs
@@ -29,7 +29,7 @@ use crate::{
29 expr::{Expr, ExprId, Label, LabelId, Pat, PatId}, 29 expr::{Expr, ExprId, Label, LabelId, Pat, PatId},
30 item_scope::BuiltinShadowMode, 30 item_scope::BuiltinShadowMode,
31 item_scope::ItemScope, 31 item_scope::ItemScope,
32 nameres::CrateDefMap, 32 nameres::DefMap,
33 path::{ModPath, Path}, 33 path::{ModPath, Path},
34 src::HasSource, 34 src::HasSource,
35 AsMacroCall, DefWithBodyId, HasModule, Lookup, ModuleId, 35 AsMacroCall, DefWithBodyId, HasModule, Lookup, ModuleId,
@@ -45,7 +45,7 @@ pub(crate) struct CfgExpander {
45 45
46pub(crate) struct Expander { 46pub(crate) struct Expander {
47 cfg_expander: CfgExpander, 47 cfg_expander: CfgExpander,
48 crate_def_map: Arc<CrateDefMap>, 48 crate_def_map: Arc<DefMap>,
49 current_file_id: HirFileId, 49 current_file_id: HirFileId,
50 ast_id_map: Arc<AstIdMap>, 50 ast_id_map: Arc<AstIdMap>,
51 module: ModuleId, 51 module: ModuleId,
diff --git a/crates/hir_def/src/db.rs b/crates/hir_def/src/db.rs
index 6ef9fe790..91c8d45cd 100644
--- a/crates/hir_def/src/db.rs
+++ b/crates/hir_def/src/db.rs
@@ -15,7 +15,7 @@ use crate::{
15 import_map::ImportMap, 15 import_map::ImportMap,
16 item_tree::ItemTree, 16 item_tree::ItemTree,
17 lang_item::{LangItemTarget, LangItems}, 17 lang_item::{LangItemTarget, LangItems},
18 nameres::CrateDefMap, 18 nameres::DefMap,
19 AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId, FunctionLoc, 19 AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId, FunctionLoc,
20 GenericDefId, ImplId, ImplLoc, LocalEnumVariantId, LocalFieldId, StaticId, StaticLoc, StructId, 20 GenericDefId, ImplId, ImplLoc, LocalEnumVariantId, LocalFieldId, StaticId, StaticLoc, StructId,
21 StructLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, VariantId, 21 StructLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, VariantId,
@@ -50,10 +50,10 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
50 50
51 #[salsa::invoke(crate_def_map_wait)] 51 #[salsa::invoke(crate_def_map_wait)]
52 #[salsa::transparent] 52 #[salsa::transparent]
53 fn crate_def_map(&self, krate: CrateId) -> Arc<CrateDefMap>; 53 fn crate_def_map(&self, krate: CrateId) -> Arc<DefMap>;
54 54
55 #[salsa::invoke(CrateDefMap::crate_def_map_query)] 55 #[salsa::invoke(DefMap::crate_def_map_query)]
56 fn crate_def_map_query(&self, krate: CrateId) -> Arc<CrateDefMap>; 56 fn crate_def_map_query(&self, krate: CrateId) -> Arc<DefMap>;
57 57
58 #[salsa::invoke(StructData::struct_data_query)] 58 #[salsa::invoke(StructData::struct_data_query)]
59 fn struct_data(&self, id: StructId) -> Arc<StructData>; 59 fn struct_data(&self, id: StructId) -> Arc<StructData>;
@@ -112,7 +112,7 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
112 fn import_map(&self, krate: CrateId) -> Arc<ImportMap>; 112 fn import_map(&self, krate: CrateId) -> Arc<ImportMap>;
113} 113}
114 114
115fn crate_def_map_wait(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> { 115fn crate_def_map_wait(db: &impl DefDatabase, krate: CrateId) -> Arc<DefMap> {
116 let _p = profile::span("crate_def_map:wait"); 116 let _p = profile::span("crate_def_map:wait");
117 db.crate_def_map_query(krate) 117 db.crate_def_map_query(krate)
118} 118}
diff --git a/crates/hir_def/src/find_path.rs b/crates/hir_def/src/find_path.rs
index 4a212d291..422a6eeb4 100644
--- a/crates/hir_def/src/find_path.rs
+++ b/crates/hir_def/src/find_path.rs
@@ -4,7 +4,7 @@ use hir_expand::name::{known, AsName, Name};
4use rustc_hash::FxHashSet; 4use rustc_hash::FxHashSet;
5use test_utils::mark; 5use test_utils::mark;
6 6
7use crate::nameres::CrateDefMap; 7use crate::nameres::DefMap;
8use crate::{ 8use crate::{
9 db::DefDatabase, 9 db::DefDatabase,
10 item_scope::ItemInNs, 10 item_scope::ItemInNs,
@@ -47,7 +47,7 @@ impl ModPath {
47 } 47 }
48} 48}
49 49
50fn check_self_super(def_map: &CrateDefMap, item: ItemInNs, from: ModuleId) -> Option<ModPath> { 50fn check_self_super(def_map: &DefMap, item: ItemInNs, from: ModuleId) -> Option<ModPath> {
51 if item == ItemInNs::Types(from.into()) { 51 if item == ItemInNs::Types(from.into()) {
52 // - if the item is the module we're in, use `self` 52 // - if the item is the module we're in, use `self`
53 Some(ModPath::from_segments(PathKind::Super(0), Vec::new())) 53 Some(ModPath::from_segments(PathKind::Super(0), Vec::new()))
diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs
index 50acc3f54..769a557ad 100644
--- a/crates/hir_def/src/nameres.rs
+++ b/crates/hir_def/src/nameres.rs
@@ -74,7 +74,7 @@ use crate::{
74 74
75/// Contains all top-level defs from a macro-expanded crate 75/// Contains all top-level defs from a macro-expanded crate
76#[derive(Debug, PartialEq, Eq)] 76#[derive(Debug, PartialEq, Eq)]
77pub struct CrateDefMap { 77pub struct DefMap {
78 pub root: LocalModuleId, 78 pub root: LocalModuleId,
79 pub modules: Arena<ModuleData>, 79 pub modules: Arena<ModuleData>,
80 pub(crate) krate: CrateId, 80 pub(crate) krate: CrateId,
@@ -88,7 +88,7 @@ pub struct CrateDefMap {
88 diagnostics: Vec<DefDiagnostic>, 88 diagnostics: Vec<DefDiagnostic>,
89} 89}
90 90
91impl std::ops::Index<LocalModuleId> for CrateDefMap { 91impl std::ops::Index<LocalModuleId> for DefMap {
92 type Output = ModuleData; 92 type Output = ModuleData;
93 fn index(&self, id: LocalModuleId) -> &ModuleData { 93 fn index(&self, id: LocalModuleId) -> &ModuleData {
94 &self.modules[id] 94 &self.modules[id]
@@ -169,8 +169,8 @@ pub struct ModuleData {
169 pub origin: ModuleOrigin, 169 pub origin: ModuleOrigin,
170} 170}
171 171
172impl CrateDefMap { 172impl DefMap {
173 pub(crate) fn crate_def_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<CrateDefMap> { 173 pub(crate) fn crate_def_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<DefMap> {
174 let _p = profile::span("crate_def_map_query").detail(|| { 174 let _p = profile::span("crate_def_map_query").detail(|| {
175 db.crate_graph()[krate].display_name.as_deref().unwrap_or_default().to_string() 175 db.crate_graph()[krate].display_name.as_deref().unwrap_or_default().to_string()
176 }); 176 });
@@ -178,7 +178,7 @@ impl CrateDefMap {
178 let edition = db.crate_graph()[krate].edition; 178 let edition = db.crate_graph()[krate].edition;
179 let mut modules: Arena<ModuleData> = Arena::default(); 179 let mut modules: Arena<ModuleData> = Arena::default();
180 let root = modules.alloc(ModuleData::default()); 180 let root = modules.alloc(ModuleData::default());
181 CrateDefMap { 181 DefMap {
182 krate, 182 krate,
183 edition, 183 edition,
184 extern_prelude: FxHashMap::default(), 184 extern_prelude: FxHashMap::default(),
@@ -227,7 +227,7 @@ impl CrateDefMap {
227 go(&mut buf, self, "crate", self.root); 227 go(&mut buf, self, "crate", self.root);
228 return buf; 228 return buf;
229 229
230 fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: LocalModuleId) { 230 fn go(buf: &mut String, map: &DefMap, path: &str, module: LocalModuleId) {
231 format_to!(buf, "{}\n", path); 231 format_to!(buf, "{}\n", path);
232 232
233 let mut entries: Vec<_> = map.modules[module].scope.resolutions().collect(); 233 let mut entries: Vec<_> = map.modules[module].scope.resolutions().collect();
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs
index 0cd61698c..61da56340 100644
--- a/crates/hir_def/src/nameres/collector.rs
+++ b/crates/hir_def/src/nameres/collector.rs
@@ -31,7 +31,7 @@ use crate::{
31 }, 31 },
32 nameres::{ 32 nameres::{
33 diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, 33 diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint,
34 BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, ResolveMode, 34 BuiltinShadowMode, DefMap, ModuleData, ModuleOrigin, ResolveMode,
35 }, 35 },
36 path::{ImportAlias, ModPath, PathKind}, 36 path::{ImportAlias, ModPath, PathKind},
37 per_ns::PerNs, 37 per_ns::PerNs,
@@ -45,7 +45,7 @@ const GLOB_RECURSION_LIMIT: usize = 100;
45const EXPANSION_DEPTH_LIMIT: usize = 128; 45const EXPANSION_DEPTH_LIMIT: usize = 128;
46const FIXED_POINT_LIMIT: usize = 8192; 46const FIXED_POINT_LIMIT: usize = 8192;
47 47
48pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { 48pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: DefMap) -> DefMap {
49 let crate_graph = db.crate_graph(); 49 let crate_graph = db.crate_graph();
50 50
51 // populate external prelude 51 // populate external prelude
@@ -210,7 +210,7 @@ struct DefData<'a> {
210/// Walks the tree of module recursively 210/// Walks the tree of module recursively
211struct DefCollector<'a> { 211struct DefCollector<'a> {
212 db: &'a dyn DefDatabase, 212 db: &'a dyn DefDatabase,
213 def_map: CrateDefMap, 213 def_map: DefMap,
214 glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, Visibility)>>, 214 glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, Visibility)>>,
215 unresolved_imports: Vec<ImportDirective>, 215 unresolved_imports: Vec<ImportDirective>,
216 resolved_imports: Vec<ImportDirective>, 216 resolved_imports: Vec<ImportDirective>,
@@ -859,7 +859,7 @@ impl DefCollector<'_> {
859 .collect(item_tree.top_level_items()); 859 .collect(item_tree.top_level_items());
860 } 860 }
861 861
862 fn finish(mut self) -> CrateDefMap { 862 fn finish(mut self) -> DefMap {
863 // Emit diagnostics for all remaining unexpanded macros. 863 // Emit diagnostics for all remaining unexpanded macros.
864 864
865 for directive in &self.unexpanded_macros { 865 for directive in &self.unexpanded_macros {
@@ -1474,7 +1474,7 @@ mod tests {
1474 1474
1475 use super::*; 1475 use super::*;
1476 1476
1477 fn do_collect_defs(db: &dyn DefDatabase, def_map: CrateDefMap) -> CrateDefMap { 1477 fn do_collect_defs(db: &dyn DefDatabase, def_map: DefMap) -> DefMap {
1478 let mut collector = DefCollector { 1478 let mut collector = DefCollector {
1479 db, 1479 db,
1480 def_map, 1480 def_map,
@@ -1493,7 +1493,7 @@ mod tests {
1493 collector.def_map 1493 collector.def_map
1494 } 1494 }
1495 1495
1496 fn do_resolve(code: &str) -> CrateDefMap { 1496 fn do_resolve(code: &str) -> DefMap {
1497 let (db, _file_id) = TestDB::with_single_file(&code); 1497 let (db, _file_id) = TestDB::with_single_file(&code);
1498 let krate = db.test_crate(); 1498 let krate = db.test_crate();
1499 1499
@@ -1501,7 +1501,7 @@ mod tests {
1501 let edition = db.crate_graph()[krate].edition; 1501 let edition = db.crate_graph()[krate].edition;
1502 let mut modules: Arena<ModuleData> = Arena::default(); 1502 let mut modules: Arena<ModuleData> = Arena::default();
1503 let root = modules.alloc(ModuleData::default()); 1503 let root = modules.alloc(ModuleData::default());
1504 CrateDefMap { 1504 DefMap {
1505 krate, 1505 krate,
1506 edition, 1506 edition,
1507 extern_prelude: FxHashMap::default(), 1507 extern_prelude: FxHashMap::default(),
diff --git a/crates/hir_def/src/nameres/path_resolution.rs b/crates/hir_def/src/nameres/path_resolution.rs
index 88e10574e..096a7d0ac 100644
--- a/crates/hir_def/src/nameres/path_resolution.rs
+++ b/crates/hir_def/src/nameres/path_resolution.rs
@@ -19,7 +19,7 @@ use test_utils::mark;
19use crate::{ 19use crate::{
20 db::DefDatabase, 20 db::DefDatabase,
21 item_scope::BUILTIN_SCOPE, 21 item_scope::BUILTIN_SCOPE,
22 nameres::{BuiltinShadowMode, CrateDefMap}, 22 nameres::{BuiltinShadowMode, DefMap},
23 path::{ModPath, PathKind}, 23 path::{ModPath, PathKind},
24 per_ns::PerNs, 24 per_ns::PerNs,
25 visibility::{RawVisibility, Visibility}, 25 visibility::{RawVisibility, Visibility},
@@ -61,7 +61,7 @@ impl ResolvePathResult {
61 } 61 }
62} 62}
63 63
64impl CrateDefMap { 64impl DefMap {
65 pub(super) fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs { 65 pub(super) fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs {
66 self.extern_prelude 66 self.extern_prelude
67 .get(name) 67 .get(name)
diff --git a/crates/hir_def/src/nameres/tests.rs b/crates/hir_def/src/nameres/tests.rs
index c459fa66d..723481c36 100644
--- a/crates/hir_def/src/nameres/tests.rs
+++ b/crates/hir_def/src/nameres/tests.rs
@@ -13,7 +13,7 @@ use test_utils::mark;
13 13
14use crate::{db::DefDatabase, nameres::*, test_db::TestDB}; 14use crate::{db::DefDatabase, nameres::*, test_db::TestDB};
15 15
16fn compute_crate_def_map(ra_fixture: &str) -> Arc<CrateDefMap> { 16fn compute_crate_def_map(ra_fixture: &str) -> Arc<DefMap> {
17 let db = TestDB::with_files(ra_fixture); 17 let db = TestDB::with_files(ra_fixture);
18 let krate = db.crate_graph().iter().next().unwrap(); 18 let krate = db.crate_graph().iter().next().unwrap();
19 db.crate_def_map(krate) 19 db.crate_def_map(krate)
diff --git a/crates/hir_def/src/resolver.rs b/crates/hir_def/src/resolver.rs
index 85ddc2c47..e7e92c72d 100644
--- a/crates/hir_def/src/resolver.rs
+++ b/crates/hir_def/src/resolver.rs
@@ -16,7 +16,7 @@ use crate::{
16 expr::{ExprId, PatId}, 16 expr::{ExprId, PatId},
17 generics::GenericParams, 17 generics::GenericParams,
18 item_scope::{BuiltinShadowMode, BUILTIN_SCOPE}, 18 item_scope::{BuiltinShadowMode, BUILTIN_SCOPE},
19 nameres::CrateDefMap, 19 nameres::DefMap,
20 path::{ModPath, PathKind}, 20 path::{ModPath, PathKind},
21 per_ns::PerNs, 21 per_ns::PerNs,
22 visibility::{RawVisibility, Visibility}, 22 visibility::{RawVisibility, Visibility},
@@ -34,7 +34,7 @@ pub struct Resolver {
34// FIXME how to store these best 34// FIXME how to store these best
35#[derive(Debug, Clone)] 35#[derive(Debug, Clone)]
36struct ModuleItemMap { 36struct ModuleItemMap {
37 crate_def_map: Arc<CrateDefMap>, 37 crate_def_map: Arc<DefMap>,
38 module_id: LocalModuleId, 38 module_id: LocalModuleId,
39} 39}
40 40
@@ -425,7 +425,7 @@ impl Resolver {
425 traits 425 traits
426 } 426 }
427 427
428 fn module_scope(&self) -> Option<(&CrateDefMap, LocalModuleId)> { 428 fn module_scope(&self) -> Option<(&DefMap, LocalModuleId)> {
429 self.scopes.iter().rev().find_map(|scope| match scope { 429 self.scopes.iter().rev().find_map(|scope| match scope {
430 Scope::ModuleScope(m) => Some((&*m.crate_def_map, m.module_id)), 430 Scope::ModuleScope(m) => Some((&*m.crate_def_map, m.module_id)),
431 431
@@ -588,11 +588,7 @@ impl Resolver {
588 self.push_scope(Scope::ImplDefScope(impl_def)) 588 self.push_scope(Scope::ImplDefScope(impl_def))
589 } 589 }
590 590
591 fn push_module_scope( 591 fn push_module_scope(self, crate_def_map: Arc<DefMap>, module_id: LocalModuleId) -> Resolver {
592 self,
593 crate_def_map: Arc<CrateDefMap>,
594 module_id: LocalModuleId,
595 ) -> Resolver {
596 self.push_scope(Scope::ModuleScope(ModuleItemMap { crate_def_map, module_id })) 592 self.push_scope(Scope::ModuleScope(ModuleItemMap { crate_def_map, module_id }))
597 } 593 }
598 594
diff --git a/crates/hir_def/src/visibility.rs b/crates/hir_def/src/visibility.rs
index f3bc9d680..3134fa43d 100644
--- a/crates/hir_def/src/visibility.rs
+++ b/crates/hir_def/src/visibility.rs
@@ -5,7 +5,7 @@ use syntax::ast;
5 5
6use crate::{ 6use crate::{
7 db::DefDatabase, 7 db::DefDatabase,
8 nameres::CrateDefMap, 8 nameres::DefMap,
9 path::{ModPath, PathKind}, 9 path::{ModPath, PathKind},
10 ModuleId, 10 ModuleId,
11}; 11};
@@ -116,7 +116,7 @@ impl Visibility {
116 116
117 pub(crate) fn is_visible_from_def_map( 117 pub(crate) fn is_visible_from_def_map(
118 self, 118 self,
119 def_map: &CrateDefMap, 119 def_map: &DefMap,
120 from_module: crate::LocalModuleId, 120 from_module: crate::LocalModuleId,
121 ) -> bool { 121 ) -> bool {
122 let to_module = match self { 122 let to_module = match self {
@@ -135,7 +135,7 @@ impl Visibility {
135 /// 135 ///
136 /// If there is no subset relation between `self` and `other`, returns `None` (ie. they're only 136 /// If there is no subset relation between `self` and `other`, returns `None` (ie. they're only
137 /// visible in unrelated modules). 137 /// visible in unrelated modules).
138 pub(crate) fn max(self, other: Visibility, def_map: &CrateDefMap) -> Option<Visibility> { 138 pub(crate) fn max(self, other: Visibility, def_map: &DefMap) -> Option<Visibility> {
139 match (self, other) { 139 match (self, other) {
140 (Visibility::Module(_), Visibility::Public) 140 (Visibility::Module(_), Visibility::Public)
141 | (Visibility::Public, Visibility::Module(_)) 141 | (Visibility::Public, Visibility::Module(_))