aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/nameres
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-12-22 14:43:15 +0000
committerGitHub <[email protected]>2019-12-22 14:43:15 +0000
commit2d003b6378edc84be42abe98c377ec0ec2bf4ae9 (patch)
tree513bb8b0a4ecb487dc1cf7be4bbc05c84ce9a975 /crates/ra_hir_def/src/nameres
parent9f616ed65a3cd3088a006ab9f116c2b9a2235cb6 (diff)
parent6c3ddcfa501060cff3a7f81c179f712ef072c808 (diff)
Merge #2645
2645: Simplify r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir_def/src/nameres')
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs124
-rw-r--r--crates/ra_hir_def/src/nameres/path_resolution.rs12
-rw-r--r--crates/ra_hir_def/src/nameres/tests.rs10
3 files changed, 56 insertions, 90 deletions
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index 2b194f488..4f1fd4801 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -18,7 +18,6 @@ use test_utils::tested_by;
18use crate::{ 18use crate::{
19 attr::Attrs, 19 attr::Attrs,
20 db::DefDatabase, 20 db::DefDatabase,
21 item_scope::Resolution,
22 nameres::{ 21 nameres::{
23 diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, 22 diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint,
24 raw, BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, ResolveMode, 23 raw, BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, ResolveMode,
@@ -215,11 +214,7 @@ where
215 // In Rust, `#[macro_export]` macros are unconditionally visible at the 214 // In Rust, `#[macro_export]` macros are unconditionally visible at the
216 // crate root, even if the parent modules is **not** visible. 215 // crate root, even if the parent modules is **not** visible.
217 if export { 216 if export {
218 self.update( 217 self.update(self.def_map.root, &[(name, PerNs::macros(macro_))]);
219 self.def_map.root,
220 None,
221 &[(name, Resolution { def: PerNs::macros(macro_), import: false })],
222 );
223 } 218 }
224 } 219 }
225 220
@@ -373,7 +368,7 @@ where
373 // Module scoped macros is included 368 // Module scoped macros is included
374 let items = scope.collect_resolutions(); 369 let items = scope.collect_resolutions();
375 370
376 self.update(module_id, Some(import_id), &items); 371 self.update(module_id, &items);
377 } else { 372 } else {
378 // glob import from same crate => we do an initial 373 // glob import from same crate => we do an initial
379 // import, and then need to propagate any further 374 // import, and then need to propagate any further
@@ -383,7 +378,7 @@ where
383 // Module scoped macros is included 378 // Module scoped macros is included
384 let items = scope.collect_resolutions(); 379 let items = scope.collect_resolutions();
385 380
386 self.update(module_id, Some(import_id), &items); 381 self.update(module_id, &items);
387 // record the glob import in case we add further items 382 // record the glob import in case we add further items
388 let glob = self.glob_imports.entry(m.local_id).or_default(); 383 let glob = self.glob_imports.entry(m.local_id).or_default();
389 if !glob.iter().any(|it| *it == (module_id, import_id)) { 384 if !glob.iter().any(|it| *it == (module_id, import_id)) {
@@ -401,14 +396,11 @@ where
401 .map(|(local_id, variant_data)| { 396 .map(|(local_id, variant_data)| {
402 let name = variant_data.name.clone(); 397 let name = variant_data.name.clone();
403 let variant = EnumVariantId { parent: e, local_id }; 398 let variant = EnumVariantId { parent: e, local_id };
404 let res = Resolution { 399 let res = PerNs::both(variant.into(), variant.into());
405 def: PerNs::both(variant.into(), variant.into()),
406 import: true,
407 };
408 (name, res) 400 (name, res)
409 }) 401 })
410 .collect::<Vec<_>>(); 402 .collect::<Vec<_>>();
411 self.update(module_id, Some(import_id), &resolutions); 403 self.update(module_id, &resolutions);
412 } 404 }
413 Some(d) => { 405 Some(d) => {
414 log::debug!("glob import {:?} from non-module/enum {:?}", import, d); 406 log::debug!("glob import {:?} from non-module/enum {:?}", import, d);
@@ -430,28 +422,21 @@ where
430 } 422 }
431 } 423 }
432 424
433 let resolution = Resolution { def, import: true }; 425 self.update(module_id, &[(name, def)]);
434 self.update(module_id, Some(import_id), &[(name, resolution)]);
435 } 426 }
436 None => tested_by!(bogus_paths), 427 None => tested_by!(bogus_paths),
437 } 428 }
438 } 429 }
439 } 430 }
440 431
441 fn update( 432 fn update(&mut self, module_id: LocalModuleId, resolutions: &[(Name, PerNs)]) {
442 &mut self, 433 self.update_recursive(module_id, resolutions, 0)
443 module_id: LocalModuleId,
444 import: Option<raw::Import>,
445 resolutions: &[(Name, Resolution)],
446 ) {
447 self.update_recursive(module_id, import, resolutions, 0)
448 } 434 }
449 435
450 fn update_recursive( 436 fn update_recursive(
451 &mut self, 437 &mut self,
452 module_id: LocalModuleId, 438 module_id: LocalModuleId,
453 import: Option<raw::Import>, 439 resolutions: &[(Name, PerNs)],
454 resolutions: &[(Name, Resolution)],
455 depth: usize, 440 depth: usize,
456 ) { 441 ) {
457 if depth > 100 { 442 if depth > 100 {
@@ -461,7 +446,7 @@ where
461 let scope = &mut self.def_map.modules[module_id].scope; 446 let scope = &mut self.def_map.modules[module_id].scope;
462 let mut changed = false; 447 let mut changed = false;
463 for (name, res) in resolutions { 448 for (name, res) in resolutions {
464 changed |= scope.push_res(name.clone(), res, import.is_some()); 449 changed |= scope.push_res(name.clone(), res);
465 } 450 }
466 451
467 if !changed { 452 if !changed {
@@ -474,9 +459,9 @@ where
474 .flat_map(|v| v.iter()) 459 .flat_map(|v| v.iter())
475 .cloned() 460 .cloned()
476 .collect::<Vec<_>>(); 461 .collect::<Vec<_>>();
477 for (glob_importing_module, glob_import) in glob_imports { 462 for (glob_importing_module, _glob_import) in glob_imports {
478 // We pass the glob import so that the tracked import in those modules is that glob import 463 // We pass the glob import so that the tracked import in those modules is that glob import
479 self.update_recursive(glob_importing_module, Some(glob_import), resolutions, depth + 1); 464 self.update_recursive(glob_importing_module, resolutions, depth + 1);
480 } 465 }
481 } 466 }
482 467
@@ -714,13 +699,10 @@ where
714 modules[res].scope.define_legacy_macro(name, mac) 699 modules[res].scope.define_legacy_macro(name, mac)
715 } 700 }
716 modules[self.module_id].children.insert(name.clone(), res); 701 modules[self.module_id].children.insert(name.clone(), res);
717 let resolution = Resolution { 702 let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res };
718 def: PerNs::types( 703 let def: ModuleDefId = module.into();
719 ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(), 704 self.def_collector.def_map.modules[self.module_id].scope.define_def(def);
720 ), 705 self.def_collector.update(self.module_id, &[(name, def.into())]);
721 import: false,
722 };
723 self.def_collector.update(self.module_id, None, &[(name, resolution)]);
724 res 706 res
725 } 707 }
726 708
@@ -734,64 +716,52 @@ where
734 716
735 let name = def.name.clone(); 717 let name = def.name.clone();
736 let container = ContainerId::ModuleId(module); 718 let container = ContainerId::ModuleId(module);
737 let def: PerNs = match def.kind { 719 let def: ModuleDefId = match def.kind {
738 raw::DefKind::Function(ast_id) => { 720 raw::DefKind::Function(ast_id) => FunctionLoc {
739 let def = FunctionLoc { 721 container: container.into(),
740 container: container.into(), 722 ast_id: AstId::new(self.file_id, ast_id),
741 ast_id: AstId::new(self.file_id, ast_id),
742 }
743 .intern(self.def_collector.db);
744
745 PerNs::values(def.into())
746 } 723 }
724 .intern(self.def_collector.db)
725 .into(),
747 raw::DefKind::Struct(ast_id) => { 726 raw::DefKind::Struct(ast_id) => {
748 let def = StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) } 727 StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
749 .intern(self.def_collector.db); 728 .intern(self.def_collector.db)
750 PerNs::both(def.into(), def.into()) 729 .into()
751 } 730 }
752 raw::DefKind::Union(ast_id) => { 731 raw::DefKind::Union(ast_id) => {
753 let def = UnionLoc { container, ast_id: AstId::new(self.file_id, ast_id) } 732 UnionLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
754 .intern(self.def_collector.db); 733 .intern(self.def_collector.db)
755 PerNs::both(def.into(), def.into()) 734 .into()
756 } 735 }
757 raw::DefKind::Enum(ast_id) => { 736 raw::DefKind::Enum(ast_id) => {
758 let def = EnumLoc { container, ast_id: AstId::new(self.file_id, ast_id) } 737 EnumLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
759 .intern(self.def_collector.db); 738 .intern(self.def_collector.db)
760 PerNs::types(def.into()) 739 .into()
761 } 740 }
762 raw::DefKind::Const(ast_id) => { 741 raw::DefKind::Const(ast_id) => {
763 let def = ConstLoc { 742 ConstLoc { container: container.into(), ast_id: AstId::new(self.file_id, ast_id) }
764 container: container.into(), 743 .intern(self.def_collector.db)
765 ast_id: AstId::new(self.file_id, ast_id), 744 .into()
766 }
767 .intern(self.def_collector.db);
768
769 PerNs::values(def.into())
770 } 745 }
771 raw::DefKind::Static(ast_id) => { 746 raw::DefKind::Static(ast_id) => {
772 let def = StaticLoc { container, ast_id: AstId::new(self.file_id, ast_id) } 747 StaticLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
773 .intern(self.def_collector.db); 748 .intern(self.def_collector.db)
774 749 .into()
775 PerNs::values(def.into())
776 } 750 }
777 raw::DefKind::Trait(ast_id) => { 751 raw::DefKind::Trait(ast_id) => {
778 let def = TraitLoc { container, ast_id: AstId::new(self.file_id, ast_id) } 752 TraitLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
779 .intern(self.def_collector.db); 753 .intern(self.def_collector.db)
780 754 .into()
781 PerNs::types(def.into())
782 } 755 }
783 raw::DefKind::TypeAlias(ast_id) => { 756 raw::DefKind::TypeAlias(ast_id) => TypeAliasLoc {
784 let def = TypeAliasLoc { 757 container: container.into(),
785 container: container.into(), 758 ast_id: AstId::new(self.file_id, ast_id),
786 ast_id: AstId::new(self.file_id, ast_id),
787 }
788 .intern(self.def_collector.db);
789
790 PerNs::types(def.into())
791 } 759 }
760 .intern(self.def_collector.db)
761 .into(),
792 }; 762 };
793 let resolution = Resolution { def, import: false }; 763 self.def_collector.def_map.modules[self.module_id].scope.define_def(def);
794 self.def_collector.update(self.module_id, None, &[(name, resolution)]) 764 self.def_collector.update(self.module_id, &[(name, def.into())])
795 } 765 }
796 766
797 fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) { 767 fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) {
diff --git a/crates/ra_hir_def/src/nameres/path_resolution.rs b/crates/ra_hir_def/src/nameres/path_resolution.rs
index 2dd779b66..378d49455 100644
--- a/crates/ra_hir_def/src/nameres/path_resolution.rs
+++ b/crates/ra_hir_def/src/nameres/path_resolution.rs
@@ -181,7 +181,7 @@ impl CrateDefMap {
181 181
182 // Since it is a qualified path here, it should not contains legacy macros 182 // Since it is a qualified path here, it should not contains legacy macros
183 match self[module.local_id].scope.get(&segment, prefer_module(i)) { 183 match self[module.local_id].scope.get(&segment, prefer_module(i)) {
184 Some(res) => res.def, 184 Some(def) => *def,
185 _ => { 185 _ => {
186 log::debug!("path segment {:?} not found", segment); 186 log::debug!("path segment {:?} not found", segment);
187 return ResolvePathResult::empty(ReachedFixedPoint::No); 187 return ResolvePathResult::empty(ReachedFixedPoint::No);
@@ -243,8 +243,7 @@ impl CrateDefMap {
243 // - std prelude 243 // - std prelude
244 let from_legacy_macro = 244 let from_legacy_macro =
245 self[module].scope.get_legacy_macro(name).map_or_else(PerNs::none, PerNs::macros); 245 self[module].scope.get_legacy_macro(name).map_or_else(PerNs::none, PerNs::macros);
246 let from_scope = 246 let from_scope = self[module].scope.get(name, shadow).copied().unwrap_or_else(PerNs::none);
247 self[module].scope.get(name, shadow).map_or_else(PerNs::none, |res| res.def);
248 let from_extern_prelude = 247 let from_extern_prelude =
249 self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)); 248 self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it));
250 let from_prelude = self.resolve_in_prelude(db, name, shadow); 249 let from_prelude = self.resolve_in_prelude(db, name, shadow);
@@ -258,7 +257,7 @@ impl CrateDefMap {
258 shadow: BuiltinShadowMode, 257 shadow: BuiltinShadowMode,
259 ) -> PerNs { 258 ) -> PerNs {
260 let from_crate_root = 259 let from_crate_root =
261 self[self.root].scope.get(name, shadow).map_or_else(PerNs::none, |res| res.def); 260 self[self.root].scope.get(name, shadow).copied().unwrap_or_else(PerNs::none);
262 let from_extern_prelude = self.resolve_name_in_extern_prelude(name); 261 let from_extern_prelude = self.resolve_name_in_extern_prelude(name);
263 262
264 from_crate_root.or(from_extern_prelude) 263 from_crate_root.or(from_extern_prelude)
@@ -279,10 +278,7 @@ impl CrateDefMap {
279 keep = db.crate_def_map(prelude.krate); 278 keep = db.crate_def_map(prelude.krate);
280 &keep 279 &keep
281 }; 280 };
282 def_map[prelude.local_id] 281 def_map[prelude.local_id].scope.get(name, shadow).copied().unwrap_or_else(PerNs::none)
283 .scope
284 .get(name, shadow)
285 .map_or_else(PerNs::none, |res| res.def)
286 } else { 282 } else {
287 PerNs::none() 283 PerNs::none()
288 } 284 }
diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs
index 4e968bcc8..ff474b53b 100644
--- a/crates/ra_hir_def/src/nameres/tests.rs
+++ b/crates/ra_hir_def/src/nameres/tests.rs
@@ -35,19 +35,19 @@ fn render_crate_def_map(map: &CrateDefMap) -> String {
35 let mut entries = map.modules[module].scope.collect_resolutions(); 35 let mut entries = map.modules[module].scope.collect_resolutions();
36 entries.sort_by_key(|(name, _)| name.clone()); 36 entries.sort_by_key(|(name, _)| name.clone());
37 37
38 for (name, res) in entries { 38 for (name, def) in entries {
39 *buf += &format!("{}:", name); 39 *buf += &format!("{}:", name);
40 40
41 if res.def.types.is_some() { 41 if def.types.is_some() {
42 *buf += " t"; 42 *buf += " t";
43 } 43 }
44 if res.def.values.is_some() { 44 if def.values.is_some() {
45 *buf += " v"; 45 *buf += " v";
46 } 46 }
47 if res.def.macros.is_some() { 47 if def.macros.is_some() {
48 *buf += " m"; 48 *buf += " m";
49 } 49 }
50 if res.def.is_none() { 50 if def.is_none() {
51 *buf += " _"; 51 *buf += " _";
52 } 52 }
53 53