aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/code_model.rs2
-rw-r--r--crates/ra_hir_def/src/body/scope.rs4
-rw-r--r--crates/ra_hir_def/src/item_scope.rs88
-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
-rw-r--r--crates/ra_hir_def/src/resolver.rs8
7 files changed, 108 insertions, 140 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 4cd28eb4e..bcfc0d03e 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -184,7 +184,7 @@ impl Module {
184 db.crate_def_map(self.id.krate)[self.id.local_id] 184 db.crate_def_map(self.id.krate)[self.id.local_id]
185 .scope 185 .scope
186 .entries() 186 .entries()
187 .map(|(name, res)| (name.clone(), res.def.into())) 187 .map(|(name, def)| (name.clone(), def.into()))
188 .collect() 188 .collect()
189 } 189 }
190 190
diff --git a/crates/ra_hir_def/src/body/scope.rs b/crates/ra_hir_def/src/body/scope.rs
index ab6599b23..a63552327 100644
--- a/crates/ra_hir_def/src/body/scope.rs
+++ b/crates/ra_hir_def/src/body/scope.rs
@@ -183,8 +183,8 @@ mod tests {
183 let crate_def_map = db.crate_def_map(krate); 183 let crate_def_map = db.crate_def_map(krate);
184 184
185 let module = crate_def_map.modules_for_file(file_id).next().unwrap(); 185 let module = crate_def_map.modules_for_file(file_id).next().unwrap();
186 let (_, res) = crate_def_map[module].scope.entries().next().unwrap(); 186 let (_, def) = crate_def_map[module].scope.entries().next().unwrap();
187 match res.def.take_values().unwrap() { 187 match def.take_values().unwrap() {
188 ModuleDefId::FunctionId(it) => it, 188 ModuleDefId::FunctionId(it) => it,
189 _ => panic!(), 189 _ => panic!(),
190 } 190 }
diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs
index 9e082c5f7..f1adc3b58 100644
--- a/crates/ra_hir_def/src/item_scope.rs
+++ b/crates/ra_hir_def/src/item_scope.rs
@@ -5,11 +5,12 @@ use hir_expand::name::Name;
5use once_cell::sync::Lazy; 5use once_cell::sync::Lazy;
6use rustc_hash::FxHashMap; 6use rustc_hash::FxHashMap;
7 7
8use crate::{per_ns::PerNs, BuiltinType, ImplId, MacroDefId, ModuleDefId, TraitId}; 8use crate::{per_ns::PerNs, AdtId, BuiltinType, ImplId, MacroDefId, ModuleDefId, TraitId};
9 9
10#[derive(Debug, Default, PartialEq, Eq)] 10#[derive(Debug, Default, PartialEq, Eq)]
11pub struct ItemScope { 11pub struct ItemScope {
12 items: FxHashMap<Name, Resolution>, 12 visible: FxHashMap<Name, PerNs>,
13 defs: Vec<ModuleDefId>,
13 impls: Vec<ImplId>, 14 impls: Vec<ImplId>,
14 /// Macros visible in current module in legacy textual scope 15 /// Macros visible in current module in legacy textual scope
15 /// 16 ///
@@ -26,12 +27,10 @@ pub struct ItemScope {
26 legacy_macros: FxHashMap<Name, MacroDefId>, 27 legacy_macros: FxHashMap<Name, MacroDefId>,
27} 28}
28 29
29static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| { 30static BUILTIN_SCOPE: Lazy<FxHashMap<Name, PerNs>> = Lazy::new(|| {
30 BuiltinType::ALL 31 BuiltinType::ALL
31 .iter() 32 .iter()
32 .map(|(name, ty)| { 33 .map(|(name, ty)| (name.clone(), PerNs::types(ty.clone().into())))
33 (name.clone(), Resolution { def: PerNs::types(ty.clone().into()), import: false })
34 })
35 .collect() 34 .collect()
36}); 35});
37 36
@@ -47,17 +46,13 @@ pub(crate) enum BuiltinShadowMode {
47/// Legacy macros can only be accessed through special methods like `get_legacy_macros`. 46/// Legacy macros can only be accessed through special methods like `get_legacy_macros`.
48/// Other methods will only resolve values, types and module scoped macros only. 47/// Other methods will only resolve values, types and module scoped macros only.
49impl ItemScope { 48impl ItemScope {
50 pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, &'a Resolution)> + 'a { 49 pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, PerNs)> + 'a {
51 //FIXME: shadowing 50 //FIXME: shadowing
52 self.items.iter().chain(BUILTIN_SCOPE.iter()) 51 self.visible.iter().chain(BUILTIN_SCOPE.iter()).map(|(n, def)| (n, *def))
53 } 52 }
54 53
55 pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ { 54 pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ {
56 self.entries() 55 self.defs.iter().copied()
57 .filter_map(|(_name, res)| if !res.import { Some(res.def) } else { None })
58 .flat_map(|per_ns| {
59 per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter())
60 })
61 } 56 }
62 57
63 pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ { 58 pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ {
@@ -66,9 +61,7 @@ impl ItemScope {
66 61
67 /// Iterate over all module scoped macros 62 /// Iterate over all module scoped macros
68 pub(crate) fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a { 63 pub(crate) fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
69 self.items 64 self.visible.iter().filter_map(|(name, def)| def.take_macros().map(|macro_| (name, macro_)))
70 .iter()
71 .filter_map(|(name, res)| res.def.take_macros().map(|macro_| (name, macro_)))
72 } 65 }
73 66
74 /// Iterate over all legacy textual scoped macros visible at the end of the module 67 /// Iterate over all legacy textual scoped macros visible at the end of the module
@@ -77,13 +70,13 @@ impl ItemScope {
77 } 70 }
78 71
79 /// Get a name from current module scope, legacy macros are not included 72 /// Get a name from current module scope, legacy macros are not included
80 pub(crate) fn get(&self, name: &Name, shadow: BuiltinShadowMode) -> Option<&Resolution> { 73 pub(crate) fn get(&self, name: &Name, shadow: BuiltinShadowMode) -> Option<&PerNs> {
81 match shadow { 74 match shadow {
82 BuiltinShadowMode::Module => self.items.get(name).or_else(|| BUILTIN_SCOPE.get(name)), 75 BuiltinShadowMode::Module => self.visible.get(name).or_else(|| BUILTIN_SCOPE.get(name)),
83 BuiltinShadowMode::Other => { 76 BuiltinShadowMode::Other => {
84 let item = self.items.get(name); 77 let item = self.visible.get(name);
85 if let Some(res) = item { 78 if let Some(def) = item {
86 if let Some(ModuleDefId::ModuleId(_)) = res.def.take_types() { 79 if let Some(ModuleDefId::ModuleId(_)) = def.take_types() {
87 return BUILTIN_SCOPE.get(name).or(item); 80 return BUILTIN_SCOPE.get(name).or(item);
88 } 81 }
89 } 82 }
@@ -94,12 +87,16 @@ impl ItemScope {
94 } 87 }
95 88
96 pub(crate) fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a { 89 pub(crate) fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a {
97 self.items.values().filter_map(|r| match r.def.take_types() { 90 self.visible.values().filter_map(|def| match def.take_types() {
98 Some(ModuleDefId::TraitId(t)) => Some(t), 91 Some(ModuleDefId::TraitId(t)) => Some(t),
99 _ => None, 92 _ => None,
100 }) 93 })
101 } 94 }
102 95
96 pub(crate) fn define_def(&mut self, def: ModuleDefId) {
97 self.defs.push(def)
98 }
99
103 pub(crate) fn get_legacy_macro(&self, name: &Name) -> Option<MacroDefId> { 100 pub(crate) fn get_legacy_macro(&self, name: &Name) -> Option<MacroDefId> {
104 self.legacy_macros.get(name).copied() 101 self.legacy_macros.get(name).copied()
105 } 102 }
@@ -112,34 +109,28 @@ impl ItemScope {
112 self.legacy_macros.insert(name, mac); 109 self.legacy_macros.insert(name, mac);
113 } 110 }
114 111
115 pub(crate) fn push_res(&mut self, name: Name, res: &Resolution, import: bool) -> bool { 112 pub(crate) fn push_res(&mut self, name: Name, def: &PerNs) -> bool {
116 let mut changed = false; 113 let mut changed = false;
117 let existing = self.items.entry(name.clone()).or_default(); 114 let existing = self.visible.entry(name.clone()).or_default();
118 115
119 if existing.def.types.is_none() && res.def.types.is_some() { 116 if existing.types.is_none() && def.types.is_some() {
120 existing.def.types = res.def.types; 117 existing.types = def.types;
121 existing.import = import || res.import;
122 changed = true; 118 changed = true;
123 } 119 }
124 if existing.def.values.is_none() && res.def.values.is_some() { 120 if existing.values.is_none() && def.values.is_some() {
125 existing.def.values = res.def.values; 121 existing.values = def.values;
126 existing.import = import || res.import;
127 changed = true; 122 changed = true;
128 } 123 }
129 if existing.def.macros.is_none() && res.def.macros.is_some() { 124 if existing.macros.is_none() && def.macros.is_some() {
130 existing.def.macros = res.def.macros; 125 existing.macros = def.macros;
131 existing.import = import || res.import;
132 changed = true; 126 changed = true;
133 } 127 }
134 128
135 if existing.def.is_none() && res.def.is_none() && !existing.import && res.import {
136 existing.import = res.import;
137 }
138 changed 129 changed
139 } 130 }
140 131
141 pub(crate) fn collect_resolutions(&self) -> Vec<(Name, Resolution)> { 132 pub(crate) fn collect_resolutions(&self) -> Vec<(Name, PerNs)> {
142 self.items.iter().map(|(name, res)| (name.clone(), res.clone())).collect() 133 self.visible.iter().map(|(name, res)| (name.clone(), res.clone())).collect()
143 } 134 }
144 135
145 pub(crate) fn collect_legacy_macros(&self) -> FxHashMap<Name, MacroDefId> { 136 pub(crate) fn collect_legacy_macros(&self) -> FxHashMap<Name, MacroDefId> {
@@ -147,9 +138,20 @@ impl ItemScope {
147 } 138 }
148} 139}
149 140
150#[derive(Debug, Clone, PartialEq, Eq, Default)] 141impl From<ModuleDefId> for PerNs {
151pub struct Resolution { 142 fn from(def: ModuleDefId) -> PerNs {
152 /// None for unresolved 143 match def {
153 pub def: PerNs, 144 ModuleDefId::ModuleId(_) => PerNs::types(def),
154 pub(crate) import: bool, 145 ModuleDefId::FunctionId(_) => PerNs::values(def),
146 ModuleDefId::AdtId(adt) => match adt {
147 AdtId::StructId(_) | AdtId::UnionId(_) => PerNs::both(def, def),
148 AdtId::EnumId(_) => PerNs::types(def),
149 },
150 ModuleDefId::EnumVariantId(_) => PerNs::both(def, def),
151 ModuleDefId::ConstId(_) | ModuleDefId::StaticId(_) => PerNs::values(def),
152 ModuleDefId::TraitId(_) => PerNs::types(def),
153 ModuleDefId::TypeAliasId(_) => PerNs::types(def),
154 ModuleDefId::BuiltinType(_) => PerNs::types(def),
155 }
156 }
155} 157}
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
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs
index 83013fed3..e70049617 100644
--- a/crates/ra_hir_def/src/resolver.rs
+++ b/crates/ra_hir_def/src/resolver.rs
@@ -413,8 +413,8 @@ impl Scope {
413 // def: m.module.into(), 413 // def: m.module.into(),
414 // }), 414 // }),
415 // ); 415 // );
416 m.crate_def_map[m.module_id].scope.entries().for_each(|(name, res)| { 416 m.crate_def_map[m.module_id].scope.entries().for_each(|(name, def)| {
417 f(name.clone(), ScopeDef::PerNs(res.def)); 417 f(name.clone(), ScopeDef::PerNs(def));
418 }); 418 });
419 m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| { 419 m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| {
420 f(name.clone(), ScopeDef::PerNs(PerNs::macros(macro_))); 420 f(name.clone(), ScopeDef::PerNs(PerNs::macros(macro_)));
@@ -424,8 +424,8 @@ impl Scope {
424 }); 424 });
425 if let Some(prelude) = m.crate_def_map.prelude { 425 if let Some(prelude) = m.crate_def_map.prelude {
426 let prelude_def_map = db.crate_def_map(prelude.krate); 426 let prelude_def_map = db.crate_def_map(prelude.krate);
427 prelude_def_map[prelude.local_id].scope.entries().for_each(|(name, res)| { 427 prelude_def_map[prelude.local_id].scope.entries().for_each(|(name, def)| {
428 f(name.clone(), ScopeDef::PerNs(res.def)); 428 f(name.clone(), ScopeDef::PerNs(def));
429 }); 429 });
430 } 430 }
431 } 431 }