]> git.lizzy.rs Git - rust.git/commitdiff
make sure that CrateDefMap is independent from syntax
authorAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 26 Jun 2019 18:50:42 +0000 (21:50 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 26 Jun 2019 18:50:42 +0000 (21:50 +0300)
crates/ra_hir/src/db.rs
crates/ra_hir/src/ids.rs
crates/ra_hir/src/lib.rs
crates/ra_hir/src/mock.rs
crates/ra_hir/src/nameres.rs
crates/ra_hir/src/nameres/collector.rs
crates/ra_ide_api/src/db.rs

index a9840905c36547561a1c3c62fcadba50de28b504..8f4de1c85c6c27d5bdd88a8d528aeb750447b928 100644 (file)
     lang_item::{LangItems, LangItemTarget}, type_alias::TypeAliasData,
 };
 
-// This database has access to source code, so queries here are not really
-// incremental.
-#[salsa::query_group(AstDatabaseStorage)]
-pub trait AstDatabase: SourceDatabase {
+/// We store all interned things in the single QueryGroup.
+///
+/// This is done mainly to allow both "volatile" `AstDatabase` and "stable"
+/// `DefDatabase` to access macros, without adding hard dependencies between the
+/// two.
+#[salsa::query_group(InternDatabaseStorage)]
+pub trait InternDatabase: SourceDatabase {
     #[salsa::interned]
     fn intern_macro(&self, macro_call: MacroCallLoc) -> ids::MacroCallId;
+    #[salsa::interned]
+    fn intern_function(&self, loc: ids::ItemLoc<ast::FnDef>) -> ids::FunctionId;
+    #[salsa::interned]
+    fn intern_struct(&self, loc: ids::ItemLoc<ast::StructDef>) -> ids::StructId;
+    #[salsa::interned]
+    fn intern_enum(&self, loc: ids::ItemLoc<ast::EnumDef>) -> ids::EnumId;
+    #[salsa::interned]
+    fn intern_const(&self, loc: ids::ItemLoc<ast::ConstDef>) -> ids::ConstId;
+    #[salsa::interned]
+    fn intern_static(&self, loc: ids::ItemLoc<ast::StaticDef>) -> ids::StaticId;
+    #[salsa::interned]
+    fn intern_trait(&self, loc: ids::ItemLoc<ast::TraitDef>) -> ids::TraitId;
+    #[salsa::interned]
+    fn intern_type_alias(&self, loc: ids::ItemLoc<ast::TypeAliasDef>) -> ids::TypeAliasId;
 
+    // Interned IDs for Chalk integration
+    #[salsa::interned]
+    fn intern_type_ctor(&self, type_ctor: TypeCtor) -> ids::TypeCtorId;
+    #[salsa::interned]
+    fn intern_impl_block(&self, impl_block: ImplBlock) -> ids::GlobalImplId;
+}
+
+/// This database has access to source code, so queries here are not really
+/// incremental.
+#[salsa::query_group(AstDatabaseStorage)]
+pub trait AstDatabase: InternDatabase {
     #[salsa::invoke(crate::source_id::AstIdMap::ast_id_map_query)]
     fn ast_id_map(&self, file_id: HirFileId) -> Arc<AstIdMap>;
     #[salsa::transparent]
@@ -40,7 +68,6 @@ pub trait AstDatabase: SourceDatabase {
 
     #[salsa::invoke(crate::ids::macro_def_query)]
     fn macro_def(&self, macro_id: MacroDefId) -> Option<Arc<mbe::MacroRules>>;
-
     #[salsa::invoke(crate::ids::macro_arg_query)]
     fn macro_arg(&self, macro_call: ids::MacroCallId) -> Option<Arc<tt::Subtree>>;
 
@@ -51,28 +78,7 @@ pub trait AstDatabase: SourceDatabase {
 // 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<ast::FnDef>) -> ids::FunctionId;
-    #[salsa::interned]
-    fn intern_struct(&self, loc: ids::ItemLoc<ast::StructDef>) -> ids::StructId;
-    #[salsa::interned]
-    fn intern_enum(&self, loc: ids::ItemLoc<ast::EnumDef>) -> ids::EnumId;
-    #[salsa::interned]
-    fn intern_const(&self, loc: ids::ItemLoc<ast::ConstDef>) -> ids::ConstId;
-    #[salsa::interned]
-    fn intern_static(&self, loc: ids::ItemLoc<ast::StaticDef>) -> ids::StaticId;
-    #[salsa::interned]
-    fn intern_trait(&self, loc: ids::ItemLoc<ast::TraitDef>) -> ids::TraitId;
-    #[salsa::interned]
-    fn intern_type_alias(&self, loc: ids::ItemLoc<ast::TypeAliasDef>) -> ids::TypeAliasId;
-
-    // Interned IDs for Chalk integration
-    #[salsa::interned]
-    fn intern_type_ctor(&self, type_ctor: TypeCtor) -> ids::TypeCtorId;
-    #[salsa::interned]
-    fn intern_impl_block(&self, impl_block: ImplBlock) -> ids::GlobalImplId;
-
+pub trait DefDatabase: InternDatabase {
     #[salsa::invoke(crate::adt::StructData::struct_data_query)]
     fn struct_data(&self, s: Struct) -> Arc<StructData>;
 
index 033af163234eb38f9245f2e5aef1feb9522d1422..b7215ac03490983a3682e8bb8dc161489d76c40e 100644 (file)
@@ -9,7 +9,7 @@
 use mbe::MacroRules;
 
 use crate::{
-    Module, DefDatabase, AstId, FileAstId, AstDatabase, Source,
+    Module, DefDatabase, AstId, FileAstId, AstDatabase, Source, InternDatabase,
 };
 
 /// hir makes heavy use of ids: integer (u32) handlers to various things. You
@@ -37,7 +37,7 @@
 impl HirFileId {
     /// For macro-expansion files, returns the file original source file the
     /// expansion originated from.
-    pub fn original_file(self, db: &impl AstDatabase) -> FileId {
+    pub fn original_file(self, db: &impl InternDatabase) -> FileId {
         match self.0 {
             HirFileIdRepr::File(file_id) => file_id,
             HirFileIdRepr::Macro(macro_file) => {
@@ -187,7 +187,7 @@ pub struct MacroCallLoc {
 }
 
 impl MacroCallId {
-    pub(crate) fn loc(self, db: &impl AstDatabase) -> MacroCallLoc {
+    pub(crate) fn loc(self, db: &impl InternDatabase) -> MacroCallLoc {
         db.lookup_intern_macro(self)
     }
 
@@ -198,7 +198,7 @@ pub(crate) fn as_file(self, kind: MacroFileKind) -> HirFileId {
 }
 
 impl MacroCallLoc {
-    pub(crate) fn id(self, db: &impl AstDatabase) -> MacroCallId {
+    pub(crate) fn id(self, db: &impl InternDatabase) -> MacroCallId {
         db.intern_macro(self)
     }
 }
@@ -235,10 +235,13 @@ pub(crate) struct LocationCtx<DB> {
     file_id: HirFileId,
 }
 
-impl<'a, DB: DefDatabase + AstDatabase> LocationCtx<&'a DB> {
+impl<'a, DB: DefDatabase> LocationCtx<&'a DB> {
     pub(crate) fn new(db: &'a DB, module: Module, file_id: HirFileId) -> LocationCtx<&'a DB> {
         LocationCtx { db, module, file_id }
     }
+}
+
+impl<'a, DB: DefDatabase + AstDatabase> LocationCtx<&'a DB> {
     pub(crate) fn to_def<N, DEF>(self, ast: &N) -> DEF
     where
         N: AstNode,
@@ -257,10 +260,7 @@ fn from_ast(ctx: LocationCtx<&(impl AstDatabase + DefDatabase)>, ast: &N) -> Sel
         let item_id = items.ast_id(ast);
         Self::from_ast_id(ctx, item_id)
     }
-    fn from_ast_id(
-        ctx: LocationCtx<&(impl AstDatabase + DefDatabase)>,
-        ast_id: FileAstId<N>,
-    ) -> Self {
+    fn from_ast_id(ctx: LocationCtx<&impl DefDatabase>, ast_id: FileAstId<N>) -> Self {
         let loc = ItemLoc { module: ctx.module, ast_id: ast_id.with_file_id(ctx.file_id) };
         Self::intern(ctx.db, loc)
     }
index f07a36926c4dc43f9489a6214f0fd8654a7b8c1c..5afd846f5221316214fc07236739f83b805c2069 100644 (file)
@@ -47,7 +47,7 @@ fn from(it: $v) -> $e {
 mod marks;
 
 use crate::{
-    db::{AstDatabase, DefDatabase, HirDatabase},
+    db::{InternDatabase, AstDatabase, DefDatabase, HirDatabase},
     name::{AsName, KnownName},
     source_id::{FileAstId, AstId},
     resolve::Resolver,
index 5d38ac76c77a3bf7d0b8fd24d1d70ae2f28c2b6d..c57dfbf01901227e5603a848cf827014722a8861 100644 (file)
@@ -15,6 +15,7 @@
 
 #[salsa::database(
     ra_db::SourceDatabaseStorage,
+    db::InternDatabaseStorage,
     db::AstDatabaseStorage,
     db::DefDatabaseStorage,
     db::HirDatabaseStorage
index 3532faf01e169699334ab30603ef4243e6f8f97f..f4ca454e4e46ed641b117d619b72ebd58b5990b4 100644 (file)
@@ -231,7 +231,9 @@ fn or(left: ItemOrMacro, right: ItemOrMacro) -> ItemOrMacro {
 
 impl CrateDefMap {
     pub(crate) fn crate_def_map_query(
-        db: &(impl DefDatabase + AstDatabase),
+        // Note that this doesn't have `+ AstDatabase`!
+        // This gurantess that `CrateDefMap` is stable across reparses.
+        db: &impl DefDatabase,
         krate: Crate,
     ) -> Arc<CrateDefMap> {
         let _p = profile("crate_def_map_query");
index b74dc33b17d43a6bdd56e317b3d0a4912f37f681..ef4d1ed70f10a15b7b2badebb860624ed5d911f1 100644 (file)
@@ -7,7 +7,7 @@
 
 use crate::{
     Function, Module, Struct, Union, Enum, Const, Static, Trait, TypeAlias, MacroDef,
-    DefDatabase, HirFileId, Name, Path, AstDatabase,
+    DefDatabase, HirFileId, Name, Path,
     KnownName, AstId,
     nameres::{
         Resolution, PerNs, ModuleDef, ReachedFixedPoint, ResolveMode,
     either::Either,
 };
 
-pub(super) fn collect_defs(
-    db: &(impl DefDatabase + AstDatabase),
-    mut def_map: CrateDefMap,
-) -> CrateDefMap {
+pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap {
     // populate external prelude
     for dep in def_map.krate.dependencies(db) {
         log::debug!("crate dep {:?} -> {:?}", dep.name, dep.krate);
@@ -95,7 +92,7 @@ struct DefCollector<DB> {
 
 impl<'a, DB> DefCollector<&'a DB>
 where
-    DB: DefDatabase + AstDatabase,
+    DB: DefDatabase,
 {
     fn collect(&mut self) {
         let crate_graph = self.db.crate_graph();
@@ -465,7 +462,7 @@ fn collect_macro_expansion(
             ModCollector { def_collector: &mut *self, file_id, module_id, raw_items: &raw_items }
                 .collect(raw_items.items());
         } else {
-            log::error!("Too deep macro expansion: {}", macro_call_id.debug_dump(self.db));
+            log::error!("Too deep macro expansion: {:?}", macro_call_id);
             self.def_map.poison_macros.insert(macro_def_id);
         }
 
@@ -487,7 +484,7 @@ struct ModCollector<'a, D> {
 
 impl<DB> ModCollector<'_, &'_ mut DefCollector<&'_ DB>>
 where
-    DB: DefDatabase + AstDatabase,
+    DB: DefDatabase,
 {
     fn collect(&mut self, items: &[raw::RawItem]) {
         for item in items {
@@ -632,7 +629,7 @@ fn is_macro_rules(path: &Path) -> bool {
 }
 
 fn resolve_submodule(
-    db: &(impl DefDatabase + AstDatabase),
+    db: &impl DefDatabase,
     file_id: HirFileId,
     name: &Name,
     is_root: bool,
@@ -675,7 +672,7 @@ mod tests {
     use rustc_hash::FxHashSet;
 
     fn do_collect_defs(
-        db: &(impl DefDatabase + AstDatabase),
+        db: &impl DefDatabase,
         def_map: CrateDefMap,
         monitor: MacroStackMonitor,
     ) -> CrateDefMap {
index 82b061419010594b343a1ab5d4d54b73afa9846a..cb7d30c4317d8ca52091500664f4caf19cdbf3c8 100644 (file)
@@ -14,6 +14,7 @@
     ra_db::SourceDatabaseStorage,
     LineIndexDatabaseStorage,
     symbol_index::SymbolsDatabaseStorage,
+    hir::db::InternDatabaseStorage,
     hir::db::AstDatabaseStorage,
     hir::db::DefDatabaseStorage,
     hir::db::HirDatabaseStorage