aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-03 20:35:48 +0000
committerAleksey Kladov <[email protected]>2019-11-03 20:35:48 +0000
commit6fba51c5fc05264abcbf971dcf28142746588d74 (patch)
treeedbdbf98b279802ee8dc03f35e941943e1e20e10 /crates
parent0933d914a37c4ab57fda6fe95464d194dab6f80c (diff)
move crate_def_map tests to hir_def
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_db/src/fixture.rs148
-rw-r--r--crates/ra_db/src/input.rs1
-rw-r--r--crates/ra_hir/src/nameres.rs3
-rw-r--r--crates/ra_hir_def/Cargo.toml4
-rw-r--r--crates/ra_hir_def/src/nameres.rs3
-rw-r--r--crates/ra_hir_def/src/nameres/tests.rs (renamed from crates/ra_hir/src/nameres/tests.rs)145
-rw-r--r--crates/ra_hir_def/src/nameres/tests/globs.rs (renamed from crates/ra_hir/src/nameres/tests/globs.rs)10
-rw-r--r--crates/ra_hir_def/src/nameres/tests/incremental.rs (renamed from crates/ra_hir/src/nameres/tests/incremental.rs)25
-rw-r--r--crates/ra_hir_def/src/nameres/tests/macros.rs (renamed from crates/ra_hir/src/nameres/tests/macros.rs)83
-rw-r--r--crates/ra_hir_def/src/nameres/tests/mod_resolution.rs (renamed from crates/ra_hir/src/nameres/tests/mod_resolution.rs)45
-rw-r--r--crates/ra_hir_def/src/nameres/tests/primitives.rs (renamed from crates/ra_hir/src/nameres/tests/primitives.rs)0
-rw-r--r--crates/ra_hir_def/src/test_db.rs36
12 files changed, 308 insertions, 195 deletions
diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs
index 469251fe9..f5dd59f84 100644
--- a/crates/ra_db/src/fixture.rs
+++ b/crates/ra_db/src/fixture.rs
@@ -3,9 +3,12 @@
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use ra_cfg::CfgOptions; 5use ra_cfg::CfgOptions;
6use rustc_hash::FxHashMap;
7use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER};
6 8
7use crate::{ 9use crate::{
8 CrateGraph, Edition, FileId, RelativePathBuf, SourceDatabaseExt, SourceRoot, SourceRootId, 10 CrateGraph, Edition, FileId, FilePosition, RelativePathBuf, SourceDatabaseExt, SourceRoot,
11 SourceRootId,
9}; 12};
10 13
11pub const WORKSPACE: SourceRootId = SourceRootId(0); 14pub const WORKSPACE: SourceRootId = SourceRootId(0);
@@ -16,6 +19,19 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static {
16 let file_id = with_single_file(&mut db, text); 19 let file_id = with_single_file(&mut db, text);
17 (db, file_id) 20 (db, file_id)
18 } 21 }
22
23 fn with_files(fixture: &str) -> Self {
24 let mut db = Self::default();
25 let pos = with_files(&mut db, fixture);
26 assert!(pos.is_none());
27 db
28 }
29
30 fn with_position(fixture: &str) -> (Self, FilePosition) {
31 let mut db = Self::default();
32 let pos = with_files(&mut db, fixture);
33 (db, pos.unwrap())
34 }
19} 35}
20 36
21impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {} 37impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {}
@@ -38,3 +54,133 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, text: &str) -> FileId {
38 54
39 file_id 55 file_id
40} 56}
57
58fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosition> {
59 let fixture = parse_fixture(fixture);
60
61 let mut crate_graph = CrateGraph::default();
62 let mut crates = FxHashMap::default();
63 let mut crate_deps = Vec::new();
64 let mut default_crate_root: Option<FileId> = None;
65
66 let mut source_root = SourceRoot::default();
67 let mut source_root_id = WORKSPACE;
68 let mut source_root_prefix: RelativePathBuf = "/".into();
69 let mut file_id = FileId(0);
70
71 let mut file_position = None;
72
73 for entry in fixture.iter() {
74 let meta = match parse_meta(&entry.meta) {
75 ParsedMeta::Root { path } => {
76 let source_root = std::mem::replace(&mut source_root, SourceRoot::default());
77 db.set_source_root(source_root_id, Arc::new(source_root));
78 source_root_id.0 += 1;
79 source_root_prefix = path;
80 continue;
81 }
82 ParsedMeta::File(it) => it,
83 };
84 assert!(meta.path.starts_with(&source_root_prefix));
85
86 if let Some(krate) = meta.krate {
87 let crate_id = crate_graph.add_crate_root(file_id, meta.edition, meta.cfg);
88 let prev = crates.insert(krate.clone(), crate_id);
89 assert!(prev.is_none());
90 for dep in meta.deps {
91 crate_deps.push((krate.clone(), dep))
92 }
93 } else if meta.path == "/main.rs" || meta.path == "/lib.rs" {
94 assert!(default_crate_root.is_none());
95 default_crate_root = Some(file_id);
96 }
97
98 let text = if entry.text.contains(CURSOR_MARKER) {
99 let (offset, text) = extract_offset(&entry.text);
100 assert!(file_position.is_none());
101 file_position = Some(FilePosition { file_id, offset });
102 text.to_string()
103 } else {
104 entry.text.to_string()
105 };
106
107 db.set_file_text(file_id, Arc::new(text));
108 db.set_file_relative_path(file_id, meta.path.clone());
109 db.set_file_source_root(file_id, source_root_id);
110 source_root.insert_file(meta.path, file_id);
111
112 file_id.0 += 1;
113 }
114
115 if crates.is_empty() {
116 let crate_root = default_crate_root.unwrap();
117 crate_graph.add_crate_root(crate_root, Edition::Edition2018, CfgOptions::default());
118 } else {
119 for (from, to) in crate_deps {
120 let from_id = crates[&from];
121 let to_id = crates[&to];
122 crate_graph.add_dep(from_id, to.into(), to_id).unwrap();
123 }
124 }
125
126 db.set_source_root(source_root_id, Arc::new(source_root));
127 db.set_crate_graph(Arc::new(crate_graph));
128
129 file_position
130}
131
132enum ParsedMeta {
133 Root { path: RelativePathBuf },
134 File(FileMeta),
135}
136
137struct FileMeta {
138 path: RelativePathBuf,
139 krate: Option<String>,
140 deps: Vec<String>,
141 cfg: CfgOptions,
142 edition: Edition,
143}
144
145//- /lib.rs crate:foo deps:bar,baz
146fn parse_meta(meta: &str) -> ParsedMeta {
147 let components = meta.split_ascii_whitespace().collect::<Vec<_>>();
148
149 if components[0] == "root" {
150 let path: RelativePathBuf = components[1].into();
151 assert!(path.starts_with("/") && path.ends_with("/"));
152 return ParsedMeta::Root { path };
153 }
154
155 let path: RelativePathBuf = components[0].into();
156 assert!(path.starts_with("/"));
157
158 let mut krate = None;
159 let mut deps = Vec::new();
160 let mut edition = Edition::Edition2018;
161 let mut cfg = CfgOptions::default();
162 for component in components[1..].iter() {
163 let (key, value) = split1(component, ':').unwrap();
164 match key {
165 "crate" => krate = Some(value.to_string()),
166 "deps" => deps = value.split(',').map(|it| it.to_string()).collect(),
167 "edition" => edition = Edition::from_string(&value),
168 "cfg" => {
169 for key in value.split(',') {
170 match split1(key, '=') {
171 None => cfg.insert_atom(key.into()),
172 Some((k, v)) => cfg.insert_key_value(k.into(), v.into()),
173 }
174 }
175 }
176 _ => panic!("bad component: {:?}", component),
177 }
178 }
179
180 ParsedMeta::File(FileMeta { path, krate, deps, edition, cfg })
181}
182
183fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> {
184 let idx = haystack.find(delim)?;
185 Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..]))
186}
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs
index eafa95921..7c8dac1d3 100644
--- a/crates/ra_db/src/input.rs
+++ b/crates/ra_db/src/input.rs
@@ -97,6 +97,7 @@ pub enum Edition {
97} 97}
98 98
99impl Edition { 99impl Edition {
100 //FIXME: replace with FromStr with proper error handling
100 pub fn from_string(s: &str) -> Edition { 101 pub fn from_string(s: &str) -> Edition {
101 match s { 102 match s {
102 "2015" => Edition::Edition2015, 103 "2015" => Edition::Edition2015,
diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs
index bb775cfc9..875addc84 100644
--- a/crates/ra_hir/src/nameres.rs
+++ b/crates/ra_hir/src/nameres.rs
@@ -47,9 +47,6 @@
47//! path and, upon success, we run macro expansion and "collect module" phase 47//! path and, upon success, we run macro expansion and "collect module" phase
48//! on the result 48//! on the result
49 49
50#[cfg(test)]
51mod tests;
52
53pub use hir_def::nameres::{ 50pub use hir_def::nameres::{
54 per_ns::{Namespace, PerNs}, 51 per_ns::{Namespace, PerNs},
55 raw::ImportId, 52 raw::ImportId,
diff --git a/crates/ra_hir_def/Cargo.toml b/crates/ra_hir_def/Cargo.toml
index 746c907e8..15055db64 100644
--- a/crates/ra_hir_def/Cargo.toml
+++ b/crates/ra_hir_def/Cargo.toml
@@ -19,3 +19,7 @@ test_utils = { path = "../test_utils" }
19mbe = { path = "../ra_mbe", package = "ra_mbe" } 19mbe = { path = "../ra_mbe", package = "ra_mbe" }
20ra_cfg = { path = "../ra_cfg" } 20ra_cfg = { path = "../ra_cfg" }
21tt = { path = "../ra_tt", package = "ra_tt" } 21tt = { path = "../ra_tt", package = "ra_tt" }
22
23[dev-dependencies]
24insta = "0.12.0"
25
diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs
index db59344aa..b3640da3d 100644
--- a/crates/ra_hir_def/src/nameres.rs
+++ b/crates/ra_hir_def/src/nameres.rs
@@ -6,6 +6,9 @@ pub mod per_ns;
6pub mod collector; 6pub mod collector;
7pub mod mod_resolution; 7pub mod mod_resolution;
8 8
9#[cfg(test)]
10mod tests;
11
9use std::sync::Arc; 12use std::sync::Arc;
10 13
11use hir_expand::{diagnostics::DiagnosticSink, name::Name, MacroDefId}; 14use hir_expand::{diagnostics::DiagnosticSink, name::Name, MacroDefId};
diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs
index 02db91a86..f9a8edd43 100644
--- a/crates/ra_hir/src/nameres/tests.rs
+++ b/crates/ra_hir_def/src/nameres/tests.rs
@@ -1,23 +1,24 @@
1mod macros;
2mod globs; 1mod globs;
3mod incremental; 2mod incremental;
4mod primitives; 3mod macros;
5mod mod_resolution; 4mod mod_resolution;
5mod primitives;
6 6
7use std::sync::Arc; 7use std::sync::Arc;
8 8
9use hir_def::{db::DefDatabase2, nameres::*, CrateModuleId};
10use insta::assert_snapshot; 9use insta::assert_snapshot;
11use ra_db::SourceDatabase; 10use ra_db::{fixture::WithFixture, SourceDatabase};
12// use test_utils::covers; 11// use test_utils::covers;
13 12
14use crate::mock::{CrateGraphFixture, MockDatabase}; 13use crate::{db::DefDatabase2, nameres::*, test_db::TestDB, CrateModuleId};
15 14
16fn compute_crate_def_map(fixture: &str, graph: Option<CrateGraphFixture>) -> Arc<CrateDefMap> { 15fn def_map(fixtute: &str) -> String {
17 let mut db = MockDatabase::with_files(fixture); 16 let dm = compute_crate_def_map(fixtute);
18 if let Some(graph) = graph { 17 render_crate_def_map(&dm)
19 db.set_crate_graph_from_fixture(graph); 18}
20 } 19
20fn compute_crate_def_map(fixture: &str) -> Arc<CrateDefMap> {
21 let db = TestDB::with_files(fixture);
21 let krate = db.crate_graph().iter().next().unwrap(); 22 let krate = db.crate_graph().iter().next().unwrap();
22 db.crate_def_map(krate) 23 db.crate_def_map(krate)
23} 24}
@@ -65,16 +66,6 @@ fn render_crate_def_map(map: &CrateDefMap) -> String {
65 } 66 }
66} 67}
67 68
68fn def_map(fixtute: &str) -> String {
69 let dm = compute_crate_def_map(fixtute, None);
70 render_crate_def_map(&dm)
71}
72
73fn def_map_with_crate_graph(fixture: &str, graph: CrateGraphFixture) -> String {
74 let dm = compute_crate_def_map(fixture, Some(graph));
75 render_crate_def_map(&dm)
76}
77
78#[test] 69#[test]
79fn crate_def_map_smoke_test() { 70fn crate_def_map_smoke_test() {
80 let map = def_map( 71 let map = def_map(
@@ -229,12 +220,12 @@ fn re_exports() {
229#[test] 220#[test]
230fn std_prelude() { 221fn std_prelude() {
231 // covers!(std_prelude); 222 // covers!(std_prelude);
232 let map = def_map_with_crate_graph( 223 let map = def_map(
233 " 224 "
234 //- /main.rs 225 //- /main.rs crate:main deps:test_crate
235 use Foo::*; 226 use Foo::*;
236 227
237 //- /lib.rs 228 //- /lib.rs crate:test_crate
238 mod prelude; 229 mod prelude;
239 #[prelude_import] 230 #[prelude_import]
240 use prelude::*; 231 use prelude::*;
@@ -242,10 +233,6 @@ fn std_prelude() {
242 //- /prelude.rs 233 //- /prelude.rs
243 pub enum Foo { Bar, Baz }; 234 pub enum Foo { Bar, Baz };
244 ", 235 ",
245 crate_graph! {
246 "main": ("/main.rs", ["test_crate"]),
247 "test_crate": ("/lib.rs", []),
248 },
249 ); 236 );
250 assert_snapshot!(map, @r###" 237 assert_snapshot!(map, @r###"
251 ⋮crate 238 ⋮crate
@@ -274,9 +261,9 @@ fn can_import_enum_variant() {
274 261
275#[test] 262#[test]
276fn edition_2015_imports() { 263fn edition_2015_imports() {
277 let map = def_map_with_crate_graph( 264 let map = def_map(
278 " 265 "
279 //- /main.rs 266 //- /main.rs crate:main deps:other_crate edition:2015
280 mod foo; 267 mod foo;
281 mod bar; 268 mod bar;
282 269
@@ -287,13 +274,9 @@ fn edition_2015_imports() {
287 use bar::Bar; 274 use bar::Bar;
288 use other_crate::FromLib; 275 use other_crate::FromLib;
289 276
290 //- /lib.rs 277 //- /lib.rs crate:other_crate edition:2018
291 struct FromLib; 278 struct FromLib;
292 ", 279 ",
293 crate_graph! {
294 "main": ("/main.rs", "2015", ["other_crate"]),
295 "other_crate": ("/lib.rs", "2018", []),
296 },
297 ); 280 );
298 281
299 assert_snapshot!(map, @r###" 282 assert_snapshot!(map, @r###"
@@ -338,18 +321,14 @@ fn item_map_using_self() {
338 321
339#[test] 322#[test]
340fn item_map_across_crates() { 323fn item_map_across_crates() {
341 let map = def_map_with_crate_graph( 324 let map = def_map(
342 " 325 "
343 //- /main.rs 326 //- /main.rs crate:main deps:test_crate
344 use test_crate::Baz; 327 use test_crate::Baz;
345 328
346 //- /lib.rs 329 //- /lib.rs crate:test_crate
347 pub struct Baz; 330 pub struct Baz;
348 ", 331 ",
349 crate_graph! {
350 "main": ("/main.rs", ["test_crate"]),
351 "test_crate": ("/lib.rs", []),
352 },
353 ); 332 );
354 333
355 assert_snapshot!(map, @r###" 334 assert_snapshot!(map, @r###"
@@ -360,9 +339,9 @@ fn item_map_across_crates() {
360 339
361#[test] 340#[test]
362fn extern_crate_rename() { 341fn extern_crate_rename() {
363 let map = def_map_with_crate_graph( 342 let map = def_map(
364 " 343 "
365 //- /main.rs 344 //- /main.rs crate:main deps:alloc
366 extern crate alloc as alloc_crate; 345 extern crate alloc as alloc_crate;
367 346
368 mod alloc; 347 mod alloc;
@@ -371,13 +350,9 @@ fn extern_crate_rename() {
371 //- /sync.rs 350 //- /sync.rs
372 use alloc_crate::Arc; 351 use alloc_crate::Arc;
373 352
374 //- /lib.rs 353 //- /lib.rs crate:alloc
375 struct Arc; 354 struct Arc;
376 ", 355 ",
377 crate_graph! {
378 "main": ("/main.rs", ["alloc"]),
379 "alloc": ("/lib.rs", []),
380 },
381 ); 356 );
382 357
383 assert_snapshot!(map, @r###" 358 assert_snapshot!(map, @r###"
@@ -392,9 +367,9 @@ fn extern_crate_rename() {
392 367
393#[test] 368#[test]
394fn extern_crate_rename_2015_edition() { 369fn extern_crate_rename_2015_edition() {
395 let map = def_map_with_crate_graph( 370 let map = def_map(
396 " 371 "
397 //- /main.rs 372 //- /main.rs crate:main deps:alloc edition:2015
398 extern crate alloc as alloc_crate; 373 extern crate alloc as alloc_crate;
399 374
400 mod alloc; 375 mod alloc;
@@ -403,13 +378,9 @@ fn extern_crate_rename_2015_edition() {
403 //- /sync.rs 378 //- /sync.rs
404 use alloc_crate::Arc; 379 use alloc_crate::Arc;
405 380
406 //- /lib.rs 381 //- /lib.rs crate:alloc
407 struct Arc; 382 struct Arc;
408 ", 383 ",
409 crate_graph! {
410 "main": ("/main.rs", "2015", ["alloc"]),
411 "alloc": ("/lib.rs", []),
412 },
413 ); 384 );
414 385
415 assert_snapshot!(map, 386 assert_snapshot!(map,
@@ -426,24 +397,21 @@ fn extern_crate_rename_2015_edition() {
426 397
427#[test] 398#[test]
428fn import_across_source_roots() { 399fn import_across_source_roots() {
429 let map = def_map_with_crate_graph( 400 let map = def_map(
430 " 401 "
431 //- /lib.rs 402 //- /main.rs crate:main deps:test_crate
403 use test_crate::a::b::C;
404
405 //- root /test_crate/
406
407 //- /test_crate/lib.rs crate:test_crate
432 pub mod a { 408 pub mod a {
433 pub mod b { 409 pub mod b {
434 pub struct C; 410 pub struct C;
435 } 411 }
436 } 412 }
437 413
438 //- root /main/
439
440 //- /main/main.rs
441 use test_crate::a::b::C;
442 ", 414 ",
443 crate_graph! {
444 "main": ("/main/main.rs", ["test_crate"]),
445 "test_crate": ("/lib.rs", []),
446 },
447 ); 415 );
448 416
449 assert_snapshot!(map, @r###" 417 assert_snapshot!(map, @r###"
@@ -454,12 +422,12 @@ fn import_across_source_roots() {
454 422
455#[test] 423#[test]
456fn reexport_across_crates() { 424fn reexport_across_crates() {
457 let map = def_map_with_crate_graph( 425 let map = def_map(
458 " 426 "
459 //- /main.rs 427 //- /main.rs crate:main deps:test_crate
460 use test_crate::Baz; 428 use test_crate::Baz;
461 429
462 //- /lib.rs 430 //- /lib.rs crate:test_crate
463 pub use foo::Baz; 431 pub use foo::Baz;
464 432
465 mod foo; 433 mod foo;
@@ -467,10 +435,6 @@ fn reexport_across_crates() {
467 //- /foo.rs 435 //- /foo.rs
468 pub struct Baz; 436 pub struct Baz;
469 ", 437 ",
470 crate_graph! {
471 "main": ("/main.rs", ["test_crate"]),
472 "test_crate": ("/lib.rs", []),
473 },
474 ); 438 );
475 439
476 assert_snapshot!(map, @r###" 440 assert_snapshot!(map, @r###"
@@ -481,19 +445,15 @@ fn reexport_across_crates() {
481 445
482#[test] 446#[test]
483fn values_dont_shadow_extern_crates() { 447fn values_dont_shadow_extern_crates() {
484 let map = def_map_with_crate_graph( 448 let map = def_map(
485 " 449 "
486 //- /main.rs 450 //- /main.rs crate:main deps:foo
487 fn foo() {} 451 fn foo() {}
488 use foo::Bar; 452 use foo::Bar;
489 453
490 //- /foo/lib.rs 454 //- /foo/lib.rs crate:foo
491 pub struct Bar; 455 pub struct Bar;
492 ", 456 ",
493 crate_graph! {
494 "main": ("/main.rs", ["foo"]),
495 "foo": ("/foo/lib.rs", []),
496 },
497 ); 457 );
498 458
499 assert_snapshot!(map, @r###" 459 assert_snapshot!(map, @r###"
@@ -505,11 +465,12 @@ fn values_dont_shadow_extern_crates() {
505 465
506#[test] 466#[test]
507fn cfg_not_test() { 467fn cfg_not_test() {
508 let map = def_map_with_crate_graph( 468 let map = def_map(
509 r#" 469 r#"
510 //- /main.rs 470 //- /main.rs crate:main deps:std
511 use {Foo, Bar, Baz}; 471 use {Foo, Bar, Baz};
512 //- /lib.rs 472
473 //- /lib.rs crate:std
513 #[prelude_import] 474 #[prelude_import]
514 pub use self::prelude::*; 475 pub use self::prelude::*;
515 mod prelude { 476 mod prelude {
@@ -521,10 +482,6 @@ fn cfg_not_test() {
521 pub struct Baz; 482 pub struct Baz;
522 } 483 }
523 "#, 484 "#,
524 crate_graph! {
525 "main": ("/main.rs", ["std"]),
526 "std": ("/lib.rs", []),
527 },
528 ); 485 );
529 486
530 assert_snapshot!(map, @r###" 487 assert_snapshot!(map, @r###"
@@ -537,11 +494,12 @@ fn cfg_not_test() {
537 494
538#[test] 495#[test]
539fn cfg_test() { 496fn cfg_test() {
540 let map = def_map_with_crate_graph( 497 let map = def_map(
541 r#" 498 r#"
542 //- /main.rs 499 //- /main.rs crate:main deps:std
543 use {Foo, Bar, Baz}; 500 use {Foo, Bar, Baz};
544 //- /lib.rs 501
502 //- /lib.rs crate:std cfg:test,feature=foo,feature=bar,opt=42
545 #[prelude_import] 503 #[prelude_import]
546 pub use self::prelude::*; 504 pub use self::prelude::*;
547 mod prelude { 505 mod prelude {
@@ -553,15 +511,6 @@ fn cfg_test() {
553 pub struct Baz; 511 pub struct Baz;
554 } 512 }
555 "#, 513 "#,
556 crate_graph! {
557 "main": ("/main.rs", ["std"]),
558 "std": ("/lib.rs", [], cfg = {
559 "test",
560 "feature" = "foo",
561 "feature" = "bar",
562 "opt" = "42",
563 }),
564 },
565 ); 514 );
566 515
567 assert_snapshot!(map, @r###" 516 assert_snapshot!(map, @r###"
diff --git a/crates/ra_hir/src/nameres/tests/globs.rs b/crates/ra_hir_def/src/nameres/tests/globs.rs
index b3e4d8d94..cf4a2a851 100644
--- a/crates/ra_hir/src/nameres/tests/globs.rs
+++ b/crates/ra_hir_def/src/nameres/tests/globs.rs
@@ -76,18 +76,14 @@ fn glob_2() {
76#[test] 76#[test]
77fn glob_across_crates() { 77fn glob_across_crates() {
78 // covers!(glob_across_crates); 78 // covers!(glob_across_crates);
79 let map = def_map_with_crate_graph( 79 let map = def_map(
80 " 80 "
81 //- /main.rs 81 //- /main.rs crate:main deps:test_crate
82 use test_crate::*; 82 use test_crate::*;
83 83
84 //- /lib.rs 84 //- /lib.rs crate:test_crate
85 pub struct Baz; 85 pub struct Baz;
86 ", 86 ",
87 crate_graph! {
88 "main": ("/main.rs", ["test_crate"]),
89 "test_crate": ("/lib.rs", []),
90 },
91 ); 87 );
92 assert_snapshot!(map, @r###" 88 assert_snapshot!(map, @r###"
93 ⋮crate 89 ⋮crate
diff --git a/crates/ra_hir/src/nameres/tests/incremental.rs b/crates/ra_hir_def/src/nameres/tests/incremental.rs
index 723ece7b0..80dcec62f 100644
--- a/crates/ra_hir/src/nameres/tests/incremental.rs
+++ b/crates/ra_hir_def/src/nameres/tests/incremental.rs
@@ -5,7 +5,7 @@ use ra_db::{SourceDatabase, SourceDatabaseExt};
5use super::*; 5use super::*;
6 6
7fn check_def_map_is_not_recomputed(initial: &str, file_change: &str) { 7fn check_def_map_is_not_recomputed(initial: &str, file_change: &str) {
8 let (mut db, pos) = MockDatabase::with_position(initial); 8 let (mut db, pos) = TestDB::with_position(initial);
9 let krate = db.crate_graph().iter().next().unwrap(); 9 let krate = db.crate_graph().iter().next().unwrap();
10 { 10 {
11 let events = db.log_executed(|| { 11 let events = db.log_executed(|| {
@@ -91,7 +91,7 @@ fn adding_inner_items_should_not_invalidate_def_map() {
91 91
92#[test] 92#[test]
93fn typing_inside_a_macro_should_not_invalidate_def_map() { 93fn typing_inside_a_macro_should_not_invalidate_def_map() {
94 let (mut db, pos) = MockDatabase::with_position( 94 let (mut db, pos) = TestDB::with_position(
95 " 95 "
96 //- /lib.rs 96 //- /lib.rs
97 macro_rules! m { 97 macro_rules! m {
@@ -111,15 +111,12 @@ fn typing_inside_a_macro_should_not_invalidate_def_map() {
111 m!(X); 111 m!(X);
112 ", 112 ",
113 ); 113 );
114 let krate = db.crate_graph().iter().next().unwrap();
114 { 115 {
115 let events = db.log_executed(|| { 116 let events = db.log_executed(|| {
116 let src = crate::Source { 117 let crate_def_map = db.crate_def_map(krate);
117 file_id: pos.file_id.into(), 118 let (_, module_data) = crate_def_map.modules.iter().last().unwrap();
118 ast: crate::ModuleSource::new(&db, Some(pos.file_id), None), 119 assert_eq!(module_data.scope.items.len(), 1);
119 };
120 let module = crate::Module::from_definition(&db, src).unwrap();
121 let decls = module.declarations(&db);
122 assert_eq!(decls.len(), 18);
123 }); 120 });
124 assert!(format!("{:?}", events).contains("crate_def_map"), "{:#?}", events) 121 assert!(format!("{:?}", events).contains("crate_def_map"), "{:#?}", events)
125 } 122 }
@@ -127,13 +124,9 @@ fn typing_inside_a_macro_should_not_invalidate_def_map() {
127 124
128 { 125 {
129 let events = db.log_executed(|| { 126 let events = db.log_executed(|| {
130 let src = crate::Source { 127 let crate_def_map = db.crate_def_map(krate);
131 file_id: pos.file_id.into(), 128 let (_, module_data) = crate_def_map.modules.iter().last().unwrap();
132 ast: crate::ModuleSource::new(&db, Some(pos.file_id), None), 129 assert_eq!(module_data.scope.items.len(), 1);
133 };
134 let module = crate::Module::from_definition(&db, src).unwrap();
135 let decls = module.declarations(&db);
136 assert_eq!(decls.len(), 18);
137 }); 130 });
138 assert!(!format!("{:?}", events).contains("crate_def_map"), "{:#?}", events) 131 assert!(!format!("{:?}", events).contains("crate_def_map"), "{:#?}", events)
139 } 132 }
diff --git a/crates/ra_hir/src/nameres/tests/macros.rs b/crates/ra_hir_def/src/nameres/tests/macros.rs
index 78bb0eb0d..9bb3895ad 100644
--- a/crates/ra_hir/src/nameres/tests/macros.rs
+++ b/crates/ra_hir_def/src/nameres/tests/macros.rs
@@ -71,16 +71,16 @@ fn macro_rules_can_define_modules() {
71 71
72#[test] 72#[test]
73fn macro_rules_from_other_crates_are_visible() { 73fn macro_rules_from_other_crates_are_visible() {
74 let map = def_map_with_crate_graph( 74 let map = def_map(
75 " 75 "
76 //- /main.rs 76 //- /main.rs crate:main deps:foo
77 foo::structs!(Foo, Bar) 77 foo::structs!(Foo, Bar)
78 mod bar; 78 mod bar;
79 79
80 //- /bar.rs 80 //- /bar.rs
81 use crate::*; 81 use crate::*;
82 82
83 //- /lib.rs 83 //- /lib.rs crate:foo
84 #[macro_export] 84 #[macro_export]
85 macro_rules! structs { 85 macro_rules! structs {
86 ($($i:ident),*) => { 86 ($($i:ident),*) => {
@@ -88,10 +88,6 @@ fn macro_rules_from_other_crates_are_visible() {
88 } 88 }
89 } 89 }
90 ", 90 ",
91 crate_graph! {
92 "main": ("/main.rs", ["foo"]),
93 "foo": ("/lib.rs", []),
94 },
95 ); 91 );
96 assert_snapshot!(map, @r###" 92 assert_snapshot!(map, @r###"
97 ⋮crate 93 ⋮crate
@@ -108,16 +104,16 @@ fn macro_rules_from_other_crates_are_visible() {
108 104
109#[test] 105#[test]
110fn macro_rules_export_with_local_inner_macros_are_visible() { 106fn macro_rules_export_with_local_inner_macros_are_visible() {
111 let map = def_map_with_crate_graph( 107 let map = def_map(
112 " 108 "
113 //- /main.rs 109 //- /main.rs crate:main deps:foo
114 foo::structs!(Foo, Bar) 110 foo::structs!(Foo, Bar)
115 mod bar; 111 mod bar;
116 112
117 //- /bar.rs 113 //- /bar.rs
118 use crate::*; 114 use crate::*;
119 115
120 //- /lib.rs 116 //- /lib.rs crate:foo
121 #[macro_export(local_inner_macros)] 117 #[macro_export(local_inner_macros)]
122 macro_rules! structs { 118 macro_rules! structs {
123 ($($i:ident),*) => { 119 ($($i:ident),*) => {
@@ -125,10 +121,6 @@ fn macro_rules_export_with_local_inner_macros_are_visible() {
125 } 121 }
126 } 122 }
127 ", 123 ",
128 crate_graph! {
129 "main": ("/main.rs", ["foo"]),
130 "foo": ("/lib.rs", []),
131 },
132 ); 124 );
133 assert_snapshot!(map, @r###" 125 assert_snapshot!(map, @r###"
134 ⋮crate 126 ⋮crate
@@ -145,9 +137,9 @@ fn macro_rules_export_with_local_inner_macros_are_visible() {
145 137
146#[test] 138#[test]
147fn unexpanded_macro_should_expand_by_fixedpoint_loop() { 139fn unexpanded_macro_should_expand_by_fixedpoint_loop() {
148 let map = def_map_with_crate_graph( 140 let map = def_map(
149 " 141 "
150 //- /main.rs 142 //- /main.rs crate:main deps:foo
151 macro_rules! baz { 143 macro_rules! baz {
152 () => { 144 () => {
153 use foo::bar; 145 use foo::bar;
@@ -158,7 +150,7 @@ fn unexpanded_macro_should_expand_by_fixedpoint_loop() {
158 bar!(); 150 bar!();
159 baz!(); 151 baz!();
160 152
161 //- /lib.rs 153 //- /lib.rs crate:foo
162 #[macro_export] 154 #[macro_export]
163 macro_rules! foo { 155 macro_rules! foo {
164 () => { 156 () => {
@@ -172,10 +164,6 @@ fn unexpanded_macro_should_expand_by_fixedpoint_loop() {
172 } 164 }
173 } 165 }
174 ", 166 ",
175 crate_graph! {
176 "main": ("/main.rs", ["foo"]),
177 "foo": ("/lib.rs", []),
178 },
179 ); 167 );
180 assert_snapshot!(map, @r###" 168 assert_snapshot!(map, @r###"
181 ⋮crate 169 ⋮crate
@@ -188,9 +176,9 @@ fn unexpanded_macro_should_expand_by_fixedpoint_loop() {
188#[test] 176#[test]
189fn macro_rules_from_other_crates_are_visible_with_macro_use() { 177fn macro_rules_from_other_crates_are_visible_with_macro_use() {
190 // covers!(macro_rules_from_other_crates_are_visible_with_macro_use); 178 // covers!(macro_rules_from_other_crates_are_visible_with_macro_use);
191 let map = def_map_with_crate_graph( 179 let map = def_map(
192 " 180 "
193 //- /main.rs 181 //- /main.rs crate:main deps:foo
194 structs!(Foo); 182 structs!(Foo);
195 structs_priv!(Bar); 183 structs_priv!(Bar);
196 structs_not_exported!(MacroNotResolved1); 184 structs_not_exported!(MacroNotResolved1);
@@ -205,7 +193,7 @@ fn macro_rules_from_other_crates_are_visible_with_macro_use() {
205 structs!(Baz); 193 structs!(Baz);
206 crate::structs!(MacroNotResolved3); 194 crate::structs!(MacroNotResolved3);
207 195
208 //- /lib.rs 196 //- /lib.rs crate:foo
209 #[macro_export] 197 #[macro_export]
210 macro_rules! structs { 198 macro_rules! structs {
211 ($i:ident) => { struct $i; } 199 ($i:ident) => { struct $i; }
@@ -222,10 +210,6 @@ fn macro_rules_from_other_crates_are_visible_with_macro_use() {
222 } 210 }
223 } 211 }
224 ", 212 ",
225 crate_graph! {
226 "main": ("/main.rs", ["foo"]),
227 "foo": ("/lib.rs", []),
228 },
229 ); 213 );
230 assert_snapshot!(map, @r###" 214 assert_snapshot!(map, @r###"
231 ⋮crate 215 ⋮crate
@@ -242,9 +226,9 @@ fn macro_rules_from_other_crates_are_visible_with_macro_use() {
242#[test] 226#[test]
243fn prelude_is_macro_use() { 227fn prelude_is_macro_use() {
244 // covers!(prelude_is_macro_use); 228 // covers!(prelude_is_macro_use);
245 let map = def_map_with_crate_graph( 229 let map = def_map(
246 " 230 "
247 //- /main.rs 231 //- /main.rs crate:main deps:foo
248 structs!(Foo); 232 structs!(Foo);
249 structs_priv!(Bar); 233 structs_priv!(Bar);
250 structs_outside!(Out); 234 structs_outside!(Out);
@@ -256,7 +240,7 @@ fn prelude_is_macro_use() {
256 structs!(Baz); 240 structs!(Baz);
257 crate::structs!(MacroNotResolved3); 241 crate::structs!(MacroNotResolved3);
258 242
259 //- /lib.rs 243 //- /lib.rs crate:foo
260 #[prelude_import] 244 #[prelude_import]
261 use self::prelude::*; 245 use self::prelude::*;
262 246
@@ -279,10 +263,6 @@ fn prelude_is_macro_use() {
279 ($i:ident) => { struct $i; } 263 ($i:ident) => { struct $i; }
280 } 264 }
281 ", 265 ",
282 crate_graph! {
283 "main": ("/main.rs", ["foo"]),
284 "foo": ("/lib.rs", []),
285 },
286 ); 266 );
287 assert_snapshot!(map, @r###" 267 assert_snapshot!(map, @r###"
288 ⋮crate 268 ⋮crate
@@ -447,16 +427,16 @@ fn type_value_macro_live_in_different_scopes() {
447 427
448#[test] 428#[test]
449fn macro_use_can_be_aliased() { 429fn macro_use_can_be_aliased() {
450 let map = def_map_with_crate_graph( 430 let map = def_map(
451 " 431 "
452 //- /main.rs 432 //- /main.rs crate:main deps:foo
453 #[macro_use] 433 #[macro_use]
454 extern crate foo; 434 extern crate foo;
455 435
456 foo!(Direct); 436 foo!(Direct);
457 bar!(Alias); 437 bar!(Alias);
458 438
459 //- /lib.rs 439 //- /lib.rs crate:foo
460 use crate::foo as bar; 440 use crate::foo as bar;
461 441
462 mod m { 442 mod m {
@@ -466,10 +446,6 @@ fn macro_use_can_be_aliased() {
466 } 446 }
467 } 447 }
468 ", 448 ",
469 crate_graph! {
470 "main": ("/main.rs", ["foo"]),
471 "foo": ("/lib.rs", []),
472 },
473 ); 449 );
474 assert_snapshot!(map, @r###" 450 assert_snapshot!(map, @r###"
475 ⋮crate 451 ⋮crate
@@ -533,9 +509,9 @@ fn path_qualified_macros() {
533fn macro_dollar_crate_is_correct_in_item() { 509fn macro_dollar_crate_is_correct_in_item() {
534 // covers!(macro_dollar_crate_self); 510 // covers!(macro_dollar_crate_self);
535 // covers!(macro_dollar_crate_other); 511 // covers!(macro_dollar_crate_other);
536 let map = def_map_with_crate_graph( 512 let map = def_map(
537 " 513 "
538 //- /main.rs 514 //- /main.rs crate:main deps:foo
539 #[macro_use] 515 #[macro_use]
540 extern crate foo; 516 extern crate foo;
541 517
@@ -554,7 +530,7 @@ fn macro_dollar_crate_is_correct_in_item() {
554 not_current1!(); 530 not_current1!();
555 foo::not_current2!(); 531 foo::not_current2!();
556 532
557 //- /lib.rs 533 //- /lib.rs crate:foo
558 mod m { 534 mod m {
559 #[macro_export] 535 #[macro_export]
560 macro_rules! not_current1 { 536 macro_rules! not_current1 {
@@ -574,10 +550,6 @@ fn macro_dollar_crate_is_correct_in_item() {
574 struct Bar; 550 struct Bar;
575 struct Baz; 551 struct Baz;
576 ", 552 ",
577 crate_graph! {
578 "main": ("/main.rs", ["foo"]),
579 "foo": ("/lib.rs", []),
580 },
581 ); 553 );
582 assert_snapshot!(map, @r###" 554 assert_snapshot!(map, @r###"
583 ⋮crate 555 ⋮crate
@@ -596,12 +568,12 @@ fn macro_dollar_crate_is_correct_in_item() {
596fn macro_dollar_crate_is_correct_in_indirect_deps() { 568fn macro_dollar_crate_is_correct_in_indirect_deps() {
597 // covers!(macro_dollar_crate_other); 569 // covers!(macro_dollar_crate_other);
598 // From std 570 // From std
599 let map = def_map_with_crate_graph( 571 let map = def_map(
600 r#" 572 r#"
601 //- /main.rs 573 //- /main.rs crate:main deps:std
602 foo!(); 574 foo!();
603 575
604 //- /std.rs 576 //- /std.rs crate:std deps:core
605 #[prelude_import] 577 #[prelude_import]
606 use self::prelude::*; 578 use self::prelude::*;
607 579
@@ -612,7 +584,7 @@ fn macro_dollar_crate_is_correct_in_indirect_deps() {
612 #[macro_use] 584 #[macro_use]
613 mod std_macros; 585 mod std_macros;
614 586
615 //- /core.rs 587 //- /core.rs crate:core
616 #[macro_export] 588 #[macro_export]
617 macro_rules! foo { 589 macro_rules! foo {
618 () => { 590 () => {
@@ -622,11 +594,6 @@ fn macro_dollar_crate_is_correct_in_indirect_deps() {
622 594
623 pub struct bar; 595 pub struct bar;
624 "#, 596 "#,
625 crate_graph! {
626 "main": ("/main.rs", ["std"]),
627 "std": ("/std.rs", ["core"]),
628 "core": ("/core.rs", []),
629 },
630 ); 597 );
631 assert_snapshot!(map, @r###" 598 assert_snapshot!(map, @r###"
632 ⋮crate 599 ⋮crate
diff --git a/crates/ra_hir/src/nameres/tests/mod_resolution.rs b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs
index abfe8b1c3..8d804a63e 100644
--- a/crates/ra_hir/src/nameres/tests/mod_resolution.rs
+++ b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs
@@ -54,18 +54,15 @@ fn nested_module_resolution() {
54 54
55#[test] 55#[test]
56fn module_resolution_works_for_non_standard_filenames() { 56fn module_resolution_works_for_non_standard_filenames() {
57 let map = def_map_with_crate_graph( 57 let map = def_map(
58 " 58 "
59 //- /my_library.rs 59 //- /my_library.rs crate:my_library
60 mod foo; 60 mod foo;
61 use self::foo::Bar; 61 use self::foo::Bar;
62 62
63 //- /foo/mod.rs 63 //- /foo/mod.rs
64 pub struct Bar; 64 pub struct Bar;
65 ", 65 ",
66 crate_graph! {
67 "my_library": ("/my_library.rs", []),
68 },
69 ); 66 );
70 67
71 assert_snapshot!(map, @r###" 68 assert_snapshot!(map, @r###"
@@ -650,7 +647,7 @@ fn module_resolution_decl_inside_inline_module_in_non_crate_root_2() {
650 647
651#[test] 648#[test]
652fn unresolved_module_diagnostics() { 649fn unresolved_module_diagnostics() {
653 let diagnostics = MockDatabase::with_files( 650 let db = TestDB::with_files(
654 r" 651 r"
655 //- /lib.rs 652 //- /lib.rs
656 mod foo; 653 mod foo;
@@ -658,11 +655,37 @@ fn unresolved_module_diagnostics() {
658 mod baz {} 655 mod baz {}
659 //- /foo.rs 656 //- /foo.rs
660 ", 657 ",
661 ) 658 );
662 .diagnostics(); 659 let krate = db.crate_graph().iter().next().unwrap();
663 660
664 assert_snapshot!(diagnostics, @r###" 661 let crate_def_map = db.crate_def_map(krate);
665 "mod bar;": unresolved module 662
663 insta::assert_debug_snapshot!(
664 crate_def_map.diagnostics,
665 @r###"
666 [
667 UnresolvedModule {
668 module: CrateModuleId(
669 0,
670 ),
671 declaration: AstId {
672 file_id: HirFileId(
673 FileId(
674 FileId(
675 0,
676 ),
677 ),
678 ),
679 file_ast_id: FileAstId {
680 raw: ErasedFileAstId(
681 1,
682 ),
683 _ty: PhantomData,
684 },
685 },
686 candidate: "bar.rs",
687 },
688 ]
666 "### 689 "###
667 ); 690 );
668} 691}
diff --git a/crates/ra_hir/src/nameres/tests/primitives.rs b/crates/ra_hir_def/src/nameres/tests/primitives.rs
index 0e2708658..0e2708658 100644
--- a/crates/ra_hir/src/nameres/tests/primitives.rs
+++ b/crates/ra_hir_def/src/nameres/tests/primitives.rs
diff --git a/crates/ra_hir_def/src/test_db.rs b/crates/ra_hir_def/src/test_db.rs
index 67714c68e..05018f8e4 100644
--- a/crates/ra_hir_def/src/test_db.rs
+++ b/crates/ra_hir_def/src/test_db.rs
@@ -1,4 +1,7 @@
1use std::{panic, sync::Arc}; 1use std::{
2 panic,
3 sync::{Arc, Mutex},
4};
2 5
3use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate}; 6use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate};
4use relative_path::RelativePath; 7use relative_path::RelativePath;
@@ -13,12 +16,20 @@ use relative_path::RelativePath;
13#[derive(Debug, Default)] 16#[derive(Debug, Default)]
14pub struct TestDB { 17pub struct TestDB {
15 runtime: salsa::Runtime<TestDB>, 18 runtime: salsa::Runtime<TestDB>,
19 events: Mutex<Option<Vec<salsa::Event<TestDB>>>>,
16} 20}
17 21
18impl salsa::Database for TestDB { 22impl salsa::Database for TestDB {
19 fn salsa_runtime(&self) -> &salsa::Runtime<Self> { 23 fn salsa_runtime(&self) -> &salsa::Runtime<Self> {
20 &self.runtime 24 &self.runtime
21 } 25 }
26
27 fn salsa_event(&self, event: impl Fn() -> salsa::Event<TestDB>) {
28 let mut events = self.events.lock().unwrap();
29 if let Some(events) = &mut *events {
30 events.push(event());
31 }
32 }
22} 33}
23 34
24impl panic::RefUnwindSafe for TestDB {} 35impl panic::RefUnwindSafe for TestDB {}
@@ -38,3 +49,26 @@ impl FileLoader for TestDB {
38 FileLoaderDelegate(self).relevant_crates(file_id) 49 FileLoaderDelegate(self).relevant_crates(file_id)
39 } 50 }
40} 51}
52
53impl TestDB {
54 pub fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event<TestDB>> {
55 *self.events.lock().unwrap() = Some(Vec::new());
56 f();
57 self.events.lock().unwrap().take().unwrap()
58 }
59
60 pub fn log_executed(&self, f: impl FnOnce()) -> Vec<String> {
61 let events = self.log(f);
62 events
63 .into_iter()
64 .filter_map(|e| match e.kind {
65 // This pretty horrible, but `Debug` is the only way to inspect
66 // QueryDescriptor at the moment.
67 salsa::EventKind::WillExecute { database_key } => {
68 Some(format!("{:?}", database_key))
69 }
70 _ => None,
71 })
72 .collect()
73 }
74}