diff options
Diffstat (limited to 'crates/ra_hir/src/nameres/tests/mod_resolution.rs')
-rw-r--r-- | crates/ra_hir/src/nameres/tests/mod_resolution.rs | 708 |
1 files changed, 708 insertions, 0 deletions
diff --git a/crates/ra_hir/src/nameres/tests/mod_resolution.rs b/crates/ra_hir/src/nameres/tests/mod_resolution.rs new file mode 100644 index 000000000..4f8398460 --- /dev/null +++ b/crates/ra_hir/src/nameres/tests/mod_resolution.rs | |||
@@ -0,0 +1,708 @@ | |||
1 | use super::*; | ||
2 | |||
3 | #[test] | ||
4 | fn 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!(map, @r###" | ||
23 | ⋮crate | ||
24 | ⋮Baz: _ | ||
25 | "###); | ||
26 | } | ||
27 | |||
28 | #[test] | ||
29 | fn 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!(map, @r###" | ||
45 | ⋮crate | ||
46 | ⋮Bar: t v | ||
47 | ⋮foo: t | ||
48 | ⋮ | ||
49 | ⋮crate::foo | ||
50 | ⋮Bar: t v | ||
51 | "###); | ||
52 | } | ||
53 | |||
54 | #[test] | ||
55 | fn 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!(map, @r###" | ||
71 | ⋮crate | ||
72 | ⋮Bar: t v | ||
73 | ⋮async: t | ||
74 | ⋮ | ||
75 | ⋮crate::async | ||
76 | ⋮Bar: t v | ||
77 | "###); | ||
78 | } | ||
79 | |||
80 | #[test] | ||
81 | fn module_resolution_decl_path() { | ||
82 | let map = def_map_with_crate_graph( | ||
83 | r###" | ||
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!(map, @r###" | ||
98 | ⋮crate | ||
99 | ⋮Bar: t v | ||
100 | ⋮foo: t | ||
101 | ⋮ | ||
102 | ⋮crate::foo | ||
103 | ⋮Bar: t v | ||
104 | "###); | ||
105 | } | ||
106 | |||
107 | #[test] | ||
108 | fn module_resolution_module_with_path_in_mod_rs() { | ||
109 | let map = def_map_with_crate_graph( | ||
110 | r###" | ||
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!(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] | ||
142 | fn module_resolution_module_with_path_non_crate_root() { | ||
143 | let map = def_map_with_crate_graph( | ||
144 | r###" | ||
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!(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] | ||
176 | fn module_resolution_module_decl_path_super() { | ||
177 | let map = def_map_with_crate_graph( | ||
178 | r###" | ||
179 | //- /main.rs | ||
180 | #[path = "bar/baz/module.rs"] | ||
181 | mod foo; | ||
182 | pub struct Baz; | ||
183 | |||
184 | //- /bar/baz/module.rs | ||
185 | use super::Baz; | ||
186 | "###, | ||
187 | crate_graph! { | ||
188 | "main": ("/main.rs", []), | ||
189 | }, | ||
190 | ); | ||
191 | |||
192 | assert_snapshot!(map, @r###" | ||
193 | ⋮crate | ||
194 | ⋮Baz: t v | ||
195 | ⋮foo: t | ||
196 | ⋮ | ||
197 | ⋮crate::foo | ||
198 | ⋮Baz: t v | ||
199 | "###); | ||
200 | } | ||
201 | |||
202 | #[test] | ||
203 | fn module_resolution_explicit_path_mod_rs() { | ||
204 | let map = def_map_with_crate_graph( | ||
205 | r###" | ||
206 | //- /main.rs | ||
207 | #[path = "module/mod.rs"] | ||
208 | mod foo; | ||
209 | |||
210 | //- /module/mod.rs | ||
211 | pub struct Baz; | ||
212 | "###, | ||
213 | crate_graph! { | ||
214 | "main": ("/main.rs", []), | ||
215 | }, | ||
216 | ); | ||
217 | |||
218 | assert_snapshot!(map, @r###" | ||
219 | ⋮crate | ||
220 | ⋮foo: t | ||
221 | ⋮ | ||
222 | ⋮crate::foo | ||
223 | ⋮Baz: t v | ||
224 | "###); | ||
225 | } | ||
226 | |||
227 | #[test] | ||
228 | fn module_resolution_relative_path() { | ||
229 | let map = def_map_with_crate_graph( | ||
230 | r###" | ||
231 | //- /main.rs | ||
232 | mod foo; | ||
233 | |||
234 | //- /foo.rs | ||
235 | #[path = "./sub.rs"] | ||
236 | pub mod foo_bar; | ||
237 | |||
238 | //- /sub.rs | ||
239 | pub struct Baz; | ||
240 | "###, | ||
241 | crate_graph! { | ||
242 | "main": ("/main.rs", []), | ||
243 | }, | ||
244 | ); | ||
245 | |||
246 | assert_snapshot!(map, @r###" | ||
247 | ⋮crate | ||
248 | ⋮foo: t | ||
249 | ⋮ | ||
250 | ⋮crate::foo | ||
251 | ⋮foo_bar: t | ||
252 | ⋮ | ||
253 | ⋮crate::foo::foo_bar | ||
254 | ⋮Baz: t v | ||
255 | "###); | ||
256 | } | ||
257 | |||
258 | #[test] | ||
259 | fn module_resolution_relative_path_2() { | ||
260 | let map = def_map_with_crate_graph( | ||
261 | r###" | ||
262 | //- /main.rs | ||
263 | mod foo; | ||
264 | |||
265 | //- /foo/mod.rs | ||
266 | #[path="../sub.rs"] | ||
267 | pub mod foo_bar; | ||
268 | |||
269 | //- /sub.rs | ||
270 | pub struct Baz; | ||
271 | "###, | ||
272 | crate_graph! { | ||
273 | "main": ("/main.rs", []), | ||
274 | }, | ||
275 | ); | ||
276 | |||
277 | assert_snapshot!(map, @r###" | ||
278 | ⋮crate | ||
279 | ⋮foo: t | ||
280 | ⋮ | ||
281 | ⋮crate::foo | ||
282 | ⋮foo_bar: t | ||
283 | ⋮ | ||
284 | ⋮crate::foo::foo_bar | ||
285 | ⋮Baz: t v | ||
286 | "###); | ||
287 | } | ||
288 | |||
289 | #[test] | ||
290 | fn module_resolution_explicit_path_mod_rs_2() { | ||
291 | let map = def_map_with_crate_graph( | ||
292 | r###" | ||
293 | //- /main.rs | ||
294 | #[path = "module/bar/mod.rs"] | ||
295 | mod foo; | ||
296 | |||
297 | //- /module/bar/mod.rs | ||
298 | pub struct Baz; | ||
299 | "###, | ||
300 | crate_graph! { | ||
301 | "main": ("/main.rs", []), | ||
302 | }, | ||
303 | ); | ||
304 | |||
305 | assert_snapshot!(map, @r###" | ||
306 | ⋮crate | ||
307 | ⋮foo: t | ||
308 | ⋮ | ||
309 | ⋮crate::foo | ||
310 | ⋮Baz: t v | ||
311 | "###); | ||
312 | } | ||
313 | |||
314 | #[test] | ||
315 | fn module_resolution_explicit_path_mod_rs_with_win_separator() { | ||
316 | let map = def_map_with_crate_graph( | ||
317 | r###" | ||
318 | //- /main.rs | ||
319 | #[path = "module\bar\mod.rs"] | ||
320 | mod foo; | ||
321 | |||
322 | //- /module/bar/mod.rs | ||
323 | pub struct Baz; | ||
324 | "###, | ||
325 | crate_graph! { | ||
326 | "main": ("/main.rs", []), | ||
327 | }, | ||
328 | ); | ||
329 | |||
330 | assert_snapshot!(map, @r###" | ||
331 | ⋮crate | ||
332 | ⋮foo: t | ||
333 | ⋮ | ||
334 | ⋮crate::foo | ||
335 | ⋮Baz: t v | ||
336 | "###); | ||
337 | } | ||
338 | |||
339 | #[test] | ||
340 | fn module_resolution_decl_inside_inline_module_with_path_attribute() { | ||
341 | let map = def_map_with_crate_graph( | ||
342 | r###" | ||
343 | //- /main.rs | ||
344 | #[path = "models"] | ||
345 | mod foo { | ||
346 | mod bar; | ||
347 | } | ||
348 | |||
349 | //- /models/bar.rs | ||
350 | pub struct Baz; | ||
351 | "###, | ||
352 | crate_graph! { | ||
353 | "main": ("/main.rs", []), | ||
354 | }, | ||
355 | ); | ||
356 | |||
357 | assert_snapshot!(map, @r###" | ||
358 | ⋮crate | ||
359 | ⋮foo: t | ||
360 | ⋮ | ||
361 | ⋮crate::foo | ||
362 | ⋮bar: t | ||
363 | ⋮ | ||
364 | ⋮crate::foo::bar | ||
365 | ⋮Baz: t v | ||
366 | "###); | ||
367 | } | ||
368 | |||
369 | #[test] | ||
370 | fn module_resolution_decl_inside_inline_module() { | ||
371 | let map = def_map_with_crate_graph( | ||
372 | r###" | ||
373 | //- /main.rs | ||
374 | mod foo { | ||
375 | mod bar; | ||
376 | } | ||
377 | |||
378 | //- /foo/bar.rs | ||
379 | pub struct Baz; | ||
380 | "###, | ||
381 | crate_graph! { | ||
382 | "main": ("/main.rs", []), | ||
383 | }, | ||
384 | ); | ||
385 | |||
386 | assert_snapshot!(map, @r###" | ||
387 | ⋮crate | ||
388 | ⋮foo: t | ||
389 | ⋮ | ||
390 | ⋮crate::foo | ||
391 | ⋮bar: t | ||
392 | ⋮ | ||
393 | ⋮crate::foo::bar | ||
394 | ⋮Baz: t v | ||
395 | "###); | ||
396 | } | ||
397 | |||
398 | #[test] | ||
399 | fn module_resolution_decl_inside_inline_module_2_with_path_attribute() { | ||
400 | let map = def_map_with_crate_graph( | ||
401 | r###" | ||
402 | //- /main.rs | ||
403 | #[path = "models/db"] | ||
404 | mod foo { | ||
405 | mod bar; | ||
406 | } | ||
407 | |||
408 | //- /models/db/bar.rs | ||
409 | pub struct Baz; | ||
410 | "###, | ||
411 | crate_graph! { | ||
412 | "main": ("/main.rs", []), | ||
413 | }, | ||
414 | ); | ||
415 | |||
416 | assert_snapshot!(map, @r###" | ||
417 | ⋮crate | ||
418 | ⋮foo: t | ||
419 | ⋮ | ||
420 | ⋮crate::foo | ||
421 | ⋮bar: t | ||
422 | ⋮ | ||
423 | ⋮crate::foo::bar | ||
424 | ⋮Baz: t v | ||
425 | "###); | ||
426 | } | ||
427 | |||
428 | #[test] | ||
429 | fn module_resolution_decl_inside_inline_module_3() { | ||
430 | let map = def_map_with_crate_graph( | ||
431 | r###" | ||
432 | //- /main.rs | ||
433 | #[path = "models/db"] | ||
434 | mod foo { | ||
435 | #[path = "users.rs"] | ||
436 | mod bar; | ||
437 | } | ||
438 | |||
439 | //- /models/db/users.rs | ||
440 | pub struct Baz; | ||
441 | "###, | ||
442 | crate_graph! { | ||
443 | "main": ("/main.rs", []), | ||
444 | }, | ||
445 | ); | ||
446 | |||
447 | assert_snapshot!(map, @r###" | ||
448 | ⋮crate | ||
449 | ⋮foo: t | ||
450 | ⋮ | ||
451 | ⋮crate::foo | ||
452 | ⋮bar: t | ||
453 | ⋮ | ||
454 | ⋮crate::foo::bar | ||
455 | ⋮Baz: t v | ||
456 | "###); | ||
457 | } | ||
458 | |||
459 | #[test] | ||
460 | fn module_resolution_decl_inside_inline_module_empty_path() { | ||
461 | let map = def_map_with_crate_graph( | ||
462 | r###" | ||
463 | //- /main.rs | ||
464 | #[path = ""] | ||
465 | mod foo { | ||
466 | #[path = "users.rs"] | ||
467 | mod bar; | ||
468 | } | ||
469 | |||
470 | //- /foo/users.rs | ||
471 | pub struct Baz; | ||
472 | "###, | ||
473 | crate_graph! { | ||
474 | "main": ("/main.rs", []), | ||
475 | }, | ||
476 | ); | ||
477 | |||
478 | assert_snapshot!(map, @r###" | ||
479 | ⋮crate | ||
480 | ⋮foo: t | ||
481 | ⋮ | ||
482 | ⋮crate::foo | ||
483 | ⋮bar: t | ||
484 | ⋮ | ||
485 | ⋮crate::foo::bar | ||
486 | ⋮Baz: t v | ||
487 | "###); | ||
488 | } | ||
489 | |||
490 | #[test] | ||
491 | fn module_resolution_decl_empty_path() { | ||
492 | let map = def_map_with_crate_graph( | ||
493 | r###" | ||
494 | //- /main.rs | ||
495 | #[path = ""] | ||
496 | mod foo; | ||
497 | |||
498 | //- /foo.rs | ||
499 | pub struct Baz; | ||
500 | "###, | ||
501 | crate_graph! { | ||
502 | "main": ("/main.rs", []), | ||
503 | }, | ||
504 | ); | ||
505 | |||
506 | assert_snapshot!(map, @r###" | ||
507 | ⋮crate | ||
508 | ⋮foo: t | ||
509 | ⋮ | ||
510 | ⋮crate::foo | ||
511 | ⋮Baz: t v | ||
512 | "###); | ||
513 | } | ||
514 | |||
515 | #[test] | ||
516 | fn module_resolution_decl_inside_inline_module_relative_path() { | ||
517 | let map = def_map_with_crate_graph( | ||
518 | r###" | ||
519 | //- /main.rs | ||
520 | #[path = "./models"] | ||
521 | mod foo { | ||
522 | mod bar; | ||
523 | } | ||
524 | |||
525 | //- /models/bar.rs | ||
526 | pub struct Baz; | ||
527 | "###, | ||
528 | crate_graph! { | ||
529 | "main": ("/main.rs", []), | ||
530 | }, | ||
531 | ); | ||
532 | |||
533 | assert_snapshot!(map, @r###" | ||
534 | ⋮crate | ||
535 | ⋮foo: t | ||
536 | ⋮ | ||
537 | ⋮crate::foo | ||
538 | ⋮bar: t | ||
539 | ⋮ | ||
540 | ⋮crate::foo::bar | ||
541 | ⋮Baz: t v | ||
542 | "###); | ||
543 | } | ||
544 | |||
545 | #[test] | ||
546 | fn module_resolution_decl_inside_inline_module_in_crate_root() { | ||
547 | let map = def_map_with_crate_graph( | ||
548 | r###" | ||
549 | //- /main.rs | ||
550 | mod foo { | ||
551 | #[path = "baz.rs"] | ||
552 | mod bar; | ||
553 | } | ||
554 | use self::foo::bar::Baz; | ||
555 | |||
556 | //- /foo/baz.rs | ||
557 | pub struct Baz; | ||
558 | "###, | ||
559 | crate_graph! { | ||
560 | "main": ("/main.rs", []), | ||
561 | }, | ||
562 | ); | ||
563 | |||
564 | assert_snapshot!(map, @r###" | ||
565 | ⋮crate | ||
566 | ⋮Baz: t v | ||
567 | ⋮foo: t | ||
568 | ⋮ | ||
569 | ⋮crate::foo | ||
570 | ⋮bar: t | ||
571 | ⋮ | ||
572 | ⋮crate::foo::bar | ||
573 | ⋮Baz: t v | ||
574 | "###); | ||
575 | } | ||
576 | |||
577 | #[test] | ||
578 | fn module_resolution_decl_inside_inline_module_in_mod_rs() { | ||
579 | let map = def_map_with_crate_graph( | ||
580 | r###" | ||
581 | //- /main.rs | ||
582 | mod foo; | ||
583 | |||
584 | //- /foo/mod.rs | ||
585 | mod bar { | ||
586 | #[path = "qwe.rs"] | ||
587 | pub mod baz; | ||
588 | } | ||
589 | use self::bar::baz::Baz; | ||
590 | |||
591 | //- /foo/bar/qwe.rs | ||
592 | pub struct Baz; | ||
593 | "###, | ||
594 | crate_graph! { | ||
595 | "main": ("/main.rs", []), | ||
596 | }, | ||
597 | ); | ||
598 | |||
599 | assert_snapshot!(map, @r###" | ||
600 | ⋮crate | ||
601 | ⋮foo: t | ||
602 | ⋮ | ||
603 | ⋮crate::foo | ||
604 | ⋮Baz: t v | ||
605 | ⋮bar: t | ||
606 | ⋮ | ||
607 | ⋮crate::foo::bar | ||
608 | ⋮baz: t | ||
609 | ⋮ | ||
610 | ⋮crate::foo::bar::baz | ||
611 | ⋮Baz: t v | ||
612 | "###); | ||
613 | } | ||
614 | |||
615 | #[test] | ||
616 | fn module_resolution_decl_inside_inline_module_in_non_crate_root() { | ||
617 | let map = def_map_with_crate_graph( | ||
618 | r###" | ||
619 | //- /main.rs | ||
620 | mod foo; | ||
621 | |||
622 | //- /foo.rs | ||
623 | mod bar { | ||
624 | #[path = "qwe.rs"] | ||
625 | pub mod baz; | ||
626 | } | ||
627 | use self::bar::baz::Baz; | ||
628 | |||
629 | //- /bar/qwe.rs | ||
630 | pub struct Baz; | ||
631 | "###, | ||
632 | crate_graph! { | ||
633 | "main": ("/main.rs", []), | ||
634 | }, | ||
635 | ); | ||
636 | |||
637 | assert_snapshot!(map, @r###" | ||
638 | ⋮crate | ||
639 | ⋮foo: t | ||
640 | ⋮ | ||
641 | ⋮crate::foo | ||
642 | ⋮Baz: t v | ||
643 | ⋮bar: t | ||
644 | ⋮ | ||
645 | ⋮crate::foo::bar | ||
646 | ⋮baz: t | ||
647 | ⋮ | ||
648 | ⋮crate::foo::bar::baz | ||
649 | ⋮Baz: t v | ||
650 | "###); | ||
651 | } | ||
652 | |||
653 | #[test] | ||
654 | fn module_resolution_decl_inside_inline_module_in_non_crate_root_2() { | ||
655 | let map = def_map_with_crate_graph( | ||
656 | r###" | ||
657 | //- /main.rs | ||
658 | mod foo; | ||
659 | |||
660 | //- /foo.rs | ||
661 | #[path = "bar"] | ||
662 | mod bar { | ||
663 | pub mod baz; | ||
664 | } | ||
665 | use self::bar::baz::Baz; | ||
666 | |||
667 | //- /bar/baz.rs | ||
668 | pub struct Baz; | ||
669 | "###, | ||
670 | crate_graph! { | ||
671 | "main": ("/main.rs", []), | ||
672 | }, | ||
673 | ); | ||
674 | |||
675 | assert_snapshot!(map, @r###" | ||
676 | ⋮crate | ||
677 | ⋮foo: t | ||
678 | ⋮ | ||
679 | ⋮crate::foo | ||
680 | ⋮Baz: t v | ||
681 | ⋮bar: t | ||
682 | ⋮ | ||
683 | ⋮crate::foo::bar | ||
684 | ⋮baz: t | ||
685 | ⋮ | ||
686 | ⋮crate::foo::bar::baz | ||
687 | ⋮Baz: t v | ||
688 | "###); | ||
689 | } | ||
690 | |||
691 | #[test] | ||
692 | fn unresolved_module_diagnostics() { | ||
693 | let diagnostics = MockDatabase::with_files( | ||
694 | r" | ||
695 | //- /lib.rs | ||
696 | mod foo; | ||
697 | mod bar; | ||
698 | mod baz {} | ||
699 | //- /foo.rs | ||
700 | ", | ||
701 | ) | ||
702 | .diagnostics(); | ||
703 | |||
704 | assert_snapshot!(diagnostics, @r###" | ||
705 | "mod bar;": unresolved module | ||
706 | "### | ||
707 | ); | ||
708 | } | ||