From fd4456d0ec88e3433a7a8be6f27d8af9afedefe5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 6 Jan 2019 17:33:27 +0300 Subject: flatten module structure --- crates/ra_hir/src/code_model_api.rs | 14 +- crates/ra_hir/src/code_model_impl.rs | 196 +---------- crates/ra_hir/src/code_model_impl/krate.rs | 45 +++ crates/ra_hir/src/code_model_impl/module.rs | 154 +++++++++ crates/ra_hir/src/db.rs | 8 +- crates/ra_hir/src/impl_block.rs | 2 +- crates/ra_hir/src/lib.rs | 11 +- crates/ra_hir/src/module.rs | 209 ------------ crates/ra_hir/src/module/imp.rs | 190 ----------- crates/ra_hir/src/module/nameres.rs | 509 ---------------------------- crates/ra_hir/src/module/nameres/tests.rs | 273 --------------- crates/ra_hir/src/module_tree.rs | 375 ++++++++++++++++++++ crates/ra_hir/src/nameres.rs | 509 ++++++++++++++++++++++++++++ crates/ra_hir/src/nameres/tests.rs | 273 +++++++++++++++ crates/ra_hir/src/query_definitions.rs | 7 +- crates/ra_hir/src/source_binder.rs | 2 +- 16 files changed, 1386 insertions(+), 1391 deletions(-) create mode 100644 crates/ra_hir/src/code_model_impl/krate.rs create mode 100644 crates/ra_hir/src/code_model_impl/module.rs delete mode 100644 crates/ra_hir/src/module.rs delete mode 100644 crates/ra_hir/src/module/imp.rs delete mode 100644 crates/ra_hir/src/module/nameres.rs delete mode 100644 crates/ra_hir/src/module/nameres/tests.rs create mode 100644 crates/ra_hir/src/module_tree.rs create mode 100644 crates/ra_hir/src/nameres.rs create mode 100644 crates/ra_hir/src/nameres/tests.rs diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index d00d3246f..09b532f74 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -1,7 +1,8 @@ +use relative_path::RelativePathBuf; use ra_db::{CrateId, Cancelable, FileId}; use ra_syntax::{ast, SyntaxNode}; -use crate::{Name, db::HirDatabase, DefId, Path, PerNs, module::{Problem, ModuleScope}}; +use crate::{Name, db::HirDatabase, DefId, Path, PerNs, nameres::ModuleScope}; /// hir::Crate describes a single crate. It's the main inteface with which /// crate's dependencies interact. Mostly, it should be just a proxy for the @@ -39,6 +40,17 @@ pub enum ModuleSource { Module(ast::ModuleNode), } +#[derive(Clone, Debug, Hash, PartialEq, Eq)] +pub enum Problem { + UnresolvedModule { + candidate: RelativePathBuf, + }, + NotDirOwner { + move_to: RelativePathBuf, + candidate: RelativePathBuf, + }, +} + impl Module { /// Name of this module. pub fn name(&self, db: &impl HirDatabase) -> Cancelable> { diff --git a/crates/ra_hir/src/code_model_impl.rs b/crates/ra_hir/src/code_model_impl.rs index 4f4b506dd..d602a439d 100644 --- a/crates/ra_hir/src/code_model_impl.rs +++ b/crates/ra_hir/src/code_model_impl.rs @@ -1,194 +1,2 @@ -use ra_db::{CrateId, Cancelable, SourceRootId, FileId}; -use ra_syntax::{ast, SyntaxNode, AstNode}; - -use crate::{ - HirFileId, Crate, CrateDependency, AsName, DefId, DefLoc, DefKind, Name, Path, PathKind, PerNs, Def, ModuleId, - module::{ModuleScope, Problem}, - db::HirDatabase, -}; - -use crate::code_model_api::{Module, ModuleSource}; - -impl Crate { - pub(crate) fn new(crate_id: CrateId) -> Crate { - Crate { crate_id } - } - pub(crate) fn dependencies_impl(&self, db: &impl HirDatabase) -> Vec { - let crate_graph = db.crate_graph(); - crate_graph - .dependencies(self.crate_id) - .map(|dep| { - let krate = Crate::new(dep.crate_id()); - let name = dep.as_name(); - CrateDependency { krate, name } - }) - .collect() - } - pub(crate) fn root_module_impl(&self, db: &impl HirDatabase) -> Cancelable> { - let crate_graph = db.crate_graph(); - let file_id = crate_graph.crate_root(self.crate_id); - let source_root_id = db.file_source_root(file_id); - let file_id = HirFileId::from(file_id); - let module_tree = db.module_tree(source_root_id)?; - // FIXME: teach module tree about crate roots instead of guessing - let (module_id, _) = ctry!(module_tree - .modules_with_sources() - .find(|(_, src)| src.file_id() == file_id)); - - let def_loc = DefLoc { - kind: DefKind::Module, - source_root_id, - module_id, - source_item_id: module_id.source(&module_tree).0, - }; - let def_id = def_loc.id(db); - - let module = Module::new(def_id); - Ok(Some(module)) - } -} - -impl Module { - pub(crate) fn new(def_id: DefId) -> Self { - crate::code_model_api::Module { def_id } - } - pub(crate) fn from_module_id( - db: &impl HirDatabase, - source_root_id: SourceRootId, - module_id: ModuleId, - ) -> Cancelable { - let module_tree = db.module_tree(source_root_id)?; - let def_loc = DefLoc { - kind: DefKind::Module, - source_root_id, - module_id, - source_item_id: module_id.source(&module_tree).0, - }; - let def_id = def_loc.id(db); - let module = Module::new(def_id); - Ok(module) - } - - pub(crate) fn name_impl(&self, db: &impl HirDatabase) -> Cancelable> { - let loc = self.def_id.loc(db); - let module_tree = db.module_tree(loc.source_root_id)?; - let link = ctry!(loc.module_id.parent_link(&module_tree)); - Ok(Some(link.name(&module_tree).clone())) - } - - pub fn defenition_source_impl( - &self, - db: &impl HirDatabase, - ) -> Cancelable<(FileId, ModuleSource)> { - let loc = self.def_id.loc(db); - let file_id = loc.source_item_id.file_id.as_original_file(); - let syntax_node = db.file_item(loc.source_item_id); - let syntax_node = syntax_node.borrowed(); - let module_source = if let Some(source_file) = ast::SourceFile::cast(syntax_node) { - ModuleSource::SourceFile(source_file.owned()) - } else { - let module = ast::Module::cast(syntax_node).unwrap(); - ModuleSource::Module(module.owned()) - }; - Ok((file_id, module_source)) - } - - pub fn declaration_source_impl( - &self, - db: &impl HirDatabase, - ) -> Cancelable> { - let loc = self.def_id.loc(db); - let module_tree = db.module_tree(loc.source_root_id)?; - let link = ctry!(loc.module_id.parent_link(&module_tree)); - let file_id = link - .owner(&module_tree) - .source(&module_tree) - .file_id() - .as_original_file(); - let src = link.bind_source(&module_tree, db); - Ok(Some((file_id, src))) - } - - pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Cancelable> { - let root = self.crate_root(db)?; - let loc = root.def_id.loc(db); - let file_id = loc.source_item_id.file_id.as_original_file(); - - let crate_graph = db.crate_graph(); - let crate_id = ctry!(crate_graph.crate_id_for_crate_root(file_id)); - Ok(Some(Crate::new(crate_id))) - } - - pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Cancelable { - let loc = self.def_id.loc(db); - let module_tree = db.module_tree(loc.source_root_id)?; - let module_id = loc.module_id.crate_root(&module_tree); - Module::from_module_id(db, loc.source_root_id, module_id) - } - /// Finds a child module with the specified name. - pub fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { - let loc = self.def_id.loc(db); - let module_tree = db.module_tree(loc.source_root_id)?; - let child_id = ctry!(loc.module_id.child(&module_tree, name)); - Module::from_module_id(db, loc.source_root_id, child_id).map(Some) - } - pub fn parent_impl(&self, db: &impl HirDatabase) -> Cancelable> { - let loc = self.def_id.loc(db); - let module_tree = db.module_tree(loc.source_root_id)?; - let parent_id = ctry!(loc.module_id.parent(&module_tree)); - Module::from_module_id(db, loc.source_root_id, parent_id).map(Some) - } - /// Returns a `ModuleScope`: a set of items, visible in this module. - pub fn scope_impl(&self, db: &impl HirDatabase) -> Cancelable { - let loc = self.def_id.loc(db); - let item_map = db.item_map(loc.source_root_id)?; - let res = item_map.per_module[&loc.module_id].clone(); - Ok(res) - } - pub fn resolve_path_impl( - &self, - db: &impl HirDatabase, - path: &Path, - ) -> Cancelable> { - let mut curr_per_ns = PerNs::types( - match path.kind { - PathKind::Crate => self.crate_root(db)?, - PathKind::Self_ | PathKind::Plain => self.clone(), - PathKind::Super => { - if let Some(p) = self.parent(db)? { - p - } else { - return Ok(PerNs::none()); - } - } - } - .def_id, - ); - - let segments = &path.segments; - for name in segments.iter() { - let curr = if let Some(r) = curr_per_ns.as_ref().take_types() { - r - } else { - return Ok(PerNs::none()); - }; - let module = match curr.resolve(db)? { - Def::Module(it) => it, - // TODO here would be the place to handle enum variants... - _ => return Ok(PerNs::none()), - }; - let scope = module.scope(db)?; - curr_per_ns = if let Some(r) = scope.get(&name) { - r.def_id - } else { - return Ok(PerNs::none()); - }; - } - Ok(curr_per_ns) - } - pub fn problems_impl(&self, db: &impl HirDatabase) -> Cancelable> { - let loc = self.def_id.loc(db); - let module_tree = db.module_tree(loc.source_root_id)?; - Ok(loc.module_id.problems(&module_tree, db)) - } -} +mod krate; // `crate` is invalid ident :( +pub(crate) mod module; diff --git a/crates/ra_hir/src/code_model_impl/krate.rs b/crates/ra_hir/src/code_model_impl/krate.rs new file mode 100644 index 000000000..591a81597 --- /dev/null +++ b/crates/ra_hir/src/code_model_impl/krate.rs @@ -0,0 +1,45 @@ +use ra_db::{CrateId, Cancelable}; + +use crate::{ + HirFileId, Crate, CrateDependency, AsName, DefLoc, DefKind, Module, + db::HirDatabase, +}; + +impl Crate { + pub(crate) fn new(crate_id: CrateId) -> Crate { + Crate { crate_id } + } + pub(crate) fn dependencies_impl(&self, db: &impl HirDatabase) -> Vec { + let crate_graph = db.crate_graph(); + crate_graph + .dependencies(self.crate_id) + .map(|dep| { + let krate = Crate::new(dep.crate_id()); + let name = dep.as_name(); + CrateDependency { krate, name } + }) + .collect() + } + pub(crate) fn root_module_impl(&self, db: &impl HirDatabase) -> Cancelable> { + let crate_graph = db.crate_graph(); + let file_id = crate_graph.crate_root(self.crate_id); + let source_root_id = db.file_source_root(file_id); + let file_id = HirFileId::from(file_id); + let module_tree = db.module_tree(source_root_id)?; + // FIXME: teach module tree about crate roots instead of guessing + let (module_id, _) = ctry!(module_tree + .modules_with_sources() + .find(|(_, src)| src.file_id() == file_id)); + + let def_loc = DefLoc { + kind: DefKind::Module, + source_root_id, + module_id, + source_item_id: module_id.source(&module_tree).0, + }; + let def_id = def_loc.id(db); + + let module = Module::new(def_id); + Ok(Some(module)) + } +} diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs new file mode 100644 index 000000000..02078f188 --- /dev/null +++ b/crates/ra_hir/src/code_model_impl/module.rs @@ -0,0 +1,154 @@ +use ra_db::{Cancelable, SourceRootId, FileId}; +use ra_syntax::{ast, SyntaxNode, AstNode}; + +use crate::{ + Module, ModuleSource, Problem, + Crate, DefId, DefLoc, DefKind, Name, Path, PathKind, PerNs, Def, ModuleId, + nameres::ModuleScope, + db::HirDatabase, +}; + +impl Module { + pub(crate) fn new(def_id: DefId) -> Self { + crate::code_model_api::Module { def_id } + } + pub(crate) fn from_module_id( + db: &impl HirDatabase, + source_root_id: SourceRootId, + module_id: ModuleId, + ) -> Cancelable { + let module_tree = db.module_tree(source_root_id)?; + let def_loc = DefLoc { + kind: DefKind::Module, + source_root_id, + module_id, + source_item_id: module_id.source(&module_tree).0, + }; + let def_id = def_loc.id(db); + let module = Module::new(def_id); + Ok(module) + } + + pub(crate) fn name_impl(&self, db: &impl HirDatabase) -> Cancelable> { + let loc = self.def_id.loc(db); + let module_tree = db.module_tree(loc.source_root_id)?; + let link = ctry!(loc.module_id.parent_link(&module_tree)); + Ok(Some(link.name(&module_tree).clone())) + } + + pub fn defenition_source_impl( + &self, + db: &impl HirDatabase, + ) -> Cancelable<(FileId, ModuleSource)> { + let loc = self.def_id.loc(db); + let file_id = loc.source_item_id.file_id.as_original_file(); + let syntax_node = db.file_item(loc.source_item_id); + let syntax_node = syntax_node.borrowed(); + let module_source = if let Some(source_file) = ast::SourceFile::cast(syntax_node) { + ModuleSource::SourceFile(source_file.owned()) + } else { + let module = ast::Module::cast(syntax_node).unwrap(); + ModuleSource::Module(module.owned()) + }; + Ok((file_id, module_source)) + } + + pub fn declaration_source_impl( + &self, + db: &impl HirDatabase, + ) -> Cancelable> { + let loc = self.def_id.loc(db); + let module_tree = db.module_tree(loc.source_root_id)?; + let link = ctry!(loc.module_id.parent_link(&module_tree)); + let file_id = link + .owner(&module_tree) + .source(&module_tree) + .file_id() + .as_original_file(); + let src = link.bind_source(&module_tree, db); + Ok(Some((file_id, src))) + } + + pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Cancelable> { + let root = self.crate_root(db)?; + let loc = root.def_id.loc(db); + let file_id = loc.source_item_id.file_id.as_original_file(); + + let crate_graph = db.crate_graph(); + let crate_id = ctry!(crate_graph.crate_id_for_crate_root(file_id)); + Ok(Some(Crate::new(crate_id))) + } + + pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Cancelable { + let loc = self.def_id.loc(db); + let module_tree = db.module_tree(loc.source_root_id)?; + let module_id = loc.module_id.crate_root(&module_tree); + Module::from_module_id(db, loc.source_root_id, module_id) + } + /// Finds a child module with the specified name. + pub fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { + let loc = self.def_id.loc(db); + let module_tree = db.module_tree(loc.source_root_id)?; + let child_id = ctry!(loc.module_id.child(&module_tree, name)); + Module::from_module_id(db, loc.source_root_id, child_id).map(Some) + } + pub fn parent_impl(&self, db: &impl HirDatabase) -> Cancelable> { + let loc = self.def_id.loc(db); + let module_tree = db.module_tree(loc.source_root_id)?; + let parent_id = ctry!(loc.module_id.parent(&module_tree)); + Module::from_module_id(db, loc.source_root_id, parent_id).map(Some) + } + /// Returns a `ModuleScope`: a set of items, visible in this module. + pub fn scope_impl(&self, db: &impl HirDatabase) -> Cancelable { + let loc = self.def_id.loc(db); + let item_map = db.item_map(loc.source_root_id)?; + let res = item_map.per_module[&loc.module_id].clone(); + Ok(res) + } + pub fn resolve_path_impl( + &self, + db: &impl HirDatabase, + path: &Path, + ) -> Cancelable> { + let mut curr_per_ns = PerNs::types( + match path.kind { + PathKind::Crate => self.crate_root(db)?, + PathKind::Self_ | PathKind::Plain => self.clone(), + PathKind::Super => { + if let Some(p) = self.parent(db)? { + p + } else { + return Ok(PerNs::none()); + } + } + } + .def_id, + ); + + let segments = &path.segments; + for name in segments.iter() { + let curr = if let Some(r) = curr_per_ns.as_ref().take_types() { + r + } else { + return Ok(PerNs::none()); + }; + let module = match curr.resolve(db)? { + Def::Module(it) => it, + // TODO here would be the place to handle enum variants... + _ => return Ok(PerNs::none()), + }; + let scope = module.scope(db)?; + curr_per_ns = if let Some(r) = scope.get(&name) { + r.def_id + } else { + return Ok(PerNs::none()); + }; + } + Ok(curr_per_ns) + } + pub fn problems_impl(&self, db: &impl HirDatabase) -> Cancelable> { + let loc = self.def_id.loc(db); + let module_tree = db.module_tree(loc.source_root_id)?; + Ok(loc.module_id.problems(&module_tree, db)) + } +} diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 96a3c60b9..2702961ba 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -9,8 +9,8 @@ use crate::{ query_definitions, FnSignature, FnScopes, macros::MacroExpansion, - module::{ModuleId, ModuleTree, ModuleSource, - nameres::{ItemMap, InputModuleItems}}, + module_tree::{ModuleId, ModuleTree, ModuleSource}, + nameres::{ItemMap, InputModuleItems}, ty::{InferenceResult, Ty}, adt::{StructData, EnumData}, impl_block::ModuleImplBlocks, @@ -71,7 +71,7 @@ pub trait HirDatabase: SyntaxDatabase use fn query_definitions::file_item; } - fn submodules(source: ModuleSource) -> Cancelable>> { + fn submodules(source: ModuleSource) -> Cancelable>> { type SubmodulesQuery; use fn query_definitions::submodules; } @@ -86,7 +86,7 @@ pub trait HirDatabase: SyntaxDatabase } fn module_tree(source_root_id: SourceRootId) -> Cancelable> { type ModuleTreeQuery; - use fn crate::module::imp::module_tree; + use fn crate::module_tree::ModuleTree::module_tree_query; } fn impls_in_module(source_root_id: SourceRootId, module_id: ModuleId) -> Cancelable> { diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index 0d1b94c42..7ce8d17e6 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs @@ -10,7 +10,7 @@ use crate::{ Function, db::HirDatabase, type_ref::TypeRef, - module::ModuleId, + module_tree::ModuleId, }; use crate::code_model_api::{Module, ModuleSource}; diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 2fa357fec..7e74f2eaf 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -24,7 +24,8 @@ pub mod source_binder; mod ids; mod macros; mod name; -mod module; +mod module_tree; +mod nameres; mod function; mod adt; mod type_ref; @@ -32,14 +33,13 @@ mod ty; mod impl_block; mod expr; -pub mod code_model_api; +mod code_model_api; mod code_model_impl; use crate::{ db::HirDatabase, name::{AsName, KnownName}, ids::{DefKind, SourceItemId, SourceFileItemId, SourceFileItems}, - code_model_api::{Crate, CrateDependency} }; pub use self::{ @@ -56,7 +56,10 @@ pub use self::{ pub use self::function::FnSignatureInfo; -pub use self::code_model_api::Module; +pub use self::code_model_api::{ + Crate, CrateDependency, + Module, ModuleSource, Problem, +}; pub enum Def { Module(Module), diff --git a/crates/ra_hir/src/module.rs b/crates/ra_hir/src/module.rs deleted file mode 100644 index ebaf5f47a..000000000 --- a/crates/ra_hir/src/module.rs +++ /dev/null @@ -1,209 +0,0 @@ -pub(super) mod imp; -pub(super) mod nameres; - -use ra_syntax::{ - algo::generate, - ast::{self, AstNode, NameOwner}, - SyntaxNode, -}; -use ra_arena::{Arena, RawId, impl_arena_id}; -use relative_path::RelativePathBuf; - -use crate::{Name, HirDatabase, SourceItemId, SourceFileItemId, HirFileId}; - -pub use self::nameres::{ModuleScope, Resolution, Namespace, PerNs}; - -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct ModuleId(RawId); -impl_arena_id!(ModuleId); - -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct LinkId(RawId); -impl_arena_id!(LinkId); - -/// Physically, rust source is organized as a set of files, but logically it is -/// organized as a tree of modules. Usually, a single file corresponds to a -/// single module, but it is not nessary the case. -/// -/// Module encapsulate the logic of transitioning from the fuzzy world of files -/// (which can have multiple parents) to the precise world of modules (which -/// always have one parent). -#[derive(Default, Debug, PartialEq, Eq)] -pub struct ModuleTree { - mods: Arena, - links: Arena, -} - -impl ModuleTree { - pub(crate) fn modules<'a>(&'a self) -> impl Iterator + 'a { - self.mods.iter().map(|(id, _)| id) - } - - pub(crate) fn modules_with_sources<'a>( - &'a self, - ) -> impl Iterator + 'a { - self.mods.iter().map(|(id, m)| (id, m.source)) - } -} - -/// `ModuleSource` is the syntax tree element that produced this module: -/// either a file, or an inlinde module. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub struct ModuleSource(pub(crate) SourceItemId); - -/// An owned syntax node for a module. Unlike `ModuleSource`, -/// this holds onto the AST for the whole file. -pub(crate) enum ModuleSourceNode { - SourceFile(ast::SourceFileNode), - Module(ast::ModuleNode), -} - -#[derive(Clone, Debug, Hash, PartialEq, Eq)] -pub enum Problem { - UnresolvedModule { - candidate: RelativePathBuf, - }, - NotDirOwner { - move_to: RelativePathBuf, - candidate: RelativePathBuf, - }, -} - -impl ModuleId { - pub(crate) fn source(self, tree: &ModuleTree) -> ModuleSource { - tree.mods[self].source - } - pub(crate) fn parent_link(self, tree: &ModuleTree) -> Option { - tree.mods[self].parent - } - pub(crate) fn parent(self, tree: &ModuleTree) -> Option { - let link = self.parent_link(tree)?; - Some(tree.links[link].owner) - } - pub(crate) fn crate_root(self, tree: &ModuleTree) -> ModuleId { - generate(Some(self), move |it| it.parent(tree)) - .last() - .unwrap() - } - pub(crate) fn child(self, tree: &ModuleTree, name: &Name) -> Option { - let link = tree.mods[self] - .children - .iter() - .map(|&it| &tree.links[it]) - .find(|it| it.name == *name)?; - Some(*link.points_to.first()?) - } - fn children<'a>(self, tree: &'a ModuleTree) -> impl Iterator + 'a { - tree.mods[self].children.iter().filter_map(move |&it| { - let link = &tree.links[it]; - let module = *link.points_to.first()?; - Some((link.name.clone(), module)) - }) - } - pub(crate) fn problems( - self, - tree: &ModuleTree, - db: &impl HirDatabase, - ) -> Vec<(SyntaxNode, Problem)> { - tree.mods[self] - .children - .iter() - .filter_map(|&it| { - let p = tree.links[it].problem.clone()?; - let s = it.bind_source(tree, db); - let s = s.borrowed().name().unwrap().syntax().owned(); - Some((s, p)) - }) - .collect() - } -} - -impl LinkId { - pub(crate) fn owner(self, tree: &ModuleTree) -> ModuleId { - tree.links[self].owner - } - pub(crate) fn name(self, tree: &ModuleTree) -> &Name { - &tree.links[self].name - } - pub(crate) fn bind_source<'a>( - self, - tree: &ModuleTree, - db: &impl HirDatabase, - ) -> ast::ModuleNode { - let owner = self.owner(tree); - match owner.source(tree).resolve(db) { - ModuleSourceNode::SourceFile(root) => { - let ast = imp::modules(root.borrowed()) - .find(|(name, _)| name == &tree.links[self].name) - .unwrap() - .1; - ast.owned() - } - ModuleSourceNode::Module(it) => it, - } - } -} - -#[derive(Debug, PartialEq, Eq, Hash)] -pub struct ModuleData { - source: ModuleSource, - parent: Option, - children: Vec, -} - -impl ModuleSource { - // precondition: item_id **must** point to module - fn new(file_id: HirFileId, item_id: Option) -> ModuleSource { - let source_item_id = SourceItemId { file_id, item_id }; - ModuleSource(source_item_id) - } - - pub(crate) fn new_file(file_id: HirFileId) -> ModuleSource { - ModuleSource::new(file_id, None) - } - - pub(crate) fn new_inline( - db: &impl HirDatabase, - file_id: HirFileId, - m: ast::Module, - ) -> ModuleSource { - assert!(!m.has_semi()); - let file_items = db.file_items(file_id); - let item_id = file_items.id_of(file_id, m.syntax()); - ModuleSource::new(file_id, Some(item_id)) - } - - pub(crate) fn file_id(self) -> HirFileId { - self.0.file_id - } - - pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { - let syntax_node = db.file_item(self.0); - let syntax_node = syntax_node.borrowed(); - if let Some(file) = ast::SourceFile::cast(syntax_node) { - return ModuleSourceNode::SourceFile(file.owned()); - } - let module = ast::Module::cast(syntax_node).unwrap(); - ModuleSourceNode::Module(module.owned()) - } -} - -#[derive(Hash, Debug, PartialEq, Eq)] -struct LinkData { - owner: ModuleId, - name: Name, - points_to: Vec, - problem: Option, -} - -impl ModuleTree { - fn push_mod(&mut self, data: ModuleData) -> ModuleId { - self.mods.alloc(data) - } - fn push_link(&mut self, data: LinkData) -> LinkId { - let owner = data.owner; - let id = self.links.alloc(data); - self.mods[owner].children.push(id); - id - } -} diff --git a/crates/ra_hir/src/module/imp.rs b/crates/ra_hir/src/module/imp.rs deleted file mode 100644 index 3849026db..000000000 --- a/crates/ra_hir/src/module/imp.rs +++ /dev/null @@ -1,190 +0,0 @@ -use std::sync::Arc; - -use ra_syntax::ast::{self, NameOwner}; -use relative_path::RelativePathBuf; -use rustc_hash::{FxHashMap, FxHashSet}; -use arrayvec::ArrayVec; -use ra_db::{SourceRoot, SourceRootId, Cancelable, FileId}; - -use crate::{ - HirDatabase, Name, AsName, -}; - -use super::{ - LinkData, LinkId, ModuleData, ModuleId, ModuleSource, - ModuleTree, Problem, -}; - -#[derive(Clone, Hash, PartialEq, Eq, Debug)] -pub enum Submodule { - Declaration(Name), - Definition(Name, ModuleSource), -} - -impl Submodule { - fn name(&self) -> &Name { - match self { - Submodule::Declaration(name) => name, - Submodule::Definition(name, _) => name, - } - } -} - -pub(crate) fn modules<'a>( - root: impl ast::ModuleItemOwner<'a>, -) -> impl Iterator)> { - root.items() - .filter_map(|item| match item { - ast::ModuleItem::Module(m) => Some(m), - _ => None, - }) - .filter_map(|module| { - let name = module.name()?.as_name(); - Some((name, module)) - }) -} - -pub(crate) fn module_tree( - db: &impl HirDatabase, - source_root: SourceRootId, -) -> Cancelable> { - db.check_canceled()?; - let res = create_module_tree(db, source_root)?; - Ok(Arc::new(res)) -} - -fn create_module_tree<'a>( - db: &impl HirDatabase, - source_root: SourceRootId, -) -> Cancelable { - let mut tree = ModuleTree::default(); - - let mut roots = FxHashMap::default(); - let mut visited = FxHashSet::default(); - - let source_root = db.source_root(source_root); - for &file_id in source_root.files.values() { - let source = ModuleSource::new_file(file_id.into()); - if visited.contains(&source) { - continue; // TODO: use explicit crate_roots here - } - assert!(!roots.contains_key(&file_id)); - let module_id = build_subtree( - db, - &source_root, - &mut tree, - &mut visited, - &mut roots, - None, - source, - )?; - roots.insert(file_id, module_id); - } - Ok(tree) -} - -fn build_subtree( - db: &impl HirDatabase, - source_root: &SourceRoot, - tree: &mut ModuleTree, - visited: &mut FxHashSet, - roots: &mut FxHashMap, - parent: Option, - source: ModuleSource, -) -> Cancelable { - visited.insert(source); - let id = tree.push_mod(ModuleData { - source, - parent, - children: Vec::new(), - }); - for sub in db.submodules(source)?.iter() { - let link = tree.push_link(LinkData { - name: sub.name().clone(), - owner: id, - points_to: Vec::new(), - problem: None, - }); - - let (points_to, problem) = match sub { - Submodule::Declaration(name) => { - let (points_to, problem) = resolve_submodule(db, source, &name); - let points_to = points_to - .into_iter() - .map(|file_id| match roots.remove(&file_id) { - Some(module_id) => { - tree.mods[module_id].parent = Some(link); - Ok(module_id) - } - None => build_subtree( - db, - source_root, - tree, - visited, - roots, - Some(link), - ModuleSource::new_file(file_id.into()), - ), - }) - .collect::>>()?; - (points_to, problem) - } - Submodule::Definition(_name, submodule_source) => { - let points_to = build_subtree( - db, - source_root, - tree, - visited, - roots, - Some(link), - *submodule_source, - )?; - (vec![points_to], None) - } - }; - - tree.links[link].points_to = points_to; - tree.links[link].problem = problem; - } - Ok(id) -} - -fn resolve_submodule( - db: &impl HirDatabase, - source: ModuleSource, - name: &Name, -) -> (Vec, Option) { - // FIXME: handle submodules of inline modules properly - let file_id = source.file_id().original_file(db); - let source_root_id = db.file_source_root(file_id); - let path = db.file_relative_path(file_id); - let root = RelativePathBuf::default(); - let dir_path = path.parent().unwrap_or(&root); - let mod_name = path.file_stem().unwrap_or("unknown"); - let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main"; - - let file_mod = dir_path.join(format!("{}.rs", name)); - let dir_mod = dir_path.join(format!("{}/mod.rs", name)); - let file_dir_mod = dir_path.join(format!("{}/{}.rs", mod_name, name)); - let mut candidates = ArrayVec::<[_; 2]>::new(); - if is_dir_owner { - candidates.push(file_mod.clone()); - candidates.push(dir_mod); - } else { - candidates.push(file_dir_mod.clone()); - }; - let sr = db.source_root(source_root_id); - let points_to = candidates - .into_iter() - .filter_map(|path| sr.files.get(&path)) - .map(|&it| it) - .collect::>(); - let problem = if points_to.is_empty() { - Some(Problem::UnresolvedModule { - candidate: if is_dir_owner { file_mod } else { file_dir_mod }, - }) - } else { - None - }; - (points_to, problem) -} diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs deleted file mode 100644 index 7d5e86c89..000000000 --- a/crates/ra_hir/src/module/nameres.rs +++ /dev/null @@ -1,509 +0,0 @@ -//! Name resolution algorithm. The end result of the algorithm is `ItemMap`: a -//! map with maps each module to it's scope: the set of items, visible in the -//! module. That is, we only resolve imports here, name resolution of item -//! bodies will be done in a separate step. -//! -//! Like Rustc, we use an interative per-crate algorithm: we start with scopes -//! containing only directly defined items, and then iteratively resolve -//! imports. -//! -//! To make this work nicely in the IDE scenarios, we place `InputModuleItems` -//! in between raw syntax and name resolution. `InputModuleItems` are computed -//! using only the module's syntax, and it is all directly defined items plus -//! imports. The plain is to make `InputModuleItems` independent of local -//! modifications (that is, typing inside a function shold not change IMIs), -//! such that the results of name resolution can be preserved unless the module -//! structure itself is modified. -use std::sync::Arc; - -use rustc_hash::FxHashMap; -use ra_syntax::{ - TextRange, - SyntaxKind::{self, *}, - ast::{self, AstNode} -}; -use ra_db::{SourceRootId, Cancelable, FileId}; - -use crate::{ - HirFileId, - DefId, DefLoc, DefKind, - SourceItemId, SourceFileItemId, SourceFileItems, - Path, PathKind, - HirDatabase, Crate, - Name, AsName, - module::{ModuleId, ModuleTree}, -}; - -/// Item map is the result of the name resolution. Item map contains, for each -/// module, the set of visible items. -// FIXME: currenty we compute item map per source-root. We should do it per crate instead. -#[derive(Default, Debug, PartialEq, Eq)] -pub struct ItemMap { - pub per_module: FxHashMap, -} - -#[derive(Debug, Default, PartialEq, Eq, Clone)] -pub struct ModuleScope { - items: FxHashMap, -} - -impl ModuleScope { - pub fn entries<'a>(&'a self) -> impl Iterator + 'a { - self.items.iter() - } - pub fn get(&self, name: &Name) -> Option<&Resolution> { - self.items.get(name) - } -} - -/// A set of items and imports declared inside a module, without relation to -/// other modules. -/// -/// This stands in-between raw syntax and name resolution and alow us to avoid -/// recomputing name res: if `InputModuleItems` are the same, we can avoid -/// running name resolution. -#[derive(Debug, Default, PartialEq, Eq)] -pub struct InputModuleItems { - pub(crate) items: Vec, - imports: Vec, -} - -#[derive(Debug, PartialEq, Eq)] -pub(crate) struct ModuleItem { - pub(crate) id: SourceItemId, - pub(crate) name: Name, - kind: SyntaxKind, - vis: Vis, -} - -#[derive(Debug, PartialEq, Eq)] -enum Vis { - // Priv, - Other, -} - -#[derive(Debug, Clone, PartialEq, Eq)] -struct Import { - path: Path, - kind: ImportKind, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct NamedImport { - pub file_item_id: SourceFileItemId, - pub relative_range: TextRange, -} - -impl NamedImport { - // FIXME: this is only here for one use-case in completion. Seems like a - // pretty gross special case. - pub fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange { - let source_item_id = SourceItemId { - file_id: file_id.into(), - item_id: Some(self.file_item_id), - }; - let syntax = db.file_item(source_item_id); - let offset = syntax.borrowed().range().start(); - self.relative_range + offset - } -} - -#[derive(Debug, Clone, PartialEq, Eq)] -enum ImportKind { - Glob, - Named(NamedImport), -} - -/// Resolution is basically `DefId` atm, but it should account for stuff like -/// multiple namespaces, ambiguity and errors. -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct Resolution { - /// None for unresolved - pub def_id: PerNs, - /// ident by whitch this is imported into local scope. - pub import: Option, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub enum Namespace { - Types, - Values, -} - -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub struct PerNs { - pub types: Option, - pub values: Option, -} - -impl PerNs { - pub fn none() -> PerNs { - PerNs { - types: None, - values: None, - } - } - - pub fn values(t: T) -> PerNs { - PerNs { - types: None, - values: Some(t), - } - } - - pub fn types(t: T) -> PerNs { - PerNs { - types: Some(t), - values: None, - } - } - - pub fn both(types: T, values: T) -> PerNs { - PerNs { - types: Some(types), - values: Some(values), - } - } - - pub fn is_none(&self) -> bool { - self.types.is_none() && self.values.is_none() - } - - pub fn take(self, namespace: Namespace) -> Option { - match namespace { - Namespace::Types => self.types, - Namespace::Values => self.values, - } - } - - pub fn take_types(self) -> Option { - self.take(Namespace::Types) - } - - pub fn take_values(self) -> Option { - self.take(Namespace::Values) - } - - pub fn get(&self, namespace: Namespace) -> Option<&T> { - self.as_ref().take(namespace) - } - - pub fn as_ref(&self) -> PerNs<&T> { - PerNs { - types: self.types.as_ref(), - values: self.values.as_ref(), - } - } - - pub fn and_then(self, f: impl Fn(T) -> Option) -> PerNs { - PerNs { - types: self.types.and_then(&f), - values: self.values.and_then(&f), - } - } - - pub fn map(self, f: impl Fn(T) -> U) -> PerNs { - PerNs { - types: self.types.map(&f), - values: self.values.map(&f), - } - } -} - -impl InputModuleItems { - pub(crate) fn add_item( - &mut self, - file_id: HirFileId, - file_items: &SourceFileItems, - item: ast::ModuleItem, - ) -> Option<()> { - match item { - ast::ModuleItem::StructDef(it) => { - self.items.push(ModuleItem::new(file_id, file_items, it)?) - } - ast::ModuleItem::EnumDef(it) => { - self.items.push(ModuleItem::new(file_id, file_items, it)?) - } - ast::ModuleItem::FnDef(it) => { - self.items.push(ModuleItem::new(file_id, file_items, it)?) - } - ast::ModuleItem::TraitDef(it) => { - self.items.push(ModuleItem::new(file_id, file_items, it)?) - } - ast::ModuleItem::TypeDef(it) => { - self.items.push(ModuleItem::new(file_id, file_items, it)?) - } - ast::ModuleItem::ImplBlock(_) => { - // impls don't define items - } - ast::ModuleItem::UseItem(it) => self.add_use_item(file_items, it), - ast::ModuleItem::ExternCrateItem(_) => { - // TODO - } - ast::ModuleItem::ConstDef(it) => { - self.items.push(ModuleItem::new(file_id, file_items, it)?) - } - ast::ModuleItem::StaticDef(it) => { - self.items.push(ModuleItem::new(file_id, file_items, it)?) - } - ast::ModuleItem::Module(it) => { - self.items.push(ModuleItem::new(file_id, file_items, it)?) - } - } - Some(()) - } - - fn add_use_item(&mut self, file_items: &SourceFileItems, item: ast::UseItem) { - let file_item_id = file_items.id_of_unchecked(item.syntax()); - let start_offset = item.syntax().range().start(); - Path::expand_use_item(item, |path, range| { - let kind = match range { - None => ImportKind::Glob, - Some(range) => ImportKind::Named(NamedImport { - file_item_id, - relative_range: range - start_offset, - }), - }; - self.imports.push(Import { kind, path }) - }) - } -} - -impl ModuleItem { - fn new<'a>( - file_id: HirFileId, - file_items: &SourceFileItems, - item: impl ast::NameOwner<'a>, - ) -> Option { - let name = item.name()?.as_name(); - let kind = item.syntax().kind(); - let vis = Vis::Other; - let item_id = Some(file_items.id_of_unchecked(item.syntax())); - let id = SourceItemId { file_id, item_id }; - let res = ModuleItem { - id, - name, - kind, - vis, - }; - Some(res) - } -} - -pub(crate) struct Resolver<'a, DB> { - db: &'a DB, - input: &'a FxHashMap>, - source_root: SourceRootId, - module_tree: Arc, - result: ItemMap, -} - -impl<'a, DB> Resolver<'a, DB> -where - DB: HirDatabase, -{ - pub(crate) fn new( - db: &'a DB, - input: &'a FxHashMap>, - source_root: SourceRootId, - module_tree: Arc, - ) -> Resolver<'a, DB> { - Resolver { - db, - input, - source_root, - module_tree, - result: ItemMap::default(), - } - } - - pub(crate) fn resolve(mut self) -> Cancelable { - for (&module_id, items) in self.input.iter() { - self.populate_module(module_id, Arc::clone(items))?; - } - - for &module_id in self.input.keys() { - self.db.check_canceled()?; - self.resolve_imports(module_id)?; - } - Ok(self.result) - } - - fn populate_module( - &mut self, - module_id: ModuleId, - input: Arc, - ) -> Cancelable<()> { - let mut module_items = ModuleScope::default(); - - // Populate extern crates prelude - { - let root_id = module_id.crate_root(&self.module_tree); - let file_id = root_id.source(&self.module_tree).file_id(); - let crate_graph = self.db.crate_graph(); - if let Some(crate_id) = crate_graph.crate_id_for_crate_root(file_id.as_original_file()) - { - let krate = Crate::new(crate_id); - for dep in krate.dependencies(self.db)? { - if let Some(module) = dep.krate.root_module(self.db)? { - let def_id = module.def_id; - self.add_module_item( - &mut module_items, - dep.name.clone(), - PerNs::types(def_id), - ); - } - } - }; - } - for import in input.imports.iter() { - if let Some(name) = import.path.segments.iter().last() { - if let ImportKind::Named(import) = import.kind { - module_items.items.insert( - name.clone(), - Resolution { - def_id: PerNs::none(), - import: Some(import), - }, - ); - } - } - } - // Populate explicitly declared items, except modules - for item in input.items.iter() { - if item.kind == MODULE { - continue; - } - // depending on the item kind, the location can define something in - // the values namespace, the types namespace, or both - let kind = DefKind::for_syntax_kind(item.kind); - let def_id = kind.map(|k| { - let def_loc = DefLoc { - kind: k, - source_root_id: self.source_root, - module_id, - source_item_id: item.id, - }; - def_loc.id(self.db) - }); - let resolution = Resolution { - def_id, - import: None, - }; - module_items.items.insert(item.name.clone(), resolution); - } - - // Populate modules - for (name, module_id) in module_id.children(&self.module_tree) { - let def_loc = DefLoc { - kind: DefKind::Module, - source_root_id: self.source_root, - module_id, - source_item_id: module_id.source(&self.module_tree).0, - }; - let def_id = def_loc.id(self.db); - self.add_module_item(&mut module_items, name, PerNs::types(def_id)); - } - - self.result.per_module.insert(module_id, module_items); - Ok(()) - } - - fn add_module_item(&self, module_items: &mut ModuleScope, name: Name, def_id: PerNs) { - let resolution = Resolution { - def_id, - import: None, - }; - module_items.items.insert(name, resolution); - } - - fn resolve_imports(&mut self, module_id: ModuleId) -> Cancelable<()> { - for import in self.input[&module_id].imports.iter() { - self.resolve_import(module_id, import)?; - } - Ok(()) - } - - fn resolve_import(&mut self, module_id: ModuleId, import: &Import) -> Cancelable<()> { - let ptr = match import.kind { - ImportKind::Glob => return Ok(()), - ImportKind::Named(ptr) => ptr, - }; - - let mut curr: ModuleId = match import.path.kind { - PathKind::Plain | PathKind::Self_ => module_id, - PathKind::Super => { - match module_id.parent(&self.module_tree) { - Some(it) => it, - // TODO: error - None => return Ok(()), - } - } - PathKind::Crate => module_id.crate_root(&self.module_tree), - }; - - for (i, name) in import.path.segments.iter().enumerate() { - let is_last = i == import.path.segments.len() - 1; - - let def_id = match self.result.per_module[&curr].items.get(name) { - Some(res) if !res.def_id.is_none() => res.def_id, - _ => return Ok(()), - }; - - if !is_last { - let type_def_id = if let Some(d) = def_id.take(Namespace::Types) { - d - } else { - return Ok(()); - }; - curr = match type_def_id.loc(self.db) { - DefLoc { - kind: DefKind::Module, - module_id: target_module_id, - source_root_id, - .. - } => { - if source_root_id == self.source_root { - target_module_id - } else { - let module = crate::code_model_api::Module::new(type_def_id); - let path = Path { - segments: import.path.segments[i + 1..].iter().cloned().collect(), - kind: PathKind::Crate, - }; - let def_id = module.resolve_path(self.db, &path)?; - if !def_id.is_none() { - self.update(module_id, |items| { - let res = Resolution { - def_id: def_id, - import: Some(ptr), - }; - items.items.insert(name.clone(), res); - }) - } - return Ok(()); - } - } - _ => return Ok(()), - } - } else { - self.update(module_id, |items| { - let res = Resolution { - def_id: def_id, - import: Some(ptr), - }; - items.items.insert(name.clone(), res); - }) - } - } - Ok(()) - } - - fn update(&mut self, module_id: ModuleId, f: impl FnOnce(&mut ModuleScope)) { - let module_items = self.result.per_module.get_mut(&module_id).unwrap(); - f(module_items) - } -} - -#[cfg(test)] -mod tests; diff --git a/crates/ra_hir/src/module/nameres/tests.rs b/crates/ra_hir/src/module/nameres/tests.rs deleted file mode 100644 index dcbe65aec..000000000 --- a/crates/ra_hir/src/module/nameres/tests.rs +++ /dev/null @@ -1,273 +0,0 @@ -use std::sync::Arc; - -use salsa::Database; -use ra_db::{FilesDatabase, CrateGraph}; -use relative_path::RelativePath; -use test_utils::assert_eq_text; - -use crate::{ - self as hir, - db::HirDatabase, - mock::MockDatabase, -}; - -fn item_map(fixture: &str) -> (Arc, hir::ModuleId) { - let (db, pos) = MockDatabase::with_position(fixture); - let source_root = db.file_source_root(pos.file_id); - let module = hir::source_binder::module_from_position(&db, pos) - .unwrap() - .unwrap(); - let module_id = module.def_id.loc(&db).module_id; - (db.item_map(source_root).unwrap(), module_id) -} - -fn check_module_item_map(map: &hir::ItemMap, module_id: hir::ModuleId, expected: &str) { - let mut lines = map.per_module[&module_id] - .items - .iter() - .map(|(name, res)| format!("{}: {}", name, dump_resolution(res))) - .collect::>(); - lines.sort(); - let actual = lines.join("\n"); - let expected = expected - .trim() - .lines() - .map(|it| it.trim()) - .collect::>() - .join("\n"); - assert_eq_text!(&actual, &expected); - - fn dump_resolution(resolution: &hir::Resolution) -> &'static str { - match ( - resolution.def_id.types.is_some(), - resolution.def_id.values.is_some(), - ) { - (true, true) => "t v", - (true, false) => "t", - (false, true) => "v", - (false, false) => "_", - } - } -} - -#[test] -fn item_map_smoke_test() { - let (item_map, module_id) = item_map( - " - //- /lib.rs - mod foo; - - use crate::foo::bar::Baz; - <|> - - //- /foo/mod.rs - pub mod bar; - - //- /foo/bar.rs - pub struct Baz; - ", - ); - check_module_item_map( - &item_map, - module_id, - " - Baz: t v - foo: t - ", - ); -} - -#[test] -fn item_map_contains_items_from_expansions() { - let (item_map, module_id) = item_map( - " - //- /lib.rs - mod foo; - - use crate::foo::bar::Baz; - <|> - - //- /foo/mod.rs - pub mod bar; - - //- /foo/bar.rs - salsa::query_group! { - trait Baz {} - } - ", - ); - check_module_item_map( - &item_map, - module_id, - " - Baz: t - foo: t - ", - ); -} - -#[test] -fn item_map_using_self() { - let (item_map, module_id) = item_map( - " - //- /lib.rs - mod foo; - use crate::foo::bar::Baz::{self}; - <|> - //- /foo/mod.rs - pub mod bar; - //- /foo/bar.rs - pub struct Baz; - ", - ); - check_module_item_map( - &item_map, - module_id, - " - Baz: t v - foo: t - ", - ); -} - -#[test] -fn item_map_across_crates() { - let (mut db, sr) = MockDatabase::with_files( - " - //- /main.rs - use test_crate::Baz; - - //- /lib.rs - pub struct Baz; - ", - ); - let main_id = sr.files[RelativePath::new("/main.rs")]; - let lib_id = sr.files[RelativePath::new("/lib.rs")]; - - let mut crate_graph = CrateGraph::default(); - let main_crate = crate_graph.add_crate_root(main_id); - let lib_crate = crate_graph.add_crate_root(lib_id); - crate_graph.add_dep(main_crate, "test_crate".into(), lib_crate); - - db.set_crate_graph(crate_graph); - - let source_root = db.file_source_root(main_id); - let module = hir::source_binder::module_from_file_id(&db, main_id) - .unwrap() - .unwrap(); - let module_id = module.def_id.loc(&db).module_id; - let item_map = db.item_map(source_root).unwrap(); - - check_module_item_map( - &item_map, - module_id, - " - Baz: t v - test_crate: t - ", - ); -} - -#[test] -fn typing_inside_a_function_should_not_invalidate_item_map() { - let (mut db, pos) = MockDatabase::with_position( - " - //- /lib.rs - mod foo; - - use crate::foo::bar::Baz; - - //- /foo/mod.rs - pub mod bar; - - //- /foo/bar.rs - <|> - salsa::query_group! { - trait Baz { - fn foo() -> i32 { 1 + 1 } - } - } - ", - ); - let source_root = db.file_source_root(pos.file_id); - { - let events = db.log_executed(|| { - db.item_map(source_root).unwrap(); - }); - assert!(format!("{:?}", events).contains("item_map")) - } - - let new_text = " - salsa::query_group! { - trait Baz { - fn foo() -> i32 { 92 } - } - } - " - .to_string(); - - db.query_mut(ra_db::FileTextQuery) - .set(pos.file_id, Arc::new(new_text)); - - { - let events = db.log_executed(|| { - db.item_map(source_root).unwrap(); - }); - assert!( - !format!("{:?}", events).contains("item_map"), - "{:#?}", - events - ) - } -} - -#[test] -fn typing_inside_a_function_inside_a_macro_should_not_invalidate_item_map() { - let (mut db, pos) = MockDatabase::with_position( - " - //- /lib.rs - mod foo;<|> - - use crate::foo::bar::Baz; - - fn foo() -> i32 { - 1 + 1 - } - //- /foo/mod.rs - pub mod bar; - - //- /foo/bar.rs - pub struct Baz; - ", - ); - let source_root = db.file_source_root(pos.file_id); - { - let events = db.log_executed(|| { - db.item_map(source_root).unwrap(); - }); - assert!(format!("{:?}", events).contains("item_map")) - } - - let new_text = " - mod foo; - - use crate::foo::bar::Baz; - - fn foo() -> i32 { 92 } - " - .to_string(); - - db.query_mut(ra_db::FileTextQuery) - .set(pos.file_id, Arc::new(new_text)); - - { - let events = db.log_executed(|| { - db.item_map(source_root).unwrap(); - }); - assert!( - !format!("{:?}", events).contains("item_map"), - "{:#?}", - events - ) - } -} diff --git a/crates/ra_hir/src/module_tree.rs b/crates/ra_hir/src/module_tree.rs new file mode 100644 index 000000000..cd82f25db --- /dev/null +++ b/crates/ra_hir/src/module_tree.rs @@ -0,0 +1,375 @@ +use std::sync::Arc; + +use rustc_hash::{FxHashMap, FxHashSet}; +use arrayvec::ArrayVec; +use relative_path::RelativePathBuf; +use ra_db::{FileId, SourceRootId, Cancelable, SourceRoot}; +use ra_syntax::{ + algo::generate, + ast::{self, AstNode, NameOwner}, + SyntaxNode, +}; +use ra_arena::{Arena, RawId, impl_arena_id}; + +use crate::{Name, AsName, HirDatabase, SourceItemId, SourceFileItemId, HirFileId, Problem}; + +#[derive(Clone, Hash, PartialEq, Eq, Debug)] +pub enum Submodule { + Declaration(Name), + Definition(Name, ModuleSource), +} + +impl Submodule { + fn name(&self) -> &Name { + match self { + Submodule::Declaration(name) => name, + Submodule::Definition(name, _) => name, + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct ModuleId(RawId); +impl_arena_id!(ModuleId); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct LinkId(RawId); +impl_arena_id!(LinkId); + +/// Physically, rust source is organized as a set of files, but logically it is +/// organized as a tree of modules. Usually, a single file corresponds to a +/// single module, but it is not nessary the case. +/// +/// Module encapsulate the logic of transitioning from the fuzzy world of files +/// (which can have multiple parents) to the precise world of modules (which +/// always have one parent). +#[derive(Default, Debug, PartialEq, Eq)] +pub struct ModuleTree { + mods: Arena, + links: Arena, +} + +#[derive(Debug, PartialEq, Eq, Hash)] +pub struct ModuleData { + source: ModuleSource, + parent: Option, + children: Vec, +} + +#[derive(Hash, Debug, PartialEq, Eq)] +struct LinkData { + owner: ModuleId, + name: Name, + points_to: Vec, + problem: Option, +} + +impl ModuleTree { + pub(crate) fn module_tree_query( + db: &impl HirDatabase, + source_root: SourceRootId, + ) -> Cancelable> { + db.check_canceled()?; + let res = create_module_tree(db, source_root)?; + Ok(Arc::new(res)) + } + + pub(crate) fn modules<'a>(&'a self) -> impl Iterator + 'a { + self.mods.iter().map(|(id, _)| id) + } + + pub(crate) fn modules_with_sources<'a>( + &'a self, + ) -> impl Iterator + 'a { + self.mods.iter().map(|(id, m)| (id, m.source)) + } +} + +/// `ModuleSource` is the syntax tree element that produced this module: +/// either a file, or an inlinde module. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub struct ModuleSource(pub(crate) SourceItemId); + +/// An owned syntax node for a module. Unlike `ModuleSource`, +/// this holds onto the AST for the whole file. +pub(crate) enum ModuleSourceNode { + SourceFile(ast::SourceFileNode), + Module(ast::ModuleNode), +} + +impl ModuleId { + pub(crate) fn source(self, tree: &ModuleTree) -> ModuleSource { + tree.mods[self].source + } + pub(crate) fn parent_link(self, tree: &ModuleTree) -> Option { + tree.mods[self].parent + } + pub(crate) fn parent(self, tree: &ModuleTree) -> Option { + let link = self.parent_link(tree)?; + Some(tree.links[link].owner) + } + pub(crate) fn crate_root(self, tree: &ModuleTree) -> ModuleId { + generate(Some(self), move |it| it.parent(tree)) + .last() + .unwrap() + } + pub(crate) fn child(self, tree: &ModuleTree, name: &Name) -> Option { + let link = tree.mods[self] + .children + .iter() + .map(|&it| &tree.links[it]) + .find(|it| it.name == *name)?; + Some(*link.points_to.first()?) + } + pub(crate) fn children<'a>( + self, + tree: &'a ModuleTree, + ) -> impl Iterator + 'a { + tree.mods[self].children.iter().filter_map(move |&it| { + let link = &tree.links[it]; + let module = *link.points_to.first()?; + Some((link.name.clone(), module)) + }) + } + pub(crate) fn problems( + self, + tree: &ModuleTree, + db: &impl HirDatabase, + ) -> Vec<(SyntaxNode, Problem)> { + tree.mods[self] + .children + .iter() + .filter_map(|&it| { + let p = tree.links[it].problem.clone()?; + let s = it.bind_source(tree, db); + let s = s.borrowed().name().unwrap().syntax().owned(); + Some((s, p)) + }) + .collect() + } +} + +impl LinkId { + pub(crate) fn owner(self, tree: &ModuleTree) -> ModuleId { + tree.links[self].owner + } + pub(crate) fn name(self, tree: &ModuleTree) -> &Name { + &tree.links[self].name + } + pub(crate) fn bind_source<'a>( + self, + tree: &ModuleTree, + db: &impl HirDatabase, + ) -> ast::ModuleNode { + let owner = self.owner(tree); + match owner.source(tree).resolve(db) { + ModuleSourceNode::SourceFile(root) => { + let ast = modules(root.borrowed()) + .find(|(name, _)| name == &tree.links[self].name) + .unwrap() + .1; + ast.owned() + } + ModuleSourceNode::Module(it) => it, + } + } +} + +impl ModuleSource { + // precondition: item_id **must** point to module + fn new(file_id: HirFileId, item_id: Option) -> ModuleSource { + let source_item_id = SourceItemId { file_id, item_id }; + ModuleSource(source_item_id) + } + + pub(crate) fn new_file(file_id: HirFileId) -> ModuleSource { + ModuleSource::new(file_id, None) + } + + pub(crate) fn new_inline( + db: &impl HirDatabase, + file_id: HirFileId, + m: ast::Module, + ) -> ModuleSource { + assert!(!m.has_semi()); + let file_items = db.file_items(file_id); + let item_id = file_items.id_of(file_id, m.syntax()); + ModuleSource::new(file_id, Some(item_id)) + } + + pub(crate) fn file_id(self) -> HirFileId { + self.0.file_id + } + + pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { + let syntax_node = db.file_item(self.0); + let syntax_node = syntax_node.borrowed(); + if let Some(file) = ast::SourceFile::cast(syntax_node) { + return ModuleSourceNode::SourceFile(file.owned()); + } + let module = ast::Module::cast(syntax_node).unwrap(); + ModuleSourceNode::Module(module.owned()) + } +} + +impl ModuleTree { + fn push_mod(&mut self, data: ModuleData) -> ModuleId { + self.mods.alloc(data) + } + fn push_link(&mut self, data: LinkData) -> LinkId { + let owner = data.owner; + let id = self.links.alloc(data); + self.mods[owner].children.push(id); + id + } +} + +fn modules<'a>( + root: impl ast::ModuleItemOwner<'a>, +) -> impl Iterator)> { + root.items() + .filter_map(|item| match item { + ast::ModuleItem::Module(m) => Some(m), + _ => None, + }) + .filter_map(|module| { + let name = module.name()?.as_name(); + Some((name, module)) + }) +} + +fn create_module_tree<'a>( + db: &impl HirDatabase, + source_root: SourceRootId, +) -> Cancelable { + let mut tree = ModuleTree::default(); + + let mut roots = FxHashMap::default(); + let mut visited = FxHashSet::default(); + + let source_root = db.source_root(source_root); + for &file_id in source_root.files.values() { + let source = ModuleSource::new_file(file_id.into()); + if visited.contains(&source) { + continue; // TODO: use explicit crate_roots here + } + assert!(!roots.contains_key(&file_id)); + let module_id = build_subtree( + db, + &source_root, + &mut tree, + &mut visited, + &mut roots, + None, + source, + )?; + roots.insert(file_id, module_id); + } + Ok(tree) +} + +fn build_subtree( + db: &impl HirDatabase, + source_root: &SourceRoot, + tree: &mut ModuleTree, + visited: &mut FxHashSet, + roots: &mut FxHashMap, + parent: Option, + source: ModuleSource, +) -> Cancelable { + visited.insert(source); + let id = tree.push_mod(ModuleData { + source, + parent, + children: Vec::new(), + }); + for sub in db.submodules(source)?.iter() { + let link = tree.push_link(LinkData { + name: sub.name().clone(), + owner: id, + points_to: Vec::new(), + problem: None, + }); + + let (points_to, problem) = match sub { + Submodule::Declaration(name) => { + let (points_to, problem) = resolve_submodule(db, source, &name); + let points_to = points_to + .into_iter() + .map(|file_id| match roots.remove(&file_id) { + Some(module_id) => { + tree.mods[module_id].parent = Some(link); + Ok(module_id) + } + None => build_subtree( + db, + source_root, + tree, + visited, + roots, + Some(link), + ModuleSource::new_file(file_id.into()), + ), + }) + .collect::>>()?; + (points_to, problem) + } + Submodule::Definition(_name, submodule_source) => { + let points_to = build_subtree( + db, + source_root, + tree, + visited, + roots, + Some(link), + *submodule_source, + )?; + (vec![points_to], None) + } + }; + + tree.links[link].points_to = points_to; + tree.links[link].problem = problem; + } + Ok(id) +} + +fn resolve_submodule( + db: &impl HirDatabase, + source: ModuleSource, + name: &Name, +) -> (Vec, Option) { + // FIXME: handle submodules of inline modules properly + let file_id = source.file_id().original_file(db); + let source_root_id = db.file_source_root(file_id); + let path = db.file_relative_path(file_id); + let root = RelativePathBuf::default(); + let dir_path = path.parent().unwrap_or(&root); + let mod_name = path.file_stem().unwrap_or("unknown"); + let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main"; + + let file_mod = dir_path.join(format!("{}.rs", name)); + let dir_mod = dir_path.join(format!("{}/mod.rs", name)); + let file_dir_mod = dir_path.join(format!("{}/{}.rs", mod_name, name)); + let mut candidates = ArrayVec::<[_; 2]>::new(); + if is_dir_owner { + candidates.push(file_mod.clone()); + candidates.push(dir_mod); + } else { + candidates.push(file_dir_mod.clone()); + }; + let sr = db.source_root(source_root_id); + let points_to = candidates + .into_iter() + .filter_map(|path| sr.files.get(&path)) + .map(|&it| it) + .collect::>(); + let problem = if points_to.is_empty() { + Some(Problem::UnresolvedModule { + candidate: if is_dir_owner { file_mod } else { file_dir_mod }, + }) + } else { + None + }; + (points_to, problem) +} diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs new file mode 100644 index 000000000..e65cbcb27 --- /dev/null +++ b/crates/ra_hir/src/nameres.rs @@ -0,0 +1,509 @@ +//! Name resolution algorithm. The end result of the algorithm is `ItemMap`: a +//! map with maps each module to it's scope: the set of items, visible in the +//! module. That is, we only resolve imports here, name resolution of item +//! bodies will be done in a separate step. +//! +//! Like Rustc, we use an interative per-crate algorithm: we start with scopes +//! containing only directly defined items, and then iteratively resolve +//! imports. +//! +//! To make this work nicely in the IDE scenarios, we place `InputModuleItems` +//! in between raw syntax and name resolution. `InputModuleItems` are computed +//! using only the module's syntax, and it is all directly defined items plus +//! imports. The plain is to make `InputModuleItems` independent of local +//! modifications (that is, typing inside a function shold not change IMIs), +//! such that the results of name resolution can be preserved unless the module +//! structure itself is modified. +use std::sync::Arc; + +use rustc_hash::FxHashMap; +use ra_syntax::{ + TextRange, + SyntaxKind::{self, *}, + ast::{self, AstNode} +}; +use ra_db::{SourceRootId, Cancelable, FileId}; + +use crate::{ + HirFileId, + DefId, DefLoc, DefKind, + SourceItemId, SourceFileItemId, SourceFileItems, + Path, PathKind, + HirDatabase, Crate, + Name, AsName, + module_tree::{ModuleId, ModuleTree}, +}; + +/// Item map is the result of the name resolution. Item map contains, for each +/// module, the set of visible items. +// FIXME: currenty we compute item map per source-root. We should do it per crate instead. +#[derive(Default, Debug, PartialEq, Eq)] +pub struct ItemMap { + pub per_module: FxHashMap, +} + +#[derive(Debug, Default, PartialEq, Eq, Clone)] +pub struct ModuleScope { + items: FxHashMap, +} + +impl ModuleScope { + pub fn entries<'a>(&'a self) -> impl Iterator + 'a { + self.items.iter() + } + pub fn get(&self, name: &Name) -> Option<&Resolution> { + self.items.get(name) + } +} + +/// A set of items and imports declared inside a module, without relation to +/// other modules. +/// +/// This stands in-between raw syntax and name resolution and alow us to avoid +/// recomputing name res: if `InputModuleItems` are the same, we can avoid +/// running name resolution. +#[derive(Debug, Default, PartialEq, Eq)] +pub struct InputModuleItems { + pub(crate) items: Vec, + imports: Vec, +} + +#[derive(Debug, PartialEq, Eq)] +pub(crate) struct ModuleItem { + pub(crate) id: SourceItemId, + pub(crate) name: Name, + kind: SyntaxKind, + vis: Vis, +} + +#[derive(Debug, PartialEq, Eq)] +enum Vis { + // Priv, + Other, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct Import { + path: Path, + kind: ImportKind, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct NamedImport { + pub file_item_id: SourceFileItemId, + pub relative_range: TextRange, +} + +impl NamedImport { + // FIXME: this is only here for one use-case in completion. Seems like a + // pretty gross special case. + pub fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange { + let source_item_id = SourceItemId { + file_id: file_id.into(), + item_id: Some(self.file_item_id), + }; + let syntax = db.file_item(source_item_id); + let offset = syntax.borrowed().range().start(); + self.relative_range + offset + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +enum ImportKind { + Glob, + Named(NamedImport), +} + +/// Resolution is basically `DefId` atm, but it should account for stuff like +/// multiple namespaces, ambiguity and errors. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Resolution { + /// None for unresolved + pub def_id: PerNs, + /// ident by whitch this is imported into local scope. + pub import: Option, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum Namespace { + Types, + Values, +} + +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub struct PerNs { + pub types: Option, + pub values: Option, +} + +impl PerNs { + pub fn none() -> PerNs { + PerNs { + types: None, + values: None, + } + } + + pub fn values(t: T) -> PerNs { + PerNs { + types: None, + values: Some(t), + } + } + + pub fn types(t: T) -> PerNs { + PerNs { + types: Some(t), + values: None, + } + } + + pub fn both(types: T, values: T) -> PerNs { + PerNs { + types: Some(types), + values: Some(values), + } + } + + pub fn is_none(&self) -> bool { + self.types.is_none() && self.values.is_none() + } + + pub fn take(self, namespace: Namespace) -> Option { + match namespace { + Namespace::Types => self.types, + Namespace::Values => self.values, + } + } + + pub fn take_types(self) -> Option { + self.take(Namespace::Types) + } + + pub fn take_values(self) -> Option { + self.take(Namespace::Values) + } + + pub fn get(&self, namespace: Namespace) -> Option<&T> { + self.as_ref().take(namespace) + } + + pub fn as_ref(&self) -> PerNs<&T> { + PerNs { + types: self.types.as_ref(), + values: self.values.as_ref(), + } + } + + pub fn and_then(self, f: impl Fn(T) -> Option) -> PerNs { + PerNs { + types: self.types.and_then(&f), + values: self.values.and_then(&f), + } + } + + pub fn map(self, f: impl Fn(T) -> U) -> PerNs { + PerNs { + types: self.types.map(&f), + values: self.values.map(&f), + } + } +} + +impl InputModuleItems { + pub(crate) fn add_item( + &mut self, + file_id: HirFileId, + file_items: &SourceFileItems, + item: ast::ModuleItem, + ) -> Option<()> { + match item { + ast::ModuleItem::StructDef(it) => { + self.items.push(ModuleItem::new(file_id, file_items, it)?) + } + ast::ModuleItem::EnumDef(it) => { + self.items.push(ModuleItem::new(file_id, file_items, it)?) + } + ast::ModuleItem::FnDef(it) => { + self.items.push(ModuleItem::new(file_id, file_items, it)?) + } + ast::ModuleItem::TraitDef(it) => { + self.items.push(ModuleItem::new(file_id, file_items, it)?) + } + ast::ModuleItem::TypeDef(it) => { + self.items.push(ModuleItem::new(file_id, file_items, it)?) + } + ast::ModuleItem::ImplBlock(_) => { + // impls don't define items + } + ast::ModuleItem::UseItem(it) => self.add_use_item(file_items, it), + ast::ModuleItem::ExternCrateItem(_) => { + // TODO + } + ast::ModuleItem::ConstDef(it) => { + self.items.push(ModuleItem::new(file_id, file_items, it)?) + } + ast::ModuleItem::StaticDef(it) => { + self.items.push(ModuleItem::new(file_id, file_items, it)?) + } + ast::ModuleItem::Module(it) => { + self.items.push(ModuleItem::new(file_id, file_items, it)?) + } + } + Some(()) + } + + fn add_use_item(&mut self, file_items: &SourceFileItems, item: ast::UseItem) { + let file_item_id = file_items.id_of_unchecked(item.syntax()); + let start_offset = item.syntax().range().start(); + Path::expand_use_item(item, |path, range| { + let kind = match range { + None => ImportKind::Glob, + Some(range) => ImportKind::Named(NamedImport { + file_item_id, + relative_range: range - start_offset, + }), + }; + self.imports.push(Import { kind, path }) + }) + } +} + +impl ModuleItem { + fn new<'a>( + file_id: HirFileId, + file_items: &SourceFileItems, + item: impl ast::NameOwner<'a>, + ) -> Option { + let name = item.name()?.as_name(); + let kind = item.syntax().kind(); + let vis = Vis::Other; + let item_id = Some(file_items.id_of_unchecked(item.syntax())); + let id = SourceItemId { file_id, item_id }; + let res = ModuleItem { + id, + name, + kind, + vis, + }; + Some(res) + } +} + +pub(crate) struct Resolver<'a, DB> { + db: &'a DB, + input: &'a FxHashMap>, + source_root: SourceRootId, + module_tree: Arc, + result: ItemMap, +} + +impl<'a, DB> Resolver<'a, DB> +where + DB: HirDatabase, +{ + pub(crate) fn new( + db: &'a DB, + input: &'a FxHashMap>, + source_root: SourceRootId, + module_tree: Arc, + ) -> Resolver<'a, DB> { + Resolver { + db, + input, + source_root, + module_tree, + result: ItemMap::default(), + } + } + + pub(crate) fn resolve(mut self) -> Cancelable { + for (&module_id, items) in self.input.iter() { + self.populate_module(module_id, Arc::clone(items))?; + } + + for &module_id in self.input.keys() { + self.db.check_canceled()?; + self.resolve_imports(module_id)?; + } + Ok(self.result) + } + + fn populate_module( + &mut self, + module_id: ModuleId, + input: Arc, + ) -> Cancelable<()> { + let mut module_items = ModuleScope::default(); + + // Populate extern crates prelude + { + let root_id = module_id.crate_root(&self.module_tree); + let file_id = root_id.source(&self.module_tree).file_id(); + let crate_graph = self.db.crate_graph(); + if let Some(crate_id) = crate_graph.crate_id_for_crate_root(file_id.as_original_file()) + { + let krate = Crate::new(crate_id); + for dep in krate.dependencies(self.db)? { + if let Some(module) = dep.krate.root_module(self.db)? { + let def_id = module.def_id; + self.add_module_item( + &mut module_items, + dep.name.clone(), + PerNs::types(def_id), + ); + } + } + }; + } + for import in input.imports.iter() { + if let Some(name) = import.path.segments.iter().last() { + if let ImportKind::Named(import) = import.kind { + module_items.items.insert( + name.clone(), + Resolution { + def_id: PerNs::none(), + import: Some(import), + }, + ); + } + } + } + // Populate explicitly declared items, except modules + for item in input.items.iter() { + if item.kind == MODULE { + continue; + } + // depending on the item kind, the location can define something in + // the values namespace, the types namespace, or both + let kind = DefKind::for_syntax_kind(item.kind); + let def_id = kind.map(|k| { + let def_loc = DefLoc { + kind: k, + source_root_id: self.source_root, + module_id, + source_item_id: item.id, + }; + def_loc.id(self.db) + }); + let resolution = Resolution { + def_id, + import: None, + }; + module_items.items.insert(item.name.clone(), resolution); + } + + // Populate modules + for (name, module_id) in module_id.children(&self.module_tree) { + let def_loc = DefLoc { + kind: DefKind::Module, + source_root_id: self.source_root, + module_id, + source_item_id: module_id.source(&self.module_tree).0, + }; + let def_id = def_loc.id(self.db); + self.add_module_item(&mut module_items, name, PerNs::types(def_id)); + } + + self.result.per_module.insert(module_id, module_items); + Ok(()) + } + + fn add_module_item(&self, module_items: &mut ModuleScope, name: Name, def_id: PerNs) { + let resolution = Resolution { + def_id, + import: None, + }; + module_items.items.insert(name, resolution); + } + + fn resolve_imports(&mut self, module_id: ModuleId) -> Cancelable<()> { + for import in self.input[&module_id].imports.iter() { + self.resolve_import(module_id, import)?; + } + Ok(()) + } + + fn resolve_import(&mut self, module_id: ModuleId, import: &Import) -> Cancelable<()> { + let ptr = match import.kind { + ImportKind::Glob => return Ok(()), + ImportKind::Named(ptr) => ptr, + }; + + let mut curr: ModuleId = match import.path.kind { + PathKind::Plain | PathKind::Self_ => module_id, + PathKind::Super => { + match module_id.parent(&self.module_tree) { + Some(it) => it, + // TODO: error + None => return Ok(()), + } + } + PathKind::Crate => module_id.crate_root(&self.module_tree), + }; + + for (i, name) in import.path.segments.iter().enumerate() { + let is_last = i == import.path.segments.len() - 1; + + let def_id = match self.result.per_module[&curr].items.get(name) { + Some(res) if !res.def_id.is_none() => res.def_id, + _ => return Ok(()), + }; + + if !is_last { + let type_def_id = if let Some(d) = def_id.take(Namespace::Types) { + d + } else { + return Ok(()); + }; + curr = match type_def_id.loc(self.db) { + DefLoc { + kind: DefKind::Module, + module_id: target_module_id, + source_root_id, + .. + } => { + if source_root_id == self.source_root { + target_module_id + } else { + let module = crate::code_model_api::Module::new(type_def_id); + let path = Path { + segments: import.path.segments[i + 1..].iter().cloned().collect(), + kind: PathKind::Crate, + }; + let def_id = module.resolve_path(self.db, &path)?; + if !def_id.is_none() { + self.update(module_id, |items| { + let res = Resolution { + def_id: def_id, + import: Some(ptr), + }; + items.items.insert(name.clone(), res); + }) + } + return Ok(()); + } + } + _ => return Ok(()), + } + } else { + self.update(module_id, |items| { + let res = Resolution { + def_id: def_id, + import: Some(ptr), + }; + items.items.insert(name.clone(), res); + }) + } + } + Ok(()) + } + + fn update(&mut self, module_id: ModuleId, f: impl FnOnce(&mut ModuleScope)) { + let module_items = self.result.per_module.get_mut(&module_id).unwrap(); + f(module_items) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs new file mode 100644 index 000000000..a6a0bea31 --- /dev/null +++ b/crates/ra_hir/src/nameres/tests.rs @@ -0,0 +1,273 @@ +use std::sync::Arc; + +use salsa::Database; +use ra_db::{FilesDatabase, CrateGraph}; +use relative_path::RelativePath; +use test_utils::assert_eq_text; + +use crate::{ + self as hir, + db::HirDatabase, + mock::MockDatabase, +}; + +fn item_map(fixture: &str) -> (Arc, hir::ModuleId) { + let (db, pos) = MockDatabase::with_position(fixture); + let source_root = db.file_source_root(pos.file_id); + let module = hir::source_binder::module_from_position(&db, pos) + .unwrap() + .unwrap(); + let module_id = module.module_id; + (db.item_map(source_root).unwrap(), module_id) +} + +fn check_module_item_map(map: &hir::ItemMap, module_id: hir::ModuleId, expected: &str) { + let mut lines = map.per_module[&module_id] + .items + .iter() + .map(|(name, res)| format!("{}: {}", name, dump_resolution(res))) + .collect::>(); + lines.sort(); + let actual = lines.join("\n"); + let expected = expected + .trim() + .lines() + .map(|it| it.trim()) + .collect::>() + .join("\n"); + assert_eq_text!(&actual, &expected); + + fn dump_resolution(resolution: &hir::Resolution) -> &'static str { + match ( + resolution.def_id.types.is_some(), + resolution.def_id.values.is_some(), + ) { + (true, true) => "t v", + (true, false) => "t", + (false, true) => "v", + (false, false) => "_", + } + } +} + +#[test] +fn item_map_smoke_test() { + let (item_map, module_id) = item_map( + " + //- /lib.rs + mod foo; + + use crate::foo::bar::Baz; + <|> + + //- /foo/mod.rs + pub mod bar; + + //- /foo/bar.rs + pub struct Baz; + ", + ); + check_module_item_map( + &item_map, + module_id, + " + Baz: t v + foo: t + ", + ); +} + +#[test] +fn item_map_contains_items_from_expansions() { + let (item_map, module_id) = item_map( + " + //- /lib.rs + mod foo; + + use crate::foo::bar::Baz; + <|> + + //- /foo/mod.rs + pub mod bar; + + //- /foo/bar.rs + salsa::query_group! { + trait Baz {} + } + ", + ); + check_module_item_map( + &item_map, + module_id, + " + Baz: t + foo: t + ", + ); +} + +#[test] +fn item_map_using_self() { + let (item_map, module_id) = item_map( + " + //- /lib.rs + mod foo; + use crate::foo::bar::Baz::{self}; + <|> + //- /foo/mod.rs + pub mod bar; + //- /foo/bar.rs + pub struct Baz; + ", + ); + check_module_item_map( + &item_map, + module_id, + " + Baz: t v + foo: t + ", + ); +} + +#[test] +fn item_map_across_crates() { + let (mut db, sr) = MockDatabase::with_files( + " + //- /main.rs + use test_crate::Baz; + + //- /lib.rs + pub struct Baz; + ", + ); + let main_id = sr.files[RelativePath::new("/main.rs")]; + let lib_id = sr.files[RelativePath::new("/lib.rs")]; + + let mut crate_graph = CrateGraph::default(); + let main_crate = crate_graph.add_crate_root(main_id); + let lib_crate = crate_graph.add_crate_root(lib_id); + crate_graph.add_dep(main_crate, "test_crate".into(), lib_crate); + + db.set_crate_graph(crate_graph); + + let source_root = db.file_source_root(main_id); + let module = hir::source_binder::module_from_file_id(&db, main_id) + .unwrap() + .unwrap(); + let module_id = module.module_id; + let item_map = db.item_map(source_root).unwrap(); + + check_module_item_map( + &item_map, + module_id, + " + Baz: t v + test_crate: t + ", + ); +} + +#[test] +fn typing_inside_a_function_should_not_invalidate_item_map() { + let (mut db, pos) = MockDatabase::with_position( + " + //- /lib.rs + mod foo; + + use crate::foo::bar::Baz; + + //- /foo/mod.rs + pub mod bar; + + //- /foo/bar.rs + <|> + salsa::query_group! { + trait Baz { + fn foo() -> i32 { 1 + 1 } + } + } + ", + ); + let source_root = db.file_source_root(pos.file_id); + { + let events = db.log_executed(|| { + db.item_map(source_root).unwrap(); + }); + assert!(format!("{:?}", events).contains("item_map")) + } + + let new_text = " + salsa::query_group! { + trait Baz { + fn foo() -> i32 { 92 } + } + } + " + .to_string(); + + db.query_mut(ra_db::FileTextQuery) + .set(pos.file_id, Arc::new(new_text)); + + { + let events = db.log_executed(|| { + db.item_map(source_root).unwrap(); + }); + assert!( + !format!("{:?}", events).contains("item_map"), + "{:#?}", + events + ) + } +} + +#[test] +fn typing_inside_a_function_inside_a_macro_should_not_invalidate_item_map() { + let (mut db, pos) = MockDatabase::with_position( + " + //- /lib.rs + mod foo;<|> + + use crate::foo::bar::Baz; + + fn foo() -> i32 { + 1 + 1 + } + //- /foo/mod.rs + pub mod bar; + + //- /foo/bar.rs + pub struct Baz; + ", + ); + let source_root = db.file_source_root(pos.file_id); + { + let events = db.log_executed(|| { + db.item_map(source_root).unwrap(); + }); + assert!(format!("{:?}", events).contains("item_map")) + } + + let new_text = " + mod foo; + + use crate::foo::bar::Baz; + + fn foo() -> i32 { 92 } + " + .to_string(); + + db.query_mut(ra_db::FileTextQuery) + .set(pos.file_id, Arc::new(new_text)); + + { + let events = db.log_executed(|| { + db.item_map(source_root).unwrap(); + }); + assert!( + !format!("{:?}", events).contains("item_map"), + "{:#?}", + events + ) + } +} diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs index d9ee9d37f..b17c00e26 100644 --- a/crates/ra_hir/src/query_definitions.rs +++ b/crates/ra_hir/src/query_definitions.rs @@ -15,11 +15,8 @@ use crate::{ MacroCallLoc, db::HirDatabase, function::FnScopes, - module::{ - ModuleSource, ModuleSourceNode, ModuleId, - imp::Submodule, - nameres::{InputModuleItems, ItemMap, Resolver}, - }, + module_tree::{ModuleId, Submodule, ModuleSource, ModuleSourceNode}, + nameres::{InputModuleItems, ItemMap, Resolver}, adt::{StructData, EnumData}, }; diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index ac097e81a..b7e3ff9b0 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -14,7 +14,7 @@ use ra_syntax::{ use crate::{ HirDatabase, Function, SourceItemId, - module::ModuleSource, + module_tree::ModuleSource, DefKind, DefLoc, AsName, }; -- cgit v1.2.3