diff options
Diffstat (limited to 'crates/ra_hir_def/src/nameres/collector.rs')
-rw-r--r-- | crates/ra_hir_def/src/nameres/collector.rs | 61 |
1 files changed, 17 insertions, 44 deletions
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index e68bf4868..8b641d8b5 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs | |||
@@ -18,9 +18,10 @@ use test_utils::tested_by; | |||
18 | use crate::{ | 18 | use crate::{ |
19 | attr::Attrs, | 19 | attr::Attrs, |
20 | db::DefDatabase, | 20 | db::DefDatabase, |
21 | item_scope::Resolution, | ||
21 | nameres::{ | 22 | nameres::{ |
22 | diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, | 23 | diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, |
23 | raw, BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, Resolution, ResolveMode, | 24 | raw, BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, ResolveMode, |
24 | }, | 25 | }, |
25 | path::{ModPath, PathKind}, | 26 | path::{ModPath, PathKind}, |
26 | per_ns::PerNs, | 27 | per_ns::PerNs, |
@@ -225,14 +226,14 @@ where | |||
225 | 226 | ||
226 | /// Define a legacy textual scoped macro in module | 227 | /// Define a legacy textual scoped macro in module |
227 | /// | 228 | /// |
228 | /// We use a map `legacy_macros` to store all legacy textual scoped macros visable per module. | 229 | /// We use a map `legacy_macros` to store all legacy textual scoped macros visible per module. |
229 | /// It will clone all macros from parent legacy scope, whose definition is prior to | 230 | /// It will clone all macros from parent legacy scope, whose definition is prior to |
230 | /// the definition of current module. | 231 | /// the definition of current module. |
231 | /// And also, `macro_use` on a module will import all legacy macros visable inside to | 232 | /// And also, `macro_use` on a module will import all legacy macros visible inside to |
232 | /// current legacy scope, with possible shadowing. | 233 | /// current legacy scope, with possible shadowing. |
233 | fn define_legacy_macro(&mut self, module_id: LocalModuleId, name: Name, macro_: MacroDefId) { | 234 | fn define_legacy_macro(&mut self, module_id: LocalModuleId, name: Name, mac: MacroDefId) { |
234 | // Always shadowing | 235 | // Always shadowing |
235 | self.def_map.modules[module_id].scope.legacy_macros.insert(name, macro_); | 236 | self.def_map.modules[module_id].scope.define_legacy_macro(name, mac); |
236 | } | 237 | } |
237 | 238 | ||
238 | /// Import macros from `#[macro_use] extern crate`. | 239 | /// Import macros from `#[macro_use] extern crate`. |
@@ -371,11 +372,7 @@ where | |||
371 | let scope = &item_map[m.local_id].scope; | 372 | let scope = &item_map[m.local_id].scope; |
372 | 373 | ||
373 | // Module scoped macros is included | 374 | // Module scoped macros is included |
374 | let items = scope | 375 | let items = scope.collect_resolutions(); |
375 | .items | ||
376 | .iter() | ||
377 | .map(|(name, res)| (name.clone(), res.clone())) | ||
378 | .collect::<Vec<_>>(); | ||
379 | 376 | ||
380 | self.update(module_id, Some(import_id), &items); | 377 | self.update(module_id, Some(import_id), &items); |
381 | } else { | 378 | } else { |
@@ -385,11 +382,7 @@ where | |||
385 | let scope = &self.def_map[m.local_id].scope; | 382 | let scope = &self.def_map[m.local_id].scope; |
386 | 383 | ||
387 | // Module scoped macros is included | 384 | // Module scoped macros is included |
388 | let items = scope | 385 | let items = scope.collect_resolutions(); |
389 | .items | ||
390 | .iter() | ||
391 | .map(|(name, res)| (name.clone(), res.clone())) | ||
392 | .collect::<Vec<_>>(); | ||
393 | 386 | ||
394 | self.update(module_id, Some(import_id), &items); | 387 | self.update(module_id, Some(import_id), &items); |
395 | // record the glob import in case we add further items | 388 | // record the glob import in case we add further items |
@@ -466,34 +459,10 @@ where | |||
466 | // prevent stack overflows (but this shouldn't be possible) | 459 | // prevent stack overflows (but this shouldn't be possible) |
467 | panic!("infinite recursion in glob imports!"); | 460 | panic!("infinite recursion in glob imports!"); |
468 | } | 461 | } |
469 | let module_items = &mut self.def_map.modules[module_id].scope; | 462 | let scope = &mut self.def_map.modules[module_id].scope; |
470 | let mut changed = false; | 463 | let mut changed = false; |
471 | for (name, res) in resolutions { | 464 | for (name, res) in resolutions { |
472 | let existing = module_items.items.entry(name.clone()).or_default(); | 465 | changed |= scope.push_res(name.clone(), res, import); |
473 | |||
474 | if existing.def.types.is_none() && res.def.types.is_some() { | ||
475 | existing.def.types = res.def.types; | ||
476 | existing.import = import.or(res.import); | ||
477 | changed = true; | ||
478 | } | ||
479 | if existing.def.values.is_none() && res.def.values.is_some() { | ||
480 | existing.def.values = res.def.values; | ||
481 | existing.import = import.or(res.import); | ||
482 | changed = true; | ||
483 | } | ||
484 | if existing.def.macros.is_none() && res.def.macros.is_some() { | ||
485 | existing.def.macros = res.def.macros; | ||
486 | existing.import = import.or(res.import); | ||
487 | changed = true; | ||
488 | } | ||
489 | |||
490 | if existing.def.is_none() | ||
491 | && res.def.is_none() | ||
492 | && existing.import.is_none() | ||
493 | && res.import.is_some() | ||
494 | { | ||
495 | existing.import = res.import; | ||
496 | } | ||
497 | } | 466 | } |
498 | 467 | ||
499 | if !changed { | 468 | if !changed { |
@@ -666,7 +635,9 @@ where | |||
666 | let impl_id = | 635 | let impl_id = |
667 | ImplLoc { container, ast_id: AstId::new(self.file_id, ast_id) } | 636 | ImplLoc { container, ast_id: AstId::new(self.file_id, ast_id) } |
668 | .intern(self.def_collector.db); | 637 | .intern(self.def_collector.db); |
669 | self.def_collector.def_map.modules[self.module_id].impls.push(impl_id) | 638 | self.def_collector.def_map.modules[self.module_id] |
639 | .scope | ||
640 | .define_impl(impl_id) | ||
670 | } | 641 | } |
671 | } | 642 | } |
672 | } | 643 | } |
@@ -740,7 +711,9 @@ where | |||
740 | let res = modules.alloc(ModuleData::default()); | 711 | let res = modules.alloc(ModuleData::default()); |
741 | modules[res].parent = Some(self.module_id); | 712 | modules[res].parent = Some(self.module_id); |
742 | modules[res].origin = ModuleOrigin::not_sure_file(definition, declaration); | 713 | modules[res].origin = ModuleOrigin::not_sure_file(definition, declaration); |
743 | modules[res].scope.legacy_macros = modules[self.module_id].scope.legacy_macros.clone(); | 714 | for (name, mac) in modules[self.module_id].scope.collect_legacy_macros() { |
715 | modules[res].scope.define_legacy_macro(name, mac) | ||
716 | } | ||
744 | modules[self.module_id].children.insert(name.clone(), res); | 717 | modules[self.module_id].children.insert(name.clone(), res); |
745 | let resolution = Resolution { | 718 | let resolution = Resolution { |
746 | def: PerNs::types( | 719 | def: PerNs::types( |
@@ -904,7 +877,7 @@ where | |||
904 | } | 877 | } |
905 | 878 | ||
906 | fn import_all_legacy_macros(&mut self, module_id: LocalModuleId) { | 879 | fn import_all_legacy_macros(&mut self, module_id: LocalModuleId) { |
907 | let macros = self.def_collector.def_map[module_id].scope.legacy_macros.clone(); | 880 | let macros = self.def_collector.def_map[module_id].scope.collect_legacy_macros(); |
908 | for (name, macro_) in macros { | 881 | for (name, macro_) in macros { |
909 | self.def_collector.define_legacy_macro(self.module_id, name.clone(), macro_); | 882 | self.def_collector.define_legacy_macro(self.module_id, name.clone(), macro_); |
910 | } | 883 | } |