1 //! Database used for testing `hir`.
9 salsa, AnchoredPath, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase, Upcast,
11 use hir_def::{db::DefDatabase, ModuleId};
12 use hir_expand::db::AstDatabase;
13 use rustc_hash::{FxHashMap, FxHashSet};
14 use syntax::TextRange;
15 use test_utils::extract_annotations;
18 base_db::SourceDatabaseExtStorage,
19 base_db::SourceDatabaseStorage,
20 hir_expand::db::AstDatabaseStorage,
21 hir_def::db::InternDatabaseStorage,
22 hir_def::db::DefDatabaseStorage,
23 crate::db::HirDatabaseStorage
26 pub(crate) struct TestDB {
27 storage: salsa::Storage<TestDB>,
28 events: Mutex<Option<Vec<salsa::Event>>>,
30 impl fmt::Debug for TestDB {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 f.debug_struct("TestDB").finish()
36 impl Upcast<dyn AstDatabase> for TestDB {
37 fn upcast(&self) -> &(dyn AstDatabase + 'static) {
42 impl Upcast<dyn DefDatabase> for TestDB {
43 fn upcast(&self) -> &(dyn DefDatabase + 'static) {
48 impl salsa::Database for TestDB {
49 fn salsa_event(&self, event: salsa::Event) {
50 let mut events = self.events.lock().unwrap();
51 if let Some(events) = &mut *events {
57 impl salsa::ParallelDatabase for TestDB {
58 fn snapshot(&self) -> salsa::Snapshot<TestDB> {
59 salsa::Snapshot::new(TestDB {
60 storage: self.storage.snapshot(),
61 events: Default::default(),
66 impl panic::RefUnwindSafe for TestDB {}
68 impl FileLoader for TestDB {
69 fn file_text(&self, file_id: FileId) -> Arc<String> {
70 FileLoaderDelegate(self).file_text(file_id)
72 fn resolve_path(&self, path: AnchoredPath) -> Option<FileId> {
73 FileLoaderDelegate(self).resolve_path(path)
75 fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
76 FileLoaderDelegate(self).relevant_crates(file_id)
81 pub(crate) fn module_for_file(&self, file_id: FileId) -> ModuleId {
82 for &krate in self.relevant_crates(file_id).iter() {
83 let crate_def_map = self.crate_def_map(krate);
84 for (local_id, data) in crate_def_map.modules() {
85 if data.origin.file_id() == Some(file_id) {
86 return crate_def_map.module_id(local_id);
90 panic!("Can't find module for file")
93 pub(crate) fn extract_annotations(&self) -> FxHashMap<FileId, Vec<(TextRange, String)>> {
94 let mut files = Vec::new();
95 let crate_graph = self.crate_graph();
96 for krate in crate_graph.iter() {
97 let crate_def_map = self.crate_def_map(krate);
98 for (module_id, _) in crate_def_map.modules() {
99 let file_id = crate_def_map[module_id].origin.file_id();
100 files.extend(file_id)
105 .filter_map(|file_id| {
106 let text = self.file_text(file_id);
107 let annotations = extract_annotations(&text);
108 if annotations.is_empty() {
111 Some((file_id, annotations))
118 pub(crate) fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event> {
119 *self.events.lock().unwrap() = Some(Vec::new());
121 self.events.lock().unwrap().take().unwrap()
124 pub(crate) fn log_executed(&self, f: impl FnOnce()) -> Vec<String> {
125 let events = self.log(f);
128 .filter_map(|e| match e.kind {
129 // This pretty horrible, but `Debug` is the only way to inspect
130 // QueryDescriptor at the moment.
131 salsa::EventKind::WillExecute { database_key } => {
132 Some(format!("{:?}", database_key.debug(self)))