Skip to content

Commit 257f785

Browse files
authored
Merge pull request rust-lang#21944 from Veykril/push-spxqtymwtzvk
Remove `RootQueryDb`
2 parents 79088b0 + dc26876 commit 257f785

54 files changed

Lines changed: 404 additions & 371 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/tools/rust-analyzer/crates/base-db/src/change.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use triomphe::Arc;
99
use vfs::FileId;
1010

1111
use crate::{
12-
CrateGraphBuilder, CratesIdMap, LibraryRoots, LocalRoots, RootQueryDb, SourceRoot, SourceRootId,
12+
CrateGraphBuilder, CratesIdMap, LibraryRoots, LocalRoots, SourceDatabase, SourceRoot,
13+
SourceRootId,
1314
};
1415

1516
/// Encapsulate a bunch of raw `.set` calls on the database.
@@ -49,7 +50,7 @@ impl FileChange {
4950
self.crate_graph = Some(graph);
5051
}
5152

52-
pub fn apply(self, db: &mut dyn RootQueryDb) -> Option<CratesIdMap> {
53+
pub fn apply(self, db: &mut dyn SourceDatabase) -> Option<CratesIdMap> {
5354
let _p = tracing::info_span!("FileChange::apply").entered();
5455
if let Some(roots) = self.roots {
5556
let mut local_roots = FxHashSet::default();

src/tools/rust-analyzer/crates/base-db/src/editioned_file_id.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,71 @@ use std::hash::Hash;
55

66
use salsa::Database;
77
use span::Edition;
8+
use syntax::{SyntaxError, ast};
89
use vfs::FileId;
910

11+
use crate::SourceDatabase;
12+
1013
#[salsa::interned(debug, constructor = from_span_file_id, no_lifetime)]
1114
#[derive(PartialOrd, Ord)]
1215
pub struct EditionedFileId {
1316
field: span::EditionedFileId,
1417
}
1518

19+
// Currently does not work due to a salsa bug
20+
// #[salsa::tracked]
21+
// impl EditionedFileId {
22+
// #[salsa::tracked(lru = 128)]
23+
// pub fn parse(self, db: &dyn SourceDatabase) -> syntax::Parse<ast::SourceFile> {
24+
// let _p = tracing::info_span!("parse", ?self).entered();
25+
// let (file_id, edition) = self.unpack(db);
26+
// let text = db.file_text(file_id).text(db);
27+
// ast::SourceFile::parse(text, edition)
28+
// }
29+
30+
// // firewall query
31+
// #[salsa::tracked(returns(as_deref))]
32+
// pub fn parse_errors(self, db: &dyn SourceDatabase) -> Option<Box<[SyntaxError]>> {
33+
// let errors = self.parse(db).errors();
34+
// match &*errors {
35+
// [] => None,
36+
// [..] => Some(errors.into()),
37+
// }
38+
// }
39+
// }
40+
41+
impl EditionedFileId {
42+
pub fn parse(self, db: &dyn SourceDatabase) -> syntax::Parse<ast::SourceFile> {
43+
#[salsa::tracked(lru = 128)]
44+
pub fn parse(
45+
db: &dyn SourceDatabase,
46+
file_id: EditionedFileId,
47+
) -> syntax::Parse<ast::SourceFile> {
48+
let _p = tracing::info_span!("parse", ?file_id).entered();
49+
let (file_id, edition) = file_id.unpack(db);
50+
let text = db.file_text(file_id).text(db);
51+
ast::SourceFile::parse(text, edition)
52+
}
53+
parse(db, self)
54+
}
55+
56+
// firewall query
57+
pub fn parse_errors(self, db: &dyn SourceDatabase) -> Option<&[SyntaxError]> {
58+
#[salsa::tracked(returns(as_deref))]
59+
pub fn parse_errors(
60+
db: &dyn SourceDatabase,
61+
file_id: EditionedFileId,
62+
) -> Option<Box<[SyntaxError]>> {
63+
let errors = file_id.parse(db).errors();
64+
match &*errors {
65+
[] => None,
66+
[..] => Some(errors.into()),
67+
}
68+
}
69+
parse_errors(db, self)
70+
}
71+
}
72+
1673
impl EditionedFileId {
1774
#[inline]
1875
pub fn new(db: &dyn Database, file_id: FileId, edition: Edition) -> Self {

src/tools/rust-analyzer/crates/base-db/src/input.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ use span::Edition;
2121
use triomphe::Arc;
2222
use vfs::{AbsPathBuf, AnchoredPath, FileId, VfsPath, file_set::FileSet};
2323

24-
use crate::{CrateWorkspaceData, EditionedFileId, FxIndexSet, RootQueryDb};
24+
use crate::{
25+
CrateWorkspaceData, EditionedFileId, FxIndexSet, SourceDatabase, all_crates,
26+
set_all_crates_with_durability,
27+
};
2528

2629
pub type ProcMacroPaths =
2730
FxHashMap<CrateBuilderId, Result<(String, AbsPathBuf), ProcMacroLoadingError>>;
@@ -490,13 +493,13 @@ impl Crate {
490493
/// including the crate itself.
491494
///
492495
/// **Warning**: do not use this query in `hir-*` crates! It kills incrementality across crate metadata modifications.
493-
pub fn transitive_rev_deps(self, db: &dyn RootQueryDb) -> Box<[Crate]> {
496+
pub fn transitive_rev_deps(self, db: &dyn SourceDatabase) -> Box<[Crate]> {
494497
let mut worklist = vec![self];
495498
let mut rev_deps = FxHashSet::default();
496499
rev_deps.insert(self);
497500

498501
let mut inverted_graph = FxHashMap::<_, Vec<_>>::default();
499-
db.all_crates().iter().for_each(|&krate| {
502+
all_crates(db).iter().for_each(|&krate| {
500503
krate
501504
.data(db)
502505
.dependencies
@@ -586,15 +589,15 @@ impl CrateGraphBuilder {
586589
Ok(())
587590
}
588591

589-
pub fn set_in_db(self, db: &mut dyn RootQueryDb) -> CratesIdMap {
592+
pub fn set_in_db(self, db: &mut dyn SourceDatabase) -> CratesIdMap {
593+
let old_all_crates = all_crates(db);
594+
590595
// For some reason in some repositories we have duplicate crates, so we use a set and not `Vec`.
591596
// We use an `IndexSet` because the list needs to be topologically sorted.
592597
let mut all_crates = FxIndexSet::with_capacity_and_hasher(self.arena.len(), FxBuildHasher);
593598
let mut visited = FxHashMap::default();
594599
let mut visited_root_files = FxHashSet::default();
595600

596-
let old_all_crates = db.all_crates();
597-
598601
let crates_map = db.crates_map();
599602
// salsa doesn't compare new input to old input to see if they are the same, so here we are doing all the work ourselves.
600603
for krate in self.iter() {
@@ -612,17 +615,14 @@ impl CrateGraphBuilder {
612615
if old_all_crates.len() != all_crates.len()
613616
|| old_all_crates.iter().any(|&krate| !all_crates.contains(&krate))
614617
{
615-
db.set_all_crates_with_durability(
616-
Arc::new(Vec::from_iter(all_crates).into_boxed_slice()),
617-
Durability::MEDIUM,
618-
);
618+
set_all_crates_with_durability(db, all_crates, Durability::MEDIUM);
619619
}
620620

621621
return visited;
622622

623623
fn go(
624624
graph: &CrateGraphBuilder,
625-
db: &mut dyn RootQueryDb,
625+
db: &mut dyn SourceDatabase,
626626
crates_map: &CratesMap,
627627
visited: &mut FxHashMap<CrateBuilderId, Crate>,
628628
visited_root_files: &mut FxHashSet<FileId>,

src/tools/rust-analyzer/crates/base-db/src/lib.rs

Lines changed: 49 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ pub use query_group;
3636
use rustc_hash::{FxHashSet, FxHasher};
3737
use salsa::{Durability, Setter};
3838
pub use semver::{BuildMetadata, Prerelease, Version, VersionReq};
39-
use syntax::{Parse, SyntaxError, ast};
4039
use triomphe::Arc;
4140
pub use vfs::{AnchoredPath, AnchoredPathBuf, FileId, VfsPath, file_set::FileSet};
4241

@@ -236,36 +235,6 @@ pub struct SourceRootInput {
236235
pub source_root: Arc<SourceRoot>,
237236
}
238237

239-
/// Database which stores all significant input facts: source code and project
240-
/// model. Everything else in rust-analyzer is derived from these queries.
241-
#[query_group::query_group]
242-
pub trait RootQueryDb: SourceDatabase + salsa::Database {
243-
/// Parses the file into the syntax tree.
244-
#[salsa::invoke(parse)]
245-
#[salsa::lru(128)]
246-
fn parse(&self, file_id: EditionedFileId) -> Parse<ast::SourceFile>;
247-
248-
/// Returns the set of errors obtained from parsing the file including validation errors.
249-
#[salsa::transparent]
250-
fn parse_errors(&self, file_id: EditionedFileId) -> Option<&[SyntaxError]>;
251-
252-
#[salsa::transparent]
253-
fn toolchain_channel(&self, krate: Crate) -> Option<ReleaseChannel>;
254-
255-
/// Crates whose root file is in `id`.
256-
#[salsa::invoke_interned(source_root_crates)]
257-
fn source_root_crates(&self, id: SourceRootId) -> Arc<[Crate]>;
258-
259-
#[salsa::transparent]
260-
fn relevant_crates(&self, file_id: FileId) -> Arc<[Crate]>;
261-
262-
/// Returns the crates in topological order.
263-
///
264-
/// **Warning**: do not use this query in `hir-*` crates! It kills incrementality across crate metadata modifications.
265-
#[salsa::input]
266-
fn all_crates(&self) -> Arc<Box<[Crate]>>;
267-
}
268-
269238
#[salsa_macros::db]
270239
pub trait SourceDatabase: salsa::Database {
271240
/// Text of the file.
@@ -353,46 +322,67 @@ impl CrateWorkspaceData {
353322
}
354323
}
355324

356-
fn toolchain_channel(db: &dyn RootQueryDb, krate: Crate) -> Option<ReleaseChannel> {
325+
pub fn toolchain_channel(db: &dyn salsa::Database, krate: Crate) -> Option<ReleaseChannel> {
357326
krate.workspace_data(db).toolchain.as_ref().and_then(|v| ReleaseChannel::from_str(&v.pre))
358327
}
359328

360-
fn parse(db: &dyn RootQueryDb, file_id: EditionedFileId) -> Parse<ast::SourceFile> {
361-
let _p = tracing::info_span!("parse", ?file_id).entered();
362-
let (file_id, edition) = file_id.unpack(db.as_dyn_database());
363-
let text = db.file_text(file_id).text(db);
364-
ast::SourceFile::parse(text, edition)
329+
#[salsa::input(singleton, debug)]
330+
struct AllCrates {
331+
crates: std::sync::Arc<[Crate]>,
365332
}
366333

367-
fn parse_errors(db: &dyn RootQueryDb, file_id: EditionedFileId) -> Option<&[SyntaxError]> {
368-
#[salsa_macros::tracked(returns(ref))]
369-
fn parse_errors(db: &dyn RootQueryDb, file_id: EditionedFileId) -> Option<Box<[SyntaxError]>> {
370-
let errors = db.parse(file_id).errors();
371-
match &*errors {
372-
[] => None,
373-
[..] => Some(errors.into()),
374-
}
375-
}
376-
parse_errors(db, file_id).as_ref().map(|it| &**it)
334+
pub fn set_all_crates_with_durability(
335+
db: &mut dyn salsa::Database,
336+
crates: impl IntoIterator<Item = Crate>,
337+
durability: Durability,
338+
) {
339+
AllCrates::try_get(db)
340+
.unwrap_or_else(|| AllCrates::new(db, std::sync::Arc::default()))
341+
.set_crates(db)
342+
.with_durability(durability)
343+
.to(crates.into_iter().collect());
377344
}
378345

379-
fn source_root_crates(db: &dyn RootQueryDb, id: SourceRootId) -> Arc<[Crate]> {
380-
let crates = db.all_crates();
381-
crates
382-
.iter()
383-
.copied()
384-
.filter(|&krate| {
385-
let root_file = krate.data(db).root_file_id;
386-
db.file_source_root(root_file).source_root_id(db) == id
387-
})
388-
.collect()
346+
/// Returns the crates in topological order.
347+
///
348+
/// **Warning**: do not use this query in `hir-*` crates! It kills incrementality across crate metadata modifications.
349+
pub fn all_crates(db: &dyn salsa::Database) -> std::sync::Arc<[Crate]> {
350+
AllCrates::try_get(db).map_or(std::sync::Arc::default(), |all_crates| all_crates.crates(db))
351+
}
352+
353+
// FIXME: VFS rewrite should allow us to get rid of this wrapper
354+
#[doc(hidden)]
355+
#[salsa::interned]
356+
pub struct InternedSourceRootId {
357+
pub id: SourceRootId,
358+
}
359+
360+
/// Crates whose root file is in `id`.
361+
pub fn source_root_crates(db: &dyn SourceDatabase, id: SourceRootId) -> &[Crate] {
362+
#[salsa::tracked(returns(deref))]
363+
pub fn source_root_crates<'db>(
364+
db: &'db dyn SourceDatabase,
365+
id: InternedSourceRootId<'db>,
366+
) -> Box<[Crate]> {
367+
let crates = AllCrates::get(db).crates(db);
368+
let id = id.id(db);
369+
crates
370+
.iter()
371+
.copied()
372+
.filter(|&krate| {
373+
let root_file = krate.data(db).root_file_id;
374+
db.file_source_root(root_file).source_root_id(db) == id
375+
})
376+
.collect()
377+
}
378+
source_root_crates(db, InternedSourceRootId::new(db, id))
389379
}
390380

391-
fn relevant_crates(db: &dyn RootQueryDb, file_id: FileId) -> Arc<[Crate]> {
381+
pub fn relevant_crates(db: &dyn SourceDatabase, file_id: FileId) -> &[Crate] {
392382
let _p = tracing::info_span!("relevant_crates").entered();
393383

394384
let source_root = db.file_source_root(file_id);
395-
db.source_root_crates(source_root.source_root_id(db))
385+
source_root_crates(db, source_root.source_root_id(db))
396386
}
397387

398388
#[must_use]

src/tools/rust-analyzer/crates/hir-def/src/attrs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,13 @@ fn attrs_source(
351351
let krate = def_map.krate();
352352
let (definition, declaration, extra_crate_attrs) = match def_map[id].origin {
353353
ModuleOrigin::CrateRoot { definition } => {
354-
let definition_source = db.parse(definition).tree();
354+
let definition_source = definition.parse(db).tree();
355355
let definition = InFile::new(definition.into(), definition_source.into());
356356
let extra_crate_attrs = parse_extra_crate_attrs(db, krate);
357357
(definition, None, extra_crate_attrs)
358358
}
359359
ModuleOrigin::File { declaration, declaration_tree_id, definition, .. } => {
360-
let definition_source = db.parse(definition).tree();
360+
let definition_source = definition.parse(db).tree();
361361
let definition = InFile::new(definition.into(), definition_source.into());
362362
let declaration = InFile::new(declaration_tree_id.file_id(), declaration);
363363
let declaration = declaration.with_value(declaration.to_node(db));
@@ -742,7 +742,7 @@ impl AttrFlags {
742742
#[salsa::tracked(returns(ref))]
743743
pub fn doc_html_root_url(db: &dyn DefDatabase, krate: Crate) -> Option<SmolStr> {
744744
let root_file_id = krate.root_file_id(db);
745-
let syntax = db.parse(root_file_id).tree();
745+
let syntax = root_file_id.parse(db).tree();
746746
let extra_crate_attrs =
747747
parse_extra_crate_attrs(db, krate).into_iter().flat_map(|src| src.attrs());
748748

src/tools/rust-analyzer/crates/hir-def/src/db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Defines database & queries for name resolution.
2-
use base_db::{Crate, RootQueryDb, SourceDatabase};
2+
use base_db::{Crate, SourceDatabase};
33
use hir_expand::{
44
EditionedFileId, HirFileId, InFile, Lookup, MacroCallId, MacroDefId, MacroDefKind,
55
db::ExpandDatabase,
@@ -22,7 +22,7 @@ use crate::{
2222
use salsa::plumbing::AsId;
2323

2424
#[query_group::query_group(InternDatabaseStorage)]
25-
pub trait InternDatabase: RootQueryDb {
25+
pub trait InternDatabase: SourceDatabase {
2626
// region: items
2727
#[salsa::interned]
2828
fn intern_use(&self, loc: UseLoc) -> UseId;

src/tools/rust-analyzer/crates/hir-def/src/expr_store/scope.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,6 @@ fn compute_expr_scopes(
371371

372372
#[cfg(test)]
373373
mod tests {
374-
use base_db::RootQueryDb;
375374
use hir_expand::{InFile, name::AsName};
376375
use span::FileId;
377376
use syntax::{AstNode, algo::find_node_at_offset, ast};
@@ -414,7 +413,7 @@ mod tests {
414413

415414
let (file_id, _) = editioned_file_id.unpack(&db);
416415

417-
let file_syntax = db.parse(editioned_file_id).syntax_node();
416+
let file_syntax = editioned_file_id.parse(&db).syntax_node();
418417
let marker: ast::PathExpr = find_node_at_offset(&file_syntax, offset).unwrap();
419418
let function = find_function(&db, file_id);
420419

@@ -570,7 +569,7 @@ fn foo() {
570569

571570
let (file_id, _) = editioned_file_id.unpack(&db);
572571

573-
let file = db.parse(editioned_file_id).ok().unwrap();
572+
let file = editioned_file_id.parse(&db).ok().unwrap();
574573
let expected_name = find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into())
575574
.expect("failed to find a name at the target offset");
576575
let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), offset).unwrap();

src/tools/rust-analyzer/crates/hir-def/src/expr_store/tests/body/block.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,13 @@ fn f() {
190190
"#,
191191
expect![[r#"
192192
ModuleIdLt {
193-
[salsa id]: Id(3803),
193+
[salsa id]: Id(3403),
194194
krate: Crate(
195-
Id(2400),
195+
Id(2000),
196196
),
197197
block: Some(
198198
BlockId(
199-
4801,
199+
4401,
200200
),
201201
),
202202
}"#]],

0 commit comments

Comments
 (0)