Skip to content

Commit 6825324

Browse files
committed
Auto merge of rust-lang#17794 - Veykril:source-db-simplify, r=Veykril
internal: Newtype `ErasedFileAstId` It wrapping `la_arena::Idx` makes it quite annoying to use
2 parents d84b970 + cdee65f commit 6825324

File tree

47 files changed

+129
-124
lines changed

Some content is hidden

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

47 files changed

+129
-124
lines changed

src/tools/rust-analyzer/Cargo.lock

+1-2
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,7 @@ name = "paths"
12681268
version = "0.0.0"
12691269
dependencies = [
12701270
"camino",
1271+
"serde",
12711272
]
12721273

12731274
[[package]]
@@ -1330,14 +1331,12 @@ dependencies = [
13301331
"base-db",
13311332
"indexmap",
13321333
"intern",
1333-
"la-arena 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
13341334
"paths",
13351335
"rustc-hash",
13361336
"serde",
13371337
"serde_json",
13381338
"span",
13391339
"stdx",
1340-
"text-size",
13411340
"tracing",
13421341
"tt",
13431342
]

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use salsa::Durability;
77
use triomphe::Arc;
88
use vfs::FileId;
99

10-
use crate::{CrateGraph, SourceDatabaseExt, SourceDatabaseExt2, SourceRoot, SourceRootId};
10+
use crate::{CrateGraph, SourceDatabaseFileInputExt, SourceRoot, SourceRootDatabase, SourceRootId};
1111

1212
/// Encapsulate a bunch of raw `.set` calls on the database.
1313
#[derive(Default)]
@@ -50,7 +50,7 @@ impl FileChange {
5050
self.crate_graph = Some(graph);
5151
}
5252

