]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/test_db.rs
Merge #7353
[rust.git] / crates / hir_def / src / test_db.rs
1 //! Database used for testing `hir_def`.
2
3 use std::{
4     fmt, panic,
5     sync::{Arc, Mutex},
6 };
7
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;
17
18 use crate::{db::DefDatabase, ModuleDefId};
19
20 #[salsa::database(
21     base_db::SourceDatabaseExtStorage,
22     base_db::SourceDatabaseStorage,
23     hir_expand::db::AstDatabaseStorage,
24     crate::db::InternDatabaseStorage,
25     crate::db::DefDatabaseStorage
26 )]
27 #[derive(Default)]
28 pub(crate) struct TestDB {
29     storage: salsa::Storage<TestDB>,
30     events: Mutex<Option<Vec<salsa::Event>>>,
31 }
32
33 impl Upcast<dyn AstDatabase> for TestDB {
34     fn upcast(&self) -> &(dyn AstDatabase + 'static) {
35         &*self
36     }
37 }
38
39 impl Upcast<dyn DefDatabase> for TestDB {
40     fn upcast(&self) -> &(dyn DefDatabase + 'static) {
41         &*self
42     }
43 }
44
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 {
49             events.push(event);
50         }
51     }
52 }
53
54 impl fmt::Debug for TestDB {
55     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56         f.debug_struct("TestDB").finish()
57     }
58 }
59
60 impl panic::RefUnwindSafe for TestDB {}
61
62 impl FileLoader for TestDB {
63     fn file_text(&self, file_id: FileId) -> Arc<String> {
64         FileLoaderDelegate(self).file_text(file_id)
65     }
66     fn resolve_path(&self, path: AnchoredPath) -> Option<FileId> {
67         FileLoaderDelegate(self).resolve_path(path)
68     }
69     fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
70         FileLoaderDelegate(self).relevant_crates(file_id)
71     }
72 }
73
74 impl TestDB {
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() {
79                 if data.origin.file_id() == Some(file_id) {
80                     return crate::ModuleId { krate, local_id };
81                 }
82             }
83         }
84         panic!("Can't find module for file")
85     }
86
87     pub(crate) fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event> {
88         *self.events.lock().unwrap() = Some(Vec::new());
89         f();
90         self.events.lock().unwrap().take().unwrap()
91     }
92
93     pub(crate) fn log_executed(&self, f: impl FnOnce()) -> Vec<String> {
94         let events = self.log(f);
95         events
96             .into_iter()
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)))
102                 }
103                 _ => None,
104             })
105             .collect()
106     }
107
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() {
114                 let file_id = crate_def_map[module_id].origin.file_id();
115                 files.extend(file_id)
116             }
117         }
118         assert!(!files.is_empty());
119         files
120             .into_iter()
121             .filter_map(|file_id| {
122                 let text = self.file_text(file_id);
123                 let annotations = extract_annotations(&text);
124                 if annotations.is_empty() {
125                     return None;
126                 }
127                 Some((file_id, annotations))
128             })
129             .collect()
130     }
131
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);
136
137             let mut sink = DiagnosticSinkBuilder::new().build(&mut cb);
138             for (module_id, module) in crate_def_map.modules() {
139                 crate_def_map.add_diagnostics(self, module_id, &mut sink);
140
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);
145                     }
146                 }
147             }
148         }
149     }
150
151     pub(crate) fn check_diagnostics(&self) {
152         let db: &TestDB = self;
153         let annotations = db.extract_annotations();
154         assert!(!annotations.is_empty());
155
156         let mut actual: FxHashMap<FileId, Vec<(TextRange, String)>> = FxHashMap::default();
157         db.diagnostics(|d| {
158             let src = d.display_source();
159             let root = db.parse_or_expand(src.file_id).unwrap();
160
161             let node = src.map(|ptr| ptr.to_node(&root));
162             let frange = node.as_ref().original_file_range(db);
163
164             let message = d.message().to_owned();
165             actual.entry(frange.file_id).or_default().push((frange.range, message));
166         });
167
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);
176                 }
177             }
178         }
179
180         assert_eq!(annotations, actual);
181     }
182 }