Skip to content

Commit 6e23699

Browse files
committed
make sure that CrateDefMap is independent from syntax
1 parent 203d5dd commit 6e23699

File tree

7 files changed

+55
-48
lines changed

7 files changed

+55
-48
lines changed

crates/ra_hir/src/db.rs

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,41 @@ use crate::{
2020
lang_item::{LangItems, LangItemTarget}, type_alias::TypeAliasData,
2121
};
2222

23-
// This database has access to source code, so queries here are not really
24-
// incremental.
25-
#[salsa::query_group(AstDatabaseStorage)]
26-
pub trait AstDatabase: SourceDatabase {
23+
/// We store all interned things in the single QueryGroup.
24+
///
25+
/// This is done mainly to allow both "volatile" `AstDatabase` and "stable"
26+
/// `DefDatabase` to access macros, without adding hard dependencies between the
27+
/// two.
28+
#[salsa::query_group(InternDatabaseStorage)]
29+
pub trait InternDatabase: SourceDatabase {
2730
#[salsa::interned]
2831
fn intern_macro(&self, macro_call: MacroCallLoc) -> ids::MacroCallId;
32+
#[salsa::interned]
33+
fn intern_function(&self, loc: ids::ItemLoc<ast::FnDef>) -> ids::FunctionId;
34+
#[salsa::interned]
35+
fn intern_struct(&self, loc: ids::ItemLoc<ast::StructDef>) -> ids::StructId;
36+
#[salsa::interned]
37+
fn intern_enum(&self, loc: ids::ItemLoc<ast::EnumDef>) -> ids::EnumId;
38+
#[salsa::interned]
39+
fn intern_const(&self, loc: ids::ItemLoc<ast::ConstDef>) -> ids::ConstId;
40+
#[salsa::interned]
41+
fn intern_static(&self, loc: ids::ItemLoc<ast::StaticDef>) -> ids::StaticId;
42+
#[salsa::interned]
43+
fn intern_trait(&self, loc: ids::ItemLoc<ast::TraitDef>) -> ids::TraitId;
44+
#[salsa::interned]
45+
fn intern_type_alias(&self, loc: ids::ItemLoc<ast::TypeAliasDef>) -> ids::TypeAliasId;
2946

47+
// Interned IDs for Chalk integration
48+
#[salsa::interned]
49+
fn intern_type_ctor(&self, type_ctor: TypeCtor) -> ids::TypeCtorId;
50+
#[salsa::interned]
51+
fn intern_impl_block(&self, impl_block: ImplBlock) -> ids::GlobalImplId;
52+
}
53+
54+
/// This database has access to source code, so queries here are not really
55+
/// incremental.
56+
#[salsa::query_group(AstDatabaseStorage)]
57+
pub trait AstDatabase: InternDatabase {
3058
#[salsa::invoke(crate::source_id::AstIdMap::ast_id_map_query)]
3159
fn ast_id_map(&self, file_id: HirFileId) -> Arc<AstIdMap>;
3260
#[salsa::transparent]
@@ -40,7 +68,6 @@ pub trait AstDatabase: SourceDatabase {
4068

4169
#[salsa::invoke(crate::ids::macro_def_query)]
4270
fn macro_def(&self, macro_id: MacroDefId) -> Option<Arc<mbe::MacroRules>>;
43-
4471
#[salsa::invoke(crate::ids::macro_arg_query)]
4572
fn macro_arg(&self, macro_call: ids::MacroCallId) -> Option<Arc<tt::Subtree>>;
4673

@@ -51,28 +78,7 @@ pub trait AstDatabase: SourceDatabase {
5178
// This database uses `AstDatabase` internally,
5279
#[salsa::query_group(DefDatabaseStorage)]
5380
#[salsa::requires(AstDatabase)]
54-
pub trait DefDatabase: SourceDatabase {
55-
#[salsa::interned]
56-
fn intern_function(&self, loc: ids::ItemLoc<ast::FnDef>) -> ids::FunctionId;
57-
#[salsa::interned]
58-
fn intern_struct(&self, loc: ids::ItemLoc<ast::StructDef>) -> ids::StructId;
59-
#[salsa::interned]
60-
fn intern_enum(&self, loc: ids::ItemLoc<ast::EnumDef>) -> ids::EnumId;
61-
#[salsa::interned]
62-
fn intern_const(&self, loc: ids::ItemLoc<ast::ConstDef>) -> ids::ConstId;
63-
#[salsa::interned]
64-
fn intern_static(&self, loc: ids::ItemLoc<ast::StaticDef>) -> ids::StaticId;
65-
#[salsa::interned]
66-
fn intern_trait(&self, loc: ids::ItemLoc<ast::TraitDef>) -> ids::TraitId;
67-
#[salsa::interned]
68-
fn intern_type_alias(&self, loc: ids::ItemLoc<ast::TypeAliasDef>) -> ids::TypeAliasId;
69-
70-
// Interned IDs for Chalk integration
71-
#[salsa::interned]
72-
fn intern_type_ctor(&self, type_ctor: TypeCtor) -> ids::TypeCtorId;
73-
#[salsa::interned]
74-
fn intern_impl_block(&self, impl_block: ImplBlock) -> ids::GlobalImplId;
75-
81+
pub trait DefDatabase: InternDatabase {
7682
#[salsa::invoke(crate::adt::StructData::struct_data_query)]
7783
fn struct_data(&self, s: Struct) -> Arc<StructData>;
7884

crates/ra_hir/src/ids.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use ra_prof::profile;
99
use mbe::MacroRules;
1010

1111
use crate::{
12-
Module, DefDatabase, AstId, FileAstId, AstDatabase, Source,
12+
Module, DefDatabase, AstId, FileAstId, AstDatabase, Source, InternDatabase,
1313
};
1414

1515
/// hir makes heavy use of ids: integer (u32) handlers to various things. You
@@ -37,7 +37,7 @@ pub struct HirFileId(HirFileIdRepr);
3737
impl HirFileId {
3838
/// For macro-expansion files, returns the file original source file the
3939
/// expansion originated from.
40-
pub fn original_file(self, db: &impl AstDatabase) -> FileId {
40+
pub fn original_file(self, db: &impl InternDatabase) -> FileId {
4141
match self.0 {
4242
HirFileIdRepr::File(file_id) => file_id,
4343
HirFileIdRepr::Macro(macro_file) => {
@@ -187,7 +187,7 @@ pub struct MacroCallLoc {
187187
}
188188

189189
impl MacroCallId {
190-
pub(crate) fn loc(self, db: &impl AstDatabase) -> MacroCallLoc {
190+
pub(crate) fn loc(self, db: &impl InternDatabase) -> MacroCallLoc {
191191
db.lookup_intern_macro(self)
192192
}
193193

@@ -198,7 +198,7 @@ impl MacroCallId {
198198
}
199199

200200
impl MacroCallLoc {
201-
pub(crate) fn id(self, db: &impl AstDatabase) -> MacroCallId {
201+
pub(crate) fn id(self, db: &impl InternDatabase) -> MacroCallId {
202202
db.intern_macro(self)
203203
}
204204
}
@@ -235,10 +235,13 @@ pub(crate) struct LocationCtx<DB> {
235235
file_id: HirFileId,
236236
}
237237

238-
impl<'a, DB: DefDatabase + AstDatabase> LocationCtx<&'a DB> {
238+
impl<'a, DB: DefDatabase> LocationCtx<&'a DB> {
239239
pub(crate) fn new(db: &'a DB, module: Module, file_id: HirFileId) -> LocationCtx<&'a DB> {
240240
LocationCtx { db, module, file_id }
241241
}
242+
}
243+
244+
impl<'a, DB: DefDatabase + AstDatabase> LocationCtx<&'a DB> {
242245
pub(crate) fn to_def<N, DEF>(self, ast: &N) -> DEF
243246
where
244247
N: AstNode,
@@ -257,10 +260,7 @@ pub(crate) trait AstItemDef<N: AstNode>: salsa::InternKey + Clone {
257260
let item_id = items.ast_id(ast);
258261
Self::from_ast_id(ctx, item_id)
259262
}
260-
fn from_ast_id(
261-
ctx: LocationCtx<&(impl AstDatabase + DefDatabase)>,
262-
ast_id: FileAstId<N>,
263-
) -> Self {
263+
fn from_ast_id(ctx: LocationCtx<&impl DefDatabase>, ast_id: FileAstId<N>) -> Self {
264264
let loc = ItemLoc { module: ctx.module, ast_id: ast_id.with_file_id(ctx.file_id) };
265265
Self::intern(ctx.db, loc)
266266
}

crates/ra_hir/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ mod code_model;
4747
mod marks;
4848

4949
use crate::{
50-
db::{AstDatabase, DefDatabase, HirDatabase},
50+
db::{InternDatabase, AstDatabase, DefDatabase, HirDatabase},
5151
name::{AsName, KnownName},
5252
source_id::{FileAstId, AstId},
5353
resolve::Resolver,

crates/ra_hir/src/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub const WORKSPACE: SourceRootId = SourceRootId(0);
1515

1616
#[salsa::database(
1717
ra_db::SourceDatabaseStorage,
18+
db::InternDatabaseStorage,
1819
db::AstDatabaseStorage,
1920
db::DefDatabaseStorage,
2021
db::HirDatabaseStorage

crates/ra_hir/src/nameres.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ fn or(left: ItemOrMacro, right: ItemOrMacro) -> ItemOrMacro {
231231

232232
impl CrateDefMap {
233233
pub(crate) fn crate_def_map_query(
234-
db: &(impl DefDatabase + AstDatabase),
234+
// Note that this doesn't have `+ AstDatabase`!
235+
// This gurantess that `CrateDefMap` is stable across reparses.
236+
db: &impl DefDatabase,
235237
krate: Crate,
236238
) -> Arc<CrateDefMap> {
237239
let _p = profile("crate_def_map_query");

crates/ra_hir/src/nameres/collector.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ra_syntax::ast;
77

88
use crate::{
99
Function, Module, Struct, Union, Enum, Const, Static, Trait, TypeAlias, MacroDef,
10-
DefDatabase, HirFileId, Name, Path, AstDatabase,
10+
DefDatabase, HirFileId, Name, Path,
1111
KnownName, AstId,
1212
nameres::{
1313
Resolution, PerNs, ModuleDef, ReachedFixedPoint, ResolveMode,
@@ -19,10 +19,7 @@ use crate::{
1919
either::Either,
2020
};
2121

22-
pub(super) fn collect_defs(
23-
db: &(impl DefDatabase + AstDatabase),
24-
mut def_map: CrateDefMap,
25-
) -> CrateDefMap {
22+
pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap {
2623
// populate external prelude
2724
for dep in def_map.krate.dependencies(db) {
2825
log::debug!("crate dep {:?} -> {:?}", dep.name, dep.krate);
@@ -95,7 +92,7 @@ struct DefCollector<DB> {
9592

9693
impl<'a, DB> DefCollector<&'a DB>
9794
where
98-
DB: DefDatabase + AstDatabase,
95+
DB: DefDatabase,
9996
{
10097
fn collect(&mut self) {
10198
let crate_graph = self.db.crate_graph();
@@ -465,7 +462,7 @@ where
465462
ModCollector { def_collector: &mut *self, file_id, module_id, raw_items: &raw_items }
466463
.collect(raw_items.items());
467464
} else {
468-
log::error!("Too deep macro expansion: {}", macro_call_id.debug_dump(self.db));
465+
log::error!("Too deep macro expansion: {:?}", macro_call_id);
469466
self.def_map.poison_macros.insert(macro_def_id);
470467
}
471468

@@ -487,7 +484,7 @@ struct ModCollector<'a, D> {
487484

488485
impl<DB> ModCollector<'_, &'_ mut DefCollector<&'_ DB>>
489486
where
490-
DB: DefDatabase + AstDatabase,
487+
DB: DefDatabase,
491488
{
492489
fn collect(&mut self, items: &[raw::RawItem]) {
493490
for item in items {
@@ -632,7 +629,7 @@ fn is_macro_rules(path: &Path) -> bool {
632629
}
633630

634631
fn resolve_submodule(
635-
db: &(impl DefDatabase + AstDatabase),
632+
db: &impl DefDatabase,
636633
file_id: HirFileId,
637634
name: &Name,
638635
is_root: bool,
@@ -675,7 +672,7 @@ mod tests {
675672
use rustc_hash::FxHashSet;
676673

677674
fn do_collect_defs(
678-
db: &(impl DefDatabase + AstDatabase),
675+
db: &impl DefDatabase,
679676
def_map: CrateDefMap,
680677
monitor: MacroStackMonitor,
681678
) -> CrateDefMap {

crates/ra_ide_api/src/db.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::{LineIndex, symbol_index::{self, SymbolsDatabase}};
1414
ra_db::SourceDatabaseStorage,
1515
LineIndexDatabaseStorage,
1616
symbol_index::SymbolsDatabaseStorage,
17+
hir::db::InternDatabaseStorage,
1718
hir::db::AstDatabaseStorage,
1819
hir::db::DefDatabaseStorage,
1920
hir::db::HirDatabaseStorage

0 commit comments

Comments
 (0)