diff options
Diffstat (limited to 'crates/hir_def/src/test_db.rs')
-rw-r--r-- | crates/hir_def/src/test_db.rs | 85 |
1 files changed, 66 insertions, 19 deletions
diff --git a/crates/hir_def/src/test_db.rs b/crates/hir_def/src/test_db.rs index 8fa703a57..a9c1e13e2 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 | ||
8 | use base_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, FilePosition, Upcast}; | 8 | use base_db::{ |
9 | salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, FilePosition, FileRange, Upcast, | ||
10 | }; | ||
9 | use base_db::{AnchoredPath, SourceDatabase}; | 11 | use base_db::{AnchoredPath, SourceDatabase}; |
10 | use hir_expand::diagnostics::Diagnostic; | ||
11 | use hir_expand::diagnostics::DiagnosticSinkBuilder; | ||
12 | use hir_expand::{db::AstDatabase, InFile}; | 12 | use hir_expand::{db::AstDatabase, InFile}; |
13 | use rustc_hash::FxHashMap; | 13 | use rustc_hash::FxHashMap; |
14 | use rustc_hash::FxHashSet; | 14 | use rustc_hash::FxHashSet; |
15 | use syntax::{algo, ast, AstNode, TextRange, TextSize}; | 15 | use syntax::{algo, ast, AstNode, SyntaxNode, SyntaxNodePtr, TextRange, TextSize}; |
16 | use test_utils::extract_annotations; | 16 | use test_utils::extract_annotations; |
17 | 17 | ||
18 | use crate::{ | 18 | use 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,72 @@ 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 { id, .. } => { | ||
282 | let item_tree = id.item_tree(self.upcast()); | ||
283 | let import = &item_tree[id.value]; | ||
284 | let node = InFile::new(id.file_id(), import.ast_id).to_node(self.upcast()); | ||
285 | (InFile::new(id.file_id(), node.syntax().clone()), "UnresolvedImport") | ||
286 | } | ||
287 | DefDiagnosticKind::UnconfiguredCode { ast, .. } => { | ||
288 | let node = ast.to_node(self.upcast()); | ||
289 | (InFile::new(ast.file_id, node.syntax().clone()), "UnconfiguredCode") | ||
290 | } | ||
291 | DefDiagnosticKind::UnresolvedProcMacro { ast, .. } => { | ||
292 | (ast.to_node(self.upcast()), "UnresolvedProcMacro") | ||
293 | } | ||
294 | DefDiagnosticKind::UnresolvedMacroCall { ast, .. } => { | ||
295 | let node = ast.to_node(self.upcast()); | ||
296 | (InFile::new(ast.file_id, node.syntax().clone()), "UnresolvedMacroCall") | ||
297 | } | ||
298 | DefDiagnosticKind::MacroError { ast, message } => { | ||
299 | (ast.to_node(self.upcast()), message.as_str()) | ||
300 | } | ||
301 | }; | ||
302 | |||
303 | let frange = node.as_ref().original_file_range(self); | ||
304 | cb(frange, message.to_string()) | ||
305 | } | ||
273 | 306 | ||
307 | for (_module_id, module) in crate_def_map.modules() { | ||
274 | for decl in module.scope.declarations() { | 308 | for decl in module.scope.declarations() { |
275 | if let ModuleDefId::FunctionId(it) = decl { | 309 | if let ModuleDefId::FunctionId(it) = decl { |
276 | let source_map = self.body_with_source_map(it.into()).1; | 310 | let source_map = self.body_with_source_map(it.into()).1; |
277 | source_map.add_diagnostics(self, &mut sink); | 311 | for diag in source_map.diagnostics() { |
312 | let (ptr, message): (InFile<SyntaxNodePtr>, &str) = match diag { | ||
313 | BodyDiagnostic::InactiveCode { node, .. } => { | ||
314 | (node.clone().map(|it| it.into()), "InactiveCode") | ||
315 | } | ||
316 | BodyDiagnostic::MacroError { node, message } => { | ||
317 | (node.clone().map(|it| it.into()), message.as_str()) | ||
318 | } | ||
319 | BodyDiagnostic::UnresolvedProcMacro { node } => { | ||
320 | (node.clone().map(|it| it.into()), "UnresolvedProcMacro") | ||
321 | } | ||
322 | BodyDiagnostic::UnresolvedMacroCall { node, .. } => { | ||
323 | (node.clone().map(|it| it.into()), "UnresolvedMacroCall") | ||
324 | } | ||
325 | }; | ||
326 | |||
327 | let root = self.parse_or_expand(ptr.file_id).unwrap(); | ||
328 | let node = ptr.map(|ptr| ptr.to_node(&root)); | ||
329 | let frange = node.as_ref().original_file_range(self); | ||
330 | cb(frange, message.to_string()) | ||
331 | } | ||
278 | } | 332 | } |
279 | } | 333 | } |
280 | } | 334 | } |
@@ -287,14 +341,7 @@ impl TestDB { | |||
287 | assert!(!annotations.is_empty()); | 341 | assert!(!annotations.is_empty()); |
288 | 342 | ||
289 | let mut actual: FxHashMap<FileId, Vec<(TextRange, String)>> = FxHashMap::default(); | 343 | let mut actual: FxHashMap<FileId, Vec<(TextRange, String)>> = FxHashMap::default(); |
290 | db.diagnostics(|d| { | 344 | 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)); | 345 | actual.entry(frange.file_id).or_default().push((frange.range, message)); |
299 | }); | 346 | }); |
300 | 347 | ||
@@ -319,7 +366,7 @@ impl TestDB { | |||
319 | assert!(annotations.is_empty()); | 366 | assert!(annotations.is_empty()); |
320 | 367 | ||
321 | let mut has_diagnostics = false; | 368 | let mut has_diagnostics = false; |
322 | db.diagnostics(|_| { | 369 | db.diagnostics(&mut |_, _| { |
323 | has_diagnostics = true; | 370 | has_diagnostics = true; |
324 | }); | 371 | }); |
325 | 372 | ||