53-
pub fn apply(self, db: &mut dyn SourceDatabaseExt) {
53+
pub fn apply(self, db: &mut dyn SourceRootDatabase) {
5454
let _p = tracing::info_span!("FileChange::apply").entered();
5555
if let Some(roots) = self.roots {
5656
for (idx, root) in roots.into_iter().enumerate() {

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

+25-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! base_db defines basic database traits. The concrete DB is defined by ide.
2-
2+
// FIXME: Rename this crate, base db is non descriptive
33
mod change;
44
mod input;
55

@@ -47,8 +47,6 @@ pub const DEFAULT_PARSE_LRU_CAP: u16 = 128;
4747
pub const DEFAULT_BORROWCK_LRU_CAP: u16 = 2024;
4848

4949
pub trait FileLoader {
50-
/// Text of the file.
51-
fn file_text(&self, file_id: FileId) -> Arc<str>;
5250
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId>;
5351
/// Crates whose root's source root is the same as the source root of `file_id`
5452
fn relevant_crates(&self, file_id: FileId) -> Arc<[CrateId]>;
@@ -58,6 +56,13 @@ pub trait FileLoader {
5856
/// model. Everything else in rust-analyzer is derived from these queries.
5957
#[salsa::query_group(SourceDatabaseStorage)]
6058
pub trait SourceDatabase: FileLoader + std::fmt::Debug {
59+
#[salsa::input]
60+
fn compressed_file_text(&self, file_id: FileId) -> Arc<[u8]>;
61+
62+
/// Text of the file.
63+
#[salsa::lru]
64+
fn file_text(&self, file_id: FileId) -> Arc<str>;
65+
6166
/// Parses the file into the syntax tree.
6267
#[salsa::lru]
6368
fn parse(&self, file_id: EditionedFileId) -> Parse<ast::SourceFile>;
@@ -99,16 +104,18 @@ fn parse_errors(db: &dyn SourceDatabase, file_id: EditionedFileId) -> Option<Arc
99104
}
100105
}
101106

107+
fn file_text(db: &dyn SourceDatabase, file_id: FileId) -> Arc<str> {
108+
let bytes = db.compressed_file_text(file_id);
109+
let bytes =
110+
lz4_flex::decompress_size_prepended(&bytes).expect("lz4 decompression should not fail");
111+
let text = std::str::from_utf8(&bytes).expect("file contents should be valid UTF-8");
112+
Arc::from(text)
113+
}
114+
102115
/// We don't want to give HIR knowledge of source roots, hence we extract these
103116
/// methods into a separate DB.
104-
#[salsa::query_group(SourceDatabaseExtStorage)]
105-
pub trait SourceDatabaseExt: SourceDatabase {
106-
#[salsa::input]
107-
fn compressed_file_text(&self, file_id: FileId) -> Arc<[u8]>;
108-
109-
#[salsa::lru]
110-
fn file_text(&self, file_id: FileId) -> Arc<str>;
111-
117+
#[salsa::query_group(SourceRootDatabaseStorage)]
118+
pub trait SourceRootDatabase: SourceDatabase {
112119
/// Path to a file, relative to the root of its source root.
113120
/// Source root of the file.
114121
#[salsa::input]
@@ -121,15 +128,7 @@ pub trait SourceDatabaseExt: SourceDatabase {
121128
fn source_root_crates(&self, id: SourceRootId) -> Arc<[CrateId]>;
122129
}
123130

124-
fn file_text(db: &dyn SourceDatabaseExt, file_id: FileId) -> Arc<str> {
125-
let bytes = db.compressed_file_text(file_id);
126-
let bytes =
127-
lz4_flex::decompress_size_prepended(&bytes).expect("lz4 decompression should not fail");
128-
let text = std::str::from_utf8(&bytes).expect("file contents should be valid UTF-8");
129-
Arc::from(text)
130-
}
131-
132-
pub trait SourceDatabaseExt2 {
131+
pub trait SourceDatabaseFileInputExt {
133132
fn set_file_text(&mut self, file_id: FileId, text: &str) {
134133
self.set_file_text_with_durability(file_id, text, Durability::LOW);
135134
}
@@ -142,7 +141,7 @@ pub trait SourceDatabaseExt2 {
142141
);
143142
}
144143

145-
impl<Db: ?Sized + SourceDatabaseExt> SourceDatabaseExt2 for Db {
144+
impl<Db: ?Sized + SourceRootDatabase> SourceDatabaseFileInputExt for Db {
146145
fn set_file_text_with_durability(
147146
&mut self,
148147
file_id: FileId,
@@ -159,7 +158,7 @@ impl<Db: ?Sized + SourceDatabaseExt> SourceDatabaseExt2 for Db {
159158
}
160159
}
161160

162-
fn source_root_crates(db: &dyn SourceDatabaseExt, id: SourceRootId) -> Arc<[CrateId]> {
161+
fn source_root_crates(db: &dyn SourceRootDatabase, id: SourceRootId) -> Arc<[CrateId]> {
163162
let graph = db.crate_graph();
164163
let mut crates = graph
165164
.iter()
@@ -173,13 +172,12 @@ fn source_root_crates(db: &dyn SourceDatabaseExt, id: SourceRootId) -> Arc<[Crat
173172
crates.into_iter().collect()
174173
}
175174

176-
/// Silly workaround for cyclic deps between the traits
175+
// FIXME: Would be nice to get rid of this somehow
176+
/// Silly workaround for cyclic deps due to the SourceRootDatabase and SourceDatabase split
177+
/// regarding FileLoader
177178
pub struct FileLoaderDelegate<T>(pub T);
178179

179-
impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
180-
fn file_text(&self, file_id: FileId) -> Arc<str> {
181-
SourceDatabaseExt::file_text(self.0, file_id)
182-
}
180+
impl<T: SourceRootDatabase> FileLoader for FileLoaderDelegate<&'_ T> {
183181
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
184182
// FIXME: this *somehow* should be platform agnostic...
185183
let source_root = self.0.file_source_root(path.anchor);

src/tools/rust-analyzer/crates/hir-def/src/nameres/tests/incremental.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use base_db::{SourceDatabase, SourceDatabaseExt2 as _};
1+
use base_db::{SourceDatabase, SourceDatabaseFileInputExt as _};
22
use test_fixture::WithFixture;
33

44
use crate::{db::DefDatabase, nameres::tests::TestDB, AdtId, ModuleDefId};

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
};
2020

2121
#[salsa::database(
22-
base_db::SourceDatabaseExtStorage,
22+
base_db::SourceRootDatabaseStorage,
2323
base_db::SourceDatabaseStorage,
2424
hir_expand::db::ExpandDatabaseStorage,
2525
crate::db::InternDatabaseStorage,
@@ -69,9 +69,6 @@ impl fmt::Debug for TestDB {
6969
impl panic::RefUnwindSafe for TestDB {}
7070

7171
impl FileLoader for TestDB {
72-
fn file_text(&self, file_id: FileId) -> Arc<str> {
73-
FileLoaderDelegate(self).file_text(file_id)
74-
}
7572
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
7673
FileLoaderDelegate(self).resolve_path(path)
7774
}

src/tools/rust-analyzer/crates/hir-expand/src/change.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Defines a unit of change that can applied to the database to get the next
22
//! state. Changes are transactional.
33
use base_db::{
4-
salsa::Durability, CrateGraph, CrateId, FileChange, SourceDatabaseExt, SourceRoot,
4+
salsa::Durability, CrateGraph, CrateId, FileChange, SourceRoot, SourceRootDatabase,
55
TargetLayoutLoadResult, Version,
66
};
77
use la_arena::RawIdx;
@@ -23,7 +23,7 @@ impl ChangeWithProcMacros {
2323
Self::default()
2424
}
2525

26-
pub fn apply(self, db: &mut (impl ExpandDatabase + SourceDatabaseExt)) {
26+
pub fn apply(self, db: &mut (impl ExpandDatabase + SourceRootDatabase)) {
2727
self.source_change.apply(db);
2828
if let Some(proc_macros) = self.proc_macros {
2929
db.set_proc_macros_with_durability(Arc::new(proc_macros), Durability::HIGH);

src/tools/rust-analyzer/crates/hir-expand/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@ impl ExpandErrorKind {
176176
&ExpandErrorKind::MissingProcMacroExpander(def_crate) => {
177177
match db.proc_macros().get_error_for_crate(def_crate) {
178178
Some((e, hard_err)) => (e.to_owned(), hard_err),
179-
None => ("missing expander".to_owned(), true),
179+
None => (
180+
format!(
181+
"internal error: proc-macro map is missing error entry for crate {def_crate:?}"
182+
),
183+
true,
184+
),
180185
}
181186
}
182187
ExpandErrorKind::MacroDefinition => {

src/tools/rust-analyzer/crates/hir-ty/src/test_db.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use test_utils::extract_annotations;
1515
use triomphe::Arc;
1616

1717
#[salsa::database(
18-
base_db::SourceDatabaseExtStorage,
18+
base_db::SourceRootDatabaseStorage,
1919
base_db::SourceDatabaseStorage,
2020
hir_expand::db::ExpandDatabaseStorage,
2121
hir_def::db::InternDatabaseStorage,
@@ -75,9 +75,6 @@ impl salsa::ParallelDatabase for TestDB {
7575
impl panic::RefUnwindSafe for TestDB {}
7676

7777
impl FileLoader for TestDB {
78-
fn file_text(&self, file_id: FileId) -> Arc<str> {
79-
FileLoaderDelegate(self).file_text(file_id)
80-
}
8178
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
8279
FileLoaderDelegate(self).resolve_path(path)
8380
}

src/tools/rust-analyzer/crates/hir-ty/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod traits;
1212

1313
use std::env;
1414

15-
use base_db::SourceDatabaseExt2 as _;
15+
use base_db::SourceDatabaseFileInputExt as _;
1616
use expect_test::Expect;
1717
use hir_def::{
1818
body::{Body, BodySourceMap, SyntheticSyntax},

src/tools/rust-analyzer/crates/hir-ty/src/tests/incremental.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use base_db::SourceDatabaseExt2 as _;
1+
use base_db::SourceDatabaseFileInputExt as _;
22
use test_fixture::WithFixture;
33

44
use crate::{db::HirDatabase, test_db::TestDB};

src/tools/rust-analyzer/crates/ide-assists/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod generated;
33
use expect_test::expect;
44
use hir::{FileRange, Semantics};
55
use ide_db::{
6-
base_db::SourceDatabaseExt,
6+
base_db::{SourceDatabase, SourceRootDatabase},
77
imports::insert_use::{ImportGranularity, InsertUseConfig},
88
source_change::FileSystemEdit,
99
EditionedFileId, RootDatabase, SnippetCap,

src/tools/rust-analyzer/crates/ide-completion/src/completions/mod_.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::iter;
44

55
use hir::{HirFileIdExt, Module};
66
use ide_db::{
7-
base_db::{SourceDatabaseExt, VfsPath},
7+
base_db::{SourceRootDatabase, VfsPath},
88
FxHashSet, RootDatabase, SymbolKind,
99
};
1010
use stdx::IsNoneOr;

src/tools/rust-analyzer/crates/ide-completion/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ mod type_pos;
2323
mod use_tree;
2424
mod visibility;
2525

26+
use base_db::SourceDatabase;
2627
use expect_test::Expect;
2728
use hir::PrefixKind;
2829
use ide_db::{
29-
base_db::FileLoader,
3030
imports::insert_use::{ImportGranularity, InsertUseConfig},
3131
FilePosition, RootDatabase, SnippetCap,
3232
};

src/tools/rust-analyzer/crates/ide-db/src/helpers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::collections::VecDeque;
44

5-
use base_db::SourceDatabaseExt;
5+
use base_db::SourceRootDatabase;
66
use hir::{Crate, DescendPreference, ItemInNs, ModuleDef, Name, Semantics};
77
use span::FileId;
88
use syntax::{

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub type FilePosition = FilePositionWrapper<FileId>;
7474
pub type FileRange = FileRangeWrapper<FileId>;
7575

7676
#[salsa::database(
77-
base_db::SourceDatabaseExtStorage,
77+
base_db::SourceRootDatabaseStorage,
7878
base_db::SourceDatabaseStorage,
7979
hir::db::ExpandDatabaseStorage,
8080
hir::db::DefDatabaseStorage,
@@ -125,9 +125,6 @@ impl Upcast<dyn HirDatabase> for RootDatabase {
125125
}
126126

127127
impl FileLoader for RootDatabase {
128-
fn file_text(&self, file_id: FileId) -> Arc<str> {
129-
FileLoaderDelegate(self).file_text(file_id)
130-
}
131128
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
132129
FileLoaderDelegate(self).resolve_path(path)
133130
}

src/tools/rust-analyzer/crates/ide-db/src/prime_caches.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use hir::db::DefDatabase;
1111
use crate::{
1212
base_db::{
1313
salsa::{Database, ParallelDatabase, Snapshot},
14-
Cancelled, CrateId, SourceDatabase, SourceDatabaseExt,
14+
Cancelled, CrateId, SourceDatabase, SourceRootDatabase,
1515
},
1616
FxIndexMap, RootDatabase,
1717
};

src/tools/rust-analyzer/crates/ide-db/src/search.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
use std::mem;
88

9-
use base_db::{salsa::Database, SourceDatabase, SourceDatabaseExt};
9+
use base_db::{salsa::Database, SourceDatabase, SourceRootDatabase};
1010
use hir::{
1111
sym, AsAssocItem, DefWithBody, DescendPreference, FileRange, HasAttrs, HasSource, HirFileIdExt,
1212
InFile, InRealFile, ModuleSource, PathResolution, Semantics, Visibility,

src/tools/rust-analyzer/crates/ide-db/src/symbol_index.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use std::{
2929

3030
use base_db::{
3131
salsa::{self, ParallelDatabase},
32-
SourceDatabaseExt, SourceRootId, Upcast,
32+
SourceRootDatabase, SourceRootId, Upcast,
3333
};
3434
use fst::{raw::IndexedValue, Automaton, Streamer};
3535
use hir::{
@@ -100,7 +100,7 @@ impl Query {
100100
}
101101

102102
#[salsa::query_group(SymbolsDatabaseStorage)]
103-
pub trait SymbolsDatabase: HirDatabase + SourceDatabaseExt + Upcast<dyn HirDatabase> {
103+
pub trait SymbolsDatabase: HirDatabase + SourceRootDatabase + Upcast<dyn HirDatabase> {
104104
/// The symbol index for a given module. These modules should only be in source roots that
105105
/// are inside local_roots.
106106
fn module_symbols(&self, module: Module) -> Arc<SymbolIndex>;

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/unlinked_file.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::iter;
44

55
use hir::{db::DefDatabase, DefMap, InFile, ModuleSource};
66
use ide_db::{
7-
base_db::{FileLoader, SourceDatabaseExt},
7+
base_db::{FileLoader, SourceDatabase, SourceRootDatabase},
88
source_change::SourceChange,
99
FileId, FileRange, LineIndexDatabase,
1010
};
@@ -47,7 +47,7 @@ pub(crate) fn unlinked_file(
4747
//
4848
// Only show this diagnostic on the first three characters of
4949
// the file, to avoid overwhelming the user during startup.
50-
range = FileLoader::file_text(ctx.sema.db, file_id)
50+
range = SourceDatabase::file_text(ctx.sema.db, file_id)
5151
.char_indices()
5252
.take(3)
5353
.last()

src/tools/rust-analyzer/crates/ide-diagnostics/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(clippy::print_stderr)]
22

33
use ide_db::{
4-
assists::AssistResolveStrategy, base_db::SourceDatabaseExt, LineIndexDatabase, RootDatabase,
4+
assists::AssistResolveStrategy, base_db::SourceDatabase, LineIndexDatabase, RootDatabase,
55
};
66
use itertools::Itertools;
77
use stdx::trim_indent;

src/tools/rust-analyzer/crates/ide-ssr/src/lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub use crate::{errors::SsrError, from_comment::ssr_from_comment, matching::Matc
8484

8585
use crate::{errors::bail, matching::MatchFailureReason};
8686
use hir::{FileRange, Semantics};
87-
use ide_db::{EditionedFileId, FileId, FxHashMap, RootDatabase};
87+
use ide_db::{base_db::SourceDatabase, EditionedFileId, FileId, FxHashMap, RootDatabase};
8888
use resolving::ResolvedRule;
8989
use syntax::{ast, AstNode, SyntaxNode, TextRange};
9090
use text_edit::TextEdit;
@@ -141,7 +141,7 @@ impl<'db> MatchFinder<'db> {
141141

142142
/// Constructs an instance using the start of the first file in `db` as the lookup context.
143143
pub fn at_first_file(db: &'db ide_db::RootDatabase) -> Result<MatchFinder<'db>, SsrError> {
144-
use ide_db::base_db::SourceDatabaseExt;
144+
use ide_db::base_db::SourceRootDatabase;
145145
use ide_db::symbol_index::SymbolsDatabase;
146146
if let Some(first_file_id) =
147147
db.local_roots().iter().next().and_then(|root| db.source_root(*root).iter().next())
@@ -172,7 +172,6 @@ impl<'db> MatchFinder<'db> {
172172

173173
/// Finds matches for all added rules and returns edits for all found matches.
174174
pub fn edits(&self) -> FxHashMap<FileId, TextEdit> {
175-
use ide_db::base_db::SourceDatabaseExt;
176175
let mut matches_by_file = FxHashMap::default();
177176
for m in self.matches().matches {
178177
matches_by_file
@@ -228,7 +227,6 @@ impl<'db> MatchFinder<'db> {
228227
file_id: EditionedFileId,
229228
snippet: &str,
230229
) -> Vec<MatchDebugInfo> {
231-
use ide_db::base_db::SourceDatabaseExt;
232230
let file = self.sema.parse(file_id);
233231
let mut res = Vec::new();
234232
let file_text = self.sema.db.file_text(file_id.into());

0 commit comments

Comments
 (0)