diff options
Diffstat (limited to 'crates/ra_hir_def/src/nameres/collector.rs')
-rw-r--r-- | crates/ra_hir_def/src/nameres/collector.rs | 109 |
1 files changed, 82 insertions, 27 deletions
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index b9f40d3dd..8a22b0585 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs | |||
@@ -24,6 +24,7 @@ use crate::{ | |||
24 | }, | 24 | }, |
25 | path::{ModPath, PathKind}, | 25 | path::{ModPath, PathKind}, |
26 | per_ns::PerNs, | 26 | per_ns::PerNs, |
27 | visibility::Visibility, | ||
27 | AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern, | 28 | AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern, |
28 | LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, | 29 | LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, |
29 | }; | 30 | }; |
@@ -108,7 +109,7 @@ struct MacroDirective { | |||
108 | struct DefCollector<'a, DB> { | 109 | struct DefCollector<'a, DB> { |
109 | db: &'a DB, | 110 | db: &'a DB, |
110 | def_map: CrateDefMap, | 111 | def_map: CrateDefMap, |
111 | glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, raw::Import)>>, | 112 | glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, Visibility)>>, |
112 | unresolved_imports: Vec<ImportDirective>, | 113 | unresolved_imports: Vec<ImportDirective>, |
113 | resolved_imports: Vec<ImportDirective>, | 114 | resolved_imports: Vec<ImportDirective>, |
114 | unexpanded_macros: Vec<MacroDirective>, | 115 | unexpanded_macros: Vec<MacroDirective>, |
@@ -214,7 +215,11 @@ where | |||
214 | // In Rust, `#[macro_export]` macros are unconditionally visible at the | 215 | // In Rust, `#[macro_export]` macros are unconditionally visible at the |
215 | // crate root, even if the parent modules is **not** visible. | 216 | // crate root, even if the parent modules is **not** visible. |
216 | if export { | 217 | if export { |
217 | self.update(self.def_map.root, &[(name, PerNs::macros(macro_))]); | 218 | self.update( |
219 | self.def_map.root, | ||
220 | &[(name, PerNs::macros(macro_, Visibility::Public))], | ||
221 | Visibility::Public, | ||
222 | ); | ||
218 | } | 223 | } |
219 | } | 224 | } |
220 | 225 | ||
@@ -348,9 +353,12 @@ where | |||
348 | 353 | ||
349 | fn record_resolved_import(&mut self, directive: &ImportDirective) { | 354 | fn record_resolved_import(&mut self, directive: &ImportDirective) { |
350 | let module_id = directive.module_id; | 355 | let module_id = directive.module_id; |
351 | let import_id = directive.import_id; | ||
352 | let import = &directive.import; | 356 | let import = &directive.import; |
353 | let def = directive.status.namespaces(); | 357 | let def = directive.status.namespaces(); |
358 | let vis = self | ||
359 | .def_map | ||
360 | .resolve_visibility(self.db, module_id, &directive.import.visibility) | ||
361 | .unwrap_or(Visibility::Public); | ||
354 | 362 | ||
355 | if import.is_glob { | 363 | if import.is_glob { |
356 | log::debug!("glob import: {:?}", import); | 364 | log::debug!("glob import: {:?}", import); |
@@ -366,9 +374,16 @@ where | |||
366 | let scope = &item_map[m.local_id].scope; | 374 | let scope = &item_map[m.local_id].scope; |
367 | 375 | ||
368 | // Module scoped macros is included | 376 | // Module scoped macros is included |
369 | let items = scope.collect_resolutions(); | 377 | let items = scope |
370 | 378 | .resolutions() | |
371 | self.update(module_id, &items); | 379 | // only keep visible names... |
380 | .map(|(n, res)| { | ||
381 | (n, res.filter_visibility(|v| v.is_visible_from_other_crate())) | ||
382 | }) | ||
383 | .filter(|(_, res)| !res.is_none()) | ||
384 | .collect::<Vec<_>>(); | ||
385 | |||
386 | self.update(module_id, &items, vis); | ||
372 | } else { | 387 | } else { |
373 | // glob import from same crate => we do an initial | 388 | // glob import from same crate => we do an initial |
374 | // import, and then need to propagate any further | 389 | // import, and then need to propagate any further |
@@ -376,13 +391,25 @@ where | |||
376 | let scope = &self.def_map[m.local_id].scope; | 391 | let scope = &self.def_map[m.local_id].scope; |
377 | 392 | ||
378 | // Module scoped macros is included | 393 | // Module scoped macros is included |
379 | let items = scope.collect_resolutions(); | 394 | let items = scope |
380 | 395 | .resolutions() | |
381 | self.update(module_id, &items); | 396 | // only keep visible names... |
397 | .map(|(n, res)| { | ||
398 | ( | ||
399 | n, | ||
400 | res.filter_visibility(|v| { | ||
401 | v.is_visible_from_def_map(&self.def_map, module_id) | ||
402 | }), | ||
403 | ) | ||
404 | }) | ||
405 | .filter(|(_, res)| !res.is_none()) | ||
406 | .collect::<Vec<_>>(); | ||
407 | |||
408 | self.update(module_id, &items, vis); | ||
382 | // record the glob import in case we add further items | 409 | // record the glob import in case we add further items |
383 | let glob = self.glob_imports.entry(m.local_id).or_default(); | 410 | let glob = self.glob_imports.entry(m.local_id).or_default(); |
384 | if !glob.iter().any(|it| *it == (module_id, import_id)) { | 411 | if !glob.iter().any(|(mid, _)| *mid == module_id) { |
385 | glob.push((module_id, import_id)); | 412 | glob.push((module_id, vis)); |
386 | } | 413 | } |
387 | } | 414 | } |
388 | } | 415 | } |
@@ -396,11 +423,11 @@ where | |||
396 | .map(|(local_id, variant_data)| { | 423 | .map(|(local_id, variant_data)| { |
397 | let name = variant_data.name.clone(); | 424 | let name = variant_data.name.clone(); |
398 | let variant = EnumVariantId { parent: e, local_id }; | 425 | let variant = EnumVariantId { parent: e, local_id }; |
399 | let res = PerNs::both(variant.into(), variant.into()); | 426 | let res = PerNs::both(variant.into(), variant.into(), vis); |
400 | (name, res) | 427 | (name, res) |
401 | }) | 428 | }) |
402 | .collect::<Vec<_>>(); | 429 | .collect::<Vec<_>>(); |
403 | self.update(module_id, &resolutions); | 430 | self.update(module_id, &resolutions, vis); |
404 | } | 431 | } |
405 | Some(d) => { | 432 | Some(d) => { |
406 | log::debug!("glob import {:?} from non-module/enum {:?}", import, d); | 433 | log::debug!("glob import {:?} from non-module/enum {:?}", import, d); |
@@ -422,21 +449,24 @@ where | |||
422 | } | 449 | } |
423 | } | 450 | } |
424 | 451 | ||
425 | self.update(module_id, &[(name, def)]); | 452 | self.update(module_id, &[(name, def)], vis); |
426 | } | 453 | } |
427 | None => tested_by!(bogus_paths), | 454 | None => tested_by!(bogus_paths), |
428 | } | 455 | } |
429 | } | 456 | } |
430 | } | 457 | } |
431 | 458 | ||
432 | fn update(&mut self, module_id: LocalModuleId, resolutions: &[(Name, PerNs)]) { | 459 | fn update(&mut self, module_id: LocalModuleId, resolutions: &[(Name, PerNs)], vis: Visibility) { |
433 | self.update_recursive(module_id, resolutions, 0) | 460 | self.update_recursive(module_id, resolutions, vis, 0) |
434 | } | 461 | } |
435 | 462 | ||
436 | fn update_recursive( | 463 | fn update_recursive( |
437 | &mut self, | 464 | &mut self, |
438 | module_id: LocalModuleId, | 465 | module_id: LocalModuleId, |
439 | resolutions: &[(Name, PerNs)], | 466 | resolutions: &[(Name, PerNs)], |
467 | // All resolutions are imported with this visibility; the visibilies in | ||
468 | // the `PerNs` values are ignored and overwritten | ||
469 | vis: Visibility, | ||
440 | depth: usize, | 470 | depth: usize, |
441 | ) { | 471 | ) { |
442 | if depth > 100 { | 472 | if depth > 100 { |
@@ -446,7 +476,7 @@ where | |||
446 | let scope = &mut self.def_map.modules[module_id].scope; | 476 | let scope = &mut self.def_map.modules[module_id].scope; |
447 | let mut changed = false; | 477 | let mut changed = false; |
448 | for (name, res) in resolutions { | 478 | for (name, res) in resolutions { |
449 | changed |= scope.push_res(name.clone(), *res); | 479 | changed |= scope.push_res(name.clone(), res.with_visibility(vis)); |
450 | } | 480 | } |
451 | 481 | ||
452 | if !changed { | 482 | if !changed { |
@@ -459,9 +489,13 @@ where | |||
459 | .flat_map(|v| v.iter()) | 489 | .flat_map(|v| v.iter()) |
460 | .cloned() | 490 | .cloned() |
461 | .collect::<Vec<_>>(); | 491 | .collect::<Vec<_>>(); |
462 | for (glob_importing_module, _glob_import) in glob_imports { | 492 | for (glob_importing_module, glob_import_vis) in glob_imports { |
463 | // We pass the glob import so that the tracked import in those modules is that glob import | 493 | // we know all resolutions have the same visibility (`vis`), so we |
464 | self.update_recursive(glob_importing_module, resolutions, depth + 1); | 494 | // just need to check that once |
495 | if !vis.is_visible_from_def_map(&self.def_map, glob_importing_module) { | ||
496 | continue; | ||
497 | } | ||
498 | self.update_recursive(glob_importing_module, resolutions, glob_import_vis, depth + 1); | ||
465 | } | 499 | } |
466 | } | 500 | } |
467 | 501 | ||
@@ -633,9 +667,13 @@ where | |||
633 | let is_macro_use = attrs.by_key("macro_use").exists(); | 667 | let is_macro_use = attrs.by_key("macro_use").exists(); |
634 | match module { | 668 | match module { |
635 | // inline module, just recurse | 669 | // inline module, just recurse |
636 | raw::ModuleData::Definition { name, items, ast_id } => { | 670 | raw::ModuleData::Definition { name, visibility, items, ast_id } => { |
637 | let module_id = | 671 | let module_id = self.push_child_module( |
638 | self.push_child_module(name.clone(), AstId::new(self.file_id, *ast_id), None); | 672 | name.clone(), |
673 | AstId::new(self.file_id, *ast_id), | ||
674 | None, | ||
675 | &visibility, | ||
676 | ); | ||
639 | 677 | ||
640 | ModCollector { | 678 | ModCollector { |
641 | def_collector: &mut *self.def_collector, | 679 | def_collector: &mut *self.def_collector, |
@@ -650,7 +688,7 @@ where | |||
650 | } | 688 | } |
651 | } | 689 | } |
652 | // out of line module, resolve, parse and recurse | 690 | // out of line module, resolve, parse and recurse |
653 | raw::ModuleData::Declaration { name, ast_id } => { | 691 | raw::ModuleData::Declaration { name, visibility, ast_id } => { |
654 | let ast_id = AstId::new(self.file_id, *ast_id); | 692 | let ast_id = AstId::new(self.file_id, *ast_id); |
655 | match self.mod_dir.resolve_declaration( | 693 | match self.mod_dir.resolve_declaration( |
656 | self.def_collector.db, | 694 | self.def_collector.db, |
@@ -659,7 +697,12 @@ where | |||
659 | path_attr, | 697 | path_attr, |
660 | ) { | 698 | ) { |
661 | Ok((file_id, mod_dir)) => { | 699 | Ok((file_id, mod_dir)) => { |
662 | let module_id = self.push_child_module(name.clone(), ast_id, Some(file_id)); | 700 | let module_id = self.push_child_module( |
701 | name.clone(), | ||
702 | ast_id, | ||
703 | Some(file_id), | ||
704 | &visibility, | ||
705 | ); | ||
663 | let raw_items = self.def_collector.db.raw_items(file_id.into()); | 706 | let raw_items = self.def_collector.db.raw_items(file_id.into()); |
664 | ModCollector { | 707 | ModCollector { |
665 | def_collector: &mut *self.def_collector, | 708 | def_collector: &mut *self.def_collector, |
@@ -690,7 +733,13 @@ where | |||
690 | name: Name, | 733 | name: Name, |
691 | declaration: AstId<ast::Module>, | 734 | declaration: AstId<ast::Module>, |
692 | definition: Option<FileId>, | 735 | definition: Option<FileId>, |
736 | visibility: &crate::visibility::RawVisibility, | ||
693 | ) -> LocalModuleId { | 737 | ) -> LocalModuleId { |
738 | let vis = self | ||
739 | .def_collector | ||
740 | .def_map | ||
741 | .resolve_visibility(self.def_collector.db, self.module_id, visibility) | ||
742 | .unwrap_or(Visibility::Public); | ||
694 | let modules = &mut self.def_collector.def_map.modules; | 743 | let modules = &mut self.def_collector.def_map.modules; |
695 | let res = modules.alloc(ModuleData::default()); | 744 | let res = modules.alloc(ModuleData::default()); |
696 | modules[res].parent = Some(self.module_id); | 745 | modules[res].parent = Some(self.module_id); |
@@ -702,7 +751,7 @@ where | |||
702 | let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res }; | 751 | let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res }; |
703 | let def: ModuleDefId = module.into(); | 752 | let def: ModuleDefId = module.into(); |
704 | self.def_collector.def_map.modules[self.module_id].scope.define_def(def); | 753 | self.def_collector.def_map.modules[self.module_id].scope.define_def(def); |
705 | self.def_collector.update(self.module_id, &[(name, def.into())]); | 754 | self.def_collector.update(self.module_id, &[(name, PerNs::from_def(def, vis))], vis); |
706 | res | 755 | res |
707 | } | 756 | } |
708 | 757 | ||
@@ -716,6 +765,7 @@ where | |||
716 | 765 | ||
717 | let name = def.name.clone(); | 766 | let name = def.name.clone(); |
718 | let container = ContainerId::ModuleId(module); | 767 | let container = ContainerId::ModuleId(module); |
768 | let vis = &def.visibility; | ||
719 | let def: ModuleDefId = match def.kind { | 769 | let def: ModuleDefId = match def.kind { |
720 | raw::DefKind::Function(ast_id) => FunctionLoc { | 770 | raw::DefKind::Function(ast_id) => FunctionLoc { |
721 | container: container.into(), | 771 | container: container.into(), |
@@ -761,7 +811,12 @@ where | |||
761 | .into(), | 811 | .into(), |
762 | }; | 812 | }; |
763 | self.def_collector.def_map.modules[self.module_id].scope.define_def(def); | 813 | self.def_collector.def_map.modules[self.module_id].scope.define_def(def); |
764 | self.def_collector.update(self.module_id, &[(name, def.into())]) | 814 | let vis = self |
815 | .def_collector | ||
816 | .def_map | ||
817 | .resolve_visibility(self.def_collector.db, self.module_id, vis) | ||
818 | .unwrap_or(Visibility::Public); | ||
819 | self.def_collector.update(self.module_id, &[(name, PerNs::from_def(def, vis))], vis) | ||
765 | } | 820 | } |
766 | 821 | ||
767 | fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) { | 822 | fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) { |