555: remove Cancelable from ids r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-01-15 16:20:31 +00:00
commit 323938dae5
14 changed files with 79 additions and 100 deletions

View File

@ -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<ModuleScope> {
pub fn scope(&self, db: &impl HirDatabase) -> ModuleScope {
self.scope_impl(db)
}
pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> Cancelable<PerNs<DefId>> {
pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs<DefId> {
self.resolve_path_impl(db, path)
}

View File

@ -2,7 +2,6 @@ mod scope;
use std::sync::Arc;
use ra_db::Cancelable;
use ra_syntax::{TreeArc, ast::{self, NameOwner}};
use crate::{
@ -24,12 +23,12 @@ impl Function {
db.body_hir(self.def_id)
}
pub(crate) fn module(&self, db: &impl HirDatabase) -> Cancelable<Module> {
pub(crate) fn module(&self, db: &impl HirDatabase) -> Module {
self.def_id.module(db)
}
/// The containing impl block, if this is a method.
pub(crate) fn impl_block(&self, db: &impl HirDatabase) -> Cancelable<Option<ImplBlock>> {
pub(crate) fn impl_block(&self, db: &impl HirDatabase) -> Option<ImplBlock> {
self.def_id.impl_block(db)
}
}

View File

@ -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<ModuleScope> {
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<PerNs<DefId>> {
pub fn resolve_path_impl(&self, db: &impl HirDatabase, path: &Path) -> PerNs<DefId> {
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(

View File

@ -87,7 +87,7 @@ pub trait HirDatabase: SyntaxDatabase
use fn query_definitions::input_module_items;
}
fn item_map(source_root_id: SourceRootId) -> Cancelable<Arc<ItemMap>> {
fn item_map(source_root_id: SourceRootId) -> Arc<ItemMap> {
type ItemMapQuery;
use fn query_definitions::item_map;
}
@ -97,7 +97,7 @@ pub trait HirDatabase: SyntaxDatabase
use fn crate::module_tree::ModuleTree::module_tree_query;
}
fn impls_in_module(source_root_id: SourceRootId, module_id: ModuleId) -> Cancelable<Arc<ModuleImplBlocks>> {
fn impls_in_module(source_root_id: SourceRootId, module_id: ModuleId) -> Arc<ModuleImplBlocks> {
type ImplsInModuleQuery;
use fn crate::impl_block::impls_in_module;
}

View File

@ -1,4 +1,4 @@
use ra_db::{SourceRootId, LocationIntener, Cancelable, FileId};
use ra_db::{SourceRootId, LocationIntener, FileId};
use ra_syntax::{TreeArc, SyntaxKind, SyntaxNode, SourceFile, AstNode, ast};
use ra_arena::{Arena, RawId, impl_arena_id};
@ -205,25 +205,21 @@ impl DefId {
}
/// For a module, returns that module; for any other def, returns the containing module.
pub fn module(self, db: &impl HirDatabase) -> Cancelable<Module> {
pub fn module(self, db: &impl HirDatabase) -> Module {
let loc = self.loc(db);
Ok(Module::from_module_id(
db,
loc.source_root_id,
loc.module_id,
))
Module::from_module_id(db, loc.source_root_id, loc.module_id)
}
/// Returns the containing crate.
pub fn krate(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> {
Ok(self.module(db)?.krate(db))
pub fn krate(&self, db: &impl HirDatabase) -> Option<Crate> {
self.module(db).krate(db)
}
/// Returns the containing impl block, if this is an impl item.
pub fn impl_block(self, db: &impl HirDatabase) -> Cancelable<Option<ImplBlock>> {
pub fn impl_block(self, db: &impl HirDatabase) -> Option<ImplBlock> {
let loc = self.loc(db);
let module_impls = db.impls_in_module(loc.source_root_id, loc.module_id)?;
Ok(ImplBlock::containing(module_impls, self))
let module_impls = db.impls_in_module(loc.source_root_id, loc.module_id);
ImplBlock::containing(module_impls, self)
}
}

View File

@ -3,7 +3,7 @@ use rustc_hash::FxHashMap;
use ra_arena::{Arena, RawId, impl_arena_id};
use ra_syntax::ast::{self, AstNode};
use ra_db::{LocationIntener, Cancelable, SourceRootId};
use ra_db::{LocationIntener, SourceRootId};
use crate::{
DefId, DefLoc, DefKind, SourceItemId, SourceFileItems,
@ -166,7 +166,7 @@ impl ModuleImplBlocks {
}
}
fn collect(&mut self, db: &impl HirDatabase, module: Module) -> Cancelable<()> {
fn collect(&mut self, db: &impl HirDatabase, module: Module) {
let (file_id, module_source) = module.definition_source(db);
let node = match &module_source {
ModuleSource::SourceFile(node) => node.syntax(),
@ -185,8 +185,6 @@ impl ModuleImplBlocks {
self.impls_by_def.insert(impl_item.def_id(), id);
}
}
Ok(())
}
}
@ -194,9 +192,9 @@ pub(crate) fn impls_in_module(
db: &impl HirDatabase,
source_root_id: SourceRootId,
module_id: ModuleId,
) -> Cancelable<Arc<ModuleImplBlocks>> {
) -> Arc<ModuleImplBlocks> {
let mut result = ModuleImplBlocks::new();
let module = Module::from_module_id(db, source_root_id, module_id);
result.collect(db, module)?;
Ok(Arc::new(result))
result.collect(db, module);
Arc::new(result)
}

View File

@ -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<ItemMap> {
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<InputModuleItems>,
) -> Cancelable<()> {
fn populate_module(&mut self, module_id: ModuleId, input: Arc<InputModuleItems>) {
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<DefId>) {
@ -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<bool> {
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)) {

View File

@ -17,7 +17,7 @@ fn item_map(fixture: &str) -> (Arc<ItemMap>, 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"),

View File

@ -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<Arc<ItemMap>> {
pub(super) fn item_map(db: &impl HirDatabase, source_root: SourceRootId) -> Arc<ItemMap> {
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::<FxHashMap<_, _>>();
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)
}

View File

@ -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);
@ -447,8 +447,8 @@ impl fmt::Display for Ty {
/// function body.
fn type_for_fn(db: &impl HirDatabase, f: Function) -> Cancelable<Ty> {
let signature = f.signature(db);
let module = f.module(db)?;
let impl_block = f.impl_block(db)?;
let module = f.module(db);
let impl_block = f.impl_block(db);
// TODO we ignore type parameters for now
let input = signature
.params()
@ -517,8 +517,8 @@ pub(super) fn type_for_field(
def_id
),
};
let module = def_id.module(db)?;
let impl_block = def_id.impl_block(db)?;
let module = def_id.module(db);
let impl_block = def_id.impl_block(db);
let type_ref = ctry!(variant_data.get_field_type_ref(&field));
Ok(Some(Ty::from_hir(
db,
@ -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));
@ -1207,8 +1207,8 @@ pub fn infer(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Arc<InferenceRe
let function = Function::new(def_id); // TODO: consts also need inference
let body = function.body(db);
let scopes = db.fn_scopes(def_id);
let module = function.module(db)?;
let impl_block = function.impl_block(db)?;
let module = function.module(db);
let impl_block = function.impl_block(db);
let mut ctx = InferenceContext::new(db, body, scopes, module, impl_block);
let signature = function.signature(db);

View File

@ -49,14 +49,14 @@ impl CrateImplBlocks {
.into_iter()
.flat_map(|i| i.iter())
.map(move |(module_id, impl_id)| {
let module_impl_blocks = db.impls_in_module(self.source_root_id, *module_id)?;
let module_impl_blocks = db.impls_in_module(self.source_root_id, *module_id);
Ok(ImplBlock::from_id(module_impl_blocks, *impl_id))
})
}
fn collect_recursive(&mut self, db: &impl HirDatabase, module: Module) -> Cancelable<()> {
let module_id = module.def_id.loc(db).module_id;
let module_impl_blocks = db.impls_in_module(self.source_root_id, module_id)?;
let module_impl_blocks = db.impls_in_module(self.source_root_id, module_id);
for (impl_id, impl_data) in module_impl_blocks.impls.iter() {
let impl_block = ImplBlock::from_id(Arc::clone(&module_impl_blocks), impl_id);
@ -100,10 +100,10 @@ impl CrateImplBlocks {
}
}
fn def_crate(db: &impl HirDatabase, ty: &Ty) -> Cancelable<Option<Crate>> {
fn def_crate(db: &impl HirDatabase, ty: &Ty) -> Option<Crate> {
match ty {
Ty::Adt { def_id, .. } => def_id.krate(db),
_ => Ok(None),
_ => None,
}
}
@ -139,7 +139,7 @@ impl Ty {
// rustc does an autoderef and then autoref again).
for derefed_ty in self.autoderef(db) {
let krate = match def_crate(db, &derefed_ty)? {
let krate = match def_crate(db, &derefed_ty) {
Some(krate) => krate,
None => continue,
};

View File

@ -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)

View File

@ -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()

View File

@ -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));