Skip to content

Commit

Permalink
Auto merge of rust-lang#17794 - Veykril:source-db-simplify, r=Veykril
Browse files Browse the repository at this point in the history
internal: Newtype `ErasedFileAstId`

It wrapping `la_arena::Idx` makes it quite annoying to use
  • Loading branch information
bors committed Aug 5, 2024
2 parents d84b970 + cdee65f commit 6825324
Show file tree
Hide file tree
Showing 47 changed files with 129 additions and 124 deletions.
3 changes: 1 addition & 2 deletions src/tools/rust-analyzer/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,7 @@ name = "paths"
version = "0.0.0"
dependencies = [
"camino",
"serde",
]

[[package]]
Expand Down Expand Up @@ -1330,14 +1331,12 @@ dependencies = [
"base-db",
"indexmap",
"intern",
"la-arena 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"paths",
"rustc-hash",
"serde",
"serde_json",
"span",
"stdx",
"text-size",
"tracing",
"tt",
]
Expand Down
4 changes: 2 additions & 2 deletions src/tools/rust-analyzer/crates/base-db/src/change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use salsa::Durability;
use triomphe::Arc;
use vfs::FileId;

use crate::{CrateGraph, SourceDatabaseExt, SourceDatabaseExt2, SourceRoot, SourceRootId};
use crate::{CrateGraph, SourceDatabaseFileInputExt, SourceRoot, SourceRootDatabase, SourceRootId};

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

