diff options
Diffstat (limited to 'crates/ra_hir_def/src/nameres')
-rw-r--r-- | crates/ra_hir_def/src/nameres/collector.rs | 61 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/tests.rs | 19 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/tests/incremental.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/tests/macros.rs | 4 |
4 files changed, 28 insertions, 60 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 | } |
diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs index 61cdd768e..4e968bcc8 100644 --- a/crates/ra_hir_def/src/nameres/tests.rs +++ b/crates/ra_hir_def/src/nameres/tests.rs | |||
@@ -32,27 +32,22 @@ fn render_crate_def_map(map: &CrateDefMap) -> String { | |||
32 | *buf += path; | 32 | *buf += path; |
33 | *buf += "\n"; | 33 | *buf += "\n"; |
34 | 34 | ||
35 | let mut entries = map.modules[module] | 35 | let mut entries = map.modules[module].scope.collect_resolutions(); |
36 | .scope | 36 | entries.sort_by_key(|(name, _)| name.clone()); |
37 | .items | ||
38 | .iter() | ||
39 | .map(|(name, res)| (name, res.def)) | ||
40 | .collect::<Vec<_>>(); | ||
41 | entries.sort_by_key(|(name, _)| *name); | ||
42 | 37 | ||
43 | for (name, res) in entries { | 38 | for (name, res) in entries { |
44 | *buf += &format!("{}:", name); | 39 | *buf += &format!("{}:", name); |
45 | 40 | ||
46 | if res.types.is_some() { | 41 | if res.def.types.is_some() { |
47 | *buf += " t"; | 42 | *buf += " t"; |
48 | } | 43 | } |
49 | if res.values.is_some() { | 44 | if res.def.values.is_some() { |
50 | *buf += " v"; | 45 | *buf += " v"; |
51 | } | 46 | } |
52 | if res.macros.is_some() { | 47 | if res.def.macros.is_some() { |
53 | *buf += " m"; | 48 | *buf += " m"; |
54 | } | 49 | } |
55 | if res.is_none() { | 50 | if res.def.is_none() { |
56 | *buf += " _"; | 51 | *buf += " _"; |
57 | } | 52 | } |
58 | 53 | ||
@@ -587,6 +582,6 @@ mod b { | |||
587 | ⋮T: v | 582 | ⋮T: v |
588 | ⋮ | 583 | ⋮ |
589 | ⋮crate::a | 584 | ⋮crate::a |
590 | ⋮T: t v | 585 | ⋮T: t v |
591 | "###); | 586 | "###); |
592 | } | 587 | } |
diff --git a/crates/ra_hir_def/src/nameres/tests/incremental.rs b/crates/ra_hir_def/src/nameres/tests/incremental.rs index 903a22771..ef2e9435c 100644 --- a/crates/ra_hir_def/src/nameres/tests/incremental.rs +++ b/crates/ra_hir_def/src/nameres/tests/incremental.rs | |||
@@ -116,7 +116,7 @@ fn typing_inside_a_macro_should_not_invalidate_def_map() { | |||
116 | let events = db.log_executed(|| { | 116 | let events = db.log_executed(|| { |
117 | let crate_def_map = db.crate_def_map(krate); | 117 | let crate_def_map = db.crate_def_map(krate); |
118 | let (_, module_data) = crate_def_map.modules.iter().last().unwrap(); | 118 | let (_, module_data) = crate_def_map.modules.iter().last().unwrap(); |
119 | assert_eq!(module_data.scope.items.len(), 1); | 119 | assert_eq!(module_data.scope.collect_resolutions().len(), 1); |
120 | }); | 120 | }); |
121 | assert!(format!("{:?}", events).contains("crate_def_map"), "{:#?}", events) | 121 | assert!(format!("{:?}", events).contains("crate_def_map"), "{:#?}", events) |
122 | } | 122 | } |
@@ -126,7 +126,7 @@ fn typing_inside_a_macro_should_not_invalidate_def_map() { | |||
126 | let events = db.log_executed(|| { | 126 | let events = db.log_executed(|| { |
127 | let crate_def_map = db.crate_def_map(krate); | 127 | let crate_def_map = db.crate_def_map(krate); |
128 | let (_, module_data) = crate_def_map.modules.iter().last().unwrap(); | 128 | let (_, module_data) = crate_def_map.modules.iter().last().unwrap(); |
129 | assert_eq!(module_data.scope.items.len(), 1); | 129 | assert_eq!(module_data.scope.collect_resolutions().len(), 1); |
130 | }); | 130 | }); |
131 | assert!(!format!("{:?}", events).contains("crate_def_map"), "{:#?}", events) | 131 | assert!(!format!("{:?}", events).contains("crate_def_map"), "{:#?}", events) |
132 | } | 132 | } |
diff --git a/crates/ra_hir_def/src/nameres/tests/macros.rs b/crates/ra_hir_def/src/nameres/tests/macros.rs index cfa4ecb1a..d104f5993 100644 --- a/crates/ra_hir_def/src/nameres/tests/macros.rs +++ b/crates/ra_hir_def/src/nameres/tests/macros.rs | |||
@@ -610,7 +610,7 @@ fn expand_derive() { | |||
610 | struct Foo; | 610 | struct Foo; |
611 | ", | 611 | ", |
612 | ); | 612 | ); |
613 | assert_eq!(map.modules[map.root].impls.len(), 1); | 613 | assert_eq!(map.modules[map.root].scope.impls().len(), 1); |
614 | } | 614 | } |
615 | 615 | ||
616 | #[test] | 616 | #[test] |
@@ -622,5 +622,5 @@ fn expand_multiple_derive() { | |||
622 | struct Foo; | 622 | struct Foo; |
623 | ", | 623 | ", |
624 | ); | 624 | ); |
625 | assert_eq!(map.modules[map.root].impls.len(), 2); | 625 | assert_eq!(map.modules[map.root].scope.impls().len(), 2); |
626 | } | 626 | } |