aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/nameres.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/nameres.rs')
-rw-r--r--crates/ra_hir_def/src/nameres.rs42
1 files changed, 20 insertions, 22 deletions
diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs
index 101203b7b..3b2e99647 100644
--- a/crates/ra_hir_def/src/nameres.rs
+++ b/crates/ra_hir_def/src/nameres.rs
@@ -47,8 +47,7 @@
47//! path and, upon success, we run macro expansion and "collect module" phase on 47//! path and, upon success, we run macro expansion and "collect module" phase on
48//! the result 48//! the result
49 49
50pub mod raw; 50pub(crate) mod raw;
51pub mod per_ns;
52mod collector; 51mod collector;
53mod mod_resolution; 52mod mod_resolution;
54mod path_resolution; 53mod path_resolution;
@@ -72,11 +71,10 @@ use rustc_hash::{FxHashMap, FxHashSet};
72use crate::{ 71use crate::{
73 builtin_type::BuiltinType, 72 builtin_type::BuiltinType,
74 db::DefDatabase, 73 db::DefDatabase,
75 nameres::{ 74 nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode},
76 diagnostics::DefDiagnostic, path_resolution::ResolveMode, per_ns::PerNs, raw::ImportId,
77 },
78 path::Path, 75 path::Path,
79 AstId, CrateModuleId, FunctionId, ImplId, ModuleDefId, ModuleId, TraitId, 76 per_ns::PerNs,
77 AstId, FunctionId, ImplId, LocalImportId, LocalModuleId, ModuleDefId, ModuleId, TraitId,
80}; 78};
81 79
82/// Contains all top-level defs from a macro-expanded crate 80/// Contains all top-level defs from a macro-expanded crate
@@ -89,8 +87,8 @@ pub struct CrateDefMap {
89 /// a dependency (`std` or `core`). 87 /// a dependency (`std` or `core`).
90 prelude: Option<ModuleId>, 88 prelude: Option<ModuleId>,
91 extern_prelude: FxHashMap<Name, ModuleDefId>, 89 extern_prelude: FxHashMap<Name, ModuleDefId>,
92 root: CrateModuleId, 90 root: LocalModuleId,
93 modules: Arena<CrateModuleId, ModuleData>, 91 modules: Arena<LocalModuleId, ModuleData>,
94 92
95 /// Some macros are not well-behavior, which leads to infinite loop 93 /// Some macros are not well-behavior, which leads to infinite loop
96 /// e.g. macro_rules! foo { ($ty:ty) => { foo!($ty); } } 94 /// e.g. macro_rules! foo { ($ty:ty) => { foo!($ty); } }
@@ -107,17 +105,17 @@ pub struct CrateDefMap {
107 diagnostics: Vec<DefDiagnostic>, 105 diagnostics: Vec<DefDiagnostic>,
108} 106}
109 107
110impl std::ops::Index<CrateModuleId> for CrateDefMap { 108impl std::ops::Index<LocalModuleId> for CrateDefMap {
111 type Output = ModuleData; 109 type Output = ModuleData;
112 fn index(&self, id: CrateModuleId) -> &ModuleData { 110 fn index(&self, id: LocalModuleId) -> &ModuleData {
113 &self.modules[id] 111 &self.modules[id]
114 } 112 }
115} 113}
116 114
117#[derive(Default, Debug, PartialEq, Eq)] 115#[derive(Default, Debug, PartialEq, Eq)]
118pub struct ModuleData { 116pub struct ModuleData {
119 pub parent: Option<CrateModuleId>, 117 pub parent: Option<LocalModuleId>,
120 pub children: FxHashMap<Name, CrateModuleId>, 118 pub children: FxHashMap<Name, LocalModuleId>,
121 pub scope: ModuleScope, 119 pub scope: ModuleScope,
122 120
123 // FIXME: these can't be both null, we need a three-state enum here. 121 // FIXME: these can't be both null, we need a three-state enum here.
@@ -213,7 +211,7 @@ pub struct Resolution {
213 /// None for unresolved 211 /// None for unresolved
214 pub def: PerNs, 212 pub def: PerNs,
215 /// ident by which this is imported into local scope. 213 /// ident by which this is imported into local scope.
216 pub import: Option<ImportId>, 214 pub import: Option<LocalImportId>,
217} 215}
218 216
219impl CrateDefMap { 217impl CrateDefMap {
@@ -227,7 +225,7 @@ impl CrateDefMap {
227 let def_map = { 225 let def_map = {
228 let crate_graph = db.crate_graph(); 226 let crate_graph = db.crate_graph();
229 let edition = crate_graph.edition(krate); 227 let edition = crate_graph.edition(krate);
230 let mut modules: Arena<CrateModuleId, ModuleData> = Arena::default(); 228 let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default();
231 let root = modules.alloc(ModuleData::default()); 229 let root = modules.alloc(ModuleData::default());
232 CrateDefMap { 230 CrateDefMap {
233 krate, 231 krate,
@@ -248,7 +246,7 @@ impl CrateDefMap {
248 self.krate 246 self.krate
249 } 247 }
250 248
251 pub fn root(&self) -> CrateModuleId { 249 pub fn root(&self) -> LocalModuleId {
252 self.root 250 self.root
253 } 251 }
254 252
@@ -263,7 +261,7 @@ impl CrateDefMap {
263 pub fn add_diagnostics( 261 pub fn add_diagnostics(
264 &self, 262 &self,
265 db: &impl DefDatabase, 263 db: &impl DefDatabase,
266 module: CrateModuleId, 264 module: LocalModuleId,
267 sink: &mut DiagnosticSink, 265 sink: &mut DiagnosticSink,
268 ) { 266 ) {
269 self.diagnostics.iter().for_each(|it| it.add_to(db, module, sink)) 267 self.diagnostics.iter().for_each(|it| it.add_to(db, module, sink))
@@ -272,18 +270,18 @@ impl CrateDefMap {
272 pub fn resolve_path( 270 pub fn resolve_path(
273 &self, 271 &self,
274 db: &impl DefDatabase, 272 db: &impl DefDatabase,
275 original_module: CrateModuleId, 273 original_module: LocalModuleId,
276 path: &Path, 274 path: &Path,
277 ) -> (PerNs, Option<usize>) { 275 ) -> (PerNs, Option<usize>) {
278 let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path); 276 let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path);
279 (res.resolved_def, res.segment_index) 277 (res.resolved_def, res.segment_index)
280 } 278 }
281 279
282 pub fn modules(&self) -> impl Iterator<Item = CrateModuleId> + '_ { 280 pub fn modules(&self) -> impl Iterator<Item = LocalModuleId> + '_ {
283 self.modules.iter().map(|(id, _data)| id) 281 self.modules.iter().map(|(id, _data)| id)
284 } 282 }
285 283
286 pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator<Item = CrateModuleId> + '_ { 284 pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator<Item = LocalModuleId> + '_ {
287 self.modules 285 self.modules
288 .iter() 286 .iter()
289 .filter(move |(_id, data)| data.definition == Some(file_id)) 287 .filter(move |(_id, data)| data.definition == Some(file_id))
@@ -319,12 +317,12 @@ mod diagnostics {
319 use ra_db::RelativePathBuf; 317 use ra_db::RelativePathBuf;
320 use ra_syntax::{ast, AstPtr}; 318 use ra_syntax::{ast, AstPtr};
321 319
322 use crate::{db::DefDatabase, diagnostics::UnresolvedModule, nameres::CrateModuleId, AstId}; 320 use crate::{db::DefDatabase, diagnostics::UnresolvedModule, nameres::LocalModuleId, AstId};
323 321
324 #[derive(Debug, PartialEq, Eq)] 322 #[derive(Debug, PartialEq, Eq)]
325 pub(super) enum DefDiagnostic { 323 pub(super) enum DefDiagnostic {
326 UnresolvedModule { 324 UnresolvedModule {
327 module: CrateModuleId, 325 module: LocalModuleId,
328 declaration: AstId<ast::Module>, 326 declaration: AstId<ast::Module>,
329 candidate: RelativePathBuf, 327 candidate: RelativePathBuf,
330 }, 328 },
@@ -334,7 +332,7 @@ mod diagnostics {
334 pub(super) fn add_to( 332 pub(super) fn add_to(
335 &self, 333 &self,
336 db: &impl DefDatabase, 334 db: &impl DefDatabase,
337 target_module: CrateModuleId, 335 target_module: LocalModuleId,
338 sink: &mut DiagnosticSink, 336 sink: &mut DiagnosticSink,
339 ) { 337 ) {
340 match self { 338 match self {