aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src/test_db.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_def/src/test_db.rs')
-rw-r--r--crates/hir_def/src/test_db.rs83
1 files changed, 64 insertions, 19 deletions
diff --git a/crates/hir_def/src/test_db.rs b/crates/hir_def/src/test_db.rs
index 8fa703a57..6c357c915 100644
--- a/crates/hir_def/src/test_db.rs
+++ b/crates/hir_def/src/test_db.rs
@@ -5,19 +5,20 @@ use std::{
5 sync::{Arc, Mutex}, 5 sync::{Arc, Mutex},
6}; 6};
7 7
8use base_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, FilePosition, Upcast}; 8use base_db::{
9 salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, FilePosition, FileRange, Upcast,
10};
9use base_db::{AnchoredPath, SourceDatabase}; 11use base_db::{AnchoredPath, SourceDatabase};
10use hir_expand::diagnostics::Diagnostic;
11use hir_expand::diagnostics::DiagnosticSinkBuilder;
12use hir_expand::{db::AstDatabase, InFile}; 12use hir_expand::{db::AstDatabase, InFile};
13use rustc_hash::FxHashMap; 13use rustc_hash::FxHashMap;
14use rustc_hash::FxHashSet; 14use rustc_hash::FxHashSet;
15use syntax::{algo, ast, AstNode, TextRange, TextSize}; 15use syntax::{algo, ast, AstNode, SyntaxNode, SyntaxNodePtr, TextRange, TextSize};
16use test_utils::extract_annotations; 16use test_utils::extract_annotations;
17 17
18use crate::{ 18use crate::{
19 body::BodyDiagnostic,
19 db::DefDatabase, 20 db::DefDatabase,
20 nameres::{DefMap, ModuleSource}, 21 nameres::{diagnostics::DefDiagnosticKind, DefMap, ModuleSource},
21 src::HasSource, 22 src::HasSource,
22 LocalModuleId, Lookup, ModuleDefId, ModuleId, 23 LocalModuleId, Lookup, ModuleDefId, ModuleId,
23}; 24};
@@ -262,19 +263,70 @@ impl TestDB {
262 .collect() 263 .collect()
263 } 264 }
264 265
265 pub(crate) fn diagnostics<F: FnMut(&dyn Diagnostic)>(&self, mut cb: F) { 266 pub(crate) fn diagnostics(&self, cb: &mut dyn FnMut(FileRange, String)) {
266 let crate_graph = self.crate_graph(); 267 let crate_graph = self.crate_graph();
267 for krate in crate_graph.iter() { 268 for krate in crate_graph.iter() {
268 let crate_def_map = self.crate_def_map(krate); 269 let crate_def_map = self.crate_def_map(krate);
269 270
270 let mut sink = DiagnosticSinkBuilder::new().build(&mut cb); 271 for diag in crate_def_map.diagnostics() {
271 for (module_id, module) in crate_def_map.modules() { 272 let (node, message): (InFile<SyntaxNode>, &str) = match &diag.kind {
272 crate_def_map.add_diagnostics(self, module_id, &mut sink); 273 DefDiagnosticKind::UnresolvedModule { ast, .. } => {
274 let node = ast.to_node(self.upcast());
275 (InFile::new(ast.file_id, node.syntax().clone()), "UnresolvedModule")
276 }
277 DefDiagnosticKind::UnresolvedExternCrate { ast, .. } => {
278 let node = ast.to_node(self.upcast());
279 (InFile::new(ast.file_id, node.syntax().clone()), "UnresolvedExternCrate")
280 }
281 DefDiagnosticKind::UnresolvedImport { ast, .. } => {
282 let node = ast.to_node(self.upcast());
283 (InFile::new(ast.file_id, node.syntax().clone()), "UnresolvedImport")
284 }
285 DefDiagnosticKind::UnconfiguredCode { ast, .. } => {
286 let node = ast.to_node(self.upcast());
287 (InFile::new(ast.file_id, node.syntax().clone()), "UnconfiguredCode")
288 }
289 DefDiagnosticKind::UnresolvedProcMacro { ast, .. } => {
290 (ast.to_node(self.upcast()), "UnresolvedProcMacro")
291 }
292 DefDiagnosticKind::UnresolvedMacroCall { ast, .. } => {
293 let node = ast.to_node(self.upcast());
294 (InFile::new(ast.file_id, node.syntax().clone()), "UnresolvedMacroCall")
295 }
296 DefDiagnosticKind::MacroError { ast, message } => {
297 (ast.to_node(self.upcast()), message.as_str())
298 }
299 };
300
301 let frange = node.as_ref().original_file_range(self);
302 cb(frange, message.to_string())
303 }
273 304
305 for (_module_id, module) in crate_def_map.modules() {
274 for decl in module.scope.declarations() { 306 for decl in module.scope.declarations() {
275 if let ModuleDefId::FunctionId(it) = decl { 307 if let ModuleDefId::FunctionId(it) = decl {
276 let source_map = self.body_with_source_map(it.into()).1; 308 let source_map = self.body_with_source_map(it.into()).1;
277 source_map.add_diagnostics(self, &mut sink); 309 for diag in source_map.diagnostics() {
310 let (ptr, message): (InFile<SyntaxNodePtr>, &str) = match diag {
311 BodyDiagnostic::InactiveCode { node, .. } => {
312 (node.clone().map(|it| it.into()), "InactiveCode")
313 }
314 BodyDiagnostic::MacroError { node, message } => {
315 (node.clone().map(|it| it.into()), message.as_str())
316 }
317 BodyDiagnostic::UnresolvedProcMacro { node } => {
318 (node.clone().map(|it| it.into()), "UnresolvedProcMacro")
319 }
320 BodyDiagnostic::UnresolvedMacroCall { node, .. } => {
321 (node.clone().map(|it| it.into()), "UnresolvedMacroCall")
322 }
323 };
324
325 let root = self.parse_or_expand(ptr.file_id).unwrap();
326 let node = ptr.map(|ptr| ptr.to_node(&root));
327 let frange = node.as_ref().original_file_range(self);
328 cb(frange, message.to_string())
329 }
278 } 330 }
279 } 331 }
280 } 332 }
@@ -287,14 +339,7 @@ impl TestDB {
287 assert!(!annotations.is_empty()); 339 assert!(!annotations.is_empty());
288 340
289 let mut actual: FxHashMap<FileId, Vec<(TextRange, String)>> = FxHashMap::default(); 341 let mut actual: FxHashMap<FileId, Vec<(TextRange, String)>> = FxHashMap::default();
290 db.diagnostics(|d| { 342 db.diagnostics(&mut |frange, message| {
291 let src = d.display_source();
292 let root = db.parse_or_expand(src.file_id).unwrap();
293
294 let node = src.map(|ptr| ptr.to_node(&root));
295 let frange = node.as_ref().original_file_range(db);
296
297 let message = d.message();
298 actual.entry(frange.file_id).or_default().push((frange.range, message)); 343 actual.entry(frange.file_id).or_default().push((frange.range, message));
299 }); 344 });
300 345
@@ -319,7 +364,7 @@ impl TestDB {
319 assert!(annotations.is_empty()); 364 assert!(annotations.is_empty());
320 365
321 let mut has_diagnostics = false; 366 let mut has_diagnostics = false;
322 db.diagnostics(|_| { 367 db.diagnostics(&mut |_, _| {
323 has_diagnostics = true; 368 has_diagnostics = true;
324 }); 369 });
325 370