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
25 pub(crate) struct TestDB {
26 storage: salsa::Storage<TestDB>,
27 events: Mutex<Option<Vec<salsa::Event>>>,
30 impl Default for TestDB {
31 fn default() -> Self {
32 let mut this = Self { storage: Default::default(), events: Default::default() };
33 this.set_enable_proc_attr_macros(true);
38 impl fmt::Debug for TestDB {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 f.debug_struct("TestDB").finish()
44 impl Upcast<dyn AstDatabase> for TestDB {
45 fn upcast(&self) -> &(dyn AstDatabase + 'static) {
50 impl Upcast<dyn DefDatabase> for TestDB {
51 fn upcast(&self) -> &(dyn DefDatabase + 'static) {
56 impl salsa::Database for TestDB {
57 fn salsa_event(&self, event: salsa::Event) {
58 let mut events = self.events.lock().unwrap();
59 if let Some(events) = &mut *events {
65 impl salsa::ParallelDatabase for TestDB {
66 fn snapshot(&self) -> salsa::Snapshot<TestDB> {
67 salsa::Snapshot::new(TestDB {
68 storage: self.storage.snapshot(),
69 events: Default::default(),
74 impl panic::RefUnwindSafe for TestDB {}
76 impl FileLoader for TestDB {
77 fn file_text(&self, file_id: FileId) -> Arc<String> {
78 FileLoaderDelegate(self).file_text(file_id)
80 fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
81 FileLoaderDelegate(self).resolve_path(path)
83 fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
84 FileLoaderDelegate(self).relevant_crates(file_id)
89 pub(crate) fn module_for_file_opt(&self, file_id: FileId) -> Option<ModuleId> {
90 for &krate in self.relevant_crates(file_id).iter() {
91 let crate_def_map = self.crate_def_map(krate);
92 for (local_id, data) in crate_def_map.modules() {
93 if data.origin.file_id() == Some(file_id) {
94 return Some(crate_def_map.module_id(local_id));
101 pub(crate) fn module_for_file(&self, file_id: FileId) -> ModuleId {
102 self.module_for_file_opt(file_id).unwrap()
105 pub(crate) fn extract_annotations(&self) -> FxHashMap<FileId, Vec<(TextRange, String)>> {
106 let mut files = Vec::new();
107 let crate_graph = self.crate_graph();
108 for krate in crate_graph.iter() {
109 let crate_def_map = self.crate_def_map(krate);
110 for (module_id, _) in crate_def_map.modules() {
111 let file_id = crate_def_map[module_id].origin.file_id();
112 files.extend(file_id)
117 .filter_map(|file_id| {
118 let text = self.file_text(file_id);
119 let annotations = extract_annotations(&text);
120 if annotations.is_empty() {
123 Some((file_id, annotations))
130 pub(crate) fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event> {
131 *self.events.lock().unwrap() = Some(Vec::new());
133 self.events.lock().unwrap().take().unwrap()
136 pub(crate) fn log_executed(&self, f: impl FnOnce()) -> Vec<String> {
137 let events = self.log(f);
140 .filter_map(|e| match e.kind {
141 // This is pretty horrible, but `Debug` is the only way to inspect
142 // QueryDescriptor at the moment.
143 salsa::EventKind::WillExecute { database_key } => {
144 Some(format!("{:?}", database_key.debug(self)))