aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-12-22 14:37:07 +0000
committerAleksey Kladov <[email protected]>2019-12-22 14:37:53 +0000
commit6c3ddcfa501060cff3a7f81c179f712ef072c808 (patch)
tree513bb8b0a4ecb487dc1cf7be4bbc05c84ce9a975 /crates/ra_hir_def/src
parente8da7d4061960844502e3064c33eef4a0dc3828e (diff)
Simplify
Diffstat (limited to 'crates/ra_hir_def/src')
-rw-r--r--crates/ra_hir_def/src/body/scope.rs4
-rw-r--r--crates/ra_hir_def/src/item_scope.rs44
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs19
-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
6 files changed, 40 insertions, 57 deletions
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 71f44b556..f1adc3b58 100644
--- a/crates/ra_hir_def/src/item_scope.rs
+++ b/crates/ra_hir_def/src/item_scope.rs
@@ -9,7 +9,7 @@ use crate::{per_ns::PerNs, AdtId, BuiltinType, ImplId, MacroDefId, ModuleDefId,
9 9
10#[derive(Debug, Default, PartialEq, Eq)] 10#[derive(Debug, Default, PartialEq, Eq)]
11pub struct ItemScope { 11pub struct ItemScope {
12 visible: FxHashMap<Name, Resolution>, 12 visible: FxHashMap<Name, PerNs>,
13 defs: Vec<ModuleDefId>, 13 defs: Vec<ModuleDefId>,
14 impls: Vec<ImplId>, 14 impls: Vec<ImplId>,
15 /// Macros visible in current module in legacy textual scope 15 /// Macros visible in current module in legacy textual scope
@@ -27,10 +27,10 @@ pub struct ItemScope {
27 legacy_macros: FxHashMap<Name, MacroDefId>, 27 legacy_macros: FxHashMap<Name, MacroDefId>,
28} 28}
29 29
30static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| { 30static BUILTIN_SCOPE: Lazy<FxHashMap<Name, PerNs>> = Lazy::new(|| {
31 BuiltinType::ALL 31 BuiltinType::ALL
32 .iter() 32 .iter()
33 .map(|(name, ty)| (name.clone(), Resolution { def: PerNs::types(ty.clone().into()) })) 33 .map(|(name, ty)| (name.clone(), PerNs::types(ty.clone().into())))
34 .collect() 34 .collect()
35}); 35});
36 36
@@ -46,9 +46,9 @@ pub(crate) enum BuiltinShadowMode {
46/// 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`.
47/// 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.
48impl ItemScope { 48impl ItemScope {
49 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 {
50 //FIXME: shadowing 50 //FIXME: shadowing
51 self.visible.iter().chain(BUILTIN_SCOPE.iter()) 51 self.visible.iter().chain(BUILTIN_SCOPE.iter()).map(|(n, def)| (n, *def))
52 } 52 }
53 53
54 pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ { 54 pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ {
@@ -61,9 +61,7 @@ impl ItemScope {
61 61
62 /// Iterate over all module scoped macros 62 /// Iterate over all module scoped macros
63 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 {
64 self.visible 64 self.visible.iter().filter_map(|(name, def)| def.take_macros().map(|macro_| (name, macro_)))
65 .iter()
66 .filter_map(|(name, res)| res.def.take_macros().map(|macro_| (name, macro_)))
67 } 65 }
68 66
69 /// 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
@@ -72,13 +70,13 @@ impl ItemScope {
72 } 70 }
73 71
74 /// 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
75 pub(crate) fn get(&self, name: &Name, shadow: BuiltinShadowMode) -> Option<&Resolution> { 73 pub(crate) fn get(&self, name: &Name, shadow: BuiltinShadowMode) -> Option<&PerNs> {
76 match shadow { 74 match shadow {
77 BuiltinShadowMode::Module => self.visible.get(name).or_else(|| BUILTIN_SCOPE.get(name)), 75 BuiltinShadowMode::Module => self.visible.get(name).or_else(|| BUILTIN_SCOPE.get(name)),
78 BuiltinShadowMode::Other => { 76 BuiltinShadowMode::Other => {
79 let item = self.visible.get(name); 77 let item = self.visible.get(name);
80 if let Some(res) = item { 78 if let Some(def) = item {
81 if let Some(ModuleDefId::ModuleId(_)) = res.def.take_types() { 79 if let Some(ModuleDefId::ModuleId(_)) = def.take_types() {
82 return BUILTIN_SCOPE.get(name).or(item); 80 return BUILTIN_SCOPE.get(name).or(item);
83 } 81 }
84 } 82 }
@@ -89,7 +87,7 @@ impl ItemScope {
89 } 87 }
90 88
91 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 {
92 self.visible.values().filter_map(|r| match r.def.take_types() { 90 self.visible.values().filter_map(|def| match def.take_types() {
93 Some(ModuleDefId::TraitId(t)) => Some(t), 91 Some(ModuleDefId::TraitId(t)) => Some(t),
94 _ => None, 92 _ => None,
95 }) 93 })
@@ -111,27 +109,27 @@ impl ItemScope {
111 self.legacy_macros.insert(name, mac); 109 self.legacy_macros.insert(name, mac);
112 } 110 }
113 111
114 pub(crate) fn push_res(&mut self, name: Name, res: &Resolution) -> bool { 112 pub(crate) fn push_res(&mut self, name: Name, def: &PerNs) -> bool {
115 let mut changed = false; 113 let mut changed = false;
116 let existing = self.visible.entry(name.clone()).or_default(); 114 let existing = self.visible.entry(name.clone()).or_default();
117 115
118 if existing.def.types.is_none() && res.def.types.is_some() { 116 if existing.types.is_none() && def.types.is_some() {
119 existing.def.types = res.def.types; 117 existing.types = def.types;
120 changed = true; 118 changed = true;
121 } 119 }
122 if existing.def.values.is_none() && res.def.values.is_some() { 120 if existing.values.is_none() && def.values.is_some() {
123 existing.def.values = res.def.values; 121 existing.values = def.values;
124 changed = true; 122 changed = true;
125 } 123 }
126 if existing.def.macros.is_none() && res.def.macros.is_some() { 124 if existing.macros.is_none() && def.macros.is_some() {
127 existing.def.macros = res.def.macros; 125 existing.macros = def.macros;
128 changed = true; 126 changed = true;
129 } 127 }
130 128
131 changed 129 changed
132 } 130 }
133 131
134 pub(crate) fn collect_resolutions(&self) -> Vec<(Name, Resolution)> { 132 pub(crate) fn collect_resolutions(&self) -> Vec<(Name, PerNs)> {
135 self.visible.iter().map(|(name, res)| (name.clone(), res.clone())).collect() 133 self.visible.iter().map(|(name, res)| (name.clone(), res.clone())).collect()
136 } 134 }
137 135
@@ -140,12 +138,6 @@ impl ItemScope {
140 } 138 }
141} 139}
142 140
143#[derive(Debug, Clone, PartialEq, Eq, Default)]
144pub struct Resolution {
145 /// None for unresolved
146 pub def: PerNs,
147}
148
149impl From<ModuleDefId> for PerNs { 141impl From<ModuleDefId> for PerNs {
150 fn from(def: ModuleDefId) -> PerNs { 142 fn from(def: ModuleDefId) -> PerNs {
151 match def { 143 match def {
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index d27c3e197..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,7 +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(self.def_map.root, &[(name, Resolution { def: PerNs::macros(macro_) })]); 217 self.update(self.def_map.root, &[(name, PerNs::macros(macro_))]);
219 } 218 }
220 } 219 }
221 220
@@ -397,8 +396,7 @@ where
397 .map(|(local_id, variant_data)| { 396 .map(|(local_id, variant_data)| {
398 let name = variant_data.name.clone(); 397 let name = variant_data.name.clone();
399 let variant = EnumVariantId { parent: e, local_id }; 398 let variant = EnumVariantId { parent: e, local_id };
400 let res = 399 let res = PerNs::both(variant.into(), variant.into());
401 Resolution { def: PerNs::both(variant.into(), variant.into()) };
402 (name, res) 400 (name, res)
403 }) 401 })
404 .collect::<Vec<_>>(); 402 .collect::<Vec<_>>();
@@ -424,22 +422,21 @@ where
424 } 422 }
425 } 423 }
426 424
427 let resolution = Resolution { def }; 425 self.update(module_id, &[(name, def)]);
428 self.update(module_id, &[(name, resolution)]);
429 } 426 }
430 None => tested_by!(bogus_paths), 427 None => tested_by!(bogus_paths),
431 } 428 }
432 } 429 }
433 } 430 }
434 431
435 fn update(&mut self, module_id: LocalModuleId, resolutions: &[(Name, Resolution)]) { 432 fn update(&mut self, module_id: LocalModuleId, resolutions: &[(Name, PerNs)]) {
436 self.update_recursive(module_id, resolutions, 0) 433 self.update_recursive(module_id, resolutions, 0)
437 } 434 }
438 435
439 fn update_recursive( 436 fn update_recursive(
440 &mut self, 437 &mut self,
441 module_id: LocalModuleId, 438 module_id: LocalModuleId,
442 resolutions: &[(Name, Resolution)], 439 resolutions: &[(Name, PerNs)],
443 depth: usize, 440 depth: usize,
444 ) { 441 ) {
445 if depth > 100 { 442 if depth > 100 {
@@ -705,8 +702,7 @@ where
705 let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res }; 702 let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res };
706 let def: ModuleDefId = module.into(); 703 let def: ModuleDefId = module.into();
707 self.def_collector.def_map.modules[self.module_id].scope.define_def(def); 704 self.def_collector.def_map.modules[self.module_id].scope.define_def(def);
708 let resolution = Resolution { def: def.into() }; 705 self.def_collector.update(self.module_id, &[(name, def.into())]);
709 self.def_collector.update(self.module_id, &[(name, resolution)]);
710 res 706 res
711 } 707 }
712 708
@@ -765,8 +761,7 @@ where
765 .into(), 761 .into(),
766 }; 762 };
767 self.def_collector.def_map.modules[self.module_id].scope.define_def(def); 763 self.def_collector.def_map.modules[self.module_id].scope.define_def(def);
768 let resolution = Resolution { def: def.into() }; 764 self.def_collector.update(self.module_id, &[(name, def.into())])
769 self.def_collector.update(self.module_id, &[(name, resolution)])
770 } 765 }
771 766
772 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 }