aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/module/nameres.rs55
1 files changed, 41 insertions, 14 deletions
diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs
index c75c1bbea..768836b65 100644
--- a/crates/ra_hir/src/module/nameres.rs
+++ b/crates/ra_hir/src/module/nameres.rs
@@ -31,7 +31,7 @@ use crate::{
31 DefId, DefLoc, DefKind, 31 DefId, DefLoc, DefKind,
32 SourceItemId, SourceFileItemId, SourceFileItems, 32 SourceItemId, SourceFileItemId, SourceFileItems,
33 Path, PathKind, 33 Path, PathKind,
34 HirDatabase, 34 HirDatabase, Crate,
35 module::{ModuleId, ModuleTree}, 35 module::{ModuleId, ModuleTree},
36}; 36};
37 37
@@ -218,8 +218,8 @@ where
218 module_tree: Arc<ModuleTree>, 218 module_tree: Arc<ModuleTree>,
219 ) -> Resolver<'a, DB> { 219 ) -> Resolver<'a, DB> {
220 Resolver { 220 Resolver {
221 db: db, 221 db,
222 input: &input, 222 input,
223 source_root, 223 source_root,
224 module_tree, 224 module_tree,
225 result: ItemMap::default(), 225 result: ItemMap::default(),
@@ -233,7 +233,7 @@ where
233 233
234 for &module_id in self.input.keys() { 234 for &module_id in self.input.keys() {
235 self.db.check_canceled()?; 235 self.db.check_canceled()?;
236 self.resolve_imports(module_id); 236 self.resolve_imports(module_id)?;
237 } 237 }
238 Ok(self.result) 238 Ok(self.result)
239 } 239 }
@@ -297,40 +297,66 @@ where
297 self.result.per_module.insert(module_id, module_items); 297 self.result.per_module.insert(module_id, module_items);
298 } 298 }
299 299
300 fn resolve_imports(&mut self, module_id: ModuleId) { 300 fn resolve_imports(&mut self, module_id: ModuleId) -> Cancelable<()> {
301 for import in self.input[&module_id].imports.iter() { 301 for import in self.input[&module_id].imports.iter() {
302 self.resolve_import(module_id, import); 302 self.resolve_import(module_id, import)?;
303 } 303 }
304 Ok(())
304 } 305 }
305 306
306 fn resolve_import(&mut self, module_id: ModuleId, import: &Import) { 307 fn resolve_import(&mut self, module_id: ModuleId, import: &Import) -> Cancelable<()> {
307 let ptr = match import.kind { 308 let ptr = match import.kind {
308 ImportKind::Glob => return, 309 ImportKind::Glob => return Ok(()),
309 ImportKind::Named(ptr) => ptr, 310 ImportKind::Named(ptr) => ptr,
310 }; 311 };
312 let mut segments = import.path.segments.iter().enumerate();
311 313
312 let mut curr = match import.path.kind { 314 let mut curr = match import.path.kind {
313 // TODO: handle extern crates 315 // TODO: handle extern crates
314 PathKind::Plain => return, 316 PathKind::Plain => {
317 let root_id = module_id.crate_root(&self.module_tree);
318 let file_id = root_id.source(&self.module_tree).file_id();
319 let crate_graph = self.db.crate_graph();
320 let crate_id = match crate_graph.crate_id_for_crate_root(file_id) {
321 None => return Ok(()),
322 Some(it) => it,
323 };
324 let krate = Crate::new(crate_id);
325 let crate_name = match segments.next() {
326 None => return Ok(()),
327 Some((_, it)) => it,
328 };
329 match krate
330 .dependencies(self.db)
331 .into_iter()
332 .find(|it| &it.name == crate_name)
333 {
334 None => return Ok(()),
335 Some(dep) => match dep.krate.root_module(self.db)? {
336 None => return Ok(()),
337 Some(it) => it.module_id,
338 },
339 }
340 }
315 PathKind::Self_ => module_id, 341 PathKind::Self_ => module_id,
316 PathKind::Super => { 342 PathKind::Super => {
317 match module_id.parent(&self.module_tree) { 343 match module_id.parent(&self.module_tree) {
318 Some(it) => it, 344 Some(it) => it,
319 // TODO: error 345 // TODO: error
320 None => return, 346 None => return Ok(()),
321 } 347 }
322 } 348 }
323 PathKind::Crate => module_id.crate_root(&self.module_tree), 349 PathKind::Crate => module_id.crate_root(&self.module_tree),
324 }; 350 };
325 351
326 for (i, name) in import.path.segments.iter().enumerate() { 352 for (i, name) in segments {
327 let is_last = i == import.path.segments.len() - 1; 353 let is_last = i == import.path.segments.len() - 1;
328 354
329 let def_id = match self.result.per_module[&curr].items.get(name) { 355 let def_id = match self.result.per_module[&curr].items.get(name) {
330 None => return, 356 None => return Ok(()),
331 Some(res) => match res.def_id { 357 Some(res) => match res.def_id {
332 Some(it) => it, 358 Some(it) => it,
333 None => return, 359 None => return Ok(()),
334 }, 360 },
335 }; 361 };
336 362
@@ -341,7 +367,7 @@ where
341 module_id, 367 module_id,
342 .. 368 ..
343 } => module_id, 369 } => module_id,
344 _ => return, 370 _ => return Ok(()),
345 } 371 }
346 } else { 372 } else {
347 self.update(module_id, |items| { 373 self.update(module_id, |items| {
@@ -353,6 +379,7 @@ where
353 }) 379 })
354 } 380 }
355 } 381 }
382 Ok(())
356 } 383 }
357 384
358 fn update(&mut self, module_id: ModuleId, f: impl FnOnce(&mut ModuleScope)) { 385 fn update(&mut self, module_id: ModuleId, f: impl FnOnce(&mut ModuleScope)) {