aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
authorAlexander Andreev <[email protected]>2019-07-07 14:06:54 +0100
committerAlexander Andreev <[email protected]>2019-07-07 14:06:54 +0100
commit1c582be63b35c6602638023d4bd0bc426b0ca744 (patch)
tree1dd7188a39afb8e18c1e57f522abb059bdecba92 /crates/ra_hir/src
parent8579a9b834555339e400f3161280305a99189a5f (diff)
Moved module resolution test in mods.rs
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/nameres.rs2
-rw-r--r--crates/ra_hir/src/nameres/tests.rs192
-rw-r--r--crates/ra_hir/src/nameres/tests/mods.rs192
-rw-r--r--crates/ra_hir/src/ty/method_resolution.rs2
4 files changed, 195 insertions, 193 deletions
diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs
index cf9ff0636..c84d2eada 100644
--- a/crates/ra_hir/src/nameres.rs
+++ b/crates/ra_hir/src/nameres.rs
@@ -1,6 +1,6 @@
1/// This module implements import-resolution/macro expansion algorithm. 1/// This module implements import-resolution/macro expansion algorithm.
2/// 2///
3/// The result of this module is `CrateDefMap`: a datastructure which contains: 3/// The result of this module is `CrateDefMap`: a data structure which contains:
4/// 4///
5/// * a tree of modules for the crate 5/// * a tree of modules for the crate
6/// * for each module, a set of items visible in the module (directly declared 6/// * for each module, a set of items visible in the module (directly declared
diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs
index 5e8ea6780..8b0887fb5 100644
--- a/crates/ra_hir/src/nameres/tests.rs
+++ b/crates/ra_hir/src/nameres/tests.rs
@@ -2,6 +2,7 @@ mod macros;
2mod globs; 2mod globs;
3mod incremental; 3mod incremental;
4mod primitives; 4mod primitives;
5mod mods;
5 6
6use std::sync::Arc; 7use std::sync::Arc;
7 8
@@ -313,178 +314,6 @@ fn edition_2015_imports() {
313} 314}
314 315
315#[test] 316#[test]
316fn module_resolution_works_for_non_standard_filenames() {
317 let map = def_map_with_crate_graph(
318 "
319 //- /my_library.rs
320 mod foo;
321 use self::foo::Bar;
322
323 //- /foo/mod.rs
324 pub struct Bar;
325 ",
326 crate_graph! {
327 "my_library": ("/my_library.rs", []),
328 },
329 );
330
331 assert_snapshot_matches!(map, @r###"
332 ⋮crate
333 ⋮Bar: t v
334 ⋮foo: t
335
336 ⋮crate::foo
337 ⋮Bar: t v
338 "###);
339}
340
341#[test]
342fn module_resolution_works_for_raw_modules() {
343 let map = def_map_with_crate_graph(
344 "
345 //- /library.rs
346 mod r#async;
347 use self::r#async::Bar;
348
349 //- /async.rs
350 pub struct Bar;
351 ",
352 crate_graph! {
353 "library": ("/library.rs", []),
354 },
355 );
356
357 assert_snapshot_matches!(map, @r###"
358 ⋮crate
359 ⋮Bar: t v
360 ⋮async: t
361
362 ⋮crate::async
363 ⋮Bar: t v
364 "###);
365}
366
367#[test]
368fn module_resolution_decl_path() {
369 let map = def_map_with_crate_graph(
370 "
371 //- /library.rs
372 #[path = \"bar/baz/foo.rs\"]
373 mod foo;
374 use self::foo::Bar;
375
376 //- /bar/baz/foo.rs
377 pub struct Bar;
378 ",
379 crate_graph! {
380 "library": ("/library.rs", []),
381 },
382 );
383
384 assert_snapshot_matches!(map, @r###"
385 ⋮crate
386 ⋮Bar: t v
387 ⋮foo: t
388
389 ⋮crate::foo
390 ⋮Bar: t v
391 "###);
392}
393
394#[test]
395fn module_resolution_module_with_path_in_mod_rs() {
396 let map = def_map_with_crate_graph(
397 "
398 //- /main.rs
399 mod foo;
400
401 //- /foo/mod.rs
402 #[path = \"baz.rs\"]
403 pub mod bar;
404
405 use self::bar::Baz;
406
407 //- /foo/baz.rs
408 pub struct Baz;
409 ",
410 crate_graph! {
411 "main": ("/main.rs", []),
412 },
413 );
414
415 assert_snapshot_matches!(map, @r###"
416 ⋮crate
417 ⋮foo: t
418
419 ⋮crate::foo
420 ⋮Baz: t v
421 ⋮bar: t
422
423 ⋮crate::foo::bar
424 ⋮Baz: t v
425 "###);
426}
427
428#[test]
429fn module_resolution_module_with_path_non_crate_root() {
430 let map = def_map_with_crate_graph(
431 "
432 //- /main.rs
433 mod foo;
434
435 //- /foo.rs
436 #[path = \"baz.rs\"]
437 pub mod bar;
438
439 use self::bar::Baz;
440
441 //- /baz.rs
442 pub struct Baz;
443 ",
444 crate_graph! {
445 "main": ("/main.rs", []),
446 },
447 );
448
449 assert_snapshot_matches!(map, @r###"
450 ⋮crate
451 ⋮foo: t
452
453 ⋮crate::foo
454 ⋮Baz: t v
455 ⋮bar: t
456
457 ⋮crate::foo::bar
458 ⋮Baz: t v
459 "###);
460}
461
462#[test]
463fn name_res_works_for_broken_modules() {
464 covers!(name_res_works_for_broken_modules);
465 let map = def_map(
466 "
467 //- /lib.rs
468 mod foo // no `;`, no body
469
470 use self::foo::Baz;
471
472 //- /foo/mod.rs
473 pub mod bar;
474
475 pub use self::bar::Baz;
476
477 //- /foo/bar.rs
478 pub struct Baz;
479 ",
480 );
481 assert_snapshot_matches!(map, @r###"
482 ⋮crate
483 ⋮Baz: _
484 "###);
485}
486
487#[test]
488fn item_map_using_self() { 317fn item_map_using_self() {
489 let map = def_map( 318 let map = def_map(
490 " 319 "
@@ -676,22 +505,3 @@ fn values_dont_shadow_extern_crates() {
676 ⋮foo: v 505 ⋮foo: v
677 "###); 506 "###);
678} 507}
679
680#[test]
681fn unresolved_module_diagnostics() {
682 let diagnostics = MockDatabase::with_files(
683 r"
684 //- /lib.rs
685 mod foo;
686 mod bar;
687 mod baz {}
688 //- /foo.rs
689 ",
690 )
691 .diagnostics();
692
693 assert_snapshot_matches!(diagnostics, @r###"
694"mod bar;": unresolved module
695"###
696 );
697}
diff --git a/crates/ra_hir/src/nameres/tests/mods.rs b/crates/ra_hir/src/nameres/tests/mods.rs
new file mode 100644
index 000000000..7c8c832fc
--- /dev/null
+++ b/crates/ra_hir/src/nameres/tests/mods.rs
@@ -0,0 +1,192 @@
1use super::*;
2
3#[test]
4fn name_res_works_for_broken_modules() {
5 covers!(name_res_works_for_broken_modules);
6 let map = def_map(
7 "
8 //- /lib.rs
9 mod foo // no `;`, no body
10
11 use self::foo::Baz;
12
13 //- /foo/mod.rs
14 pub mod bar;
15
16 pub use self::bar::Baz;
17
18 //- /foo/bar.rs
19 pub struct Baz;
20 ",
21 );
22 assert_snapshot_matches!(map, @r###"
23 ⋮crate
24 ⋮Baz: _
25 "###);
26}
27
28#[test]
29fn module_resolution_works_for_non_standard_filenames() {
30 let map = def_map_with_crate_graph(
31 "
32 //- /my_library.rs
33 mod foo;
34 use self::foo::Bar;
35
36 //- /foo/mod.rs
37 pub struct Bar;
38 ",
39 crate_graph! {
40 "my_library": ("/my_library.rs", []),
41 },
42 );
43
44 assert_snapshot_matches!(map, @r###"
45 ⋮crate
46 ⋮Bar: t v
47 ⋮foo: t
48
49 ⋮crate::foo
50 ⋮Bar: t v
51 "###);
52}
53
54#[test]
55fn module_resolution_works_for_raw_modules() {
56 let map = def_map_with_crate_graph(
57 "
58 //- /library.rs
59 mod r#async;
60 use self::r#async::Bar;
61
62 //- /async.rs
63 pub struct Bar;
64 ",
65 crate_graph! {
66 "library": ("/library.rs", []),
67 },
68 );
69
70 assert_snapshot_matches!(map, @r###"
71 ⋮crate
72 ⋮Bar: t v
73 ⋮async: t
74
75 ⋮crate::async
76 ⋮Bar: t v
77 "###);
78}
79
80#[test]
81fn module_resolution_decl_path() {
82 let map = def_map_with_crate_graph(
83 "
84 //- /library.rs
85 #[path = \"bar/baz/foo.rs\"]
86 mod foo;
87 use self::foo::Bar;
88
89 //- /bar/baz/foo.rs
90 pub struct Bar;
91 ",
92 crate_graph! {
93 "library": ("/library.rs", []),
94 },
95 );
96
97 assert_snapshot_matches!(map, @r###"
98 ⋮crate
99 ⋮Bar: t v
100 ⋮foo: t
101
102 ⋮crate::foo
103 ⋮Bar: t v
104 "###);
105}
106
107#[test]
108fn module_resolution_module_with_path_in_mod_rs() {
109 let map = def_map_with_crate_graph(
110 "
111 //- /main.rs
112 mod foo;
113
114 //- /foo/mod.rs
115 #[path = \"baz.rs\"]
116 pub mod bar;
117
118 use self::bar::Baz;
119
120 //- /foo/baz.rs
121 pub struct Baz;
122 ",
123 crate_graph! {
124 "main": ("/main.rs", []),
125 },
126 );
127
128 assert_snapshot_matches!(map, @r###"
129 ⋮crate
130 ⋮foo: t
131
132 ⋮crate::foo
133 ⋮Baz: t v
134 ⋮bar: t
135
136 ⋮crate::foo::bar
137 ⋮Baz: t v
138 "###);
139}
140
141#[test]
142fn module_resolution_module_with_path_non_crate_root() {
143 let map = def_map_with_crate_graph(
144 "
145 //- /main.rs
146 mod foo;
147
148 //- /foo.rs
149 #[path = \"baz.rs\"]
150 pub mod bar;
151
152 use self::bar::Baz;
153
154 //- /baz.rs
155 pub struct Baz;
156 ",
157 crate_graph! {
158 "main": ("/main.rs", []),
159 },
160 );
161
162 assert_snapshot_matches!(map, @r###"
163 ⋮crate
164 ⋮foo: t
165
166 ⋮crate::foo
167 ⋮Baz: t v
168 ⋮bar: t
169
170 ⋮crate::foo::bar
171 ⋮Baz: t v
172 "###);
173}
174
175#[test]
176fn unresolved_module_diagnostics() {
177 let diagnostics = MockDatabase::with_files(
178 r"
179 //- /lib.rs
180 mod foo;
181 mod bar;
182 mod baz {}
183 //- /foo.rs
184 ",
185 )
186 .diagnostics();
187
188 assert_snapshot_matches!(diagnostics, @r###"
189"mod bar;": unresolved module
190"###
191 );
192}
diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs
index 10a6e0b10..76ace66ea 100644
--- a/crates/ra_hir/src/ty/method_resolution.rs
+++ b/crates/ra_hir/src/ty/method_resolution.rs
@@ -116,7 +116,7 @@ impl CrateImplBlocks {
116 116
117fn def_crates(db: &impl HirDatabase, cur_crate: Crate, ty: &Ty) -> Option<ArrayVec<[Crate; 2]>> { 117fn def_crates(db: &impl HirDatabase, cur_crate: Crate, ty: &Ty) -> Option<ArrayVec<[Crate; 2]>> {
118 // Types like slice can have inherent impls in several crates, (core and alloc). 118 // Types like slice can have inherent impls in several crates, (core and alloc).
119 // The correspoinding impls are marked with lang items, so we can use them to find the required crates. 119 // The corresponding impls are marked with lang items, so we can use them to find the required crates.
120 macro_rules! lang_item_crate { 120 macro_rules! lang_item_crate {
121 ($db:expr, $cur_crate:expr, $($name:expr),+ $(,)?) => {{ 121 ($db:expr, $cur_crate:expr, $($name:expr),+ $(,)?) => {{
122 let mut v = ArrayVec::<[Crate; 2]>::new(); 122 let mut v = ArrayVec::<[Crate; 2]>::new();