aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-12-21 11:38:40 +0000
committerAleksey Kladov <[email protected]>2019-12-21 11:38:40 +0000
commitab7a70fb14d281507b6e6726b47614035b073a28 (patch)
treebecb28dbc24c9772017a84f2e6f02bc15a03475d /crates/ra_hir_def
parent67ba9072fad8698af4e96b38b8b4acfdd801f7f7 (diff)
Don't track imports
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/item_scope.rs35
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs39
2 files changed, 26 insertions, 48 deletions
diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs
index 6b9be8325..5c14fefff 100644
--- a/crates/ra_hir_def/src/item_scope.rs
+++ b/crates/ra_hir_def/src/item_scope.rs
@@ -5,7 +5,7 @@ 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, LocalImportId, MacroDefId, ModuleDefId, TraitId}; 8use crate::{per_ns::PerNs, 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 {
@@ -30,7 +30,7 @@ static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| {
30 BuiltinType::ALL 30 BuiltinType::ALL
31 .iter() 31 .iter()
32 .map(|(name, ty)| { 32 .map(|(name, ty)| {
33 (name.clone(), Resolution { def: PerNs::types(ty.clone().into()), import: None }) 33 (name.clone(), Resolution { def: PerNs::types(ty.clone().into()), declaration: false })
34 }) 34 })
35 .collect() 35 .collect()
36}); 36});
@@ -53,11 +53,9 @@ impl ItemScope {
53 } 53 }
54 54
55 pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ { 55 pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ {
56 self.entries() 56 self.entries().filter(|(_name, res)| res.declaration).flat_map(|(_name, res)| {
57 .filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None }) 57 res.def.take_types().into_iter().chain(res.def.take_values().into_iter())
58 .flat_map(|per_ns| { 58 })
59 per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter())
60 })
61 } 59 }
62 60
63 pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ { 61 pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ {
@@ -112,38 +110,26 @@ impl ItemScope {
112 self.legacy_macros.insert(name, mac); 110 self.legacy_macros.insert(name, mac);
113 } 111 }
114 112
115 pub(crate) fn push_res( 113 pub(crate) fn push_res(&mut self, name: Name, res: &Resolution, declaration: bool) -> bool {
116 &mut self,
117 name: Name,
118 res: &Resolution,
119 import: Option<LocalImportId>,
120 ) -> bool {
121 let mut changed = false; 114 let mut changed = false;
122 let existing = self.items.entry(name.clone()).or_default(); 115 let existing = self.items.entry(name.clone()).or_default();
123 116
124 if existing.def.types.is_none() && res.def.types.is_some() { 117 if existing.def.types.is_none() && res.def.types.is_some() {
125 existing.def.types = res.def.types; 118 existing.def.types = res.def.types;
126 existing.import = import.or(res.import); 119 existing.declaration |= declaration;
127 changed = true; 120 changed = true;
128 } 121 }
129 if existing.def.values.is_none() && res.def.values.is_some() { 122 if existing.def.values.is_none() && res.def.values.is_some() {
130 existing.def.values = res.def.values; 123 existing.def.values = res.def.values;
131 existing.import = import.or(res.import); 124 existing.declaration |= declaration;
132 changed = true; 125 changed = true;
133 } 126 }
134 if existing.def.macros.is_none() && res.def.macros.is_some() { 127 if existing.def.macros.is_none() && res.def.macros.is_some() {
135 existing.def.macros = res.def.macros; 128 existing.def.macros = res.def.macros;
136 existing.import = import.or(res.import); 129 existing.declaration |= declaration;
137 changed = true; 130 changed = true;
138 } 131 }
139 132
140 if existing.def.is_none()
141 && res.def.is_none()
142 && existing.import.is_none()
143 && res.import.is_some()
144 {
145 existing.import = res.import;
146 }
147 changed 133 changed
148 } 134 }
149 135
@@ -160,6 +146,5 @@ impl ItemScope {
160pub struct Resolution { 146pub struct Resolution {
161 /// None for unresolved 147 /// None for unresolved
162 pub def: PerNs, 148 pub def: PerNs,
163 /// ident by which this is imported into local scope. 149 pub declaration: bool,
164 pub import: Option<LocalImportId>,
165} 150}
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index 45199fa11..e43aafedb 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -218,8 +218,7 @@ where
218 if export { 218 if export {
219 self.update( 219 self.update(
220 self.def_map.root, 220 self.def_map.root,
221 None, 221 &[(name, Resolution { def: PerNs::macros(macro_), declaration: false })],
222 &[(name, Resolution { def: PerNs::macros(macro_), import: None })],
223 ); 222 );
224 } 223 }
225 } 224 }
@@ -374,7 +373,7 @@ where
374 // Module scoped macros is included 373 // Module scoped macros is included
375 let items = scope.collect_resolutions(); 374 let items = scope.collect_resolutions();
376 375
377 self.update(module_id, Some(import_id), &items); 376 self.update(module_id, &items);
378 } else { 377 } else {
379 // glob import from same crate => we do an initial 378 // glob import from same crate => we do an initial
380 // import, and then need to propagate any further 379 // import, and then need to propagate any further
@@ -384,7 +383,7 @@ where
384 // Module scoped macros is included 383 // Module scoped macros is included
385 let items = scope.collect_resolutions(); 384 let items = scope.collect_resolutions();
386 385
387 self.update(module_id, Some(import_id), &items); 386 self.update(module_id, &items);
388 // record the glob import in case we add further items 387 // record the glob import in case we add further items
389 let glob = self.glob_imports.entry(m.local_id).or_default(); 388 let glob = self.glob_imports.entry(m.local_id).or_default();
390 if !glob.iter().any(|it| *it == (module_id, import_id)) { 389 if !glob.iter().any(|it| *it == (module_id, import_id)) {
@@ -404,12 +403,12 @@ where
404 let variant = EnumVariantId { parent: e, local_id }; 403 let variant = EnumVariantId { parent: e, local_id };
405 let res = Resolution { 404 let res = Resolution {
406 def: PerNs::both(variant.into(), variant.into()), 405 def: PerNs::both(variant.into(), variant.into()),
407 import: Some(import_id), 406 declaration: false,
408 }; 407 };
409 (name, res) 408 (name, res)
410 }) 409 })
411 .collect::<Vec<_>>(); 410 .collect::<Vec<_>>();
412 self.update(module_id, Some(import_id), &resolutions); 411 self.update(module_id, &resolutions);
413 } 412 }
414 Some(d) => { 413 Some(d) => {
415 log::debug!("glob import {:?} from non-module/enum {:?}", import, d); 414 log::debug!("glob import {:?} from non-module/enum {:?}", import, d);
@@ -431,27 +430,21 @@ where
431 } 430 }
432 } 431 }
433 432
434 let resolution = Resolution { def, import: Some(import_id) }; 433 let resolution = Resolution { def, declaration: false };
435 self.update(module_id, Some(import_id), &[(name, resolution)]); 434 self.update(module_id, &[(name, resolution)]);
436 } 435 }
437 None => tested_by!(bogus_paths), 436 None => tested_by!(bogus_paths),
438 } 437 }
439 } 438 }
440 } 439 }
441 440
442 fn update( 441 fn update(&mut self, module_id: LocalModuleId, resolutions: &[(Name, Resolution)]) {
443 &mut self, 442 self.update_recursive(module_id, resolutions, 0)
444 module_id: LocalModuleId,
445 import: Option<LocalImportId>,
446 resolutions: &[(Name, Resolution)],
447 ) {
448 self.update_recursive(module_id, import, resolutions, 0)
449 } 443 }
450 444
451 fn update_recursive( 445 fn update_recursive(
452 &mut self, 446 &mut self,
453 module_id: LocalModuleId, 447 module_id: LocalModuleId,
454 import: Option<LocalImportId>,
455 resolutions: &[(Name, Resolution)], 448 resolutions: &[(Name, Resolution)],
456 depth: usize, 449 depth: usize,
457 ) { 450 ) {
@@ -462,7 +455,7 @@ where
462 let scope = &mut self.def_map.modules[module_id].scope; 455 let scope = &mut self.def_map.modules[module_id].scope;
463 let mut changed = false; 456 let mut changed = false;
464 for (name, res) in resolutions { 457 for (name, res) in resolutions {
465 changed |= scope.push_res(name.clone(), res, import); 458 changed |= scope.push_res(name.clone(), res, depth == 0 && res.declaration);
466 } 459 }
467 460
468 if !changed { 461 if !changed {
@@ -475,9 +468,9 @@ where
475 .flat_map(|v| v.iter()) 468 .flat_map(|v| v.iter())
476 .cloned() 469 .cloned()
477 .collect::<Vec<_>>(); 470 .collect::<Vec<_>>();
478 for (glob_importing_module, glob_import) in glob_imports { 471 for (glob_importing_module, _glob_import) in glob_imports {
479 // We pass the glob import so that the tracked import in those modules is that glob import 472 // We pass the glob import so that the tracked import in those modules is that glob import
480 self.update_recursive(glob_importing_module, Some(glob_import), resolutions, depth + 1); 473 self.update_recursive(glob_importing_module, resolutions, depth + 1);
481 } 474 }
482 } 475 }
483 476
@@ -719,9 +712,9 @@ where
719 def: PerNs::types( 712 def: PerNs::types(
720 ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(), 713 ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(),
721 ), 714 ),
722 import: None, 715 declaration: true,
723 }; 716 };
724 self.def_collector.update(self.module_id, None, &[(name, resolution)]); 717 self.def_collector.update(self.module_id, &[(name, resolution)]);
725 res 718 res
726 } 719 }
727 720
@@ -791,8 +784,8 @@ where
791 PerNs::types(def.into()) 784 PerNs::types(def.into())
792 } 785 }
793 }; 786 };
794 let resolution = Resolution { def, import: None }; 787 let resolution = Resolution { def, declaration: true };
795 self.def_collector.update(self.module_id, None, &[(name, resolution)]) 788 self.def_collector.update(self.module_id, &[(name, resolution)])
796 } 789 }
797 790
798 fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) { 791 fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) {