From 490112dea10d476e9b5829499bc6670f03f7d138 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jan 2019 19:15:01 +0300 Subject: remove Cancelable from nameres --- crates/ra_hir/src/code_model_api.rs | 4 +-- crates/ra_hir/src/code_model_impl/module.rs | 33 ++++++++--------- crates/ra_hir/src/db.rs | 2 +- crates/ra_hir/src/nameres.rs | 42 ++++++++++------------ crates/ra_hir/src/nameres/tests.rs | 12 +++---- crates/ra_hir/src/query_definitions.rs | 11 +++--- crates/ra_hir/src/ty.rs | 6 ++-- crates/ra_ide_api/src/completion/complete_path.rs | 4 +-- crates/ra_ide_api/src/completion/complete_scope.rs | 2 +- crates/ra_ide_api/src/goto_definition.rs | 2 +- 10 files changed, 52 insertions(+), 66 deletions(-) diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 4d7925ac4..7ccd29e2f 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -134,11 +134,11 @@ impl Module { } /// Returns a `ModuleScope`: a set of items, visible in this module. - pub fn scope(&self, db: &impl HirDatabase) -> Cancelable { + pub fn scope(&self, db: &impl HirDatabase) -> ModuleScope { self.scope_impl(db) } - pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> Cancelable> { + pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs { self.resolve_path_impl(db, path) } diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs index 331b0500e..04301ae53 100644 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ b/crates/ra_hir/src/code_model_impl/module.rs @@ -114,18 +114,13 @@ impl Module { } /// Returns a `ModuleScope`: a set of items, visible in this module. - pub fn scope_impl(&self, db: &impl HirDatabase) -> Cancelable { + pub fn scope_impl(&self, db: &impl HirDatabase) -> ModuleScope { 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) + let item_map = db.item_map(loc.source_root_id); + item_map.per_module[&loc.module_id].clone() } - pub fn resolve_path_impl( - &self, - db: &impl HirDatabase, - path: &Path, - ) -> Cancelable> { + pub fn resolve_path_impl(&self, db: &impl HirDatabase, path: &Path) -> PerNs { let mut curr_per_ns = PerNs::types( match path.kind { PathKind::Crate => self.crate_root(db), @@ -134,7 +129,7 @@ impl Module { if let Some(p) = self.parent(db) { p } else { - return Ok(PerNs::none()); + return PerNs::none(); } } } @@ -146,7 +141,7 @@ impl Module { let curr = if let Some(r) = curr_per_ns.as_ref().take_types() { r } else { - return Ok(PerNs::none()); + return PerNs::none(); }; let module = match curr.resolve(db) { Def::Module(it) => it, @@ -157,28 +152,28 @@ impl Module { e.variants(db).into_iter().find(|(n, _variant)| n == name); if let Some((_n, variant)) = matching_variant { - return Ok(PerNs::both(variant.def_id(), e.def_id())); + return PerNs::both(variant.def_id(), e.def_id()); } else { - return Ok(PerNs::none()); + return PerNs::none(); } } else if segments.len() == idx { // enum - return Ok(PerNs::types(e.def_id())); + return PerNs::types(e.def_id()); } else { // malformed enum? - return Ok(PerNs::none()); + return PerNs::none(); } } - _ => return Ok(PerNs::none()), + _ => return PerNs::none(), }; - let scope = module.scope(db)?; + let scope = module.scope(db); curr_per_ns = if let Some(r) = scope.get(&name) { r.def_id } else { - return Ok(PerNs::none()); + return PerNs::none(); }; } - Ok(curr_per_ns) + curr_per_ns } pub fn problems_impl( diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index d20c03f43..1466c3438 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -87,7 +87,7 @@ pub trait HirDatabase: SyntaxDatabase use fn query_definitions::input_module_items; } - fn item_map(source_root_id: SourceRootId) -> Cancelable> { + fn item_map(source_root_id: SourceRootId) -> Arc { type ItemMapQuery; use fn query_definitions::item_map; } diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index e51cbe786..484f668d0 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -22,7 +22,7 @@ use ra_syntax::{ SyntaxKind::{self, *}, ast::{self, AstNode} }; -use ra_db::{SourceRootId, Cancelable, FileId}; +use ra_db::{SourceRootId, FileId}; use crate::{ HirFileId, @@ -319,30 +319,26 @@ where } } - pub(crate) fn resolve(mut self) -> Cancelable { + pub(crate) fn resolve(mut self) -> ItemMap { for (&module_id, items) in self.input.iter() { - self.populate_module(module_id, Arc::clone(items))?; + self.populate_module(module_id, Arc::clone(items)); } loop { let processed_imports_count = self.processed_imports.len(); for &module_id in self.input.keys() { self.db.check_canceled(); - self.resolve_imports(module_id)?; + self.resolve_imports(module_id); } if processed_imports_count == self.processed_imports.len() { // no new imports resolved break; } } - Ok(self.result) + self.result } - fn populate_module( - &mut self, - module_id: ModuleId, - input: Arc, - ) -> Cancelable<()> { + fn populate_module(&mut self, module_id: ModuleId, input: Arc) { let mut module_items = ModuleScope::default(); // Populate extern crates prelude @@ -415,7 +411,6 @@ where } self.result.per_module.insert(module_id, module_items); - Ok(()) } fn add_module_item(&self, module_items: &mut ModuleScope, name: Name, def_id: PerNs) { @@ -426,24 +421,23 @@ where module_items.items.insert(name, resolution); } - fn resolve_imports(&mut self, module_id: ModuleId) -> Cancelable<()> { + fn resolve_imports(&mut self, module_id: ModuleId) { for (i, import) in self.input[&module_id].imports.iter().enumerate() { if self.processed_imports.contains(&(module_id, i)) { // already done continue; } - if self.resolve_import(module_id, import)? { + if self.resolve_import(module_id, import) { log::debug!("import {:?} resolved (or definite error)", import); self.processed_imports.insert((module_id, i)); } } - Ok(()) } - fn resolve_import(&mut self, module_id: ModuleId, import: &Import) -> Cancelable { + fn resolve_import(&mut self, module_id: ModuleId, import: &Import) -> bool { log::debug!("resolving import: {:?}", import); let ptr = match import.kind { - ImportKind::Glob => return Ok(false), + ImportKind::Glob => return false, ImportKind::Named(ptr) => ptr, }; @@ -455,7 +449,7 @@ where None => { // TODO: error log::debug!("super path in root module"); - return Ok(true); // this can't suddenly resolve if we just resolve some other imports + return true; // this can't suddenly resolve if we just resolve some other imports } } } @@ -469,7 +463,7 @@ where Some(res) if !res.def_id.is_none() => res.def_id, _ => { log::debug!("path segment {:?} not found", name); - return Ok(false); + return false; } }; @@ -481,7 +475,7 @@ where "path segment {:?} resolved to value only, but is not last", name ); - return Ok(false); + return false; }; curr = match type_def_id.loc(self.db) { DefLoc { @@ -499,7 +493,7 @@ where kind: PathKind::Crate, }; log::debug!("resolving {:?} in other source root", path); - let def_id = module.resolve_path(self.db, &path)?; + let def_id = module.resolve_path(self.db, &path); if !def_id.is_none() { let name = path.segments.last().unwrap(); self.update(module_id, |items| { @@ -515,10 +509,10 @@ where import, def_id.map(|did| did.loc(self.db)) ); - return Ok(true); + return true; } else { log::debug!("rest of path did not resolve in other source root"); - return Ok(true); + return true; } } } @@ -528,7 +522,7 @@ where name, type_def_id.loc(self.db) ); - return Ok(true); // this resolved to a non-module, so the path won't ever resolve + return true; // this resolved to a non-module, so the path won't ever resolve } } } else { @@ -547,7 +541,7 @@ where }) } } - Ok(true) + true } fn update(&mut self, module_id: ModuleId, f: impl FnOnce(&mut ModuleScope)) { diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index ea8ab4c83..9a0474045 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -17,7 +17,7 @@ fn item_map(fixture: &str) -> (Arc, ModuleId) { let source_root = db.file_source_root(pos.file_id); let module = crate::source_binder::module_from_position(&db, pos).unwrap(); let module_id = module.def_id.loc(&db).module_id; - (db.item_map(source_root).unwrap(), module_id) + (db.item_map(source_root), module_id) } fn check_module_item_map(map: &ItemMap, module_id: ModuleId, expected: &str) { @@ -242,7 +242,7 @@ fn item_map_across_crates() { let source_root = db.file_source_root(main_id); let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); let module_id = module.def_id.loc(&db).module_id; - let item_map = db.item_map(source_root).unwrap(); + let item_map = db.item_map(source_root); check_module_item_map( &item_map, @@ -294,7 +294,7 @@ fn import_across_source_roots() { let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); let module_id = module.def_id.loc(&db).module_id; - let item_map = db.item_map(source_root).unwrap(); + let item_map = db.item_map(source_root); check_module_item_map( &item_map, @@ -337,7 +337,7 @@ fn reexport_across_crates() { let source_root = db.file_source_root(main_id); let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); let module_id = module.def_id.loc(&db).module_id; - let item_map = db.item_map(source_root).unwrap(); + let item_map = db.item_map(source_root); check_module_item_map( &item_map, @@ -354,7 +354,7 @@ fn check_item_map_is_not_recomputed(initial: &str, file_change: &str) { let source_root = db.file_source_root(pos.file_id); { let events = db.log_executed(|| { - db.item_map(source_root).unwrap(); + db.item_map(source_root); }); assert!(format!("{:?}", events).contains("item_map")) } @@ -363,7 +363,7 @@ fn check_item_map_is_not_recomputed(initial: &str, file_change: &str) { { let events = db.log_executed(|| { - db.item_map(source_root).unwrap(); + db.item_map(source_root); }); assert!( !format!("{:?}", events).contains("item_map"), diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs index 8f33ec707..24cb5c752 100644 --- a/crates/ra_hir/src/query_definitions.rs +++ b/crates/ra_hir/src/query_definitions.rs @@ -8,7 +8,7 @@ use ra_syntax::{ AstNode, SyntaxNode, TreeArc, ast::{self, ModuleItemOwner} }; -use ra_db::{SourceRootId, Cancelable,}; +use ra_db::SourceRootId; use crate::{ SourceFileItems, SourceItemId, DefId, HirFileId, ModuleSource, @@ -93,10 +93,7 @@ pub(super) fn input_module_items( Arc::new(res) } -pub(super) fn item_map( - db: &impl HirDatabase, - source_root: SourceRootId, -) -> Cancelable> { +pub(super) fn item_map(db: &impl HirDatabase, source_root: SourceRootId) -> Arc { let start = Instant::now(); let module_tree = db.module_tree(source_root); let input = module_tree @@ -105,8 +102,8 @@ pub(super) fn item_map( .collect::>(); let resolver = Resolver::new(db, &input, source_root, module_tree); - let res = resolver.resolve()?; + let res = resolver.resolve(); let elapsed = start.elapsed(); log::info!("item_map: {:?}", elapsed); - Ok(Arc::new(res)) + Arc::new(res) } diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 54eece165..6df7820ea 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -349,7 +349,7 @@ impl Ty { } // Resolve in module (in type namespace) - let resolved = if let Some(r) = module.resolve_path(db, path)?.take_types() { + let resolved = if let Some(r) = module.resolve_path(db, path).take_types() { r } else { return Ok(Ty::Unknown); @@ -860,7 +860,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { }; // resolve in module - let resolved = ctry!(self.module.resolve_path(self.db, &path)?.take_values()); + let resolved = ctry!(self.module.resolve_path(self.db, &path).take_values()); let ty = self.db.type_for_def(resolved)?; let ty = self.insert_type_vars(ty); Ok(Some(ty)) @@ -872,7 +872,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } else { return Ok((Ty::Unknown, None)); }; - let def_id = if let Some(def_id) = self.module.resolve_path(self.db, &path)?.take_types() { + let def_id = if let Some(def_id) = self.module.resolve_path(self.db, &path).take_types() { def_id } else { return Ok((Ty::Unknown, None)); diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index 207a509b3..42468681a 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -8,13 +8,13 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C (Some(path), Some(module)) => (path.clone(), module), _ => return Ok(()), }; - let def_id = match module.resolve_path(ctx.db, &path)?.take_types() { + let def_id = match module.resolve_path(ctx.db, &path).take_types() { Some(it) => it, None => return Ok(()), }; match def_id.resolve(ctx.db) { hir::Def::Module(module) => { - let module_scope = module.scope(ctx.db)?; + let module_scope = module.scope(ctx.db); for (name, res) in module_scope.entries() { CompletionItem::new(CompletionKind::Reference, name.to_string()) .from_resolution(ctx, res) diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs index fdb64895e..660c7d16e 100644 --- a/crates/ra_ide_api/src/completion/complete_scope.rs +++ b/crates/ra_ide_api/src/completion/complete_scope.rs @@ -19,7 +19,7 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) -> complete_fn(acc, &scopes, ctx.offset); } - let module_scope = module.scope(ctx.db)?; + let module_scope = module.scope(ctx.db); let (file_id, _) = module.definition_source(ctx.db); module_scope .entries() diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 5d522181b..cdd8e211d 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -85,7 +85,7 @@ pub(crate) fn reference_definition( .find_map(ast::Path::cast) .and_then(hir::Path::from_ast) { - let resolved = module.resolve_path(db, &path)?; + let resolved = module.resolve_path(db, &path); if let Some(def_id) = resolved.take_types().or(resolved.take_values()) { if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)) { return Ok(Exact(target)); -- cgit v1.2.3