remove Cancelable from module_tree_query

This commit is contained in:
Aleksey Kladov 2019-01-15 17:55:15 +03:00
parent 443ff27724
commit 68ff52566d
6 changed files with 28 additions and 31 deletions

View file

@ -25,7 +25,7 @@ impl Crate {
let file_id = crate_graph.crate_root(self.crate_id); let file_id = crate_graph.crate_root(self.crate_id);
let source_root_id = db.file_source_root(file_id); let source_root_id = db.file_source_root(file_id);
let file_id = HirFileId::from(file_id); let file_id = HirFileId::from(file_id);
let module_tree = db.module_tree(source_root_id)?; let module_tree = db.module_tree(source_root_id);
// FIXME: teach module tree about crate roots instead of guessing // FIXME: teach module tree about crate roots instead of guessing
let source = SourceItemId { let source = SourceItemId {
file_id, file_id,

View file

@ -19,7 +19,7 @@ impl Module {
source_root_id: SourceRootId, source_root_id: SourceRootId,
module_id: ModuleId, module_id: ModuleId,
) -> Cancelable<Self> { ) -> Cancelable<Self> {
let module_tree = db.module_tree(source_root_id)?; let module_tree = db.module_tree(source_root_id);
let def_loc = DefLoc { let def_loc = DefLoc {
kind: DefKind::Module, kind: DefKind::Module,
source_root_id, source_root_id,
@ -33,7 +33,7 @@ impl Module {
pub(crate) fn name_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> { pub(crate) fn name_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
let loc = self.def_id.loc(db); let loc = self.def_id.loc(db);
let module_tree = db.module_tree(loc.source_root_id)?; let module_tree = db.module_tree(loc.source_root_id);
let link = ctry!(loc.module_id.parent_link(&module_tree)); let link = ctry!(loc.module_id.parent_link(&module_tree));
Ok(Some(link.name(&module_tree).clone())) Ok(Some(link.name(&module_tree).clone()))
} }
@ -59,7 +59,7 @@ impl Module {
db: &impl HirDatabase, db: &impl HirDatabase,
) -> Cancelable<Option<(FileId, TreeArc<ast::Module>)>> { ) -> Cancelable<Option<(FileId, TreeArc<ast::Module>)>> {
let loc = self.def_id.loc(db); let loc = self.def_id.loc(db);
let module_tree = db.module_tree(loc.source_root_id)?; let module_tree = db.module_tree(loc.source_root_id);
let link = ctry!(loc.module_id.parent_link(&module_tree)); let link = ctry!(loc.module_id.parent_link(&module_tree));
let file_id = link let file_id = link
.owner(&module_tree) .owner(&module_tree)
@ -82,7 +82,7 @@ impl Module {
pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Cancelable<Module> { pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Cancelable<Module> {
let loc = self.def_id.loc(db); let loc = self.def_id.loc(db);
let module_tree = db.module_tree(loc.source_root_id)?; let module_tree = db.module_tree(loc.source_root_id);
let module_id = loc.module_id.crate_root(&module_tree); let module_id = loc.module_id.crate_root(&module_tree);
Module::from_module_id(db, loc.source_root_id, module_id) Module::from_module_id(db, loc.source_root_id, module_id)
} }
@ -90,7 +90,7 @@ impl Module {
/// Finds a child module with the specified name. /// Finds a child module with the specified name.
pub fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> { pub fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
let loc = self.def_id.loc(db); let loc = self.def_id.loc(db);
let module_tree = db.module_tree(loc.source_root_id)?; let module_tree = db.module_tree(loc.source_root_id);
let child_id = ctry!(loc.module_id.child(&module_tree, name)); let child_id = ctry!(loc.module_id.child(&module_tree, name));
Module::from_module_id(db, loc.source_root_id, child_id).map(Some) Module::from_module_id(db, loc.source_root_id, child_id).map(Some)
} }
@ -101,7 +101,7 @@ impl Module {
// it's kind of hard since the iterator needs to keep a reference to the // it's kind of hard since the iterator needs to keep a reference to the
// module tree. // module tree.
let loc = self.def_id.loc(db); let loc = self.def_id.loc(db);
let module_tree = db.module_tree(loc.source_root_id)?; let module_tree = db.module_tree(loc.source_root_id);
let children = loc let children = loc
.module_id .module_id
.children(&module_tree) .children(&module_tree)
@ -112,7 +112,7 @@ impl Module {
pub fn parent_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> { pub fn parent_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> {
let loc = self.def_id.loc(db); let loc = self.def_id.loc(db);
let module_tree = db.module_tree(loc.source_root_id)?; let module_tree = db.module_tree(loc.source_root_id);
let parent_id = ctry!(loc.module_id.parent(&module_tree)); let parent_id = ctry!(loc.module_id.parent(&module_tree));
Module::from_module_id(db, loc.source_root_id, parent_id).map(Some) Module::from_module_id(db, loc.source_root_id, parent_id).map(Some)
} }
@ -190,7 +190,7 @@ impl Module {
db: &impl HirDatabase, db: &impl HirDatabase,
) -> Cancelable<Vec<(TreeArc<SyntaxNode>, Problem)>> { ) -> Cancelable<Vec<(TreeArc<SyntaxNode>, Problem)>> {
let loc = self.def_id.loc(db); let loc = self.def_id.loc(db);
let module_tree = db.module_tree(loc.source_root_id)?; let module_tree = db.module_tree(loc.source_root_id);
Ok(loc.module_id.problems(&module_tree, db)) Ok(loc.module_id.problems(&module_tree, db))
} }
} }

View file

@ -77,7 +77,7 @@ pub trait HirDatabase: SyntaxDatabase
use fn query_definitions::file_item; use fn query_definitions::file_item;
} }
fn submodules(source: SourceItemId) -> Cancelable<Arc<Vec<crate::module_tree::Submodule>>> { fn submodules(source: SourceItemId) -> Arc<Vec<crate::module_tree::Submodule>> {
type SubmodulesQuery; type SubmodulesQuery;
use fn crate::module_tree::Submodule::submodules_query; use fn crate::module_tree::Submodule::submodules_query;
} }
@ -92,7 +92,7 @@ pub trait HirDatabase: SyntaxDatabase
use fn query_definitions::item_map; use fn query_definitions::item_map;
} }
fn module_tree(source_root_id: SourceRootId) -> Cancelable<Arc<ModuleTree>> { fn module_tree(source_root_id: SourceRootId) -> Arc<ModuleTree> {
type ModuleTreeQuery; type ModuleTreeQuery;
use fn crate::module_tree::ModuleTree::module_tree_query; use fn crate::module_tree::ModuleTree::module_tree_query;
} }

View file

@ -3,7 +3,7 @@ use std::sync::Arc;
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use ra_db::{FileId, SourceRootId, Cancelable, SourceRoot}; use ra_db::{FileId, SourceRootId, SourceRoot};
use ra_syntax::{ use ra_syntax::{
SyntaxNode, TreeArc, SyntaxNode, TreeArc,
algo::generate, algo::generate,
@ -41,7 +41,7 @@ impl Submodule {
pub(crate) fn submodules_query( pub(crate) fn submodules_query(
db: &impl HirDatabase, db: &impl HirDatabase,
source: SourceItemId, source: SourceItemId,
) -> Cancelable<Arc<Vec<Submodule>>> { ) -> Arc<Vec<Submodule>> {
db.check_canceled(); db.check_canceled();
let file_id = source.file_id; let file_id = source.file_id;
let file_items = db.file_items(file_id); let file_items = db.file_items(file_id);
@ -54,7 +54,7 @@ impl Submodule {
collect_submodules(file_id, &file_items, module.item_list().unwrap()) collect_submodules(file_id, &file_items, module.item_list().unwrap())
} }
}; };
return Ok(Arc::new(submodules)); return Arc::new(submodules);
fn collect_submodules( fn collect_submodules(
file_id: HirFileId, file_id: HirFileId,
@ -116,10 +116,10 @@ impl ModuleTree {
pub(crate) fn module_tree_query( pub(crate) fn module_tree_query(
db: &impl HirDatabase, db: &impl HirDatabase,
source_root: SourceRootId, source_root: SourceRootId,
) -> Cancelable<Arc<ModuleTree>> { ) -> Arc<ModuleTree> {
db.check_canceled(); db.check_canceled();
let res = create_module_tree(db, source_root); let res = create_module_tree(db, source_root);
Ok(Arc::new(res?)) Arc::new(res)
} }
pub(crate) fn modules<'a>(&'a self) -> impl Iterator<Item = ModuleId> + 'a { pub(crate) fn modules<'a>(&'a self) -> impl Iterator<Item = ModuleId> + 'a {
@ -225,10 +225,7 @@ fn modules(root: &impl ast::ModuleItemOwner) -> impl Iterator<Item = (Name, &ast
}) })
} }
fn create_module_tree<'a>( fn create_module_tree<'a>(db: &impl HirDatabase, source_root: SourceRootId) -> ModuleTree {
db: &impl HirDatabase,
source_root: SourceRootId,
) -> Cancelable<ModuleTree> {
let mut tree = ModuleTree::default(); let mut tree = ModuleTree::default();
let mut roots = FxHashMap::default(); let mut roots = FxHashMap::default();
@ -252,10 +249,10 @@ fn create_module_tree<'a>(
&mut roots, &mut roots,
None, None,
source, source,
)?; );
roots.insert(file_id, module_id); roots.insert(file_id, module_id);
} }
Ok(tree) tree
} }
fn build_subtree( fn build_subtree(
@ -266,14 +263,14 @@ fn build_subtree(
roots: &mut FxHashMap<FileId, ModuleId>, roots: &mut FxHashMap<FileId, ModuleId>,
parent: Option<LinkId>, parent: Option<LinkId>,
source: SourceItemId, source: SourceItemId,
) -> Cancelable<ModuleId> { ) -> ModuleId {
visited.insert(source); visited.insert(source);
let id = tree.push_mod(ModuleData { let id = tree.push_mod(ModuleData {
source, source,
parent, parent,
children: Vec::new(), children: Vec::new(),
}); });
for sub in db.submodules(source)?.iter() { for sub in db.submodules(source).iter() {
let link = tree.push_link(LinkData { let link = tree.push_link(LinkData {
source: sub.source, source: sub.source,
name: sub.name.clone(), name: sub.name.clone(),
@ -289,7 +286,7 @@ fn build_subtree(
.map(|file_id| match roots.remove(&file_id) { .map(|file_id| match roots.remove(&file_id) {
Some(module_id) => { Some(module_id) => {
tree.mods[module_id].parent = Some(link); tree.mods[module_id].parent = Some(link);
Ok(module_id) module_id
} }
None => build_subtree( None => build_subtree(
db, db,
@ -304,7 +301,7 @@ fn build_subtree(
}, },
), ),
}) })
.collect::<Cancelable<Vec<_>>>()?; .collect::<Vec<_>>();
(points_to, problem) (points_to, problem)
} else { } else {
let points_to = build_subtree( let points_to = build_subtree(
@ -315,14 +312,14 @@ fn build_subtree(
roots, roots,
Some(link), Some(link),
sub.source, sub.source,
)?; );
(vec![points_to], None) (vec![points_to], None)
}; };
tree.links[link].points_to = points_to; tree.links[link].points_to = points_to;
tree.links[link].problem = problem; tree.links[link].problem = problem;
} }
Ok(id) id
} }
fn resolve_submodule( fn resolve_submodule(

View file

@ -48,7 +48,7 @@ pub(super) fn input_module_items(
source_root_id: SourceRootId, source_root_id: SourceRootId,
module_id: ModuleId, module_id: ModuleId,
) -> Cancelable<Arc<InputModuleItems>> { ) -> Cancelable<Arc<InputModuleItems>> {
let module_tree = db.module_tree(source_root_id)?; let module_tree = db.module_tree(source_root_id);
let source = module_id.source(&module_tree); let source = module_id.source(&module_tree);
let file_id = source.file_id; let file_id = source.file_id;
let source = ModuleSource::from_source_item_id(db, source); let source = ModuleSource::from_source_item_id(db, source);
@ -98,7 +98,7 @@ pub(super) fn item_map(
source_root: SourceRootId, source_root: SourceRootId,
) -> Cancelable<Arc<ItemMap>> { ) -> Cancelable<Arc<ItemMap>> {
let start = Instant::now(); let start = Instant::now();
let module_tree = db.module_tree(source_root)?; let module_tree = db.module_tree(source_root);
let input = module_tree let input = module_tree
.modules() .modules()
.map(|id| { .map(|id| {

View file

@ -92,7 +92,7 @@ pub fn module_from_child_node(
fn module_from_source(db: &impl HirDatabase, source: SourceItemId) -> Cancelable<Option<Module>> { fn module_from_source(db: &impl HirDatabase, source: SourceItemId) -> Cancelable<Option<Module>> {
let source_root_id = db.file_source_root(source.file_id.as_original_file()); let source_root_id = db.file_source_root(source.file_id.as_original_file());
let module_tree = db.module_tree(source_root_id)?; let module_tree = db.module_tree(source_root_id);
let module_id = ctry!(module_tree.find_module_by_source(source)); let module_id = ctry!(module_tree.find_module_by_source(source));
Ok(Some(Module::from_module_id(db, source_root_id, module_id)?)) Ok(Some(Module::from_module_id(db, source_root_id, module_id)?))
} }