From 5af9e475f4acce54f8383ed22febc412cf2541d0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 1 Jun 2019 21:17:57 +0300 Subject: add AstDatabase --- crates/ra_batch/src/lib.rs | 7 +++- crates/ra_hir/src/adt.rs | 16 +++++--- crates/ra_hir/src/code_model.rs | 70 ++++++++++++++++++++++++---------- crates/ra_hir/src/db.rs | 50 +++++++++++++----------- crates/ra_hir/src/generics.rs | 4 +- crates/ra_hir/src/ids.rs | 31 ++++++++------- crates/ra_hir/src/impl_block.rs | 17 ++++++--- crates/ra_hir/src/lang_item.rs | 13 +++++-- crates/ra_hir/src/lib.rs | 2 +- crates/ra_hir/src/mock.rs | 7 +++- crates/ra_hir/src/nameres.rs | 13 ++++--- crates/ra_hir/src/nameres/collector.rs | 15 +++++--- crates/ra_hir/src/nameres/raw.rs | 9 +++-- crates/ra_hir/src/source_id.rs | 8 ++-- crates/ra_hir/src/traits.rs | 10 ++++- crates/ra_hir/src/type_alias.rs | 7 +++- crates/ra_ide_api/src/db.rs | 5 ++- 17 files changed, 186 insertions(+), 98 deletions(-) diff --git a/crates/ra_batch/src/lib.rs b/crates/ra_batch/src/lib.rs index 0cafe617d..a4a8462de 100644 --- a/crates/ra_batch/src/lib.rs +++ b/crates/ra_batch/src/lib.rs @@ -16,7 +16,12 @@ use vfs_filter::IncludeRustFiles; type Result = std::result::Result; -#[salsa::database(ra_db::SourceDatabaseStorage, db::HirDatabaseStorage, db::DefDatabaseStorage)] +#[salsa::database( + ra_db::SourceDatabaseStorage, + db::AstDatabaseStorage, + db::DefDatabaseStorage, + db::HirDatabaseStorage +)] #[derive(Debug)] pub struct BatchDatabase { runtime: salsa::Runtime, diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index 5e5905f15..45a12dd4d 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -10,7 +10,7 @@ use ra_syntax::{ }; use crate::{ - Name, AsName, Struct, Union, Enum, EnumVariant, Crate, + Name, AsName, Struct, Union, Enum, EnumVariant, Crate, AstDatabase, HirDatabase, HirFileId, StructField, FieldSource, type_ref::TypeRef, DefDatabase, }; @@ -55,7 +55,10 @@ impl StructData { StructData { name, variant_data } } - pub(crate) fn struct_data_query(db: &impl DefDatabase, struct_: Struct) -> Arc { + pub(crate) fn struct_data_query( + db: &(impl DefDatabase + AstDatabase), + struct_: Struct, + ) -> Arc { let (_, struct_def) = struct_.source(db); Arc::new(StructData::new(&*struct_def)) } @@ -68,7 +71,7 @@ fn variants(enum_def: &ast::EnumDef) -> impl Iterator impl EnumVariant { pub(crate) fn source_impl( &self, - db: &impl DefDatabase, + db: &(impl DefDatabase + AstDatabase), ) -> (HirFileId, TreeArc) { let (file_id, enum_def) = self.parent.source(db); let var = variants(&*enum_def) @@ -91,7 +94,7 @@ pub struct EnumData { } impl EnumData { - pub(crate) fn enum_data_query(db: &impl DefDatabase, e: Enum) -> Arc { + pub(crate) fn enum_data_query(db: &(impl DefDatabase + AstDatabase), e: Enum) -> Arc { let (_file_id, enum_def) = e.source(db); let name = enum_def.name().map(|n| n.as_name()); let variants = variants(&*enum_def) @@ -198,7 +201,10 @@ impl VariantDef { } impl StructField { - pub(crate) fn source_impl(&self, db: &impl DefDatabase) -> (HirFileId, FieldSource) { + pub(crate) fn source_impl( + &self, + db: &(impl DefDatabase + AstDatabase), + ) -> (HirFileId, FieldSource) { let var_data = self.parent.variant_data(db); let fields = var_data.fields().unwrap(); let ss; diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index e3c765674..9c02b3995 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -5,7 +5,7 @@ use ra_syntax::{ast::{self, NameOwner, TypeAscriptionOwner}, TreeArc}; use crate::{ Name, AsName, AstId, Ty, HirFileId, Either, KnownName, - HirDatabase, DefDatabase, + HirDatabase, DefDatabase, AstDatabase, type_ref::TypeRef, nameres::{ModuleScope, Namespace, ImportId, CrateModuleId}, expr::{Body, BodySourceMap, validation::ExprValidator}, @@ -147,7 +147,7 @@ pub enum ModuleSource { impl ModuleSource { pub(crate) fn new( - db: &impl DefDatabase, + db: &(impl DefDatabase + AstDatabase), file_id: Option, decl_id: Option>, ) -> ModuleSource { @@ -168,7 +168,7 @@ impl ModuleSource { impl Module { /// Name of this module. - pub fn name(self, db: &impl HirDatabase) -> Option { + pub fn name(self, db: &impl DefDatabase) -> Option { let def_map = db.crate_def_map(self.krate); let parent = def_map[self.module_id].parent?; def_map[parent].children.iter().find_map(|(name, module_id)| { @@ -181,7 +181,10 @@ impl Module { } /// 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) -> (HirFileId, ModuleSource) { + pub fn definition_source( + self, + db: &(impl DefDatabase + AstDatabase), + ) -> (HirFileId, ModuleSource) { let def_map = db.crate_def_map(self.krate); let decl_id = def_map[self.module_id].declaration; let file_id = def_map[self.module_id].definition; @@ -340,7 +343,7 @@ impl StructField { self.parent.variant_data(db).fields().unwrap()[self.id].name.clone() } - pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, FieldSource) { + pub fn source(&self, db: &(impl DefDatabase + AstDatabase)) -> (HirFileId, FieldSource) { self.source_impl(db) } @@ -368,7 +371,10 @@ pub struct Struct { } impl Struct { - pub fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { + pub fn source( + self, + db: &(impl DefDatabase + AstDatabase), + ) -> (HirFileId, TreeArc) { self.id.source(db) } @@ -376,7 +382,7 @@ impl Struct { self.id.module(db) } - pub fn name(self, db: &impl HirDatabase) -> Option { + pub fn name(self, db: &impl DefDatabase) -> Option { db.struct_data(self).name.clone() } @@ -432,11 +438,14 @@ pub struct Union { } impl Union { - pub fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { + pub fn source( + self, + db: &(impl DefDatabase + AstDatabase), + ) -> (HirFileId, TreeArc) { self.id.source(db) } - pub fn name(self, db: &impl HirDatabase) -> Option { + pub fn name(self, db: &impl DefDatabase) -> Option { db.struct_data(Struct { id: self.id }).name.clone() } @@ -468,7 +477,10 @@ pub struct Enum { } impl Enum { - pub fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { + pub fn source( + self, + db: &(impl DefDatabase + AstDatabase), + ) -> (HirFileId, TreeArc) { self.id.source(db) } @@ -476,7 +488,7 @@ impl Enum { self.id.module(db) } - pub fn name(self, db: &impl HirDatabase) -> Option { + pub fn name(self, db: &impl DefDatabase) -> Option { db.enum_data(self).name.clone() } @@ -521,7 +533,10 @@ pub struct EnumVariant { } impl EnumVariant { - pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { + pub fn source( + &self, + db: &(impl DefDatabase + AstDatabase), + ) -> (HirFileId, TreeArc) { self.source_impl(db) } pub fn module(&self, db: &impl HirDatabase) -> Module { @@ -610,7 +625,10 @@ pub struct FnSignature { } impl FnSignature { - pub(crate) fn fn_signature_query(db: &impl DefDatabase, func: Function) -> Arc { + pub(crate) fn fn_signature_query( + db: &(impl DefDatabase + AstDatabase), + func: Function, + ) -> Arc { let (_, node) = func.source(db); let name = node.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); let mut params = Vec::new(); @@ -668,7 +686,7 @@ impl FnSignature { } impl Function { - pub fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { + pub fn source(self, db: &(impl DefDatabase + AstDatabase)) -> (HirFileId, TreeArc) { self.id.source(db) } @@ -752,7 +770,10 @@ pub struct Const { } impl Const { - pub fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { + pub fn source( + self, + db: &(impl DefDatabase + AstDatabase), + ) -> (HirFileId, TreeArc) { self.id.source(db) } @@ -809,7 +830,7 @@ impl ConstSignature { } pub(crate) fn const_signature_query( - db: &impl DefDatabase, + db: &(impl DefDatabase + AstDatabase), konst: Const, ) -> Arc { let (_, node) = konst.source(db); @@ -817,7 +838,7 @@ impl ConstSignature { } pub(crate) fn static_signature_query( - db: &impl DefDatabase, + db: &(impl DefDatabase + AstDatabase), konst: Static, ) -> Arc { let (_, node) = konst.source(db); @@ -838,7 +859,10 @@ pub struct Static { } impl Static { - pub fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { + pub fn source( + self, + db: &(impl DefDatabase + AstDatabase), + ) -> (HirFileId, TreeArc) { self.id.source(db) } @@ -873,7 +897,10 @@ pub struct Trait { } impl Trait { - pub fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { + pub fn source( + self, + db: &(impl DefDatabase + AstDatabase), + ) -> (HirFileId, TreeArc) { self.id.source(db) } @@ -922,7 +949,10 @@ pub struct TypeAlias { } impl TypeAlias { - pub fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { + pub fn source( + self, + db: &(impl DefDatabase + AstDatabase), + ) -> (HirFileId, TreeArc) { self.id.source(db) } diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index e8b4f0ddc..872103219 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -21,10 +21,35 @@ use crate::{ lang_item::{LangItems, LangItemTarget}, }; -#[salsa::query_group(DefDatabaseStorage)] -pub trait DefDatabase: SourceDatabase { +// This database has access to source code, so queries here are not really +// incremental. +#[salsa::query_group(AstDatabaseStorage)] +pub trait AstDatabase: SourceDatabase { #[salsa::interned] fn intern_macro(&self, macro_call: MacroCallLoc) -> ids::MacroCallId; + + #[salsa::invoke(crate::source_id::AstIdMap::ast_id_map_query)] + fn ast_id_map(&self, file_id: HirFileId) -> Arc; + #[salsa::transparent] + #[salsa::invoke(crate::source_id::AstIdMap::file_item_query)] + fn ast_id_to_node(&self, file_id: HirFileId, ast_id: ErasedFileAstId) -> TreeArc; + #[salsa::invoke(crate::ids::HirFileId::parse_or_expand_query)] + fn parse_or_expand(&self, file_id: HirFileId) -> Option>; + + #[salsa::invoke(crate::ids::macro_def_query)] + fn macro_def(&self, macro_id: MacroDefId) -> Option>; + + #[salsa::invoke(crate::ids::macro_arg_query)] + fn macro_arg(&self, macro_call: ids::MacroCallId) -> Option>; + + #[salsa::invoke(crate::ids::macro_expand_query)] + fn macro_expand(&self, macro_call: ids::MacroCallId) -> Result, String>; +} + +// This database uses `AstDatabase` internally, +#[salsa::query_group(DefDatabaseStorage)] +#[salsa::requires(AstDatabase)] +pub trait DefDatabase: SourceDatabase { #[salsa::interned] fn intern_function(&self, loc: ids::ItemLoc) -> ids::FunctionId; #[salsa::interned] @@ -46,18 +71,6 @@ pub trait DefDatabase: SourceDatabase { #[salsa::interned] fn intern_impl_block(&self, impl_block: ImplBlock) -> ids::GlobalImplId; - #[salsa::invoke(crate::ids::macro_def_query)] - fn macro_def(&self, macro_id: MacroDefId) -> Option>; - - #[salsa::invoke(crate::ids::macro_arg_query)] - fn macro_arg(&self, macro_call: ids::MacroCallId) -> Option>; - - #[salsa::invoke(crate::ids::macro_expand_query)] - fn macro_expand(&self, macro_call: ids::MacroCallId) -> Result, String>; - - #[salsa::invoke(crate::ids::HirFileId::parse_or_expand_query)] - fn parse_or_expand(&self, file_id: HirFileId) -> Option>; - #[salsa::invoke(crate::adt::StructData::struct_data_query)] fn struct_data(&self, s: Struct) -> Arc; @@ -70,13 +83,6 @@ pub trait DefDatabase: SourceDatabase { #[salsa::invoke(crate::traits::TraitItemsIndex::trait_items_index)] fn trait_items_index(&self, module: Module) -> crate::traits::TraitItemsIndex; - #[salsa::invoke(crate::source_id::AstIdMap::ast_id_map_query)] - fn ast_id_map(&self, file_id: HirFileId) -> Arc; - - #[salsa::invoke(crate::source_id::AstIdMap::file_item_query)] - #[salsa::transparent] - fn ast_id_to_node(&self, file_id: HirFileId, ast_id: ErasedFileAstId) -> TreeArc; - #[salsa::invoke(RawItems::raw_items_query)] fn raw_items(&self, file_id: HirFileId) -> Arc; @@ -121,7 +127,7 @@ pub trait DefDatabase: SourceDatabase { } #[salsa::query_group(HirDatabaseStorage)] -pub trait HirDatabase: DefDatabase { +pub trait HirDatabase: DefDatabase + AstDatabase { #[salsa::invoke(ExprScopes::expr_scopes_query)] fn expr_scopes(&self, def: DefWithBody) -> Arc; diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs index 8effbbe35..b6c5e18d3 100644 --- a/crates/ra_hir/src/generics.rs +++ b/crates/ra_hir/src/generics.rs @@ -8,7 +8,7 @@ use std::sync::Arc; use ra_syntax::ast::{self, NameOwner, TypeParamsOwner, TypeBoundsOwner, DefaultTypeParamOwner}; use crate::{ - db::{ HirDatabase, DefDatabase}, + db::{HirDatabase, DefDatabase, AstDatabase}, Name, AsName, Function, Struct, Union, Enum, Trait, TypeAlias, ImplBlock, Container, path::Path, type_ref::TypeRef, AdtDef }; @@ -52,7 +52,7 @@ impl_froms!(GenericDef: Function, Struct, Union, Enum, Trait, TypeAlias, ImplBlo impl GenericParams { pub(crate) fn generic_params_query( - db: &impl DefDatabase, + db: &(impl DefDatabase + AstDatabase), def: GenericDef, ) -> Arc { let mut generics = GenericParams::default(); diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index 06b6888f4..79e32e579 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -9,7 +9,7 @@ use ra_prof::profile; use mbe::MacroRules; use crate::{ - Module, DefDatabase, AstId, FileAstId, + Module, DefDatabase, AstId, FileAstId, AstDatabase, }; /// hir makes heavy use of ids: integer (u32) handlers to various things. You @@ -37,7 +37,7 @@ pub struct HirFileId(HirFileIdRepr); impl HirFileId { /// For macro-expansion files, returns the file original source file the /// expansion originated from. - pub fn original_file(self, db: &impl DefDatabase) -> FileId { + pub fn original_file(self, db: &impl AstDatabase) -> FileId { match self.0 { HirFileIdRepr::File(file_id) => file_id, HirFileIdRepr::Macro(macro_file) => { @@ -58,7 +58,7 @@ impl HirFileId { } pub(crate) fn parse_or_expand_query( - db: &impl DefDatabase, + db: &impl AstDatabase, file_id: HirFileId, ) -> Option> { db.check_canceled(); @@ -120,7 +120,7 @@ impl From for HirFileId { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct MacroDefId(pub(crate) AstId); -pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option> { +pub(crate) fn macro_def_query(db: &impl AstDatabase, id: MacroDefId) -> Option> { let macro_call = id.0.to_node(db); let arg = macro_call.token_tree()?; let (tt, _) = mbe::ast_to_token_tree(arg).or_else(|| { @@ -134,7 +134,7 @@ pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option Option> { +pub(crate) fn macro_arg_query(db: &impl AstDatabase, id: MacroCallId) -> Option> { let loc = id.loc(db); let macro_call = loc.ast_id.to_node(db); let arg = macro_call.token_tree()?; @@ -143,7 +143,7 @@ pub(crate) fn macro_arg_query(db: &impl DefDatabase, id: MacroCallId) -> Option< } pub(crate) fn macro_expand_query( - db: &impl DefDatabase, + db: &impl AstDatabase, id: MacroCallId, ) -> Result, String> { let loc = id.loc(db); @@ -185,7 +185,7 @@ pub struct MacroCallLoc { } impl MacroCallId { - pub(crate) fn loc(self, db: &impl DefDatabase) -> MacroCallLoc { + pub(crate) fn loc(self, db: &impl AstDatabase) -> MacroCallLoc { db.lookup_intern_macro(self) } @@ -196,7 +196,7 @@ impl MacroCallId { } impl MacroCallLoc { - pub(crate) fn id(self, db: &impl DefDatabase) -> MacroCallId { + pub(crate) fn id(self, db: &impl AstDatabase) -> MacroCallId { db.intern_macro(self) } } @@ -233,7 +233,7 @@ pub(crate) struct LocationCtx { file_id: HirFileId, } -impl<'a, DB: DefDatabase> LocationCtx<&'a DB> { +impl<'a, DB: DefDatabase + AstDatabase> LocationCtx<&'a DB> { pub(crate) fn new(db: &'a DB, module: Module, file_id: HirFileId) -> LocationCtx<&'a DB> { LocationCtx { db, module, file_id } } @@ -250,16 +250,19 @@ pub(crate) trait AstItemDef: salsa::InternKey + Clone { fn intern(db: &impl DefDatabase, loc: ItemLoc) -> Self; fn lookup_intern(self, db: &impl DefDatabase) -> ItemLoc; - fn from_ast(ctx: LocationCtx<&impl DefDatabase>, ast: &N) -> Self { + fn from_ast(ctx: LocationCtx<&(impl AstDatabase + DefDatabase)>, ast: &N) -> Self { let items = ctx.db.ast_id_map(ctx.file_id); let item_id = items.ast_id(ast); Self::from_ast_id(ctx, item_id) } - fn from_ast_id(ctx: LocationCtx<&impl DefDatabase>, ast_id: FileAstId) -> Self { + fn from_ast_id( + ctx: LocationCtx<&(impl AstDatabase + DefDatabase)>, + ast_id: FileAstId, + ) -> Self { let loc = ItemLoc { module: ctx.module, ast_id: ast_id.with_file_id(ctx.file_id) }; Self::intern(ctx.db, loc) } - fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { + fn source(self, db: &(impl AstDatabase + DefDatabase)) -> (HirFileId, TreeArc) { let loc = self.lookup_intern(db); let ast = loc.ast_id.to_node(db); (loc.ast_id.file_id(), ast) @@ -356,7 +359,7 @@ impl AstItemDef for TypeAliasId { } impl MacroCallId { - pub fn debug_dump(&self, db: &impl DefDatabase) -> String { + pub fn debug_dump(&self, db: &impl AstDatabase) -> String { let loc = self.clone().loc(db); let node = loc.ast_id.to_node(db); let syntax_str = node.syntax().text().chunks().collect::>().join(" "); @@ -367,7 +370,7 @@ impl MacroCallId { let macro_rules = db.macro_def(loc.def); format!( - "macro call [file: {:#?}] : {}\nhas rules: {}", + "macro call [file: {:?}] : {}\nhas rules: {}", db.file_relative_path(original), syntax_str, macro_rules.is_some() diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index 072670444..ba90e67e9 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs @@ -8,7 +8,7 @@ use ra_syntax::{ }; use crate::{ - Const, TypeAlias, Function, HirFileId, + Const, TypeAlias, Function, HirFileId, AstDatabase, HirDatabase, DefDatabase, TraitRef, type_ref::TypeRef, ids::LocationCtx, @@ -58,7 +58,10 @@ impl ImplBlock { } /// Returns the syntax of the impl block - pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { + pub fn source( + &self, + db: &(impl DefDatabase + AstDatabase), + ) -> (HirFileId, TreeArc) { let source_map = db.impls_in_module_with_source_map(self.module).1; let (file_id, source) = self.module.definition_source(db); (file_id, source_map.get(&source, self.impl_id)) @@ -117,7 +120,7 @@ pub struct ImplData { impl ImplData { pub(crate) fn from_ast( - db: &impl DefDatabase, + db: &(impl DefDatabase + AstDatabase), file_id: HirFileId, module: Module, node: &ast::ImplBlock, @@ -187,7 +190,11 @@ pub struct ModuleImplBlocks { } impl ModuleImplBlocks { - fn collect(db: &impl DefDatabase, module: Module, source_map: &mut ImplSourceMap) -> Self { + fn collect( + db: &(impl DefDatabase + AstDatabase), + module: Module, + source_map: &mut ImplSourceMap, + ) -> Self { let mut m = ModuleImplBlocks { module, impls: Arena::default(), @@ -218,7 +225,7 @@ impl ModuleImplBlocks { } pub(crate) fn impls_in_module_with_source_map_query( - db: &impl DefDatabase, + db: &(impl DefDatabase + AstDatabase), module: Module, ) -> (Arc, Arc) { let mut source_map = ImplSourceMap::default(); diff --git a/crates/ra_hir/src/lang_item.rs b/crates/ra_hir/src/lang_item.rs index 5f3f91cba..4cacaeba4 100644 --- a/crates/ra_hir/src/lang_item.rs +++ b/crates/ra_hir/src/lang_item.rs @@ -4,7 +4,7 @@ use rustc_hash::FxHashMap; use ra_syntax::{SmolStr, ast::AttrsOwner}; use crate::{ - Crate, DefDatabase, Enum, Function, HirDatabase, ImplBlock, Module, Static, Struct, Trait + Crate, DefDatabase, Enum, Function, HirDatabase, ImplBlock, Module, Static, Struct, Trait, AstDatabase, }; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -41,7 +41,10 @@ impl LangItems { } /// Salsa query. This will look for lang items in a specific crate. - pub(crate) fn lang_items_query(db: &impl DefDatabase, krate: Crate) -> Arc { + pub(crate) fn lang_items_query( + db: &(impl DefDatabase + AstDatabase), + krate: Crate, + ) -> Arc { let mut lang_items = LangItems { items: FxHashMap::default() }; if let Some(module) = krate.root_module(db) { @@ -74,7 +77,11 @@ impl LangItems { } } - fn collect_lang_items_recursive(&mut self, db: &impl DefDatabase, module: &Module) { + fn collect_lang_items_recursive( + &mut self, + db: &(impl DefDatabase + AstDatabase), + module: &Module, + ) { // Look for impl targets let (impl_blocks, source_map) = db.impls_in_module_with_source_map(module.clone()); let source = module.definition_source(db).1; diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 3e00eea26..f2a31795d 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -48,7 +48,7 @@ mod code_model; mod marks; use crate::{ - db::{HirDatabase, DefDatabase}, + db::{AstDatabase, DefDatabase, HirDatabase}, name::{AsName, KnownName}, source_id::{FileAstId, AstId}, resolve::Resolver, diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index b84cb7503..5d38ac76c 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs @@ -13,7 +13,12 @@ use crate::{db, diagnostics::DiagnosticSink}; pub const WORKSPACE: SourceRootId = SourceRootId(0); -#[salsa::database(ra_db::SourceDatabaseStorage, db::HirDatabaseStorage, db::DefDatabaseStorage)] +#[salsa::database( + ra_db::SourceDatabaseStorage, + db::AstDatabaseStorage, + db::DefDatabaseStorage, + db::HirDatabaseStorage +)] #[derive(Debug)] pub struct MockDatabase { events: Mutex>>>, diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index 6b1160aa7..d649aa820 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -65,7 +65,7 @@ use ra_prof::profile; use once_cell::sync::Lazy; use crate::{ - ModuleDef, Name, Crate, Module, MacroDef, AsName, BuiltinType, + ModuleDef, Name, Crate, Module, MacroDef, AsName, BuiltinType, AstDatabase, DefDatabase, Path, PathKind, HirFileId, Trait, ids::MacroDefId, diagnostics::DiagnosticSink, @@ -232,7 +232,10 @@ fn or(left: ItemOrMacro, right: ItemOrMacro) -> ItemOrMacro { } impl CrateDefMap { - pub(crate) fn crate_def_map_query(db: &impl DefDatabase, krate: Crate) -> Arc { + pub(crate) fn crate_def_map_query( + db: &(impl DefDatabase + AstDatabase), + krate: Crate, + ) -> Arc { db.check_canceled(); let _p = profile("crate_def_map_query"); let def_map = { @@ -278,7 +281,7 @@ impl CrateDefMap { pub(crate) fn add_diagnostics( &self, - db: &impl DefDatabase, + db: &(impl DefDatabase + AstDatabase), module: CrateModuleId, sink: &mut DiagnosticSink, ) { @@ -534,7 +537,7 @@ mod diagnostics { use ra_syntax::{AstPtr, ast}; use crate::{ - AstId, DefDatabase, + AstId, DefDatabase, AstDatabase, nameres::CrateModuleId, diagnostics::{DiagnosticSink, UnresolvedModule} }; @@ -551,7 +554,7 @@ mod diagnostics { impl DefDiagnostic { pub(super) fn add_to( &self, - db: &impl DefDatabase, + db: &(impl DefDatabase + AstDatabase), target_module: CrateModuleId, sink: &mut DiagnosticSink, ) { diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index af8ac064c..693c3fe8e 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -8,7 +8,7 @@ use ra_syntax::ast; use crate::{ Function, Module, Struct, Union, Enum, Const, Static, Trait, TypeAlias, MacroDef, - DefDatabase, HirFileId, Name, Path, + DefDatabase, HirFileId, Name, Path, AstDatabase, KnownName, nameres::{ Resolution, PerNs, ModuleDef, ReachedFixedPoint, ResolveMode, @@ -20,7 +20,10 @@ use crate::{ AstId, }; -pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { +pub(super) fn collect_defs( + db: &(impl DefDatabase + AstDatabase), + mut def_map: CrateDefMap, +) -> CrateDefMap { // populate external prelude for dep in def_map.krate.dependencies(db) { log::debug!("crate dep {:?} -> {:?}", dep.name, dep.krate); @@ -93,7 +96,7 @@ struct DefCollector { impl<'a, DB> DefCollector<&'a DB> where - DB: DefDatabase, + DB: DefDatabase + AstDatabase, { fn collect(&mut self) { let crate_graph = self.db.crate_graph(); @@ -470,7 +473,7 @@ struct ModCollector<'a, D> { impl ModCollector<'_, &'_ mut DefCollector<&'_ DB>> where - DB: DefDatabase, + DB: DefDatabase + AstDatabase, { fn collect(&mut self, items: &[raw::RawItem]) { for item in items { @@ -615,7 +618,7 @@ fn is_macro_rules(path: &Path) -> bool { } fn resolve_submodule( - db: &impl DefDatabase, + db: &(impl DefDatabase + AstDatabase), file_id: HirFileId, name: &Name, is_root: bool, @@ -658,7 +661,7 @@ mod tests { use rustc_hash::FxHashSet; fn do_collect_defs( - db: &impl DefDatabase, + db: &(impl DefDatabase + AstDatabase), def_map: CrateDefMap, monitor: MacroStackMonitor, ) -> CrateDefMap { diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs index 1b4dcbb7a..21935dbb9 100644 --- a/crates/ra_hir/src/nameres/raw.rs +++ b/crates/ra_hir/src/nameres/raw.rs @@ -7,7 +7,7 @@ use ra_syntax::{ ast::{self, NameOwner, AttrsOwner}, }; -use crate::{DefDatabase, Name, AsName, Path, HirFileId, ModuleSource, AstIdMap, FileAstId, Either}; +use crate::{DefDatabase, Name, AsName, Path, HirFileId, ModuleSource, AstIdMap, FileAstId, Either, AstDatabase}; /// `RawItems` is a set of top-level items in a file (except for impls). /// @@ -56,12 +56,15 @@ impl ImportSourceMap { } impl RawItems { - pub(crate) fn raw_items_query(db: &impl DefDatabase, file_id: HirFileId) -> Arc { + pub(crate) fn raw_items_query( + db: &(impl DefDatabase + AstDatabase), + file_id: HirFileId, + ) -> Arc { db.raw_items_with_source_map(file_id).0 } pub(crate) fn raw_items_with_source_map_query( - db: &impl DefDatabase, + db: &(impl DefDatabase + AstDatabase), file_id: HirFileId, ) -> (Arc, Arc) { let mut collector = RawItemsCollector { diff --git a/crates/ra_hir/src/source_id.rs b/crates/ra_hir/src/source_id.rs index 13f548eaf..986269e00 100644 --- a/crates/ra_hir/src/source_id.rs +++ b/crates/ra_hir/src/source_id.rs @@ -3,7 +3,7 @@ use std::{marker::PhantomData, sync::Arc, hash::{Hash, Hasher}}; use ra_arena::{Arena, RawId, impl_arena_id}; use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, AstNode, ast}; -use crate::{HirFileId, DefDatabase}; +use crate::{HirFileId, AstDatabase}; /// `AstId` points to an AST node in any file. /// @@ -38,7 +38,7 @@ impl AstId { self.file_id } - pub(crate) fn to_node(&self, db: &impl DefDatabase) -> TreeArc { + pub(crate) fn to_node(&self, db: &impl AstDatabase) -> TreeArc { let syntax_node = db.ast_id_to_node(self.file_id, self.file_ast_id.raw); N::cast(&syntax_node).unwrap().to_owned() } @@ -87,7 +87,7 @@ pub struct AstIdMap { } impl AstIdMap { - pub(crate) fn ast_id_map_query(db: &impl DefDatabase, file_id: HirFileId) -> Arc { + pub(crate) fn ast_id_map_query(db: &impl AstDatabase, file_id: HirFileId) -> Arc { let map = if let Some(node) = db.parse_or_expand(file_id) { AstIdMap::from_source(&*node) } else { @@ -97,7 +97,7 @@ impl AstIdMap { } pub(crate) fn file_item_query( - db: &impl DefDatabase, + db: &impl AstDatabase, file_id: HirFileId, ast_id: ErasedFileAstId, ) -> TreeArc { diff --git a/crates/ra_hir/src/traits.rs b/crates/ra_hir/src/traits.rs index dfe883fa4..2a7c2b791 100644 --- a/crates/ra_hir/src/traits.rs +++ b/crates/ra_hir/src/traits.rs @@ -5,7 +5,10 @@ use rustc_hash::FxHashMap; use ra_syntax::ast::{self, NameOwner}; -use crate::{Function, Const, TypeAlias, Name, DefDatabase, Trait, ids::LocationCtx, name::AsName, Module}; +use crate::{ + Function, Const, TypeAlias, Name, DefDatabase, Trait, AstDatabase, Module, + ids::LocationCtx, name::AsName, +}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct TraitData { @@ -15,7 +18,10 @@ pub struct TraitData { } impl TraitData { - pub(crate) fn trait_data_query(db: &impl DefDatabase, tr: Trait) -> Arc { + pub(crate) fn trait_data_query( + db: &(impl DefDatabase + AstDatabase), + tr: Trait, + ) -> Arc { let (file_id, node) = tr.source(db); let name = node.name().map(|n| n.as_name()); let module = tr.module(db); diff --git a/crates/ra_hir/src/type_alias.rs b/crates/ra_hir/src/type_alias.rs index e35adcb2f..970468e3c 100644 --- a/crates/ra_hir/src/type_alias.rs +++ b/crates/ra_hir/src/type_alias.rs @@ -2,9 +2,12 @@ use std::sync::Arc; -use crate::{TypeAlias, db::DefDatabase, type_ref::TypeRef}; +use crate::{TypeAlias, DefDatabase, AstDatabase, type_ref::TypeRef}; -pub(crate) fn type_alias_ref_query(db: &impl DefDatabase, typ: TypeAlias) -> Arc { +pub(crate) fn type_alias_ref_query( + db: &(impl DefDatabase + AstDatabase), + typ: TypeAlias, +) -> Arc { let (_, node) = typ.source(db); Arc::new(TypeRef::from_ast_opt(node.type_ref())) } diff --git a/crates/ra_ide_api/src/db.rs b/crates/ra_ide_api/src/db.rs index d84a0e7be..d1a452ecb 100644 --- a/crates/ra_ide_api/src/db.rs +++ b/crates/ra_ide_api/src/db.rs @@ -14,8 +14,9 @@ use crate::{LineIndex, symbol_index::{self, SymbolsDatabase}}; ra_db::SourceDatabaseStorage, LineIndexDatabaseStorage, symbol_index::SymbolsDatabaseStorage, - hir::db::HirDatabaseStorage, - hir::db::DefDatabaseStorage + hir::db::AstDatabaseStorage, + hir::db::DefDatabaseStorage, + hir::db::HirDatabaseStorage )] #[derive(Debug)] pub(crate) struct RootDatabase { -- cgit v1.2.3