aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/nameres.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/nameres.rs')
-rw-r--r--crates/ra_hir/src/nameres.rs56
1 files changed, 26 insertions, 30 deletions
diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs
index 1d163edf7..a3bc98958 100644
--- a/crates/ra_hir/src/nameres.rs
+++ b/crates/ra_hir/src/nameres.rs
@@ -16,19 +16,19 @@
16//! structure itself is modified. 16//! structure itself is modified.
17pub(crate) mod lower; 17pub(crate) mod lower;
18 18
19use crate::nameres::lower::*;
20
21use std::sync::Arc; 19use std::sync::Arc;
22 20
21use ra_db::CrateId;
23use rustc_hash::{FxHashMap, FxHashSet}; 22use rustc_hash::{FxHashMap, FxHashSet};
24use ra_db::SourceRootId;
25 23
26use crate::{ 24use crate::{
27 DefId, DefLoc, DefKind, 25 Module, ModuleDef,
28 Path, PathKind, 26 Path, PathKind,
29 HirDatabase, Crate, 27 HirDatabase, Crate,
30 Name, 28 Name,
31 module_tree::{ModuleId, ModuleTree}, 29 module_tree::{ModuleId, ModuleTree},
30//FIXME: deglobify
31 nameres::lower::*,
32}; 32};
33 33
34/// `ItemMap` is the result of name resolution. It contains, for each 34/// `ItemMap` is the result of name resolution. It contains, for each
@@ -58,7 +58,7 @@ impl ModuleScope {
58#[derive(Debug, Clone, PartialEq, Eq)] 58#[derive(Debug, Clone, PartialEq, Eq)]
59pub struct Resolution { 59pub struct Resolution {
60 /// None for unresolved 60 /// None for unresolved
61 pub def_id: PerNs<DefId>, 61 pub def_id: PerNs<ModuleDef>,
62 /// ident by which this is imported into local scope. 62 /// ident by which this is imported into local scope.
63 pub import: Option<ImportId>, 63 pub import: Option<ImportId>,
64} 64}
@@ -152,7 +152,7 @@ impl<T> PerNs<T> {
152pub(crate) struct Resolver<'a, DB> { 152pub(crate) struct Resolver<'a, DB> {
153 db: &'a DB, 153 db: &'a DB,
154 input: &'a FxHashMap<ModuleId, Arc<LoweredModule>>, 154 input: &'a FxHashMap<ModuleId, Arc<LoweredModule>>,
155 source_root: SourceRootId, 155 krate: CrateId,
156 module_tree: Arc<ModuleTree>, 156 module_tree: Arc<ModuleTree>,
157 processed_imports: FxHashSet<(ModuleId, ImportId)>, 157 processed_imports: FxHashSet<(ModuleId, ImportId)>,
158 result: ItemMap, 158 result: ItemMap,
@@ -165,13 +165,13 @@ where
165 pub(crate) fn new( 165 pub(crate) fn new(
166 db: &'a DB, 166 db: &'a DB,
167 input: &'a FxHashMap<ModuleId, Arc<LoweredModule>>, 167 input: &'a FxHashMap<ModuleId, Arc<LoweredModule>>,
168 source_root: SourceRootId, 168 krate: CrateId,
169 module_tree: Arc<ModuleTree>,
170 ) -> Resolver<'a, DB> { 169 ) -> Resolver<'a, DB> {
170 let module_tree = db.module_tree(krate);
171 Resolver { 171 Resolver {
172 db, 172 db,
173 input, 173 input,
174 source_root, 174 krate,
175 module_tree, 175 module_tree,
176 processed_imports: FxHashSet::default(), 176 processed_imports: FxHashSet::default(),
177 result: ItemMap::default(), 177 result: ItemMap::default(),
@@ -210,7 +210,7 @@ where
210 let krate = Crate::new(crate_id); 210 let krate = Crate::new(crate_id);
211 for dep in krate.dependencies(self.db) { 211 for dep in krate.dependencies(self.db) {
212 if let Some(module) = dep.krate.root_module(self.db) { 212 if let Some(module) = dep.krate.root_module(self.db) {
213 let def_id = module.def_id; 213 let def_id = module.into();
214 self.add_module_item( 214 self.add_module_item(
215 &mut module_items, 215 &mut module_items,
216 dep.name.clone(), 216 dep.name.clone(),
@@ -244,20 +244,22 @@ where
244 244
245 // Populate modules 245 // Populate modules
246 for (name, module_id) in module_id.children(&self.module_tree) { 246 for (name, module_id) in module_id.children(&self.module_tree) {
247 let def_loc = DefLoc { 247 let module = Module {
248 kind: DefKind::Module,
249 source_root_id: self.source_root,
250 module_id, 248 module_id,
251 source_item_id: module_id.source(&self.module_tree), 249 krate: self.krate,
252 }; 250 };
253 let def_id = def_loc.id(self.db); 251 self.add_module_item(&mut module_items, name, PerNs::types(module.into()));
254 self.add_module_item(&mut module_items, name, PerNs::types(def_id));
255 } 252 }
256 253
257 self.result.per_module.insert(module_id, module_items); 254 self.result.per_module.insert(module_id, module_items);
258 } 255 }
259 256
260 fn add_module_item(&self, module_items: &mut ModuleScope, name: Name, def_id: PerNs<DefId>) { 257 fn add_module_item(
258 &self,
259 module_items: &mut ModuleScope,
260 name: Name,
261 def_id: PerNs<ModuleDef>,
262 ) {
261 let resolution = Resolution { 263 let resolution = Resolution {
262 def_id, 264 def_id,
263 import: None, 265 import: None,
@@ -329,17 +331,11 @@ where
329 ); 331 );
330 return false; 332 return false;
331 }; 333 };
332 curr = match type_def_id.loc(self.db) { 334 curr = match type_def_id {
333 DefLoc { 335 ModuleDef::Module(module) => {
334 kind: DefKind::Module, 336 if module.krate == self.krate {
335 module_id: target_module_id, 337 module.module_id
336 source_root_id,
337 ..
338 } => {
339 if source_root_id == self.source_root {
340 target_module_id
341 } else { 338 } else {
342 let module = crate::code_model_api::Module::new(type_def_id);
343 let path = Path { 339 let path = Path {
344 segments: import.path.segments[i + 1..].iter().cloned().collect(), 340 segments: import.path.segments[i + 1..].iter().cloned().collect(),
345 kind: PathKind::Crate, 341 kind: PathKind::Crate,
@@ -359,7 +355,7 @@ where
359 "resolved import {:?} ({:?}) cross-source root to {:?}", 355 "resolved import {:?} ({:?}) cross-source root to {:?}",
360 last_segment.name, 356 last_segment.name,
361 import, 357 import,
362 def_id.map(|did| did.loc(self.db)) 358 def_id,
363 ); 359 );
364 return true; 360 return true;
365 } else { 361 } else {
@@ -372,7 +368,7 @@ where
372 log::debug!( 368 log::debug!(
373 "path segment {:?} resolved to non-module {:?}, but is not last", 369 "path segment {:?} resolved to non-module {:?}, but is not last",
374 segment.name, 370 segment.name,
375 type_def_id.loc(self.db) 371 type_def_id,
376 ); 372 );
377 return true; // this resolved to a non-module, so the path won't ever resolve 373 return true; // this resolved to a non-module, so the path won't ever resolve
378 } 374 }
@@ -382,7 +378,7 @@ where
382 "resolved import {:?} ({:?}) within source root to {:?}", 378 "resolved import {:?} ({:?}) within source root to {:?}",
383 segment.name, 379 segment.name,
384 import, 380 import,
385 def_id.map(|did| did.loc(self.db)) 381 def_id,
386 ); 382 );
387 self.update(module_id, |items| { 383 self.update(module_id, |items| {
388 let res = Resolution { 384 let res = Resolution {