From c159e414b42e2fd43ed88bbf4113fbe8c8e372fe Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jan 2019 18:30:58 +0300 Subject: remove Cancelable from Module API, part 2 --- crates/ra_hir/src/code_model_api.rs | 14 +++++++------- crates/ra_hir/src/code_model_impl/module.rs | 30 +++++++++++++---------------- crates/ra_hir/src/ids.rs | 2 +- crates/ra_hir/src/ty/method_resolution.rs | 2 +- crates/ra_ide_api/src/imp.rs | 10 +++++----- crates/ra_ide_api/src/lib.rs | 2 +- crates/ra_ide_api/src/runnables.rs | 1 - 7 files changed, 28 insertions(+), 33 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index aed0ea958..81638e384 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -97,14 +97,14 @@ impl Module { } /// Returns the crate this module is part of. - pub fn krate(&self, db: &impl HirDatabase) -> Cancelable> { + pub fn krate(&self, db: &impl HirDatabase) -> Option { self.krate_impl(db) } /// Topmost parent of this module. Every module has a `crate_root`, but some /// might be missing `krate`. This can happen if a module's file is not included /// in the module tree of any target in Cargo.toml. - pub fn crate_root(&self, db: &impl HirDatabase) -> Cancelable { + pub fn crate_root(&self, db: &impl HirDatabase) -> Module { self.crate_root_impl(db) } @@ -114,23 +114,23 @@ impl Module { } /// Iterates over all child modules. - pub fn children(&self, db: &impl HirDatabase) -> Cancelable> { + pub fn children(&self, db: &impl HirDatabase) -> impl Iterator { self.children_impl(db) } /// Finds a parent module. - pub fn parent(&self, db: &impl HirDatabase) -> Cancelable> { + pub fn parent(&self, db: &impl HirDatabase) -> Option { self.parent_impl(db) } - pub fn path_to_root(&self, db: &impl HirDatabase) -> Cancelable> { + pub fn path_to_root(&self, db: &impl HirDatabase) -> Vec { let mut res = vec![self.clone()]; let mut curr = self.clone(); - while let Some(next) = curr.parent(db)? { + while let Some(next) = curr.parent(db) { res.push(next.clone()); curr = next } - Ok(res) + res } /// Returns a `ModuleScope`: a set of items, visible in this module. diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs index 127dcc570..8668d6c8a 100644 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ b/crates/ra_hir/src/code_model_impl/module.rs @@ -66,21 +66,21 @@ impl Module { Some((file_id, src)) } - pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Cancelable> { - let root = self.crate_root(db)?; + pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Option { + 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))) + let crate_id = crate_graph.crate_id_for_crate_root(file_id)?; + Some(Crate::new(crate_id)) } - pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Cancelable { + pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Module { 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); - Ok(Module::from_module_id(db, loc.source_root_id, module_id)) + Module::from_module_id(db, loc.source_root_id, module_id) } /// Finds a child module with the specified name. @@ -92,7 +92,7 @@ impl Module { } /// Iterates over all child modules. - pub fn children_impl(&self, db: &impl HirDatabase) -> Cancelable> { + pub fn children_impl(&self, db: &impl HirDatabase) -> impl Iterator { // FIXME this should be implementable without collecting into a vec, but // it's kind of hard since the iterator needs to keep a reference to the // module tree. @@ -103,18 +103,14 @@ impl Module { .children(&module_tree) .map(|(_, module_id)| Module::from_module_id(db, loc.source_root_id, module_id)) .collect::>(); - Ok(children.into_iter()) + children.into_iter() } - pub fn parent_impl(&self, db: &impl HirDatabase) -> Cancelable> { + pub fn parent_impl(&self, db: &impl HirDatabase) -> Option { 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)); - Ok(Some(Module::from_module_id( - db, - loc.source_root_id, - parent_id, - ))) + let parent_id = loc.module_id.parent(&module_tree)?; + Some(Module::from_module_id(db, loc.source_root_id, parent_id)) } /// Returns a `ModuleScope`: a set of items, visible in this module. @@ -132,10 +128,10 @@ impl Module { ) -> Cancelable> { let mut curr_per_ns = PerNs::types( match path.kind { - PathKind::Crate => self.crate_root(db)?, + PathKind::Crate => self.crate_root(db), PathKind::Self_ | PathKind::Plain => self.clone(), PathKind::Super => { - if let Some(p) = self.parent(db)? { + if let Some(p) = self.parent(db) { p } else { return Ok(PerNs::none()); diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index d7cc9b4ca..7b572061a 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -217,7 +217,7 @@ impl DefId { /// Returns the containing crate. pub fn krate(&self, db: &impl HirDatabase) -> Cancelable> { - Ok(self.module(db)?.krate(db)?) + Ok(self.module(db)?.krate(db)) } /// Returns the containing impl block, if this is an impl item. diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 7c3839388..f0da2ee2b 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -75,7 +75,7 @@ impl CrateImplBlocks { } } - for child in module.children(db)? { + for child in module.children(db) { self.collect_recursive(db, child)?; } diff --git a/crates/ra_ide_api/src/imp.rs b/crates/ra_ide_api/src/imp.rs index 76cb312dd..3ef11dfa1 100644 --- a/crates/ra_ide_api/src/imp.rs +++ b/crates/ra_ide_api/src/imp.rs @@ -99,16 +99,16 @@ impl db::RootDatabase { impl db::RootDatabase { /// Returns `Vec` for the same reason as `parent_module` - pub(crate) fn crate_for(&self, file_id: FileId) -> Cancelable> { + pub(crate) fn crate_for(&self, file_id: FileId) -> Vec { let module = match source_binder::module_from_file_id(self, file_id) { Some(it) => it, - None => return Ok(Vec::new()), + None => return Vec::new(), }; - let krate = match module.krate(self)? { + let krate = match module.krate(self) { Some(it) => it, - None => return Ok(Vec::new()), + None => return Vec::new(), }; - Ok(vec![krate.crate_id()]) + vec![krate.crate_id()] } pub(crate) fn find_all_refs( &self, diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index e0b8410d1..0f690fc84 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -419,7 +419,7 @@ impl Analysis { /// Returns crates this file belongs too. pub fn crate_for(&self, file_id: FileId) -> Cancelable> { - self.with_db(|db| db.crate_for(file_id))? + self.with_db(|db| db.crate_for(file_id)) } /// Returns the root file of the given crate. diff --git a/crates/ra_ide_api/src/runnables.rs b/crates/ra_ide_api/src/runnables.rs index c0d4bda94..a3207fdd2 100644 --- a/crates/ra_ide_api/src/runnables.rs +++ b/crates/ra_ide_api/src/runnables.rs @@ -80,7 +80,6 @@ fn runnable_mod(db: &RootDatabase, file_id: FileId, module: &ast::Module) -> Opt // FIXME: thread cancellation instead of `.ok`ing let path = module .path_to_root(db) - .ok()? .into_iter() .rev() .filter_map(|it| it.name(db)) -- cgit v1.2.3