diff options
Diffstat (limited to 'crates/hir_def/src/test_db.rs')
-rw-r--r-- | crates/hir_def/src/test_db.rs | 152 |
1 files changed, 4 insertions, 148 deletions
diff --git a/crates/hir_def/src/test_db.rs b/crates/hir_def/src/test_db.rs index b20b066e2..2635b556e 100644 --- a/crates/hir_def/src/test_db.rs +++ b/crates/hir_def/src/test_db.rs | |||
@@ -6,19 +6,16 @@ use std::{ | |||
6 | }; | 6 | }; |
7 | 7 | ||
8 | use base_db::{ | 8 | use base_db::{ |
9 | salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, FilePosition, FileRange, Upcast, | 9 | salsa, AnchoredPath, CrateId, FileId, FileLoader, FileLoaderDelegate, FilePosition, |
10 | SourceDatabase, Upcast, | ||
10 | }; | 11 | }; |
11 | use base_db::{AnchoredPath, SourceDatabase}; | ||
12 | use hir_expand::{db::AstDatabase, InFile}; | 12 | use hir_expand::{db::AstDatabase, InFile}; |
13 | use rustc_hash::FxHashMap; | ||
14 | use rustc_hash::FxHashSet; | 13 | use rustc_hash::FxHashSet; |
15 | use syntax::{algo, ast, AstNode, SyntaxNode, SyntaxNodePtr, TextRange, TextSize}; | 14 | use syntax::{algo, ast, AstNode}; |
16 | use test_utils::extract_annotations; | ||
17 | 15 | ||
18 | use crate::{ | 16 | use crate::{ |
19 | body::BodyDiagnostic, | ||
20 | db::DefDatabase, | 17 | db::DefDatabase, |
21 | nameres::{diagnostics::DefDiagnosticKind, DefMap, ModuleSource}, | 18 | nameres::{DefMap, ModuleSource}, |
22 | src::HasSource, | 19 | src::HasSource, |
23 | LocalModuleId, Lookup, ModuleDefId, ModuleId, | 20 | LocalModuleId, Lookup, ModuleDefId, ModuleId, |
24 | }; | 21 | }; |
@@ -245,145 +242,4 @@ impl TestDB { | |||
245 | }) | 242 | }) |
246 | .collect() | 243 | .collect() |
247 | } | 244 | } |
248 | |||
249 | pub(crate) fn extract_annotations(&self) -> FxHashMap<FileId, Vec<(TextRange, String)>> { | ||
250 | let mut files = Vec::new(); | ||
251 | let crate_graph = self.crate_graph(); | ||
252 | for krate in crate_graph.iter() { | ||
253 | let crate_def_map = self.crate_def_map(krate); | ||
254 | for (module_id, _) in crate_def_map.modules() { | ||
255 | let file_id = crate_def_map[module_id].origin.file_id(); | ||
256 | files.extend(file_id) | ||
257 | } | ||
258 | } | ||
259 | assert!(!files.is_empty()); | ||
260 | files | ||
261 | .into_iter() | ||
262 | .filter_map(|file_id| { | ||
263 | let text = self.file_text(file_id); | ||
264 | let annotations = extract_annotations(&text); | ||
265 | if annotations.is_empty() { | ||
266 | return None; | ||
267 | } | ||
268 | Some((file_id, annotations)) | ||
269 | }) | ||
270 | .collect() | ||
271 | } | ||
272 | |||
273 | pub(crate) fn diagnostics(&self, cb: &mut dyn FnMut(FileRange, String)) { | ||
274 | let crate_graph = self.crate_graph(); | ||
275 | for krate in crate_graph.iter() { | ||
276 | let crate_def_map = self.crate_def_map(krate); | ||
277 | |||
278 | for diag in crate_def_map.diagnostics() { | ||
279 | let (node, message): (InFile<SyntaxNode>, &str) = match &diag.kind { | ||
280 | DefDiagnosticKind::UnresolvedModule { ast, .. } => { | ||
281 | let node = ast.to_node(self.upcast()); | ||
282 | (InFile::new(ast.file_id, node.syntax().clone()), "UnresolvedModule") | ||
283 | } | ||
284 | DefDiagnosticKind::UnresolvedExternCrate { ast, .. } => { | ||
285 | let node = ast.to_node(self.upcast()); | ||
286 | (InFile::new(ast.file_id, node.syntax().clone()), "UnresolvedExternCrate") | ||
287 | } | ||
288 | DefDiagnosticKind::UnresolvedImport { id, .. } => { | ||
289 | let item_tree = id.item_tree(self.upcast()); | ||
290 | let import = &item_tree[id.value]; | ||
291 | let node = InFile::new(id.file_id(), import.ast_id).to_node(self.upcast()); | ||
292 | (InFile::new(id.file_id(), node.syntax().clone()), "UnresolvedImport") | ||
293 | } | ||
294 | DefDiagnosticKind::UnconfiguredCode { ast, .. } => { | ||
295 | let node = ast.to_node(self.upcast()); | ||
296 | (InFile::new(ast.file_id, node.syntax().clone()), "UnconfiguredCode") | ||
297 | } | ||
298 | DefDiagnosticKind::UnresolvedProcMacro { ast, .. } => { | ||
299 | (ast.to_node(self.upcast()), "UnresolvedProcMacro") | ||
300 | } | ||
301 | DefDiagnosticKind::UnresolvedMacroCall { ast, .. } => { | ||
302 | let node = ast.to_node(self.upcast()); | ||
303 | (InFile::new(ast.file_id, node.syntax().clone()), "UnresolvedMacroCall") | ||
304 | } | ||
305 | DefDiagnosticKind::MacroError { ast, message } => { | ||
306 | (ast.to_node(self.upcast()), message.as_str()) | ||
307 | } | ||
308 | DefDiagnosticKind::UnimplementedBuiltinMacro { ast } => { | ||
309 | let node = ast.to_node(self.upcast()); | ||
310 | ( | ||
311 | InFile::new(ast.file_id, node.syntax().clone()), | ||
312 | "UnimplementedBuiltinMacro", | ||
313 | ) | ||
314 | } | ||
315 | }; | ||
316 | |||
317 | let frange = node.as_ref().original_file_range(self); | ||
318 | cb(frange, message.to_string()) | ||
319 | } | ||
320 | |||
321 | for (_module_id, module) in crate_def_map.modules() { | ||
322 | for decl in module.scope.declarations() { | ||
323 | if let ModuleDefId::FunctionId(it) = decl { | ||
324 | let source_map = self.body_with_source_map(it.into()).1; | ||
325 | for diag in source_map.diagnostics() { | ||
326 | let (ptr, message): (InFile<SyntaxNodePtr>, &str) = match diag { | ||
327 | BodyDiagnostic::InactiveCode { node, .. } => { | ||
328 | (node.clone().map(|it| it.into()), "InactiveCode") | ||
329 | } | ||
330 | BodyDiagnostic::MacroError { node, message } => { | ||
331 | (node.clone().map(|it| it.into()), message.as_str()) | ||
332 | } | ||
333 | BodyDiagnostic::UnresolvedProcMacro { node } => { | ||
334 | (node.clone().map(|it| it.into()), "UnresolvedProcMacro") | ||
335 | } | ||
336 | BodyDiagnostic::UnresolvedMacroCall { node, .. } => { | ||
337 | (node.clone().map(|it| it.into()), "UnresolvedMacroCall") | ||
338 | } | ||
339 | }; | ||
340 | |||
341 | let root = self.parse_or_expand(ptr.file_id).unwrap(); | ||
342 | let node = ptr.map(|ptr| ptr.to_node(&root)); | ||
343 | let frange = node.as_ref().original_file_range(self); | ||
344 | cb(frange, message.to_string()) | ||
345 | } | ||
346 | } | ||
347 | } | ||
348 | } | ||
349 | } | ||
350 | } | ||
351 | |||
352 | pub(crate) fn check_diagnostics(&self) { | ||
353 | let db: &TestDB = self; | ||
354 | let annotations = db.extract_annotations(); | ||
355 | assert!(!annotations.is_empty()); | ||
356 | |||
357 | let mut actual: FxHashMap<FileId, Vec<(TextRange, String)>> = FxHashMap::default(); | ||
358 | db.diagnostics(&mut |frange, message| { | ||
359 | actual.entry(frange.file_id).or_default().push((frange.range, message)); | ||
360 | }); | ||
361 | |||
362 | for (file_id, diags) in actual.iter_mut() { | ||
363 | diags.sort_by_key(|it| it.0.start()); | ||
364 | let text = db.file_text(*file_id); | ||
365 | // For multiline spans, place them on line start | ||
366 | for (range, content) in diags { | ||
367 | if text[*range].contains('\n') { | ||
368 | *range = TextRange::new(range.start(), range.start() + TextSize::from(1)); | ||
369 | *content = format!("... {}", content); | ||
370 | } | ||
371 | } | ||
372 | } | ||
373 | |||
374 | assert_eq!(annotations, actual); | ||
375 | } | ||
376 | |||
377 | pub(crate) fn check_no_diagnostics(&self) { | ||
378 | let db: &TestDB = self; | ||
379 | let annotations = db.extract_annotations(); | ||
380 | assert!(annotations.is_empty()); | ||
381 | |||
382 | let mut has_diagnostics = false; | ||
383 | db.diagnostics(&mut |_, _| { | ||
384 | has_diagnostics = true; | ||
385 | }); | ||
386 | |||
387 | assert!(!has_diagnostics); | ||
388 | } | ||
389 | } | 245 | } |