2251: Privatize modules r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-11-15 07:45:57 +00:00 committed by GitHub
commit 31d01efb06
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 35 deletions

View file

@ -157,7 +157,7 @@ impl Module {
} }
/// Finds a child module with the specified name. /// Finds a child module with the specified name.
pub fn child(self, db: &impl HirDatabase, name: &Name) -> Option<Module> { pub fn child(self, db: &impl DefDatabase, name: &Name) -> Option<Module> {
let def_map = db.crate_def_map(self.id.krate); let def_map = db.crate_def_map(self.id.krate);
let child_id = def_map[self.id.module_id].children.get(name)?; let child_id = def_map[self.id.module_id].children.get(name)?;
Some(self.with_module_id(*child_id)) Some(self.with_module_id(*child_id))

View file

@ -1,6 +1,6 @@
//! FIXME: write short doc here //! FIXME: write short doc here
use hir_def::{StructId, StructOrUnionId, UnionId}; use hir_def::{ModuleId, StructId, StructOrUnionId, UnionId};
use hir_expand::name::AsName; use hir_expand::name::AsName;
use ra_syntax::{ use ra_syntax::{
ast::{self, AstNode, NameOwner}, ast::{self, AstNode, NameOwner},
@ -10,9 +10,9 @@ use ra_syntax::{
use crate::{ use crate::{
db::{AstDatabase, DefDatabase, HirDatabase}, db::{AstDatabase, DefDatabase, HirDatabase},
ids::{AstItemDef, LocationCtx}, ids::{AstItemDef, LocationCtx},
AstId, Const, Crate, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasBody, HasSource, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasBody, HasSource, ImplBlock,
ImplBlock, Local, Module, ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias, Local, Module, ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias, Union,
Union, VariantDef, VariantDef,
}; };
pub trait FromSource: Sized { pub trait FromSource: Sized {
@ -152,44 +152,48 @@ impl Local {
} }
impl Module { impl Module {
pub fn from_declaration(db: &impl HirDatabase, src: Source<ast::Module>) -> Option<Self> { pub fn from_declaration(db: &impl DefDatabase, src: Source<ast::Module>) -> Option<Self> {
let src_parent = Source { let parent_declaration = src.ast.syntax().ancestors().skip(1).find_map(ast::Module::cast);
file_id: src.file_id,
ast: ModuleSource::new(db, Some(src.file_id.original_file(db)), None), let parent_module = match parent_declaration {
}; Some(parent_declaration) => {
let parent_module = Module::from_definition(db, src_parent)?; let src_parent = Source { file_id: src.file_id, ast: parent_declaration };
Module::from_declaration(db, src_parent)
}
_ => {
let src_parent = Source {
file_id: src.file_id,
ast: ModuleSource::new(db, Some(src.file_id.original_file(db)), None),
};
Module::from_definition(db, src_parent)
}
}?;
let child_name = src.ast.name()?; let child_name = src.ast.name()?;
parent_module.child(db, &child_name.as_name()) parent_module.child(db, &child_name.as_name())
} }
pub fn from_definition( pub fn from_definition(db: &impl DefDatabase, src: Source<ModuleSource>) -> Option<Self> {
db: &(impl DefDatabase + AstDatabase), match src.ast {
src: Source<ModuleSource>,
) -> Option<Self> {
let decl_id = match src.ast {
ModuleSource::Module(ref module) => { ModuleSource::Module(ref module) => {
assert!(!module.has_semi()); assert!(!module.has_semi());
let ast_id_map = db.ast_id_map(src.file_id); return Module::from_declaration(
let item_id = AstId::new(src.file_id, ast_id_map.ast_id(module)); db,
Some(item_id) Source { file_id: src.file_id, ast: module.clone() },
);
} }
ModuleSource::SourceFile(_) => None, ModuleSource::SourceFile(_) => (),
}; };
db.relevant_crates(src.file_id.original_file(db)).iter().find_map(|&crate_id| { let original_file = src.file_id.original_file(db);
let def_map = db.crate_def_map(crate_id);
let (module_id, _module_data) = let (krate, module_id) =
def_map.modules.iter().find(|(_module_id, module_data)| { db.relevant_crates(original_file).iter().find_map(|&crate_id| {
if decl_id.is_some() { let crate_def_map = db.crate_def_map(crate_id);
module_data.declaration == decl_id let local_module_id = crate_def_map.modules_for_file(original_file).next()?;
} else { Some((crate_id, local_module_id))
module_data.definition.map(|it| it.into()) == Some(src.file_id) })?;
} Some(Module { id: ModuleId { krate, module_id } })
})?;
Some(Module::new(Crate { crate_id }, module_id))
})
} }
} }

View file

@ -81,7 +81,7 @@ impl TestDB {
let crate_graph = self.crate_graph(); let crate_graph = self.crate_graph();
for krate in crate_graph.iter().next() { for krate in crate_graph.iter().next() {
let crate_def_map = self.crate_def_map(krate); let crate_def_map = self.crate_def_map(krate);
for (module_id, _) in crate_def_map.modules.iter() { for module_id in crate_def_map.modules() {
let module_id = ModuleId { krate, module_id }; let module_id = ModuleId { krate, module_id };
let module = crate::Module::from(module_id); let module = crate::Module::from(module_id);
module.diagnostics( module.diagnostics(

View file

@ -87,7 +87,7 @@ pub struct CrateDefMap {
prelude: Option<ModuleId>, prelude: Option<ModuleId>,
extern_prelude: FxHashMap<Name, ModuleDefId>, extern_prelude: FxHashMap<Name, ModuleDefId>,
root: CrateModuleId, root: CrateModuleId,
pub modules: Arena<CrateModuleId, ModuleData>, modules: Arena<CrateModuleId, ModuleData>,
/// Some macros are not well-behavior, which leads to infinite loop /// Some macros are not well-behavior, which leads to infinite loop
/// e.g. macro_rules! foo { ($ty:ty) => { foo!($ty); } } /// e.g. macro_rules! foo { ($ty:ty) => { foo!($ty); } }
@ -258,6 +258,17 @@ impl CrateDefMap {
let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path); let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path);
(res.resolved_def, res.segment_index) (res.resolved_def, res.segment_index)
} }
pub fn modules(&self) -> impl Iterator<Item = CrateModuleId> + '_ {
self.modules.iter().map(|(id, _data)| id)
}
pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator<Item = CrateModuleId> + '_ {
self.modules
.iter()
.filter(move |(_id, data)| data.definition == Some(file_id))
.map(|(id, _data)| id)
}
} }
mod diagnostics { mod diagnostics {