From 9faea2364dee4fbc9391ad233c570b70256ef002 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 13 Mar 2020 16:05:46 +0100 Subject: Use `dyn Trait` for working with databse It improves compile time in `--release` mode quite a bit, it doesn't really slow things down and, conceptually, it seems closer to what we want the physical architecture to look like (we don't want to monomorphise EVERYTHING in a single leaf crate). --- crates/ra_hir_def/src/adt.rs | 16 +++--- crates/ra_hir_def/src/attr.rs | 14 +++--- crates/ra_hir_def/src/body.rs | 22 ++++----- crates/ra_hir_def/src/body/lower.rs | 11 ++--- crates/ra_hir_def/src/body/scope.rs | 2 +- crates/ra_hir_def/src/child_by_source.rs | 20 ++++---- crates/ra_hir_def/src/data.rs | 18 +++---- crates/ra_hir_def/src/db.rs | 10 +++- crates/ra_hir_def/src/docs.rs | 4 +- crates/ra_hir_def/src/find_path.rs | 8 +-- crates/ra_hir_def/src/generics.rs | 8 +-- crates/ra_hir_def/src/lang_item.rs | 10 ++-- crates/ra_hir_def/src/lib.rs | 48 +++++++++--------- crates/ra_hir_def/src/nameres.rs | 25 +++++----- crates/ra_hir_def/src/nameres/collector.rs | 22 +++------ crates/ra_hir_def/src/nameres/mod_resolution.rs | 4 +- crates/ra_hir_def/src/nameres/path_resolution.rs | 8 +-- crates/ra_hir_def/src/nameres/raw.rs | 8 +-- crates/ra_hir_def/src/resolver.rs | 62 ++++++++++++------------ crates/ra_hir_def/src/src.rs | 12 ++--- crates/ra_hir_def/src/test_db.rs | 18 ++++++- crates/ra_hir_def/src/visibility.rs | 12 ++--- 22 files changed, 185 insertions(+), 177 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs index 2bdfc2b8d..d55c49938 100644 --- a/crates/ra_hir_def/src/adt.rs +++ b/crates/ra_hir_def/src/adt.rs @@ -52,14 +52,14 @@ pub struct StructFieldData { } impl StructData { - pub(crate) fn struct_data_query(db: &impl DefDatabase, id: StructId) -> Arc { + pub(crate) fn struct_data_query(db: &dyn DefDatabase, id: StructId) -> Arc { let src = id.lookup(db).source(db); let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); let variant_data = VariantData::new(db, src.map(|s| s.kind())); let variant_data = Arc::new(variant_data); Arc::new(StructData { name, variant_data }) } - pub(crate) fn union_data_query(db: &impl DefDatabase, id: UnionId) -> Arc { + pub(crate) fn union_data_query(db: &dyn DefDatabase, id: UnionId) -> Arc { let src = id.lookup(db).source(db); let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); let variant_data = VariantData::new( @@ -76,7 +76,7 @@ impl StructData { } impl EnumData { - pub(crate) fn enum_data_query(db: &impl DefDatabase, e: EnumId) -> Arc { + pub(crate) fn enum_data_query(db: &dyn DefDatabase, e: EnumId) -> Arc { let _p = profile("enum_data_query"); let src = e.lookup(db).source(db); let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); @@ -94,7 +94,7 @@ impl EnumData { impl HasChildSource for EnumId { type ChildId = LocalEnumVariantId; type Value = ast::EnumVariant; - fn child_source(&self, db: &impl DefDatabase) -> InFile> { + fn child_source(&self, db: &dyn DefDatabase) -> InFile> { let src = self.lookup(db).source(db); let mut trace = Trace::new_for_map(); lower_enum(db, &mut trace, &src); @@ -103,7 +103,7 @@ impl HasChildSource for EnumId { } fn lower_enum( - db: &impl DefDatabase, + db: &dyn DefDatabase, trace: &mut Trace, ast: &InFile, ) { @@ -119,7 +119,7 @@ fn lower_enum( } impl VariantData { - fn new(db: &impl DefDatabase, flavor: InFile) -> Self { + fn new(db: &dyn DefDatabase, flavor: InFile) -> Self { let mut trace = Trace::new_for_arena(); match lower_struct(db, &mut trace, &flavor) { StructKind::Tuple => VariantData::Tuple(trace.into_arena()), @@ -153,7 +153,7 @@ impl HasChildSource for VariantId { type ChildId = LocalStructFieldId; type Value = Either; - fn child_source(&self, db: &impl DefDatabase) -> InFile> { + fn child_source(&self, db: &dyn DefDatabase) -> InFile> { let src = match self { VariantId::EnumVariantId(it) => { // I don't really like the fact that we call into parent source @@ -182,7 +182,7 @@ pub enum StructKind { } fn lower_struct( - db: &impl DefDatabase, + db: &dyn DefDatabase, trace: &mut Trace< LocalStructFieldId, StructFieldData, diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index 9efa4970c..71a18f5e1 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs @@ -32,7 +32,7 @@ impl ops::Deref for Attrs { } impl Attrs { - pub(crate) fn attrs_query(db: &impl DefDatabase, def: AttrDefId) -> Attrs { + pub(crate) fn attrs_query(db: &dyn DefDatabase, def: AttrDefId) -> Attrs { match def { AttrDefId::ModuleId(module) => { let def_map = db.crate_def_map(module.krate); @@ -71,8 +71,8 @@ impl Attrs { } } - fn from_attrs_owner(db: &impl DefDatabase, owner: InFile<&dyn AttrsOwner>) -> Attrs { - let hygiene = Hygiene::new(db, owner.file_id); + fn from_attrs_owner(db: &dyn DefDatabase, owner: InFile<&dyn AttrsOwner>) -> Attrs { + let hygiene = Hygiene::new(db.upcast(), owner.file_id); Attrs::new(owner.value, &hygiene) } @@ -155,20 +155,18 @@ impl<'a> AttrQuery<'a> { } } -fn attrs_from_ast(src: AstId, db: &D) -> Attrs +fn attrs_from_ast(src: AstId, db: &dyn DefDatabase) -> Attrs where N: ast::AttrsOwner, - D: DefDatabase, { - let src = InFile::new(src.file_id, src.to_node(db)); + let src = InFile::new(src.file_id, src.to_node(db.upcast())); Attrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner)) } -fn attrs_from_loc(node: T, db: &D) -> Attrs +fn attrs_from_loc(node: T, db: &dyn DefDatabase) -> Attrs where T: HasSource, T::Value: ast::AttrsOwner, - D: DefDatabase, { let src = node.source(db); Attrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner)) diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index 2bc405a59..34561ee73 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -34,19 +34,19 @@ pub(crate) struct Expander { impl Expander { pub(crate) fn new( - db: &impl DefDatabase, + db: &dyn DefDatabase, current_file_id: HirFileId, module: ModuleId, ) -> Expander { let crate_def_map = db.crate_def_map(module.krate); - let hygiene = Hygiene::new(db, current_file_id); + let hygiene = Hygiene::new(db.upcast(), current_file_id); let ast_id_map = db.ast_id_map(current_file_id); Expander { crate_def_map, current_file_id, hygiene, ast_id_map, module } } - pub(crate) fn enter_expand( + pub(crate) fn enter_expand( &mut self, - db: &DB, + db: &dyn DefDatabase, local_scope: Option<&ItemScope>, macro_call: ast::MacroCall, ) -> Option<(Mark, T)> { @@ -70,7 +70,7 @@ impl Expander { ast_id_map: mem::take(&mut self.ast_id_map), bomb: DropBomb::new("expansion mark dropped"), }; - self.hygiene = Hygiene::new(db, file_id); + self.hygiene = Hygiene::new(db.upcast(), file_id); self.current_file_id = file_id; self.ast_id_map = db.ast_id_map(file_id); @@ -84,8 +84,8 @@ impl Expander { None } - pub(crate) fn exit(&mut self, db: &impl DefDatabase, mut mark: Mark) { - self.hygiene = Hygiene::new(db, mark.file_id); + pub(crate) fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) { + self.hygiene = Hygiene::new(db.upcast(), mark.file_id); self.current_file_id = mark.file_id; self.ast_id_map = mem::take(&mut mark.ast_id_map); mark.bomb.defuse(); @@ -99,7 +99,7 @@ impl Expander { Path::from_src(path, &self.hygiene) } - fn resolve_path_as_macro(&self, db: &impl DefDatabase, path: &ModPath) -> Option { + fn resolve_path_as_macro(&self, db: &dyn DefDatabase, path: &ModPath) -> Option { self.crate_def_map .resolve_path(db, self.module.local_id, path, BuiltinShadowMode::Other) .0 @@ -167,7 +167,7 @@ pub struct SyntheticSyntax; impl Body { pub(crate) fn body_with_source_map_query( - db: &impl DefDatabase, + db: &dyn DefDatabase, def: DefWithBodyId, ) -> (Arc, Arc) { let _p = profile("body_with_source_map_query"); @@ -196,12 +196,12 @@ impl Body { (Arc::new(body), Arc::new(source_map)) } - pub(crate) fn body_query(db: &impl DefDatabase, def: DefWithBodyId) -> Arc { + pub(crate) fn body_query(db: &dyn DefDatabase, def: DefWithBodyId) -> Arc { db.body_with_source_map(def).0 } fn new( - db: &impl DefDatabase, + db: &dyn DefDatabase, def: DefWithBodyId, expander: Expander, params: Option, diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 54b5591d3..6238de606 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -36,7 +36,7 @@ use crate::{ }; pub(super) fn lower( - db: &impl DefDatabase, + db: &dyn DefDatabase, def: DefWithBodyId, expander: Expander, params: Option, @@ -58,8 +58,8 @@ pub(super) fn lower( .collect(params, body) } -struct ExprCollector { - db: DB, +struct ExprCollector<'a> { + db: &'a dyn DefDatabase, def: DefWithBodyId, expander: Expander, @@ -67,10 +67,7 @@ struct ExprCollector { source_map: BodySourceMap, } -impl<'a, DB> ExprCollector<&'a DB> -where - DB: DefDatabase, -{ +impl ExprCollector<'_> { fn collect( mut self, param_list: Option, diff --git a/crates/ra_hir_def/src/body/scope.rs b/crates/ra_hir_def/src/body/scope.rs index a58a7b21f..7c3db8869 100644 --- a/crates/ra_hir_def/src/body/scope.rs +++ b/crates/ra_hir_def/src/body/scope.rs @@ -45,7 +45,7 @@ pub struct ScopeData { } impl ExprScopes { - pub(crate) fn expr_scopes_query(db: &impl DefDatabase, def: DefWithBodyId) -> Arc { + pub(crate) fn expr_scopes_query(db: &dyn DefDatabase, def: DefWithBodyId) -> Arc { let body = db.body(def); Arc::new(ExprScopes::new(&*body)) } diff --git a/crates/ra_hir_def/src/child_by_source.rs b/crates/ra_hir_def/src/child_by_source.rs index 8b6c773ee..7009f21d1 100644 --- a/crates/ra_hir_def/src/child_by_source.rs +++ b/crates/ra_hir_def/src/child_by_source.rs @@ -17,11 +17,11 @@ use crate::{ }; pub trait ChildBySource { - fn child_by_source(&self, db: &impl DefDatabase) -> DynMap; + fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap; } impl ChildBySource for TraitId { - fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { + fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap { let mut res = DynMap::default(); let data = db.trait_data(*self); @@ -47,7 +47,7 @@ impl ChildBySource for TraitId { } impl ChildBySource for ImplId { - fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { + fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap { let mut res = DynMap::default(); let data = db.impl_data(*self); @@ -73,7 +73,7 @@ impl ChildBySource for ImplId { } impl ChildBySource for ModuleId { - fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { + fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap { let crate_def_map = db.crate_def_map(self.krate); let module_data = &crate_def_map[self.local_id]; module_data.scope.child_by_source(db) @@ -81,13 +81,13 @@ impl ChildBySource for ModuleId { } impl ChildBySource for ItemScope { - fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { + fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap { let mut res = DynMap::default(); self.declarations().for_each(|item| add_module_def(db, &mut res, item)); self.impls().for_each(|imp| add_impl(db, &mut res, imp)); return res; - fn add_module_def(db: &impl DefDatabase, map: &mut DynMap, item: ModuleDefId) { + fn add_module_def(db: &dyn DefDatabase, map: &mut DynMap, item: ModuleDefId) { match item { ModuleDefId::FunctionId(func) => { let src = func.lookup(db).source(db); @@ -126,7 +126,7 @@ impl ChildBySource for ItemScope { _ => (), } } - fn add_impl(db: &impl DefDatabase, map: &mut DynMap, imp: ImplId) { + fn add_impl(db: &dyn DefDatabase, map: &mut DynMap, imp: ImplId) { let src = imp.lookup(db).source(db); map[keys::IMPL].insert(src, imp) } @@ -134,7 +134,7 @@ impl ChildBySource for ItemScope { } impl ChildBySource for VariantId { - fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { + fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap { let mut res = DynMap::default(); let arena_map = self.child_source(db); @@ -155,7 +155,7 @@ impl ChildBySource for VariantId { } impl ChildBySource for EnumId { - fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { + fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap { let mut res = DynMap::default(); let arena_map = self.child_source(db); @@ -170,7 +170,7 @@ impl ChildBySource for EnumId { } impl ChildBySource for DefWithBodyId { - fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { + fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap { let body = db.body(*self); body.item_scope.child_by_source(db) } diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index c0b16b7fa..04bd4a305 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -105,7 +105,7 @@ pub struct TypeAliasData { impl TypeAliasData { pub(crate) fn type_alias_data_query( - db: &impl DefDatabase, + db: &dyn DefDatabase, typ: TypeAliasId, ) -> Arc { let loc = typ.lookup(db); @@ -127,7 +127,7 @@ pub struct TraitData { } impl TraitData { - pub(crate) fn trait_data_query(db: &impl DefDatabase, tr: TraitId) -> Arc { + pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc { let src = tr.lookup(db).source(db); let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); let auto = src.value.is_auto(); @@ -200,7 +200,7 @@ pub struct ImplData { } impl ImplData { - pub(crate) fn impl_data_query(db: &impl DefDatabase, id: ImplId) -> Arc { + pub(crate) fn impl_data_query(db: &dyn DefDatabase, id: ImplId) -> Arc { let _p = profile("impl_data_query"); let impl_loc = id.lookup(db); let src = impl_loc.source(db); @@ -235,20 +235,20 @@ pub struct ConstData { } impl ConstData { - pub(crate) fn const_data_query(db: &impl DefDatabase, konst: ConstId) -> Arc { + pub(crate) fn const_data_query(db: &dyn DefDatabase, konst: ConstId) -> Arc { let loc = konst.lookup(db); let node = loc.source(db); let vis_default = RawVisibility::default_for_container(loc.container); Arc::new(ConstData::new(db, vis_default, node)) } - pub(crate) fn static_data_query(db: &impl DefDatabase, konst: StaticId) -> Arc { + pub(crate) fn static_data_query(db: &dyn DefDatabase, konst: StaticId) -> Arc { let node = konst.lookup(db).source(db); Arc::new(ConstData::new(db, RawVisibility::private(), node)) } fn new( - db: &impl DefDatabase, + db: &dyn DefDatabase, vis_default: RawVisibility, node: InFile, ) -> ConstData { @@ -261,7 +261,7 @@ impl ConstData { } fn collect_impl_items_in_macros( - db: &impl DefDatabase, + db: &dyn DefDatabase, module_id: ModuleId, impl_def: &InFile, id: ImplId, @@ -280,7 +280,7 @@ fn collect_impl_items_in_macros( } fn collect_impl_items_in_macro( - db: &impl DefDatabase, + db: &dyn DefDatabase, expander: &mut Expander, m: ast::MacroCall, id: ImplId, @@ -312,7 +312,7 @@ fn collect_impl_items_in_macro( } fn collect_impl_items( - db: &impl DefDatabase, + db: &dyn DefDatabase, impl_items: impl Iterator, file_id: crate::HirFileId, id: ImplId, diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index dcd377aae..7f8c1ea21 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use hir_expand::{db::AstDatabase, HirFileId}; -use ra_db::{salsa, CrateId, SourceDatabase}; +use ra_db::{salsa, CrateId, SourceDatabase, Upcast}; use ra_prof::profile; use ra_syntax::SmolStr; @@ -43,7 +43,7 @@ pub trait InternDatabase: SourceDatabase { } #[salsa::query_group(DefDatabaseStorage)] -pub trait DefDatabase: InternDatabase + AstDatabase { +pub trait DefDatabase: InternDatabase + AstDatabase + Upcast { #[salsa::invoke(RawItems::raw_items_query)] fn raw_items(&self, file_id: HirFileId) -> Arc; @@ -109,6 +109,12 @@ pub trait DefDatabase: InternDatabase + AstDatabase { fn documentation(&self, def: AttrDefId) -> Option; } +// impl Upcast for T { +// fn upcast(&self) -> &dyn AstDatabase { +// &*self +// } +// } + fn crate_def_map_wait(db: &impl DefDatabase, krate: CrateId) -> Arc { let _p = profile("crate_def_map:wait"); db.crate_def_map_query(krate) diff --git a/crates/ra_hir_def/src/docs.rs b/crates/ra_hir_def/src/docs.rs index b29f142e3..0539a77d4 100644 --- a/crates/ra_hir_def/src/docs.rs +++ b/crates/ra_hir_def/src/docs.rs @@ -34,7 +34,7 @@ impl Documentation { } pub(crate) fn documentation_query( - db: &impl DefDatabase, + db: &dyn DefDatabase, def: AttrDefId, ) -> Option { match def { @@ -60,7 +60,7 @@ impl Documentation { docs_from_ast(&src.value[it.local_id]) } AttrDefId::TraitId(it) => docs_from_ast(&it.lookup(db).source(db).value), - AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id?.to_node(db)), + AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id?.to_node(db.upcast())), AttrDefId::ConstId(it) => docs_from_ast(&it.lookup(db).source(db).value), AttrDefId::StaticId(it) => docs_from_ast(&it.lookup(db).source(db).value), AttrDefId::FunctionId(it) => docs_from_ast(&it.lookup(db).source(db).value), diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs index 07ca74ec3..d58ac6ba5 100644 --- a/crates/ra_hir_def/src/find_path.rs +++ b/crates/ra_hir_def/src/find_path.rs @@ -44,12 +44,12 @@ impl ModPath { /// Find a path that can be used to refer to a certain item. This can depend on /// *from where* you're referring to the item, hence the `from` parameter. -pub fn find_path(db: &impl DefDatabase, item: ItemInNs, from: ModuleId) -> Option { +pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option { find_path_inner(db, item, from, MAX_PATH_LEN) } fn find_path_inner( - db: &impl DefDatabase, + db: &dyn DefDatabase, item: ItemInNs, from: ModuleId, max_len: usize, @@ -165,7 +165,7 @@ fn select_best_path(old_path: ModPath, new_path: ModPath) -> ModPath { } fn find_importable_locations( - db: &impl DefDatabase, + db: &dyn DefDatabase, item: ItemInNs, from: ModuleId, ) -> Vec<(ModuleId, Name)> { @@ -195,7 +195,7 @@ fn find_importable_locations( /// Note that the crate doesn't need to be the one in which the item is defined; /// it might be re-exported in other crates. fn importable_locations_in_crate( - db: &impl DefDatabase, + db: &dyn DefDatabase, item: ItemInNs, krate: CrateId, ) -> Vec<(ModuleId, Name, Visibility)> { diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index 519c60de0..24adc8153 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs @@ -69,7 +69,7 @@ type SourceMap = ArenaMap Arc { let _p = profile("generic_params_query"); @@ -77,7 +77,7 @@ impl GenericParams { Arc::new(params) } - fn new(db: &impl DefDatabase, def: GenericDefId) -> (GenericParams, InFile) { + fn new(db: &dyn DefDatabase, def: GenericDefId) -> (GenericParams, InFile) { let mut generics = GenericParams { types: Arena::default(), where_predicates: Vec::new() }; let mut sm = ArenaMap::default(); // FIXME: add `: Sized` bound for everything except for `Self` in traits @@ -242,14 +242,14 @@ impl GenericParams { impl HasChildSource for GenericDefId { type ChildId = LocalTypeParamId; type Value = Either; - fn child_source(&self, db: &impl DefDatabase) -> InFile { + fn child_source(&self, db: &dyn DefDatabase) -> InFile { let (_, sm) = GenericParams::new(db, *self); sm } } impl ChildBySource for GenericDefId { - fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { + fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap { let mut res = DynMap::default(); let arena_map = self.child_source(db); let arena_map = arena_map.as_ref(); diff --git a/crates/ra_hir_def/src/lang_item.rs b/crates/ra_hir_def/src/lang_item.rs index 6de49730e..01b367278 100644 --- a/crates/ra_hir_def/src/lang_item.rs +++ b/crates/ra_hir_def/src/lang_item.rs @@ -77,7 +77,7 @@ impl LangItems { } /// Salsa query. This will look for lang items in a specific crate. - pub(crate) fn crate_lang_items_query(db: &impl DefDatabase, krate: CrateId) -> Arc { + pub(crate) fn crate_lang_items_query(db: &dyn DefDatabase, krate: CrateId) -> Arc { let mut lang_items = LangItems::default(); let crate_def_map = db.crate_def_map(krate); @@ -92,7 +92,7 @@ impl LangItems { } pub(crate) fn module_lang_items_query( - db: &impl DefDatabase, + db: &dyn DefDatabase, module: ModuleId, ) -> Option> { let mut lang_items = LangItems::default(); @@ -107,7 +107,7 @@ impl LangItems { /// Salsa query. Look for a lang item, starting from the specified crate and recursively /// traversing its dependencies. pub(crate) fn lang_item_query( - db: &impl DefDatabase, + db: &dyn DefDatabase, start_crate: CrateId, item: SmolStr, ) -> Option { @@ -122,7 +122,7 @@ impl LangItems { .find_map(|dep| db.lang_item(dep.crate_id, item.clone())) } - fn collect_lang_items(&mut self, db: &impl DefDatabase, module: ModuleId) { + fn collect_lang_items(&mut self, db: &dyn DefDatabase, module: ModuleId) { // Look for impl targets let def_map = db.crate_def_map(module.krate); let module_data = &def_map[module.local_id]; @@ -152,7 +152,7 @@ impl LangItems { fn collect_lang_item( &mut self, - db: &impl DefDatabase, + db: &dyn DefDatabase, item: T, constructor: fn(T) -> LangItemTarget, ) where diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index a3d617e1f..24f9eb9e0 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -47,8 +47,8 @@ mod marks; use std::hash::Hash; use hir_expand::{ - ast_id_map::FileAstId, db::AstDatabase, eager::expand_eager_macro, hygiene::Hygiene, AstId, - HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, + ast_id_map::FileAstId, eager::expand_eager_macro, hygiene::Hygiene, AstId, HirFileId, InFile, + MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, }; use ra_arena::{impl_arena_id, RawId}; use ra_db::{impl_intern_key, salsa, CrateId}; @@ -87,14 +87,14 @@ macro_rules! impl_intern { impl Intern for $loc { type ID = $id; - fn intern(self, db: &impl db::DefDatabase) -> $id { + fn intern(self, db: &dyn db::DefDatabase) -> $id { db.$intern(self) } } impl Lookup for $id { type Data = $loc; - fn lookup(&self, db: &impl db::DefDatabase) -> $loc { + fn lookup(&self, db: &dyn db::DefDatabase) -> $loc { db.$lookup(*self) } } @@ -339,20 +339,20 @@ impl_froms!(VariantId: EnumVariantId, StructId, UnionId); trait Intern { type ID; - fn intern(self, db: &impl db::DefDatabase) -> Self::ID; + fn intern(self, db: &dyn db::DefDatabase) -> Self::ID; } pub trait Lookup { type Data; - fn lookup(&self, db: &impl db::DefDatabase) -> Self::Data; + fn lookup(&self, db: &dyn db::DefDatabase) -> Self::Data; } pub trait HasModule { - fn module(&self, db: &impl db::DefDatabase) -> ModuleId; + fn module(&self, db: &dyn db::DefDatabase) -> ModuleId; } impl HasModule for ContainerId { - fn module(&self, db: &impl db::DefDatabase) -> ModuleId { + fn module(&self, db: &dyn db::DefDatabase) -> ModuleId { match *self { ContainerId::ModuleId(it) => it, ContainerId::DefWithBodyId(it) => it.module(db), @@ -361,7 +361,7 @@ impl HasModule for ContainerId { } impl HasModule for AssocContainerId { - fn module(&self, db: &impl db::DefDatabase) -> ModuleId { + fn module(&self, db: &dyn db::DefDatabase) -> ModuleId { match *self { AssocContainerId::ContainerId(it) => it.module(db), AssocContainerId::ImplId(it) => it.lookup(db).container.module(db), @@ -371,13 +371,13 @@ impl HasModule for AssocContainerId { } impl HasModule for AssocItemLoc { - fn module(&self, db: &impl db::DefDatabase) -> ModuleId { + fn module(&self, db: &dyn db::DefDatabase) -> ModuleId { self.container.module(db) } } impl HasModule for AdtId { - fn module(&self, db: &impl db::DefDatabase) -> ModuleId { + fn module(&self, db: &dyn db::DefDatabase) -> ModuleId { match self { AdtId::StructId(it) => it.lookup(db).container, AdtId::UnionId(it) => it.lookup(db).container, @@ -388,7 +388,7 @@ impl HasModule for AdtId { } impl HasModule for DefWithBodyId { - fn module(&self, db: &impl db::DefDatabase) -> ModuleId { + fn module(&self, db: &dyn db::DefDatabase) -> ModuleId { match self { DefWithBodyId::FunctionId(it) => it.lookup(db).module(db), DefWithBodyId::StaticId(it) => it.lookup(db).module(db), @@ -398,7 +398,7 @@ impl HasModule for DefWithBodyId { } impl HasModule for GenericDefId { - fn module(&self, db: &impl db::DefDatabase) -> ModuleId { + fn module(&self, db: &dyn db::DefDatabase) -> ModuleId { match self { GenericDefId::FunctionId(it) => it.lookup(db).module(db), GenericDefId::AdtId(it) => it.module(db), @@ -412,7 +412,7 @@ impl HasModule for GenericDefId { } impl HasModule for StaticLoc { - fn module(&self, db: &impl db::DefDatabase) -> ModuleId { + fn module(&self, db: &dyn db::DefDatabase) -> ModuleId { self.container.module(db) } } @@ -421,7 +421,7 @@ impl HasModule for StaticLoc { pub trait AsMacroCall { fn as_call_id( &self, - db: &(impl db::DefDatabase + AstDatabase), + db: &dyn db::DefDatabase, resolver: impl Fn(path::ModPath) -> Option, ) -> Option; } @@ -429,11 +429,11 @@ pub trait AsMacroCall { impl AsMacroCall for InFile<&ast::MacroCall> { fn as_call_id( &self, - db: &(impl db::DefDatabase + AstDatabase), + db: &dyn db::DefDatabase, resolver: impl Fn(path::ModPath) -> Option, ) -> Option { let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value)); - let h = Hygiene::new(db, self.file_id); + let h = Hygiene::new(db.upcast(), self.file_id); let path = path::ModPath::from_src(self.value.path()?, &h)?; AstIdWithPath::new(ast_id.file_id, ast_id.value, path).as_call_id(db, resolver) @@ -456,23 +456,23 @@ impl AstIdWithPath { impl AsMacroCall for AstIdWithPath { fn as_call_id( &self, - db: &impl AstDatabase, + db: &dyn db::DefDatabase, resolver: impl Fn(path::ModPath) -> Option, ) -> Option { let def: MacroDefId = resolver(self.path.clone())?; if let MacroDefKind::BuiltInEager(_) = def.kind { - let macro_call = InFile::new(self.ast_id.file_id, self.ast_id.to_node(db)); - let hygiene = Hygiene::new(db, self.ast_id.file_id); + let macro_call = InFile::new(self.ast_id.file_id, self.ast_id.to_node(db.upcast())); + let hygiene = Hygiene::new(db.upcast(), self.ast_id.file_id); Some( - expand_eager_macro(db, macro_call, def, &|path: ast::Path| { + expand_eager_macro(db.upcast(), macro_call, def, &|path: ast::Path| { resolver(path::ModPath::from_src(path, &hygiene)?) })? .into(), ) } else { - Some(def.as_lazy_macro(db, MacroCallKind::FnLike(self.ast_id)).into()) + Some(def.as_lazy_macro(db.upcast(), MacroCallKind::FnLike(self.ast_id)).into()) } } } @@ -480,10 +480,10 @@ impl AsMacroCall for AstIdWithPath { impl AsMacroCall for AstIdWithPath { fn as_call_id( &self, - db: &impl AstDatabase, + db: &dyn db::DefDatabase, resolver: impl Fn(path::ModPath) -> Option, ) -> Option { let def = resolver(self.path.clone())?; - Some(def.as_lazy_macro(db, MacroCallKind::Attr(self.ast_id)).into()) + Some(def.as_lazy_macro(db.upcast(), MacroCallKind::Attr(self.ast_id)).into()) } } diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 03515309e..be53313ee 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -151,16 +151,17 @@ impl ModuleOrigin { /// Returns a node which defines this module. /// That is, a file or a `mod foo {}` with items. - fn definition_source(&self, db: &impl DefDatabase) -> InFile { + fn definition_source(&self, db: &dyn DefDatabase) -> InFile { match self { ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => { let file_id = *definition; let sf = db.parse(file_id).tree(); InFile::new(file_id.into(), ModuleSource::SourceFile(sf)) } - ModuleOrigin::Inline { definition } => { - InFile::new(definition.file_id, ModuleSource::Module(definition.to_node(db))) - } + ModuleOrigin::Inline { definition } => InFile::new( + definition.file_id, + ModuleSource::Module(definition.to_node(db.upcast())), + ), } } } @@ -176,7 +177,7 @@ pub struct ModuleData { } impl CrateDefMap { - pub(crate) fn crate_def_map_query(db: &impl DefDatabase, krate: CrateId) -> Arc { + pub(crate) fn crate_def_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc { let _p = profile("crate_def_map_query").detail(|| { db.crate_graph()[krate] .display_name @@ -204,7 +205,7 @@ impl CrateDefMap { pub fn add_diagnostics( &self, - db: &impl DefDatabase, + db: &dyn DefDatabase, module: LocalModuleId, sink: &mut DiagnosticSink, ) { @@ -220,7 +221,7 @@ impl CrateDefMap { pub(crate) fn resolve_path( &self, - db: &impl DefDatabase, + db: &dyn DefDatabase, original_module: LocalModuleId, path: &ModPath, shadow: BuiltinShadowMode, @@ -273,15 +274,15 @@ impl CrateDefMap { impl ModuleData { /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. - pub fn definition_source(&self, db: &impl DefDatabase) -> InFile { + pub fn definition_source(&self, db: &dyn DefDatabase) -> InFile { self.origin.definition_source(db) } /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. /// `None` for the crate root or block. - pub fn declaration_source(&self, db: &impl DefDatabase) -> Option> { + pub fn declaration_source(&self, db: &dyn DefDatabase) -> Option> { let decl = self.origin.declaration()?; - let value = decl.to_node(db); + let value = decl.to_node(db.upcast()); Some(InFile { file_id: decl.file_id, value }) } } @@ -311,7 +312,7 @@ mod diagnostics { impl DefDiagnostic { pub(super) fn add_to( &self, - db: &impl DefDatabase, + db: &dyn DefDatabase, target_module: LocalModuleId, sink: &mut DiagnosticSink, ) { @@ -320,7 +321,7 @@ mod diagnostics { if *module != target_module { return; } - let decl = declaration.to_node(db); + let decl = declaration.to_node(db.upcast()); sink.push(UnresolvedModule { file: declaration.file_id, decl: AstPtr::new(&decl), diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index db9838cb5..7a042e69f 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -30,7 +30,7 @@ use crate::{ TraitLoc, TypeAliasLoc, UnionLoc, }; -pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { +pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { let crate_graph = db.crate_graph(); // populate external prelude @@ -112,8 +112,8 @@ struct DeriveDirective { } /// Walks the tree of module recursively -struct DefCollector<'a, DB> { - db: &'a DB, +struct DefCollector<'a> { + db: &'a dyn DefDatabase, def_map: CrateDefMap, glob_imports: FxHashMap>, unresolved_imports: Vec, @@ -124,10 +124,7 @@ struct DefCollector<'a, DB> { cfg_options: &'a CfgOptions, } -impl DefCollector<'_, DB> -where - DB: DefDatabase, -{ +impl DefCollector<'_> { fn collect(&mut self) { let file_id = self.db.crate_graph()[self.def_map.krate].root_file_id; let raw_items = self.db.raw_items(file_id.into()); @@ -605,8 +602,8 @@ where } /// Walks a single module, populating defs, imports and macros -struct ModCollector<'a, D> { - def_collector: D, +struct ModCollector<'a, 'b> { + def_collector: &'a mut DefCollector<'b>, macro_depth: usize, module_id: LocalModuleId, file_id: HirFileId, @@ -614,10 +611,7 @@ struct ModCollector<'a, D> { mod_dir: ModDir, } -impl ModCollector<'_, &'_ mut DefCollector<'_, DB>> -where - DB: DefDatabase, -{ +impl ModCollector<'_, '_> { fn collect(&mut self, items: &[raw::RawItem]) { // Note: don't assert that inserted value is fresh: it's simply not true // for macros. @@ -950,7 +944,7 @@ mod tests { use super::*; - fn do_collect_defs(db: &impl DefDatabase, def_map: CrateDefMap) -> CrateDefMap { + fn do_collect_defs(db: &dyn DefDatabase, def_map: CrateDefMap) -> CrateDefMap { let mut collector = DefCollector { db, def_map, diff --git a/crates/ra_hir_def/src/nameres/mod_resolution.rs b/crates/ra_hir_def/src/nameres/mod_resolution.rs index 14fb8ba3a..386c5cade 100644 --- a/crates/ra_hir_def/src/nameres/mod_resolution.rs +++ b/crates/ra_hir_def/src/nameres/mod_resolution.rs @@ -40,12 +40,12 @@ impl ModDir { pub(super) fn resolve_declaration( &self, - db: &impl DefDatabase, + db: &dyn DefDatabase, file_id: HirFileId, name: &Name, attr_path: Option<&SmolStr>, ) -> Result<(FileId, ModDir), RelativePathBuf> { - let file_id = file_id.original_file(db); + let file_id = file_id.original_file(db.upcast()); let mut candidate_files = Vec::new(); match attr_to_path(attr_path) { diff --git a/crates/ra_hir_def/src/nameres/path_resolution.rs b/crates/ra_hir_def/src/nameres/path_resolution.rs index c058e70aa..35a0a0c98 100644 --- a/crates/ra_hir_def/src/nameres/path_resolution.rs +++ b/crates/ra_hir_def/src/nameres/path_resolution.rs @@ -70,7 +70,7 @@ impl CrateDefMap { pub(crate) fn resolve_visibility( &self, - db: &impl DefDatabase, + db: &dyn DefDatabase, original_module: LocalModuleId, visibility: &RawVisibility, ) -> Option { @@ -98,7 +98,7 @@ impl CrateDefMap { // the result. pub(super) fn resolve_path_fp_with_macro( &self, - db: &impl DefDatabase, + db: &dyn DefDatabase, mode: ResolveMode, original_module: LocalModuleId, path: &ModPath, @@ -262,7 +262,7 @@ impl CrateDefMap { fn resolve_name_in_module( &self, - db: &impl DefDatabase, + db: &dyn DefDatabase, module: LocalModuleId, name: &Name, shadow: BuiltinShadowMode, @@ -304,7 +304,7 @@ impl CrateDefMap { from_crate_root.or(from_extern_prelude) } - fn resolve_in_prelude(&self, db: &impl DefDatabase, name: &Name) -> PerNs { + fn resolve_in_prelude(&self, db: &dyn DefDatabase, name: &Name) -> PerNs { if let Some(prelude) = self.prelude { let keep; let def_map = if prelude.krate == self.krate { diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index ea3c00da8..0e4931f58 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -9,7 +9,6 @@ use std::{ops::Index, sync::Arc}; use hir_expand::{ ast_id_map::AstIdMap, - db::AstDatabase, hygiene::Hygiene, name::{AsName, Name}, }; @@ -45,16 +44,13 @@ pub struct RawItems { } impl RawItems { - pub(crate) fn raw_items_query( - db: &(impl DefDatabase + AstDatabase), - file_id: HirFileId, - ) -> Arc { + pub(crate) fn raw_items_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc { let _p = profile("raw_items_query"); let mut collector = RawItemsCollector { raw_items: RawItems::default(), source_ast_id_map: db.ast_id_map(file_id), file_id, - hygiene: Hygiene::new(db, file_id), + hygiene: Hygiene::new(db.upcast(), file_id), }; if let Some(node) = db.parse_or_expand(file_id) { if let Some(source_file) = ast::SourceFile::cast(node.clone()) { diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs index 123fae72a..717506358 100644 --- a/crates/ra_hir_def/src/resolver.rs +++ b/crates/ra_hir_def/src/resolver.rs @@ -96,7 +96,7 @@ pub enum ValueNs { impl Resolver { /// Resolve known trait from std, like `std::futures::Future` - pub fn resolve_known_trait(&self, db: &impl DefDatabase, path: &ModPath) -> Option { + pub fn resolve_known_trait(&self, db: &dyn DefDatabase, path: &ModPath) -> Option { let res = self.resolve_module_path(db, path, BuiltinShadowMode::Other).take_types()?; match res { ModuleDefId::TraitId(it) => Some(it), @@ -105,7 +105,7 @@ impl Resolver { } /// Resolve known struct from std, like `std::boxed::Box` - pub fn resolve_known_struct(&self, db: &impl DefDatabase, path: &ModPath) -> Option { + pub fn resolve_known_struct(&self, db: &dyn DefDatabase, path: &ModPath) -> Option { let res = self.resolve_module_path(db, path, BuiltinShadowMode::Other).take_types()?; match res { ModuleDefId::AdtId(AdtId::StructId(it)) => Some(it), @@ -114,7 +114,7 @@ impl Resolver { } /// Resolve known enum from std, like `std::result::Result` - pub fn resolve_known_enum(&self, db: &impl DefDatabase, path: &ModPath) -> Option { + pub fn resolve_known_enum(&self, db: &dyn DefDatabase, path: &ModPath) -> Option { let res = self.resolve_module_path(db, path, BuiltinShadowMode::Other).take_types()?; match res { ModuleDefId::AdtId(AdtId::EnumId(it)) => Some(it), @@ -124,7 +124,7 @@ impl Resolver { fn resolve_module_path( &self, - db: &impl DefDatabase, + db: &dyn DefDatabase, path: &ModPath, shadow: BuiltinShadowMode, ) -> PerNs { @@ -139,13 +139,13 @@ impl Resolver { module_res } - pub fn resolve_module_path_in_items(&self, db: &impl DefDatabase, path: &ModPath) -> PerNs { + pub fn resolve_module_path_in_items(&self, db: &dyn DefDatabase, path: &ModPath) -> PerNs { self.resolve_module_path(db, path, BuiltinShadowMode::Module) } pub fn resolve_path_in_type_ns( &self, - db: &impl DefDatabase, + db: &dyn DefDatabase, path: &ModPath, ) -> Option<(TypeNs, Option)> { let first_name = path.segments.first()?; @@ -222,7 +222,7 @@ impl Resolver { pub fn resolve_path_in_type_ns_fully( &self, - db: &impl DefDatabase, + db: &dyn DefDatabase, path: &ModPath, ) -> Option { let (res, unresolved) = self.resolve_path_in_type_ns(db, path)?; @@ -234,7 +234,7 @@ impl Resolver { pub fn resolve_visibility( &self, - db: &impl DefDatabase, + db: &dyn DefDatabase, visibility: &RawVisibility, ) -> Option { match visibility { @@ -251,7 +251,7 @@ impl Resolver { pub fn resolve_path_in_value_ns( &self, - db: &impl DefDatabase, + db: &dyn DefDatabase, path: &ModPath, ) -> Option { let n_segments = path.segments.len(); @@ -367,7 +367,7 @@ impl Resolver { pub fn resolve_path_in_value_ns_fully( &self, - db: &impl DefDatabase, + db: &dyn DefDatabase, path: &ModPath, ) -> Option { match self.resolve_path_in_value_ns(db, path)? { @@ -378,7 +378,7 @@ impl Resolver { pub fn resolve_path_as_macro( &self, - db: &impl DefDatabase, + db: &dyn DefDatabase, path: &ModPath, ) -> Option { // Search item scope legacy macro first @@ -390,13 +390,13 @@ impl Resolver { item_map.resolve_path(db, module, &path, BuiltinShadowMode::Other).0.take_macros() } - pub fn process_all_names(&self, db: &impl DefDatabase, f: &mut dyn FnMut(Name, ScopeDef)) { + pub fn process_all_names(&self, db: &dyn DefDatabase, f: &mut dyn FnMut(Name, ScopeDef)) { for scope in self.scopes.iter().rev() { scope.process_names(db, f); } } - pub fn traits_in_scope(&self, db: &impl DefDatabase) -> FxHashSet { + pub fn traits_in_scope(&self, db: &dyn DefDatabase) -> FxHashSet { let mut traits = FxHashSet::default(); for scope in &self.scopes { if let Scope::ModuleScope(m) = scope { @@ -474,7 +474,7 @@ pub enum ScopeDef { } impl Scope { - fn process_names(&self, db: &impl DefDatabase, f: &mut dyn FnMut(Name, ScopeDef)) { + fn process_names(&self, db: &dyn DefDatabase, f: &mut dyn FnMut(Name, ScopeDef)) { match self { Scope::ModuleScope(m) => { // FIXME: should we provide `self` here? @@ -534,13 +534,13 @@ impl Scope { } // needs arbitrary_self_types to be a method... or maybe move to the def? -pub fn resolver_for_expr(db: &impl DefDatabase, owner: DefWithBodyId, expr_id: ExprId) -> Resolver { +pub fn resolver_for_expr(db: &dyn DefDatabase, owner: DefWithBodyId, expr_id: ExprId) -> Resolver { let scopes = db.expr_scopes(owner); resolver_for_scope(db, owner, scopes.scope_for(expr_id)) } pub fn resolver_for_scope( - db: &impl DefDatabase, + db: &dyn DefDatabase, owner: DefWithBodyId, scope_id: Option, ) -> Resolver { @@ -560,7 +560,7 @@ impl Resolver { self } - fn push_generic_params_scope(self, db: &impl DefDatabase, def: GenericDefId) -> Resolver { + fn push_generic_params_scope(self, db: &dyn DefDatabase, def: GenericDefId) -> Resolver { let params = db.generic_params(def); self.push_scope(Scope::GenericParams { def, params }) } @@ -593,24 +593,24 @@ impl Resolver { pub trait HasResolver: Copy { /// Builds a resolver for type references inside this def. - fn resolver(self, db: &impl DefDatabase) -> Resolver; + fn resolver(self, db: &dyn DefDatabase) -> Resolver; } impl HasResolver for ModuleId { - fn resolver(self, db: &impl DefDatabase) -> Resolver { + fn resolver(self, db: &dyn DefDatabase) -> Resolver { let def_map = db.crate_def_map(self.krate); Resolver::default().push_module_scope(def_map, self.local_id) } } impl HasResolver for TraitId { - fn resolver(self, db: &impl DefDatabase) -> Resolver { + fn resolver(self, db: &dyn DefDatabase) -> Resolver { self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into()) } } impl + Copy> HasResolver for T { - fn resolver(self, db: &impl DefDatabase) -> Resolver { + fn resolver(self, db: &dyn DefDatabase) -> Resolver { let def = self.into(); def.module(db) .resolver(db) @@ -620,31 +620,31 @@ impl + Copy> HasResolver for T { } impl HasResolver for FunctionId { - fn resolver(self, db: &impl DefDatabase) -> Resolver { + fn resolver(self, db: &dyn DefDatabase) -> Resolver { self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into()) } } impl HasResolver for ConstId { - fn resolver(self, db: &impl DefDatabase) -> Resolver { + fn resolver(self, db: &dyn DefDatabase) -> Resolver { self.lookup(db).container.resolver(db) } } impl HasResolver for StaticId { - fn resolver(self, db: &impl DefDatabase) -> Resolver { + fn resolver(self, db: &dyn DefDatabase) -> Resolver { self.lookup(db).container.resolver(db) } } impl HasResolver for TypeAliasId { - fn resolver(self, db: &impl DefDatabase) -> Resolver { + fn resolver(self, db: &dyn DefDatabase) -> Resolver { self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into()) } } impl HasResolver for ImplId { - fn resolver(self, db: &impl DefDatabase) -> Resolver { + fn resolver(self, db: &dyn DefDatabase) -> Resolver { self.lookup(db) .container .resolver(db) @@ -654,7 +654,7 @@ impl HasResolver for ImplId { } impl HasResolver for DefWithBodyId { - fn resolver(self, db: &impl DefDatabase) -> Resolver { + fn resolver(self, db: &dyn DefDatabase) -> Resolver { match self { DefWithBodyId::ConstId(c) => c.resolver(db), DefWithBodyId::FunctionId(f) => f.resolver(db), @@ -664,7 +664,7 @@ impl HasResolver for DefWithBodyId { } impl HasResolver for ContainerId { - fn resolver(self, db: &impl DefDatabase) -> Resolver { + fn resolver(self, db: &dyn DefDatabase) -> Resolver { match self { ContainerId::ModuleId(it) => it.resolver(db), ContainerId::DefWithBodyId(it) => it.module(db).resolver(db), @@ -673,7 +673,7 @@ impl HasResolver for ContainerId { } impl HasResolver for AssocContainerId { - fn resolver(self, db: &impl DefDatabase) -> Resolver { + fn resolver(self, db: &dyn DefDatabase) -> Resolver { match self { AssocContainerId::ContainerId(it) => it.resolver(db), AssocContainerId::TraitId(it) => it.resolver(db), @@ -683,7 +683,7 @@ impl HasResolver for AssocContainerId { } impl HasResolver for GenericDefId { - fn resolver(self, db: &impl DefDatabase) -> Resolver { + fn resolver(self, db: &dyn DefDatabase) -> Resolver { match self { GenericDefId::FunctionId(inner) => inner.resolver(db), GenericDefId::AdtId(adt) => adt.resolver(db), @@ -697,7 +697,7 @@ impl HasResolver for GenericDefId { } impl HasResolver for VariantId { - fn resolver(self, db: &impl DefDatabase) -> Resolver { + fn resolver(self, db: &dyn DefDatabase) -> Resolver { match self { VariantId::EnumVariantId(it) => it.parent.resolver(db), VariantId::StructId(it) => it.resolver(db), diff --git a/crates/ra_hir_def/src/src.rs b/crates/ra_hir_def/src/src.rs index 499375b80..46e90da70 100644 --- a/crates/ra_hir_def/src/src.rs +++ b/crates/ra_hir_def/src/src.rs @@ -8,14 +8,14 @@ use crate::{db::DefDatabase, AssocItemLoc, ItemLoc}; pub trait HasSource { type Value; - fn source(&self, db: &impl DefDatabase) -> InFile; + fn source(&self, db: &dyn DefDatabase) -> InFile; } impl HasSource for AssocItemLoc { type Value = N; - fn source(&self, db: &impl DefDatabase) -> InFile { - let node = self.ast_id.to_node(db); + fn source(&self, db: &dyn DefDatabase) -> InFile { + let node = self.ast_id.to_node(db.upcast()); InFile::new(self.ast_id.file_id, node) } } @@ -23,8 +23,8 @@ impl HasSource for AssocItemLoc { impl HasSource for ItemLoc { type Value = N; - fn source(&self, db: &impl DefDatabase) -> InFile { - let node = self.ast_id.to_node(db); + fn source(&self, db: &dyn DefDatabase) -> InFile { + let node = self.ast_id.to_node(db.upcast()); InFile::new(self.ast_id.file_id, node) } } @@ -32,5 +32,5 @@ impl HasSource for ItemLoc { pub trait HasChildSource { type ChildId; type Value; - fn child_source(&self, db: &impl DefDatabase) -> InFile>; + fn child_source(&self, db: &dyn DefDatabase) -> InFile>; } diff --git a/crates/ra_hir_def/src/test_db.rs b/crates/ra_hir_def/src/test_db.rs index 0756916a8..eb83dee79 100644 --- a/crates/ra_hir_def/src/test_db.rs +++ b/crates/ra_hir_def/src/test_db.rs @@ -5,8 +5,12 @@ use std::{ sync::{Arc, Mutex}, }; +use hir_expand::db::AstDatabase; +use ra_db::{ + salsa, CrateId, ExternSourceId, FileId, FileLoader, FileLoaderDelegate, RelativePath, Upcast, +}; + use crate::db::DefDatabase; -use ra_db::{salsa, CrateId, ExternSourceId, FileId, FileLoader, FileLoaderDelegate, RelativePath}; #[salsa::database( ra_db::SourceDatabaseExtStorage, @@ -21,6 +25,18 @@ pub struct TestDB { events: Mutex>>>, } +impl Upcast for TestDB { + fn upcast(&self) -> &(dyn AstDatabase + 'static) { + &*self + } +} + +impl Upcast for TestDB { + fn upcast(&self) -> &(dyn DefDatabase + 'static) { + &*self + } +} + impl salsa::Database for TestDB { fn salsa_runtime(&self) -> &salsa::Runtime { &self.runtime diff --git a/crates/ra_hir_def/src/visibility.rs b/crates/ra_hir_def/src/visibility.rs index e0c59e905..62513873e 100644 --- a/crates/ra_hir_def/src/visibility.rs +++ b/crates/ra_hir_def/src/visibility.rs @@ -33,22 +33,22 @@ impl RawVisibility { } pub(crate) fn from_ast_with_default( - db: &impl DefDatabase, + db: &dyn DefDatabase, default: RawVisibility, node: InFile>, ) -> RawVisibility { Self::from_ast_with_hygiene_and_default( node.value, default, - &Hygiene::new(db, node.file_id), + &Hygiene::new(db.upcast(), node.file_id), ) } pub(crate) fn from_ast( - db: &impl DefDatabase, + db: &dyn DefDatabase, node: InFile>, ) -> RawVisibility { - Self::from_ast_with_hygiene(node.value, &Hygiene::new(db, node.file_id)) + Self::from_ast_with_hygiene(node.value, &Hygiene::new(db.upcast(), node.file_id)) } pub(crate) fn from_ast_with_hygiene( @@ -90,7 +90,7 @@ impl RawVisibility { pub fn resolve( &self, - db: &impl DefDatabase, + db: &dyn DefDatabase, resolver: &crate::resolver::Resolver, ) -> Visibility { // we fall back to public visibility (i.e. fail open) if the path can't be resolved @@ -108,7 +108,7 @@ pub enum Visibility { } impl Visibility { - pub fn is_visible_from(self, db: &impl DefDatabase, from_module: ModuleId) -> bool { + pub fn is_visible_from(self, db: &dyn DefDatabase, from_module: ModuleId) -> bool { let to_module = match self { Visibility::Module(m) => m, Visibility::Public => return true, -- cgit v1.2.3