From 04e8eaa14b11c432d43ad95f3766f8649da30347 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 26 Dec 2019 15:49:13 +0100 Subject: Handle privacy for modules --- crates/ra_hir_def/src/nameres/collector.rs | 26 ++++++++++++++++++++------ crates/ra_hir_def/src/nameres/raw.rs | 18 +++++++++++++++--- crates/ra_hir_def/src/nameres/tests/globs.rs | 3 +-- 3 files changed, 36 insertions(+), 11 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 51df44d2f..a80c4f8e9 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -677,9 +677,13 @@ where let is_macro_use = attrs.by_key("macro_use").exists(); match module { // inline module, just recurse - raw::ModuleData::Definition { name, items, ast_id } => { - let module_id = - self.push_child_module(name.clone(), AstId::new(self.file_id, *ast_id), None); + raw::ModuleData::Definition { name, visibility, items, ast_id } => { + let module_id = self.push_child_module( + name.clone(), + AstId::new(self.file_id, *ast_id), + None, + &visibility, + ); ModCollector { def_collector: &mut *self.def_collector, @@ -694,7 +698,7 @@ where } } // out of line module, resolve, parse and recurse - raw::ModuleData::Declaration { name, ast_id } => { + raw::ModuleData::Declaration { name, visibility, ast_id } => { let ast_id = AstId::new(self.file_id, *ast_id); match self.mod_dir.resolve_declaration( self.def_collector.db, @@ -703,7 +707,12 @@ where path_attr, ) { Ok((file_id, mod_dir)) => { - let module_id = self.push_child_module(name.clone(), ast_id, Some(file_id)); + let module_id = self.push_child_module( + name.clone(), + ast_id, + Some(file_id), + &visibility, + ); let raw_items = self.def_collector.db.raw_items(file_id.into()); ModCollector { def_collector: &mut *self.def_collector, @@ -734,7 +743,13 @@ where name: Name, declaration: AstId, definition: Option, + visibility: &crate::visibility::Visibility, ) -> LocalModuleId { + let vis = self + .def_collector + .def_map + .resolve_visibility(self.def_collector.db, self.module_id, visibility) + .unwrap_or(ResolvedVisibility::Public); let modules = &mut self.def_collector.def_map.modules; let res = modules.alloc(ModuleData::default()); modules[res].parent = Some(self.module_id); @@ -745,7 +760,6 @@ where modules[self.module_id].children.insert(name.clone(), res); let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res }; let def: ModuleDefId = module.into(); - let vis = ResolvedVisibility::Public; // TODO handle module visibility self.def_collector.def_map.modules[self.module_id].scope.define_def(def); self.def_collector.update(self.module_id, &[(name, PerNs::from_def(def, vis))], vis); res diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 9dabb5b6d..59f79f7cd 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -125,8 +125,17 @@ impl_arena_id!(Module); #[derive(Debug, PartialEq, Eq)] pub(super) enum ModuleData { - Declaration { name: Name, ast_id: FileAstId }, - Definition { name: Name, ast_id: FileAstId, items: Vec }, + Declaration { + name: Name, + visibility: Visibility, + ast_id: FileAstId, + }, + Definition { + name: Name, + visibility: Visibility, + ast_id: FileAstId, + items: Vec, + }, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -283,10 +292,12 @@ impl RawItemsCollector { None => return, }; let attrs = self.parse_attrs(&module); + let visibility = Visibility::from_ast_with_hygiene(module.visibility(), &self.hygiene); let ast_id = self.source_ast_id_map.ast_id(&module); if module.has_semi() { - let item = self.raw_items.modules.alloc(ModuleData::Declaration { name, ast_id }); + let item = + self.raw_items.modules.alloc(ModuleData::Declaration { name, visibility, ast_id }); self.push_item(current_module, attrs, RawItemKind::Module(item)); return; } @@ -294,6 +305,7 @@ impl RawItemsCollector { if let Some(item_list) = module.item_list() { let item = self.raw_items.modules.alloc(ModuleData::Definition { name, + visibility, ast_id, items: Vec::new(), }); diff --git a/crates/ra_hir_def/src/nameres/tests/globs.rs b/crates/ra_hir_def/src/nameres/tests/globs.rs index 5f137d3a6..82d947b78 100644 --- a/crates/ra_hir_def/src/nameres/tests/globs.rs +++ b/crates/ra_hir_def/src/nameres/tests/globs.rs @@ -122,7 +122,7 @@ fn glob_privacy_2() { use foo::bar::*; //- /foo/mod.rs - pub mod bar; + mod bar; fn Foo() {}; pub struct Foo {}; @@ -136,7 +136,6 @@ fn glob_privacy_2() { crate Foo: t PubCrateStruct: t v - bar: t foo: t crate::foo -- cgit v1.2.3