diff options
author | Aleksey Kladov <[email protected]> | 2021-06-13 16:45:38 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2021-06-13 16:45:38 +0100 |
commit | 4af7a35197a1cb159458694e69e17bd83dc9edff (patch) | |
tree | e1a50270e17e19d9be974b9ea8516667f17469e9 /crates/hir_def | |
parent | 00303284b5cc3a82e32dc3ecbbcfeb2f99de6818 (diff) |
internal: remove def-level diagnostics tests
Diffstat (limited to 'crates/hir_def')
-rw-r--r-- | crates/hir_def/src/body/tests.rs | 28 | ||||
-rw-r--r-- | crates/hir_def/src/test_db.rs | 152 |
2 files changed, 9 insertions, 171 deletions
diff --git a/crates/hir_def/src/body/tests.rs b/crates/hir_def/src/body/tests.rs index 0dccabcfd..27d837d47 100644 --- a/crates/hir_def/src/body/tests.rs +++ b/crates/hir_def/src/body/tests.rs | |||
@@ -3,7 +3,7 @@ mod block; | |||
3 | use base_db::{fixture::WithFixture, SourceDatabase}; | 3 | use base_db::{fixture::WithFixture, SourceDatabase}; |
4 | use expect_test::Expect; | 4 | use expect_test::Expect; |
5 | 5 | ||
6 | use crate::{test_db::TestDB, ModuleDefId}; | 6 | use crate::ModuleDefId; |
7 | 7 | ||
8 | use super::*; | 8 | use super::*; |
9 | 9 | ||
@@ -28,11 +28,6 @@ fn lower(ra_fixture: &str) -> Arc<Body> { | |||
28 | db.body(fn_def.unwrap().into()) | 28 | db.body(fn_def.unwrap().into()) |
29 | } | 29 | } |
30 | 30 | ||
31 | fn check_diagnostics(ra_fixture: &str) { | ||
32 | let db: TestDB = TestDB::with_files(ra_fixture); | ||
33 | db.check_diagnostics(); | ||
34 | } | ||
35 | |||
36 | fn block_def_map_at(ra_fixture: &str) -> String { | 31 | fn block_def_map_at(ra_fixture: &str) -> String { |
37 | let (db, position) = crate::test_db::TestDB::with_position(ra_fixture); | 32 | let (db, position) = crate::test_db::TestDB::with_position(ra_fixture); |
38 | 33 | ||
@@ -57,7 +52,7 @@ fn check_at(ra_fixture: &str, expect: Expect) { | |||
57 | fn your_stack_belongs_to_me() { | 52 | fn your_stack_belongs_to_me() { |
58 | cov_mark::check!(your_stack_belongs_to_me); | 53 | cov_mark::check!(your_stack_belongs_to_me); |
59 | lower( | 54 | lower( |
60 | " | 55 | r#" |
61 | macro_rules! n_nuple { | 56 | macro_rules! n_nuple { |
62 | ($e:tt) => (); | 57 | ($e:tt) => (); |
63 | ($($rest:tt)*) => {{ | 58 | ($($rest:tt)*) => {{ |
@@ -65,7 +60,7 @@ macro_rules! n_nuple { | |||
65 | }}; | 60 | }}; |
66 | } | 61 | } |
67 | fn main() { n_nuple!(1,2,3); } | 62 | fn main() { n_nuple!(1,2,3); } |
68 | ", | 63 | "#, |
69 | ); | 64 | ); |
70 | } | 65 | } |
71 | 66 | ||
@@ -73,7 +68,7 @@ fn main() { n_nuple!(1,2,3); } | |||
73 | fn macro_resolve() { | 68 | fn macro_resolve() { |
74 | // Regression test for a path resolution bug introduced with inner item handling. | 69 | // Regression test for a path resolution bug introduced with inner item handling. |
75 | lower( | 70 | lower( |
76 | r" | 71 | r#" |
77 | macro_rules! vec { | 72 | macro_rules! vec { |
78 | () => { () }; | 73 | () => { () }; |
79 | ($elem:expr; $n:expr) => { () }; | 74 | ($elem:expr; $n:expr) => { () }; |
@@ -84,19 +79,6 @@ mod m { | |||
84 | let _ = vec![FileSet::default(); self.len()]; | 79 | let _ = vec![FileSet::default(); self.len()]; |
85 | } | 80 | } |
86 | } | 81 | } |
87 | ", | 82 | "#, |
88 | ); | 83 | ); |
89 | } | 84 | } |
90 | |||
91 | #[test] | ||
92 | fn unresolved_macro_diag() { | ||
93 | check_diagnostics( | ||
94 | r#" | ||
95 | fn f() { | ||
96 | m!(); | ||
97 | //^^^^ UnresolvedMacroCall | ||
98 | } | ||
99 | "#, | ||
100 | ); | ||
101 | } | ||
102 | |||
diff --git a/crates/hir_def/src/test_db.rs b/crates/hir_def/src/test_db.rs index a16203fdb..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), "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 | } |