Handle privacy for modules

This commit is contained in:
Florian Diebold 2019-12-26 15:49:13 +01:00
parent 1a4a3eb69b
commit 04e8eaa14b
3 changed files with 36 additions and 11 deletions

View file

@ -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<ast::Module>,
definition: Option<FileId>,
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

View file

@ -125,8 +125,17 @@ impl_arena_id!(Module);
#[derive(Debug, PartialEq, Eq)]
pub(super) enum ModuleData {
Declaration { name: Name, ast_id: FileAstId<ast::Module> },
Definition { name: Name, ast_id: FileAstId<ast::Module>, items: Vec<RawItem> },
Declaration {
name: Name,
visibility: Visibility,
ast_id: FileAstId<ast::Module>,
},
Definition {
name: Name,
visibility: Visibility,
ast_id: FileAstId<ast::Module>,
items: Vec<RawItem>,
},
}
#[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(),
});

View file

@ -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