1 //! Database used for testing `hir_def`.
8 use base_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, Upcast};
9 use base_db::{AnchoredPath, SourceDatabase};
10 use hir_expand::db::AstDatabase;
11 use hir_expand::diagnostics::Diagnostic;
12 use hir_expand::diagnostics::DiagnosticSinkBuilder;
13 use rustc_hash::FxHashMap;
14 use rustc_hash::FxHashSet;
15 use syntax::{TextRange, TextSize};
16 use test_utils::extract_annotations;
18 use crate::{db::DefDatabase, ModuleDefId};
21 base_db::SourceDatabaseExtStorage,
22 base_db::SourceDatabaseStorage,
23 hir_expand::db::AstDatabaseStorage,
24 crate::db::InternDatabaseStorage,
25 crate::db::DefDatabaseStorage
28 pub(crate) struct TestDB {
29 storage: salsa::Storage<TestDB>,
30 events: Mutex<Option<Vec<salsa::Event>>>,
33 impl Upcast<dyn AstDatabase> for TestDB {
34 fn upcast(&self) -> &(dyn AstDatabase + 'static) {
39 impl Upcast<dyn DefDatabase> for TestDB {
40 fn upcast(&self) -> &(dyn DefDatabase + 'static) {
45 impl salsa::Database for TestDB {
46 fn salsa_event(&self, event: salsa::Event) {
47 let mut events = self.events.lock().unwrap();
48 if let Some(events) = &mut *events {
54 impl fmt::Debug for TestDB {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 f.debug_struct("TestDB").finish()
60 impl panic::RefUnwindSafe for TestDB {}
62 impl FileLoader for TestDB {
63 fn file_text(&self, file_id: FileId) -> Arc<String> {
64 FileLoaderDelegate(self).file_text(file_id)
66 fn resolve_path(&self, path: AnchoredPath) -> Option<FileId> {
67 FileLoaderDelegate(self).resolve_path(path)
69 fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
70 FileLoaderDelegate(self).relevant_crates(file_id)
75 pub(crate) fn module_for_file(&self, file_id: FileId) -> crate::ModuleId {
76 for &krate in self.relevant_crates(file_id).iter() {
77 let crate_def_map = self.crate_def_map(krate);
78 for (local_id, data) in crate_def_map.modules.iter() {
79 if data.origin.file_id() == Some(file_id) {
80 return crate::ModuleId { krate, local_id };
84 panic!("Can't find module for file")
87 pub(crate) fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event> {
88 *self.events.lock().unwrap() = Some(Vec::new());
90 self.events.lock().unwrap().take().unwrap()
93 pub(crate) fn log_executed(&self, f: impl FnOnce()) -> Vec<String> {
94 let events = self.log(f);
97 .filter_map(|e| match e.kind {
98 // This pretty horrible, but `Debug` is the only way to inspect
99 // QueryDescriptor at the moment.
100 salsa::EventKind::WillExecute { database_key } => {
101 Some(format!("{:?}", database_key.debug(self)))
108 pub(crate) fn extract_annotations(&self) -> FxHashMap<FileId, Vec<(TextRange, String)>> {
109 let mut files = Vec::new();
110 let crate_graph = self.crate_graph();
111 for krate in crate_graph.iter() {
112 let crate_def_map = self.crate_def_map(krate);
113 for (module_id, _) in crate_def_map.modules.iter() {
114 let file_id = crate_def_map[module_id].origin.file_id();
115 files.extend(file_id)
118 assert!(!files.is_empty());
121 .filter_map(|file_id| {
122 let text = self.file_text(file_id);
123 let annotations = extract_annotations(&text);
124 if annotations.is_empty() {
127 Some((file_id, annotations))
132 pub(crate) fn diagnostics<F: FnMut(&dyn Diagnostic)>(&self, mut cb: F) {
133 let crate_graph = self.crate_graph();
134 for krate in crate_graph.iter() {
135 let crate_def_map = self.crate_def_map(krate);
137 let mut sink = DiagnosticSinkBuilder::new().build(&mut cb);
138 for (module_id, module) in crate_def_map.modules.iter() {
139 crate_def_map.add_diagnostics(self, module_id, &mut sink);
141 for decl in module.scope.declarations() {
142 if let ModuleDefId::FunctionId(it) = decl {
143 let source_map = self.body_with_source_map(it.into()).1;
144 source_map.add_diagnostics(self, &mut sink);
151 pub(crate) fn check_diagnostics(&self) {
152 let db: &TestDB = self;
153 let annotations = db.extract_annotations();
154 assert!(!annotations.is_empty());
156 let mut actual: FxHashMap<FileId, Vec<(TextRange, String)>> = FxHashMap::default();
158 let src = d.display_source();
159 let root = db.parse_or_expand(src.file_id).unwrap();
161 let node = src.map(|ptr| ptr.to_node(&root));
162 let frange = node.as_ref().original_file_range(db);
164 let message = d.message().to_owned();
165 actual.entry(frange.file_id).or_default().push((frange.range, message));
168 for (file_id, diags) in actual.iter_mut() {
169 diags.sort_by_key(|it| it.0.start());
170 let text = db.file_text(*file_id);
171 // For multiline spans, place them on line start
172 for (range, content) in diags {
173 if text[*range].contains('\n') {
174 *range = TextRange::new(range.start(), range.start() + TextSize::from(1));
175 *content = format!("... {}", content);
180 assert_eq!(annotations, actual);