diff options
Diffstat (limited to 'crates/hir_def/src')
-rw-r--r-- | crates/hir_def/src/find_path.rs | 27 | ||||
-rw-r--r-- | crates/hir_def/src/import_map.rs | 2 | ||||
-rw-r--r-- | crates/hir_def/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/hir_def/src/nameres.rs | 4 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/collector.rs | 27 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/path_resolution.rs | 24 | ||||
-rw-r--r-- | crates/hir_def/src/resolver.rs | 2 | ||||
-rw-r--r-- | crates/hir_def/src/test_db.rs | 6 |
8 files changed, 33 insertions, 63 deletions
diff --git a/crates/hir_def/src/find_path.rs b/crates/hir_def/src/find_path.rs index c01b6daf2..94a1d567d 100644 --- a/crates/hir_def/src/find_path.rs +++ b/crates/hir_def/src/find_path.rs | |||
@@ -53,12 +53,8 @@ fn check_self_super(def_map: &DefMap, item: ItemInNs, from: ModuleId) -> Option< | |||
53 | Some(ModPath::from_segments(PathKind::Super(0), Vec::new())) | 53 | Some(ModPath::from_segments(PathKind::Super(0), Vec::new())) |
54 | } else if let Some(parent_id) = def_map[from.local_id].parent { | 54 | } else if let Some(parent_id) = def_map[from.local_id].parent { |
55 | // - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly) | 55 | // - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly) |
56 | if item | 56 | let parent_id = def_map.module_id(parent_id); |
57 | == ItemInNs::Types(ModuleDefId::ModuleId(ModuleId { | 57 | if item == ItemInNs::Types(ModuleDefId::ModuleId(parent_id)) { |
58 | krate: from.krate, | ||
59 | local_id: parent_id, | ||
60 | })) | ||
61 | { | ||
62 | Some(ModPath::from_segments(PathKind::Super(1), Vec::new())) | 58 | Some(ModPath::from_segments(PathKind::Super(1), Vec::new())) |
63 | } else { | 59 | } else { |
64 | None | 60 | None |
@@ -120,12 +116,8 @@ fn find_path_inner( | |||
120 | } | 116 | } |
121 | 117 | ||
122 | // - if the item is the crate root, return `crate` | 118 | // - if the item is the crate root, return `crate` |
123 | if item | 119 | let root = def_map.module_id(def_map.root()); |
124 | == ItemInNs::Types(ModuleDefId::ModuleId(ModuleId { | 120 | if item == ItemInNs::Types(ModuleDefId::ModuleId(root)) { |
125 | krate: from.krate, | ||
126 | local_id: def_map.root(), | ||
127 | })) | ||
128 | { | ||
129 | return Some(ModPath::from_segments(PathKind::Crate, Vec::new())); | 121 | return Some(ModPath::from_segments(PathKind::Crate, Vec::new())); |
130 | } | 122 | } |
131 | 123 | ||
@@ -175,7 +167,7 @@ fn find_path_inner( | |||
175 | 167 | ||
176 | // - otherwise, look for modules containing (reexporting) it and import it from one of those | 168 | // - otherwise, look for modules containing (reexporting) it and import it from one of those |
177 | 169 | ||
178 | let crate_root = ModuleId { local_id: def_map.root(), krate: from.krate }; | 170 | let crate_root = def_map.module_id(def_map.root()); |
179 | let crate_attrs = db.attrs(crate_root.into()); | 171 | let crate_attrs = db.attrs(crate_root.into()); |
180 | let prefer_no_std = crate_attrs.by_key("no_std").exists(); | 172 | let prefer_no_std = crate_attrs.by_key("no_std").exists(); |
181 | let mut best_path = None; | 173 | let mut best_path = None; |
@@ -288,14 +280,11 @@ fn find_local_import_locations( | |||
288 | // Compute the initial worklist. We start with all direct child modules of `from` as well as all | 280 | // Compute the initial worklist. We start with all direct child modules of `from` as well as all |
289 | // of its (recursive) parent modules. | 281 | // of its (recursive) parent modules. |
290 | let data = &def_map[from.local_id]; | 282 | let data = &def_map[from.local_id]; |
291 | let mut worklist = data | 283 | let mut worklist = |
292 | .children | 284 | data.children.values().map(|child| def_map.module_id(*child)).collect::<Vec<_>>(); |
293 | .values() | ||
294 | .map(|child| ModuleId { krate: from.krate, local_id: *child }) | ||
295 | .collect::<Vec<_>>(); | ||
296 | let mut parent = data.parent; | 285 | let mut parent = data.parent; |
297 | while let Some(p) = parent { | 286 | while let Some(p) = parent { |
298 | worklist.push(ModuleId { krate: from.krate, local_id: p }); | 287 | worklist.push(def_map.module_id(p)); |
299 | parent = def_map[p].parent; | 288 | parent = def_map[p].parent; |
300 | } | 289 | } |
301 | 290 | ||
diff --git a/crates/hir_def/src/import_map.rs b/crates/hir_def/src/import_map.rs index 0b7830445..0a3dc7956 100644 --- a/crates/hir_def/src/import_map.rs +++ b/crates/hir_def/src/import_map.rs | |||
@@ -75,7 +75,7 @@ impl ImportMap { | |||
75 | 75 | ||
76 | // We look only into modules that are public(ly reexported), starting with the crate root. | 76 | // We look only into modules that are public(ly reexported), starting with the crate root. |
77 | let empty = ImportPath { segments: vec![] }; | 77 | let empty = ImportPath { segments: vec![] }; |
78 | let root = ModuleId { krate, local_id: def_map.root() }; | 78 | let root = def_map.module_id(def_map.root()); |
79 | let mut worklist = vec![(root, empty)]; | 79 | let mut worklist = vec![(root, empty)]; |
80 | while let Some((module, mod_path)) = worklist.pop() { | 80 | while let Some((module, mod_path)) = worklist.pop() { |
81 | let ext_def_map; | 81 | let ext_def_map; |
diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index c8dbb2aeb..cf09ebd3f 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs | |||
@@ -78,10 +78,6 @@ pub struct ModuleId { | |||
78 | } | 78 | } |
79 | 79 | ||
80 | impl ModuleId { | 80 | impl ModuleId { |
81 | pub fn top_level(krate: CrateId, local_id: LocalModuleId) -> Self { | ||
82 | Self { krate, local_id } | ||
83 | } | ||
84 | |||
85 | pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> { | 81 | pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> { |
86 | db.crate_def_map(self.krate) | 82 | db.crate_def_map(self.krate) |
87 | } | 83 | } |
diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index bd3ea9b8b..4fbbecb38 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs | |||
@@ -265,6 +265,10 @@ impl DefMap { | |||
265 | self.extern_prelude.iter() | 265 | self.extern_prelude.iter() |
266 | } | 266 | } |
267 | 267 | ||
268 | pub fn module_id(&self, local_id: LocalModuleId) -> ModuleId { | ||
269 | ModuleId { krate: self.krate, local_id } | ||
270 | } | ||
271 | |||
268 | pub(crate) fn resolve_path( | 272 | pub(crate) fn resolve_path( |
269 | &self, | 273 | &self, |
270 | db: &dyn DefDatabase, | 274 | db: &dyn DefDatabase, |
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index adfcf879a..393170b32 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs | |||
@@ -37,8 +37,8 @@ use crate::{ | |||
37 | per_ns::PerNs, | 37 | per_ns::PerNs, |
38 | visibility::{RawVisibility, Visibility}, | 38 | visibility::{RawVisibility, Visibility}, |
39 | AdtId, AsMacroCall, AstId, AstIdWithPath, ConstLoc, ContainerId, EnumLoc, EnumVariantId, | 39 | AdtId, AsMacroCall, AstId, AstIdWithPath, ConstLoc, ContainerId, EnumLoc, EnumVariantId, |
40 | FunctionLoc, ImplLoc, Intern, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, | 40 | FunctionLoc, ImplLoc, Intern, LocalModuleId, ModuleDefId, StaticLoc, StructLoc, TraitLoc, |
41 | TraitLoc, TypeAliasLoc, UnionLoc, | 41 | TypeAliasLoc, UnionLoc, |
42 | }; | 42 | }; |
43 | 43 | ||
44 | const GLOB_RECURSION_LIMIT: usize = 100; | 44 | const GLOB_RECURSION_LIMIT: usize = 100; |
@@ -56,10 +56,9 @@ pub(super) fn collect_defs( | |||
56 | for dep in &crate_graph[def_map.krate].dependencies { | 56 | for dep in &crate_graph[def_map.krate].dependencies { |
57 | log::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id); | 57 | log::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id); |
58 | let dep_def_map = db.crate_def_map(dep.crate_id); | 58 | let dep_def_map = db.crate_def_map(dep.crate_id); |
59 | def_map.extern_prelude.insert( | 59 | def_map |
60 | dep.as_name(), | 60 | .extern_prelude |
61 | ModuleId { krate: dep.crate_id, local_id: dep_def_map.root }.into(), | 61 | .insert(dep.as_name(), dep_def_map.module_id(dep_def_map.root).into()); |
62 | ); | ||
63 | 62 | ||
64 | // look for the prelude | 63 | // look for the prelude |
65 | // If the dependency defines a prelude, we overwrite an already defined | 64 | // If the dependency defines a prelude, we overwrite an already defined |
@@ -332,11 +331,9 @@ impl DefCollector<'_> { | |||
332 | // exported in type/value namespace. This function reduces the visibility of all items | 331 | // exported in type/value namespace. This function reduces the visibility of all items |
333 | // in the crate root that aren't proc macros. | 332 | // in the crate root that aren't proc macros. |
334 | let root = self.def_map.root; | 333 | let root = self.def_map.root; |
334 | let module_id = self.def_map.module_id(root); | ||
335 | let root = &mut self.def_map.modules[root]; | 335 | let root = &mut self.def_map.modules[root]; |
336 | root.scope.censor_non_proc_macros(ModuleId { | 336 | root.scope.censor_non_proc_macros(module_id); |
337 | krate: self.def_map.krate, | ||
338 | local_id: self.def_map.root, | ||
339 | }); | ||
340 | } | 337 | } |
341 | } | 338 | } |
342 | 339 | ||
@@ -1029,8 +1026,7 @@ impl ModCollector<'_, '_> { | |||
1029 | continue; | 1026 | continue; |
1030 | } | 1027 | } |
1031 | } | 1028 | } |
1032 | let module = | 1029 | let module = self.def_collector.def_map.module_id(self.module_id); |
1033 | ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id }; | ||
1034 | let container = ContainerId::ModuleId(module); | 1030 | let container = ContainerId::ModuleId(module); |
1035 | 1031 | ||
1036 | let mut def = None; | 1032 | let mut def = None; |
@@ -1097,10 +1093,7 @@ impl ModCollector<'_, '_> { | |||
1097 | } | 1093 | } |
1098 | } | 1094 | } |
1099 | ModItem::Impl(imp) => { | 1095 | ModItem::Impl(imp) => { |
1100 | let module = ModuleId { | 1096 | let module = self.def_collector.def_map.module_id(self.module_id); |
1101 | krate: self.def_collector.def_map.krate, | ||
1102 | local_id: self.module_id, | ||
1103 | }; | ||
1104 | let container = ContainerId::ModuleId(module); | 1097 | let container = ContainerId::ModuleId(module); |
1105 | let impl_id = ImplLoc { container, id: ItemTreeId::new(self.file_id, imp) } | 1098 | let impl_id = ImplLoc { container, id: ItemTreeId::new(self.file_id, imp) } |
1106 | .intern(self.def_collector.db); | 1099 | .intern(self.def_collector.db); |
@@ -1343,7 +1336,7 @@ impl ModCollector<'_, '_> { | |||
1343 | modules[res].scope.define_legacy_macro(name, mac) | 1336 | modules[res].scope.define_legacy_macro(name, mac) |
1344 | } | 1337 | } |
1345 | modules[self.module_id].children.insert(name.clone(), res); | 1338 | modules[self.module_id].children.insert(name.clone(), res); |
1346 | let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res }; | 1339 | let module = self.def_collector.def_map.module_id(res); |
1347 | let def: ModuleDefId = module.into(); | 1340 | let def: ModuleDefId = module.into(); |
1348 | self.def_collector.def_map.modules[self.module_id].scope.define_def(def); | 1341 | self.def_collector.def_map.modules[self.module_id].scope.define_def(def); |
1349 | self.def_collector.update( | 1342 | self.def_collector.update( |
diff --git a/crates/hir_def/src/nameres/path_resolution.rs b/crates/hir_def/src/nameres/path_resolution.rs index 7b5fe24a7..c1eded5f2 100644 --- a/crates/hir_def/src/nameres/path_resolution.rs +++ b/crates/hir_def/src/nameres/path_resolution.rs | |||
@@ -24,7 +24,7 @@ use crate::{ | |||
24 | path::{ModPath, PathKind}, | 24 | path::{ModPath, PathKind}, |
25 | per_ns::PerNs, | 25 | per_ns::PerNs, |
26 | visibility::{RawVisibility, Visibility}, | 26 | visibility::{RawVisibility, Visibility}, |
27 | AdtId, CrateId, EnumVariantId, LocalModuleId, ModuleDefId, ModuleId, | 27 | AdtId, CrateId, EnumVariantId, LocalModuleId, ModuleDefId, |
28 | }; | 28 | }; |
29 | 29 | ||
30 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 30 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
@@ -66,10 +66,7 @@ impl DefMap { | |||
66 | pub(super) fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs { | 66 | pub(super) fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs { |
67 | if name == &name!(self) { | 67 | if name == &name!(self) { |
68 | mark::hit!(extern_crate_self_as); | 68 | mark::hit!(extern_crate_self_as); |
69 | return PerNs::types( | 69 | return PerNs::types(self.module_id(self.root).into(), Visibility::Public); |
70 | ModuleId { krate: self.krate, local_id: self.root }.into(), | ||
71 | Visibility::Public, | ||
72 | ); | ||
73 | } | 70 | } |
74 | self.extern_prelude | 71 | self.extern_prelude |
75 | .get(name) | 72 | .get(name) |
@@ -154,21 +151,15 @@ impl DefMap { | |||
154 | PathKind::DollarCrate(krate) => { | 151 | PathKind::DollarCrate(krate) => { |
155 | if krate == self.krate { | 152 | if krate == self.krate { |
156 | mark::hit!(macro_dollar_crate_self); | 153 | mark::hit!(macro_dollar_crate_self); |
157 | PerNs::types( | 154 | PerNs::types(self.module_id(self.root).into(), Visibility::Public) |
158 | ModuleId { krate: self.krate, local_id: self.root }.into(), | ||
159 | Visibility::Public, | ||
160 | ) | ||
161 | } else { | 155 | } else { |
162 | let def_map = db.crate_def_map(krate); | 156 | let def_map = db.crate_def_map(krate); |
163 | let module = ModuleId { krate, local_id: def_map.root }; | 157 | let module = def_map.module_id(def_map.root); |
164 | mark::hit!(macro_dollar_crate_other); | 158 | mark::hit!(macro_dollar_crate_other); |
165 | PerNs::types(module.into(), Visibility::Public) | 159 | PerNs::types(module.into(), Visibility::Public) |
166 | } | 160 | } |
167 | } | 161 | } |
168 | PathKind::Crate => PerNs::types( | 162 | PathKind::Crate => PerNs::types(self.module_id(self.root).into(), Visibility::Public), |
169 | ModuleId { krate: self.krate, local_id: self.root }.into(), | ||
170 | Visibility::Public, | ||
171 | ), | ||
172 | // plain import or absolute path in 2015: crate-relative with | 163 | // plain import or absolute path in 2015: crate-relative with |
173 | // fallback to extern prelude (with the simplification in | 164 | // fallback to extern prelude (with the simplification in |
174 | // rust-lang/rust#57745) | 165 | // rust-lang/rust#57745) |
@@ -205,10 +196,7 @@ impl DefMap { | |||
205 | let m = successors(Some(original_module), |m| self.modules[*m].parent) | 196 | let m = successors(Some(original_module), |m| self.modules[*m].parent) |
206 | .nth(lvl as usize); | 197 | .nth(lvl as usize); |
207 | if let Some(local_id) = m { | 198 | if let Some(local_id) = m { |
208 | PerNs::types( | 199 | PerNs::types(self.module_id(local_id).into(), Visibility::Public) |
209 | ModuleId { krate: self.krate, local_id }.into(), | ||
210 | Visibility::Public, | ||
211 | ) | ||
212 | } else { | 200 | } else { |
213 | log::debug!("super path in root module"); | 201 | log::debug!("super path in root module"); |
214 | return ResolvePathResult::empty(ReachedFixedPoint::Yes); | 202 | return ResolvePathResult::empty(ReachedFixedPoint::Yes); |
diff --git a/crates/hir_def/src/resolver.rs b/crates/hir_def/src/resolver.rs index 130c074f0..9021ea712 100644 --- a/crates/hir_def/src/resolver.rs +++ b/crates/hir_def/src/resolver.rs | |||
@@ -459,7 +459,7 @@ impl Resolver { | |||
459 | 459 | ||
460 | pub fn module(&self) -> Option<ModuleId> { | 460 | pub fn module(&self) -> Option<ModuleId> { |
461 | let (def_map, local_id) = self.module_scope()?; | 461 | let (def_map, local_id) = self.module_scope()?; |
462 | Some(ModuleId { krate: def_map.krate(), local_id }) | 462 | Some(def_map.module_id(local_id)) |
463 | } | 463 | } |
464 | 464 | ||
465 | pub fn krate(&self) -> Option<CrateId> { | 465 | pub fn krate(&self) -> Option<CrateId> { |
diff --git a/crates/hir_def/src/test_db.rs b/crates/hir_def/src/test_db.rs index 4ff219fb7..c4e36eda5 100644 --- a/crates/hir_def/src/test_db.rs +++ b/crates/hir_def/src/test_db.rs | |||
@@ -15,7 +15,7 @@ use rustc_hash::FxHashSet; | |||
15 | use syntax::{TextRange, TextSize}; | 15 | use syntax::{TextRange, TextSize}; |
16 | use test_utils::extract_annotations; | 16 | use test_utils::extract_annotations; |
17 | 17 | ||
18 | use crate::{db::DefDatabase, ModuleDefId}; | 18 | use crate::{db::DefDatabase, ModuleDefId, ModuleId}; |
19 | 19 | ||
20 | #[salsa::database( | 20 | #[salsa::database( |
21 | base_db::SourceDatabaseExtStorage, | 21 | base_db::SourceDatabaseExtStorage, |
@@ -72,12 +72,12 @@ impl FileLoader for TestDB { | |||
72 | } | 72 | } |
73 | 73 | ||
74 | impl TestDB { | 74 | impl TestDB { |
75 | pub(crate) fn module_for_file(&self, file_id: FileId) -> crate::ModuleId { | 75 | pub(crate) fn module_for_file(&self, file_id: FileId) -> ModuleId { |
76 | for &krate in self.relevant_crates(file_id).iter() { | 76 | for &krate in self.relevant_crates(file_id).iter() { |
77 | let crate_def_map = self.crate_def_map(krate); | 77 | let crate_def_map = self.crate_def_map(krate); |
78 | for (local_id, data) in crate_def_map.modules() { | 78 | for (local_id, data) in crate_def_map.modules() { |
79 | if data.origin.file_id() == Some(file_id) { | 79 | if data.origin.file_id() == Some(file_id) { |
80 | return crate::ModuleId { krate, local_id }; | 80 | return crate_def_map.module_id(local_id); |
81 | } | 81 | } |
82 | } | 82 | } |
83 | } | 83 | } |