Skip to content

Commit 4af7a35

Browse files
committed
internal: remove def-level diagnostics tests
1 parent 0030328 commit 4af7a35

File tree

3 files changed

+21
-171
lines changed

3 files changed

+21
-171
lines changed

crates/hir_def/src/body/tests.rs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod block;
33
use base_db::{fixture::WithFixture, SourceDatabase};
44
use expect_test::Expect;
55

6-
use crate::{test_db::TestDB, ModuleDefId};
6+
use crate::ModuleDefId;
77

88
use super::*;
99

@@ -28,11 +28,6 @@ fn lower(ra_fixture: &str) -> Arc<Body> {
2828
db.body(fn_def.unwrap().into())
2929
}
3030

31-
fn check_diagnostics(ra_fixture: &str) {
32-
let db: TestDB = TestDB::with_files(ra_fixture);
33-
db.check_diagnostics();
34-
}
35-
3631
fn block_def_map_at(ra_fixture: &str) -> String {
3732
let (db, position) = crate::test_db::TestDB::with_position(ra_fixture);
3833

@@ -57,23 +52,23 @@ fn check_at(ra_fixture: &str, expect: Expect) {
5752
fn your_stack_belongs_to_me() {
5853
cov_mark::check!(your_stack_belongs_to_me);
5954
lower(
60-
"
55+
r#"
6156
macro_rules! n_nuple {
6257
($e:tt) => ();
6358
($($rest:tt)*) => {{
6459
(n_nuple!($($rest)*)None,)
6560
}};
6661
}
6762
fn main() { n_nuple!(1,2,3); }
68-
",
63+
"#,
6964
);
7065
}
7166

7267
#[test]
7368
fn macro_resolve() {
7469
// Regression test for a path resolution bug introduced with inner item handling.
7570
lower(
76-
r"
71+
r#"
7772
macro_rules! vec {
7873
() => { () };
7974
($elem:expr; $n:expr) => { () };
@@ -84,19 +79,6 @@ mod m {
8479
let _ = vec![FileSet::default(); self.len()];
8580
}
8681
}
87-
",
82+
"#,
8883
);
8984
}
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-

crates/hir_def/src/test_db.rs

Lines changed: 4 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@ use std::{
66
};
77

88
use base_db::{
9-
salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, FilePosition, FileRange, Upcast,
9+
salsa, AnchoredPath, CrateId, FileId, FileLoader, FileLoaderDelegate, FilePosition,
10+
SourceDatabase, Upcast,
1011
};
11-
use base_db::{AnchoredPath, SourceDatabase};
1212
use hir_expand::{db::AstDatabase, InFile};
13-
use rustc_hash::FxHashMap;
1413
use rustc_hash::FxHashSet;
15-
use syntax::{algo, ast, AstNode, SyntaxNode, SyntaxNodePtr, TextRange, TextSize};
16-
use test_utils::extract_annotations;
14+
use syntax::{algo, ast, AstNode};
1715

1816
use crate::{
19-
body::BodyDiagnostic,
2017
db::DefDatabase,
21-
nameres::{diagnostics::DefDiagnosticKind, DefMap, ModuleSource},
18+
nameres::{DefMap, ModuleSource},
2219
src::HasSource,
2320
LocalModuleId, Lookup, ModuleDefId, ModuleId,
2421
};
@@ -245,145 +242,4 @@ impl TestDB {
245242
})
246243
.collect()
247244
}
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-
}
389245
}

crates/ide/src/diagnostics/unresolved_macro_call.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ pub(super) fn unresolved_macro_call(
3434
mod tests {
3535
use crate::diagnostics::tests::check_diagnostics;
3636

37+
#[test]
38+
fn unresolved_macro_diag() {
39+
check_diagnostics(
40+
r#"
41+
fn f() {
42+
m!();
43+
} //^ unresolved macro `m!`
44+
45+
"#,
46+
);
47+
}
48+
3749
#[test]
3850
fn test_unresolved_macro_range() {
3951
check_diagnostics(

0 commit comments

Comments
 (0)