pub fn apply(self, db: &mut dyn SourceDatabaseExt) {
pub fn apply(self, db: &mut dyn SourceRootDatabase) {
let _p = tracing::info_span!("FileChange::apply").entered();
if let Some(roots) = self.roots {
for (idx, root) in roots.into_iter().enumerate() {
Expand Down
52 changes: 25 additions & 27 deletions src/tools/rust-analyzer/crates/base-db/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! base_db defines basic database traits. The concrete DB is defined by ide.

// FIXME: Rename this crate, base db is non descriptive
mod change;
mod input;

Expand Down Expand Up @@ -47,8 +47,6 @@ pub const DEFAULT_PARSE_LRU_CAP: u16 = 128;
pub const DEFAULT_BORROWCK_LRU_CAP: u16 = 2024;

pub trait FileLoader {
/// Text of the file.
fn file_text(&self, file_id: FileId) -> Arc<str>;
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId>;
/// Crates whose root's source root is the same as the source root of `file_id`
fn relevant_crates(&self, file_id: FileId) -> Arc<[CrateId]>;
Expand All @@ -58,6 +56,13 @@ pub trait FileLoader {
/// model. Everything else in rust-analyzer is derived from these queries.
#[salsa::query_group(SourceDatabaseStorage)]
pub trait SourceDatabase: FileLoader + std::fmt::Debug {
#[salsa::input]
fn compressed_file_text(&self, file_id: FileId) -> Arc<[u8]>;

/// Text of the file.
#[salsa::lru]
fn file_text(&self, file_id: FileId) -> Arc<str>;

/// Parses the file into the syntax tree.
#[salsa::lru]
fn parse(&self, file_id: EditionedFileId) -> Parse<ast::SourceFile>;
Expand Down Expand Up @@ -99,16 +104,18 @@ fn parse_errors(db: &dyn SourceDatabase, file_id: EditionedFileId) -> Option<Arc
}
}

fn file_text(db: &dyn SourceDatabase, file_id: FileId) -> Arc<str> {
let bytes = db.compressed_file_text(file_id);
let bytes =
lz4_flex::decompress_size_prepended(&bytes).expect("lz4 decompression should not fail");
let text = std::str::from_utf8(&bytes).expect("file contents should be valid UTF-8");
Arc::from(text)
}

/// We don't want to give HIR knowledge of source roots, hence we extract these
/// methods into a separate DB.
#[salsa::query_group(SourceDatabaseExtStorage)]
pub trait SourceDatabaseExt: SourceDatabase {
#[salsa::input]
fn compressed_file_text(&self, file_id: FileId) -> Arc<[u8]>;

#[salsa::lru]
fn file_text(&self, file_id: FileId) -> Arc<str>;

#[salsa::query_group(SourceRootDatabaseStorage)]
pub trait SourceRootDatabase: SourceDatabase {
/// Path to a file, relative to the root of its source root.
/// Source root of the file.
#[salsa::input]
Expand All @@ -121,15 +128,7 @@ pub trait SourceDatabaseExt: SourceDatabase {
fn source_root_crates(&self, id: SourceRootId) -> Arc<[CrateId]>;
}

fn file_text(db: &dyn SourceDatabaseExt, file_id: FileId) -> Arc<str> {
let bytes = db.compressed_file_text(file_id);
let bytes =
lz4_flex::decompress_size_prepended(&bytes).expect("lz4 decompression should not fail");
let text = std::str::from_utf8(&bytes).expect("file contents should be valid UTF-8");
Arc::from(text)
}

pub trait SourceDatabaseExt2 {
pub trait SourceDatabaseFileInputExt {
fn set_file_text(&mut self, file_id: FileId, text: &str) {
self.set_file_text_with_durability(file_id, text, Durability::LOW);
}
Expand All @@ -142,7 +141,7 @@ pub trait SourceDatabaseExt2 {
);
}

impl<Db: ?Sized + SourceDatabaseExt> SourceDatabaseExt2 for Db {
impl<Db: ?Sized + SourceRootDatabase> SourceDatabaseFileInputExt for Db {
fn set_file_text_with_durability(
&mut self,
file_id: FileId,
Expand All @@ -159,7 +158,7 @@ impl<Db: ?Sized + SourceDatabaseExt> SourceDatabaseExt2 for Db {
}
}

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

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

impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
fn file_text(&self, file_id: FileId) -> Arc<str> {
SourceDatabaseExt::file_text(self.0, file_id)
}
impl<T: SourceRootDatabase> FileLoader for FileLoaderDelegate<&'_ T> {
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
// FIXME: this *somehow* should be platform agnostic...
let source_root = self.0.file_source_root(path.anchor);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use base_db::{SourceDatabase, SourceDatabaseExt2 as _};
use base_db::{SourceDatabase, SourceDatabaseFileInputExt as _};
use test_fixture::WithFixture;

use crate::{db::DefDatabase, nameres::tests::TestDB, AdtId, ModuleDefId};
Expand Down
5 changes: 1 addition & 4 deletions src/tools/rust-analyzer/crates/hir-def/src/test_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
};

#[salsa::database(
base_db::SourceDatabaseExtStorage,
base_db::SourceRootDatabaseStorage,
base_db::SourceDatabaseStorage,
hir_expand::db::ExpandDatabaseStorage,
crate::db::InternDatabaseStorage,
Expand Down Expand Up @@ -69,9 +69,6 @@ impl fmt::Debug for TestDB {
impl panic::RefUnwindSafe for TestDB {}

impl FileLoader for TestDB {
fn file_text(&self, file_id: FileId) -> Arc<str> {
FileLoaderDelegate(self).file_text(file_id)
}
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(path)
}
Expand Down
4 changes: 2 additions & 2 deletions src/tools/rust-analyzer/crates/hir-expand/src/change.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Defines a unit of change that can applied to the database to get the next
//! state. Changes are transactional.
use base_db::{
salsa::Durability, CrateGraph, CrateId, FileChange, SourceDatabaseExt, SourceRoot,
salsa::Durability, CrateGraph, CrateId, FileChange, SourceRoot, SourceRootDatabase,
TargetLayoutLoadResult, Version,
};
use la_arena::RawIdx;
Expand All @@ -23,7 +23,7 @@ impl ChangeWithProcMacros {
Self::default()
}

pub fn apply(self, db: &mut (impl ExpandDatabase + SourceDatabaseExt)) {
pub fn apply(self, db: &mut (impl ExpandDatabase + SourceRootDatabase)) {
self.source_change.apply(db);
if let Some(proc_macros) = self.proc_macros {
db.set_proc_macros_with_durability(Arc::new(proc_macros), Durability::HIGH);
Expand Down
7 changes: 6 additions & 1 deletion src/tools/rust-analyzer/crates/hir-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,12 @@ impl ExpandErrorKind {
&ExpandErrorKind::MissingProcMacroExpander(def_crate) => {
match db.proc_macros().get_error_for_crate(def_crate) {
Some((e, hard_err)) => (e.to_owned(), hard_err),
None => ("missing expander".to_owned(), true),
None => (
format!(
"internal error: proc-macro map is missing error entry for crate {def_crate:?}"
),
true,
),
}
}
ExpandErrorKind::MacroDefinition => {
Expand Down
5 changes: 1 addition & 4 deletions src/tools/rust-analyzer/crates/hir-ty/src/test_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use test_utils::extract_annotations;
use triomphe::Arc;

#[salsa::database(
base_db::SourceDatabaseExtStorage,
base_db::SourceRootDatabaseStorage,
base_db::SourceDatabaseStorage,
hir_expand::db::ExpandDatabaseStorage,
hir_def::db::InternDatabaseStorage,
Expand Down Expand Up @@ -75,9 +75,6 @@ impl salsa::ParallelDatabase for TestDB {
impl panic::RefUnwindSafe for TestDB {}

impl FileLoader for TestDB {
fn file_text(&self, file_id: FileId) -> Arc<str> {
FileLoaderDelegate(self).file_text(file_id)
}
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(path)
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/hir-ty/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod traits;

use std::env;

use base_db::SourceDatabaseExt2 as _;
use base_db::SourceDatabaseFileInputExt as _;
use expect_test::Expect;
use hir_def::{
body::{Body, BodySourceMap, SyntheticSyntax},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use base_db::SourceDatabaseExt2 as _;
use base_db::SourceDatabaseFileInputExt as _;
use test_fixture::WithFixture;

use crate::{db::HirDatabase, test_db::TestDB};
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/ide-assists/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod generated;
use expect_test::expect;
use hir::{FileRange, Semantics};
use ide_db::{
base_db::SourceDatabaseExt,
base_db::{SourceDatabase, SourceRootDatabase},
imports::insert_use::{ImportGranularity, InsertUseConfig},
source_change::FileSystemEdit,
EditionedFileId, RootDatabase, SnippetCap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::iter;

use hir::{HirFileIdExt, Module};
use ide_db::{
base_db::{SourceDatabaseExt, VfsPath},
base_db::{SourceRootDatabase, VfsPath},
FxHashSet, RootDatabase, SymbolKind,
};
use stdx::IsNoneOr;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/ide-completion/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ mod type_pos;
mod use_tree;
mod visibility;

use base_db::SourceDatabase;
use expect_test::Expect;
use hir::PrefixKind;
use ide_db::{
base_db::FileLoader,
imports::insert_use::{ImportGranularity, InsertUseConfig},
FilePosition, RootDatabase, SnippetCap,
};
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/ide-db/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::collections::VecDeque;

use base_db::SourceDatabaseExt;
use base_db::SourceRootDatabase;
use hir::{Crate, DescendPreference, ItemInNs, ModuleDef, Name, Semantics};
use span::FileId;
use syntax::{
Expand Down
5 changes: 1 addition & 4 deletions src/tools/rust-analyzer/crates/ide-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub type FilePosition = FilePositionWrapper<FileId>;
pub type FileRange = FileRangeWrapper<FileId>;

#[salsa::database(
base_db::SourceDatabaseExtStorage,
base_db::SourceRootDatabaseStorage,
base_db::SourceDatabaseStorage,
hir::db::ExpandDatabaseStorage,
hir::db::DefDatabaseStorage,
Expand Down Expand Up @@ -125,9 +125,6 @@ impl Upcast<dyn HirDatabase> for RootDatabase {
}

impl FileLoader for RootDatabase {
fn file_text(&self, file_id: FileId) -> Arc<str> {
FileLoaderDelegate(self).file_text(file_id)
}
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(path)
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/ide-db/src/prime_caches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use hir::db::DefDatabase;
use crate::{
base_db::{
salsa::{Database, ParallelDatabase, Snapshot},
Cancelled, CrateId, SourceDatabase, SourceDatabaseExt,
Cancelled, CrateId, SourceDatabase, SourceRootDatabase,
},
FxIndexMap, RootDatabase,
};
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/ide-db/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use std::mem;

use base_db::{salsa::Database, SourceDatabase, SourceDatabaseExt};
use base_db::{salsa::Database, SourceDatabase, SourceRootDatabase};
use hir::{
sym, AsAssocItem, DefWithBody, DescendPreference, FileRange, HasAttrs, HasSource, HirFileIdExt,
InFile, InRealFile, ModuleSource, PathResolution, Semantics, Visibility,
Expand Down
4 changes: 2 additions & 2 deletions src/tools/rust-analyzer/crates/ide-db/src/symbol_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use std::{

use base_db::{
salsa::{self, ParallelDatabase},
SourceDatabaseExt, SourceRootId, Upcast,
SourceRootDatabase, SourceRootId, Upcast,
};
use fst::{raw::IndexedValue, Automaton, Streamer};
use hir::{
Expand Down Expand Up @@ -100,7 +100,7 @@ impl Query {
}

#[salsa::query_group(SymbolsDatabaseStorage)]
pub trait SymbolsDatabase: HirDatabase + SourceDatabaseExt + Upcast<dyn HirDatabase> {
pub trait SymbolsDatabase: HirDatabase + SourceRootDatabase + Upcast<dyn HirDatabase> {
/// The symbol index for a given module. These modules should only be in source roots that
/// are inside local_roots.
fn module_symbols(&self, module: Module) -> Arc<SymbolIndex>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::iter;

use hir::{db::DefDatabase, DefMap, InFile, ModuleSource};
use ide_db::{
base_db::{FileLoader, SourceDatabaseExt},
base_db::{FileLoader, SourceDatabase, SourceRootDatabase},
source_change::SourceChange,
FileId, FileRange, LineIndexDatabase,
};
Expand Down Expand Up @@ -47,7 +47,7 @@ pub(crate) fn unlinked_file(
//
// Only show this diagnostic on the first three characters of
// the file, to avoid overwhelming the user during startup.
range = FileLoader::file_text(ctx.sema.db, file_id)
range = SourceDatabase::file_text(ctx.sema.db, file_id)
.char_indices()
.take(3)
.last()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(clippy::print_stderr)]

use ide_db::{
assists::AssistResolveStrategy, base_db::SourceDatabaseExt, LineIndexDatabase, RootDatabase,
assists::AssistResolveStrategy, base_db::SourceDatabase, LineIndexDatabase, RootDatabase,
};
use itertools::Itertools;
use stdx::trim_indent;
Expand Down
6 changes: 2 additions & 4 deletions src/tools/rust-analyzer/crates/ide-ssr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub use crate::{errors::SsrError, from_comment::ssr_from_comment, matching::Matc

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

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

/// Finds matches for all added rules and returns edits for all found matches.
pub fn edits(&self) -> FxHashMap<FileId, TextEdit> {
use ide_db::base_db::SourceDatabaseExt;
let mut matches_by_file = FxHashMap::default();
for m in self.matches().matches {
matches_by_file
Expand Down Expand Up @@ -228,7 +227,6 @@ impl<'db> MatchFinder<'db> {
file_id: EditionedFileId,
snippet: &str,
) -> Vec<MatchDebugInfo> {
use ide_db::base_db::SourceDatabaseExt;
let file = self.sema.parse(file_id);
let mut res = Vec::new();
let file_text = self.sema.db.file_text(file_id.into());
Expand Down
Loading

0 comments on commit 6825324

Please sign in to comment.