diff options
author | Igor Aleksanov <[email protected]> | 2020-08-14 05:34:07 +0100 |
---|---|---|
committer | Igor Aleksanov <[email protected]> | 2020-08-14 05:34:07 +0100 |
commit | c26c911ec1e6c2ad1dcb7d155a6a1d528839ad1a (patch) | |
tree | 7cff36c38234be0afb65273146d8247083a5cfeb /crates/hir_def/src/nameres/tests/mod_resolution.rs | |
parent | 3c018bf84de5c693b5ee1c6bec0fed3b201c2060 (diff) | |
parent | f1f73649a686dc6e6449afc35e0fa6fed00e225d (diff) |
Merge branch 'master' into add-disable-diagnostics
Diffstat (limited to 'crates/hir_def/src/nameres/tests/mod_resolution.rs')
-rw-r--r-- | crates/hir_def/src/nameres/tests/mod_resolution.rs | 796 |
1 files changed, 796 insertions, 0 deletions
diff --git a/crates/hir_def/src/nameres/tests/mod_resolution.rs b/crates/hir_def/src/nameres/tests/mod_resolution.rs new file mode 100644 index 000000000..1f619787e --- /dev/null +++ b/crates/hir_def/src/nameres/tests/mod_resolution.rs | |||
@@ -0,0 +1,796 @@ | |||
1 | use super::*; | ||
2 | |||
3 | #[test] | ||
4 | fn name_res_works_for_broken_modules() { | ||
5 | mark::check!(name_res_works_for_broken_modules); | ||
6 | check( | ||
7 | r" | ||
8 | //- /lib.rs | ||
9 | mod foo // no `;`, no body | ||
10 | use self::foo::Baz; | ||
11 | |||
12 | //- /foo/mod.rs | ||
13 | pub mod bar; | ||
14 | pub use self::bar::Baz; | ||
15 | |||
16 | //- /foo/bar.rs | ||
17 | pub struct Baz; | ||
18 | ", | ||
19 | expect![[r#" | ||
20 | crate | ||
21 | Baz: _ | ||
22 | foo: t | ||
23 | |||
24 | crate::foo | ||
25 | "#]], | ||
26 | ); | ||
27 | } | ||
28 | |||
29 | #[test] | ||
30 | fn nested_module_resolution() { | ||
31 | check( | ||
32 | r#" | ||
33 | //- /lib.rs | ||
34 | mod n1; | ||
35 | |||
36 | //- /n1.rs | ||
37 | mod n2; | ||
38 | |||
39 | //- /n1/n2.rs | ||
40 | struct X; | ||
41 | "#, | ||
42 | expect![[r#" | ||
43 | crate | ||
44 | n1: t | ||
45 | |||
46 | crate::n1 | ||
47 | n2: t | ||
48 | |||
49 | crate::n1::n2 | ||
50 | X: t v | ||
51 | "#]], | ||
52 | ); | ||
53 | } | ||
54 | |||
55 | #[test] | ||
56 | fn nested_module_resolution_2() { | ||
57 | check( | ||
58 | r#" | ||
59 | //- /lib.rs | ||
60 | mod prelude; | ||
61 | mod iter; | ||
62 | |||
63 | //- /prelude.rs | ||
64 | pub use crate::iter::Iterator; | ||
65 | |||
66 | //- /iter.rs | ||
67 | pub use self::traits::Iterator; | ||
68 | mod traits; | ||
69 | |||
70 | //- /iter/traits.rs | ||
71 | pub use self::iterator::Iterator; | ||
72 | mod iterator; | ||
73 | |||
74 | //- /iter/traits/iterator.rs | ||
75 | pub trait Iterator; | ||
76 | "#, | ||
77 | expect![[r#" | ||
78 | crate | ||
79 | iter: t | ||
80 | prelude: t | ||
81 | |||
82 | crate::iter | ||
83 | Iterator: t | ||
84 | traits: t | ||
85 | |||
86 | crate::iter::traits | ||
87 | Iterator: t | ||
88 | iterator: t | ||
89 | |||
90 | crate::iter::traits::iterator | ||
91 | Iterator: t | ||
92 | |||
93 | crate::prelude | ||
94 | Iterator: t | ||
95 | "#]], | ||
96 | ); | ||
97 | } | ||
98 | |||
99 | #[test] | ||
100 | fn module_resolution_works_for_non_standard_filenames() { | ||
101 | check( | ||
102 | r#" | ||
103 | //- /my_library.rs crate:my_library | ||
104 | mod foo; | ||
105 | use self::foo::Bar; | ||
106 | |||
107 | //- /foo/mod.rs | ||
108 | pub struct Bar; | ||
109 | "#, | ||
110 | expect![[r#" | ||
111 | crate | ||
112 | Bar: t v | ||
113 | foo: t | ||
114 | |||
115 | crate::foo | ||
116 | Bar: t v | ||
117 | "#]], | ||
118 | ); | ||
119 | } | ||
120 | |||
121 | #[test] | ||
122 | fn module_resolution_works_for_raw_modules() { | ||
123 | check( | ||
124 | r#" | ||
125 | //- /lib.rs | ||
126 | mod r#async; | ||
127 | use self::r#async::Bar; | ||
128 | |||
129 | //- /async.rs | ||
130 | pub struct Bar; | ||
131 | "#, | ||
132 | expect![[r#" | ||
133 | crate | ||
134 | Bar: t v | ||
135 | async: t | ||
136 | |||
137 | crate::async | ||
138 | Bar: t v | ||
139 | "#]], | ||
140 | ); | ||
141 | } | ||
142 | |||
143 | #[test] | ||
144 | fn module_resolution_decl_path() { | ||
145 | check( | ||
146 | r#" | ||
147 | //- /lib.rs | ||
148 | #[path = "bar/baz/foo.rs"] | ||
149 | mod foo; | ||
150 | use self::foo::Bar; | ||
151 | |||
152 | //- /bar/baz/foo.rs | ||
153 | pub struct Bar; | ||
154 | "#, | ||
155 | expect![[r#" | ||
156 | crate | ||
157 | Bar: t v | ||
158 | foo: t | ||
159 | |||
160 | crate::foo | ||
161 | Bar: t v | ||
162 | "#]], | ||
163 | ); | ||
164 | } | ||
165 | |||
166 | #[test] | ||
167 | fn module_resolution_module_with_path_in_mod_rs() { | ||
168 | check( | ||
169 | r#" | ||
170 | //- /main.rs | ||
171 | mod foo; | ||
172 | |||
173 | //- /foo/mod.rs | ||
174 | #[path = "baz.rs"] | ||
175 | pub mod bar; | ||
176 | use self::bar::Baz; | ||
177 | |||
178 | //- /foo/baz.rs | ||
179 | pub struct Baz; | ||
180 | "#, | ||
181 | expect![[r#" | ||
182 | crate | ||
183 | foo: t | ||
184 | |||
185 | crate::foo | ||
186 | Baz: t v | ||
187 | bar: t | ||
188 | |||
189 | crate::foo::bar | ||
190 | Baz: t v | ||
191 | "#]], | ||
192 | ); | ||
193 | } | ||
194 | |||
195 | #[test] | ||
196 | fn module_resolution_module_with_path_non_crate_root() { | ||
197 | check( | ||
198 | r#" | ||
199 | //- /main.rs | ||
200 | mod foo; | ||
201 | |||
202 | //- /foo.rs | ||
203 | #[path = "baz.rs"] | ||
204 | pub mod bar; | ||
205 | use self::bar::Baz; | ||
206 | |||
207 | //- /baz.rs | ||
208 | pub struct Baz; | ||
209 | "#, | ||
210 | expect![[r#" | ||
211 | crate | ||
212 | foo: t | ||
213 | |||
214 | crate::foo | ||
215 | Baz: t v | ||
216 | bar: t | ||
217 | |||
218 | crate::foo::bar | ||
219 | Baz: t v | ||
220 | "#]], | ||
221 | ); | ||
222 | } | ||
223 | |||
224 | #[test] | ||
225 | fn module_resolution_module_decl_path_super() { | ||
226 | check( | ||
227 | r#" | ||
228 | //- /main.rs | ||
229 | #[path = "bar/baz/module.rs"] | ||
230 | mod foo; | ||
231 | pub struct Baz; | ||
232 | |||
233 | //- /bar/baz/module.rs | ||
234 | use super::Baz; | ||
235 | "#, | ||
236 | expect![[r#" | ||
237 | crate | ||
238 | Baz: t v | ||
239 | foo: t | ||
240 | |||
241 | crate::foo | ||
242 | Baz: t v | ||
243 | "#]], | ||
244 | ); | ||
245 | } | ||
246 | |||
247 | #[test] | ||
248 | fn module_resolution_explicit_path_mod_rs() { | ||
249 | check( | ||
250 | r#" | ||
251 | //- /main.rs | ||
252 | #[path = "module/mod.rs"] | ||
253 | mod foo; | ||
254 | |||
255 | //- /module/mod.rs | ||
256 | pub struct Baz; | ||
257 | "#, | ||
258 | expect![[r#" | ||
259 | crate | ||
260 | foo: t | ||
261 | |||
262 | crate::foo | ||
263 | Baz: t v | ||
264 | "#]], | ||
265 | ); | ||
266 | } | ||
267 | |||
268 | #[test] | ||
269 | fn module_resolution_relative_path() { | ||
270 | check( | ||
271 | r#" | ||
272 | //- /main.rs | ||
273 | mod foo; | ||
274 | |||
275 | //- /foo.rs | ||
276 | #[path = "./sub.rs"] | ||
277 | pub mod foo_bar; | ||
278 | |||
279 | //- /sub.rs | ||
280 | pub struct Baz; | ||
281 | "#, | ||
282 | expect![[r#" | ||
283 | crate | ||
284 | foo: t | ||
285 | |||
286 | crate::foo | ||
287 | foo_bar: t | ||
288 | |||
289 | crate::foo::foo_bar | ||
290 | Baz: t v | ||
291 | "#]], | ||
292 | ); | ||
293 | } | ||
294 | |||
295 | #[test] | ||
296 | fn module_resolution_relative_path_2() { | ||
297 | check( | ||
298 | r#" | ||
299 | //- /main.rs | ||
300 | mod foo; | ||
301 | |||
302 | //- /foo/mod.rs | ||
303 | #[path="../sub.rs"] | ||
304 | pub mod foo_bar; | ||
305 | |||
306 | //- /sub.rs | ||
307 | pub struct Baz; | ||
308 | "#, | ||
309 | expect![[r#" | ||
310 | crate | ||
311 | foo: t | ||
312 | |||
313 | crate::foo | ||
314 | foo_bar: t | ||
315 | |||
316 | crate::foo::foo_bar | ||
317 | Baz: t v | ||
318 | "#]], | ||
319 | ); | ||
320 | } | ||
321 | |||
322 | #[test] | ||
323 | fn module_resolution_relative_path_outside_root() { | ||
324 | check( | ||
325 | r#" | ||
326 | //- /main.rs | ||
327 | #[path="../../../../../outside.rs"] | ||
328 | mod foo; | ||
329 | "#, | ||
330 | expect![[r#" | ||
331 | crate | ||
332 | "#]], | ||
333 | ); | ||
334 | } | ||
335 | |||
336 | #[test] | ||
337 | fn module_resolution_explicit_path_mod_rs_2() { | ||
338 | check( | ||
339 | r#" | ||
340 | //- /main.rs | ||
341 | #[path = "module/bar/mod.rs"] | ||
342 | mod foo; | ||
343 | |||
344 | //- /module/bar/mod.rs | ||
345 | pub struct Baz; | ||
346 | "#, | ||
347 | expect![[r#" | ||
348 | crate | ||
349 | foo: t | ||
350 | |||
351 | crate::foo | ||
352 | Baz: t v | ||
353 | "#]], | ||
354 | ); | ||
355 | } | ||
356 | |||
357 | #[test] | ||
358 | fn module_resolution_explicit_path_mod_rs_with_win_separator() { | ||
359 | check( | ||
360 | r#" | ||
361 | //- /main.rs | ||
362 | #[path = "module\bar\mod.rs"] | ||
363 | mod foo; | ||
364 | |||
365 | //- /module/bar/mod.rs | ||
366 | pub struct Baz; | ||
367 | "#, | ||
368 | expect![[r#" | ||
369 | crate | ||
370 | foo: t | ||
371 | |||
372 | crate::foo | ||
373 | Baz: t v | ||
374 | "#]], | ||
375 | ); | ||
376 | } | ||
377 | |||
378 | #[test] | ||
379 | fn module_resolution_decl_inside_inline_module_with_path_attribute() { | ||
380 | check( | ||
381 | r#" | ||
382 | //- /main.rs | ||
383 | #[path = "models"] | ||
384 | mod foo { mod bar; } | ||
385 | |||
386 | //- /models/bar.rs | ||
387 | pub struct Baz; | ||
388 | "#, | ||
389 | expect![[r#" | ||
390 | crate | ||
391 | foo: t | ||
392 | |||
393 | crate::foo | ||
394 | bar: t | ||
395 | |||
396 | crate::foo::bar | ||
397 | Baz: t v | ||
398 | "#]], | ||
399 | ); | ||
400 | } | ||
401 | |||
402 | #[test] | ||
403 | fn module_resolution_decl_inside_inline_module() { | ||
404 | check( | ||
405 | r#" | ||
406 | //- /main.rs | ||
407 | mod foo { mod bar; } | ||
408 | |||
409 | //- /foo/bar.rs | ||
410 | pub struct Baz; | ||
411 | "#, | ||
412 | expect![[r#" | ||
413 | crate | ||
414 | foo: t | ||
415 | |||
416 | crate::foo | ||
417 | bar: t | ||
418 | |||
419 | crate::foo::bar | ||
420 | Baz: t v | ||
421 | "#]], | ||
422 | ); | ||
423 | } | ||
424 | |||
425 | #[test] | ||
426 | fn module_resolution_decl_inside_inline_module_2_with_path_attribute() { | ||
427 | check( | ||
428 | r#" | ||
429 | //- /main.rs | ||
430 | #[path = "models/db"] | ||
431 | mod foo { mod bar; } | ||
432 | |||
433 | //- /models/db/bar.rs | ||
434 | pub struct Baz; | ||
435 | "#, | ||
436 | expect![[r#" | ||
437 | crate | ||
438 | foo: t | ||
439 | |||
440 | crate::foo | ||
441 | bar: t | ||
442 | |||
443 | crate::foo::bar | ||
444 | Baz: t v | ||
445 | "#]], | ||
446 | ); | ||
447 | } | ||
448 | |||
449 | #[test] | ||
450 | fn module_resolution_decl_inside_inline_module_3() { | ||
451 | check( | ||
452 | r#" | ||
453 | //- /main.rs | ||
454 | #[path = "models/db"] | ||
455 | mod foo { | ||
456 | #[path = "users.rs"] | ||
457 | mod bar; | ||
458 | } | ||
459 | |||
460 | //- /models/db/users.rs | ||
461 | pub struct Baz; | ||
462 | "#, | ||
463 | expect![[r#" | ||
464 | crate | ||
465 | foo: t | ||
466 | |||
467 | crate::foo | ||
468 | bar: t | ||
469 | |||
470 | crate::foo::bar | ||
471 | Baz: t v | ||
472 | "#]], | ||
473 | ); | ||
474 | } | ||
475 | |||
476 | #[test] | ||
477 | fn module_resolution_decl_inside_inline_module_empty_path() { | ||
478 | check( | ||
479 | r#" | ||
480 | //- /main.rs | ||
481 | #[path = ""] | ||
482 | mod foo { | ||
483 | #[path = "users.rs"] | ||
484 | mod bar; | ||
485 | } | ||
486 | |||
487 | //- /users.rs | ||
488 | pub struct Baz; | ||
489 | "#, | ||
490 | expect![[r#" | ||
491 | crate | ||
492 | foo: t | ||
493 | |||
494 | crate::foo | ||
495 | bar: t | ||
496 | |||
497 | crate::foo::bar | ||
498 | Baz: t v | ||
499 | "#]], | ||
500 | ); | ||
501 | } | ||
502 | |||
503 | #[test] | ||
504 | fn module_resolution_decl_empty_path() { | ||
505 | check( | ||
506 | r#" | ||
507 | //- /main.rs | ||
508 | #[path = ""] // Should try to read `/` (a directory) | ||
509 | mod foo; | ||
510 | |||
511 | //- /foo.rs | ||
512 | pub struct Baz; | ||
513 | "#, | ||
514 | expect![[r#" | ||
515 | crate | ||
516 | "#]], | ||
517 | ); | ||
518 | } | ||
519 | |||
520 | #[test] | ||
521 | fn module_resolution_decl_inside_inline_module_relative_path() { | ||
522 | check( | ||
523 | r#" | ||
524 | //- /main.rs | ||
525 | #[path = "./models"] | ||
526 | mod foo { mod bar; } | ||
527 | |||
528 | //- /models/bar.rs | ||
529 | pub struct Baz; | ||
530 | "#, | ||
531 | expect![[r#" | ||
532 | crate | ||
533 | foo: t | ||
534 | |||
535 | crate::foo | ||
536 | bar: t | ||
537 | |||
538 | crate::foo::bar | ||
539 | Baz: t v | ||
540 | "#]], | ||
541 | ); | ||
542 | } | ||
543 | |||
544 | #[test] | ||
545 | fn module_resolution_decl_inside_inline_module_in_crate_root() { | ||
546 | check( | ||
547 | r#" | ||
548 | //- /main.rs | ||
549 | mod foo { | ||
550 | #[path = "baz.rs"] | ||
551 | mod bar; | ||
552 | } | ||
553 | use self::foo::bar::Baz; | ||
554 | |||
555 | //- /foo/baz.rs | ||
556 | pub struct Baz; | ||
557 | "#, | ||
558 | expect![[r#" | ||
559 | crate | ||
560 | Baz: t v | ||
561 | foo: t | ||
562 | |||
563 | crate::foo | ||
564 | bar: t | ||
565 | |||
566 | crate::foo::bar | ||
567 | Baz: t v | ||
568 | "#]], | ||
569 | ); | ||
570 | } | ||
571 | |||
572 | #[test] | ||
573 | fn module_resolution_decl_inside_inline_module_in_mod_rs() { | ||
574 | check( | ||
575 | r#" | ||
576 | //- /main.rs | ||
577 | mod foo; | ||
578 | |||
579 | //- /foo/mod.rs | ||
580 | mod bar { | ||
581 | #[path = "qwe.rs"] | ||
582 | pub mod baz; | ||
583 | } | ||
584 | use self::bar::baz::Baz; | ||
585 | |||
586 | //- /foo/bar/qwe.rs | ||
587 | pub struct Baz; | ||
588 | "#, | ||
589 | expect![[r#" | ||
590 | crate | ||
591 | foo: t | ||
592 | |||
593 | crate::foo | ||
594 | Baz: t v | ||
595 | bar: t | ||
596 | |||
597 | crate::foo::bar | ||
598 | baz: t | ||
599 | |||
600 | crate::foo::bar::baz | ||
601 | Baz: t v | ||
602 | "#]], | ||
603 | ); | ||
604 | } | ||
605 | |||
606 | #[test] | ||
607 | fn module_resolution_decl_inside_inline_module_in_non_crate_root() { | ||
608 | check( | ||
609 | r#" | ||
610 | //- /main.rs | ||
611 | mod foo; | ||
612 | |||
613 | //- /foo.rs | ||
614 | mod bar { | ||
615 | #[path = "qwe.rs"] | ||
616 | pub mod baz; | ||
617 | } | ||
618 | use self::bar::baz::Baz; | ||
619 | |||
620 | //- /foo/bar/qwe.rs | ||
621 | pub struct Baz; | ||
622 | "#, | ||
623 | expect![[r#" | ||
624 | crate | ||
625 | foo: t | ||
626 | |||
627 | crate::foo | ||
628 | Baz: t v | ||
629 | bar: t | ||
630 | |||
631 | crate::foo::bar | ||
632 | baz: t | ||
633 | |||
634 | crate::foo::bar::baz | ||
635 | Baz: t v | ||
636 | "#]], | ||
637 | ); | ||
638 | } | ||
639 | |||
640 | #[test] | ||
641 | fn module_resolution_decl_inside_inline_module_in_non_crate_root_2() { | ||
642 | check( | ||
643 | r#" | ||
644 | //- /main.rs | ||
645 | mod foo; | ||
646 | |||
647 | //- /foo.rs | ||
648 | #[path = "bar"] | ||
649 | mod bar { | ||
650 | pub mod baz; | ||
651 | } | ||
652 | use self::bar::baz::Baz; | ||
653 | |||
654 | //- /bar/baz.rs | ||
655 | pub struct Baz; | ||
656 | "#, | ||
657 | expect![[r#" | ||
658 | crate | ||
659 | foo: t | ||
660 | |||
661 | crate::foo | ||
662 | Baz: t v | ||
663 | bar: t | ||
664 | |||
665 | crate::foo::bar | ||
666 | baz: t | ||
667 | |||
668 | crate::foo::bar::baz | ||
669 | Baz: t v | ||
670 | "#]], | ||
671 | ); | ||
672 | } | ||
673 | |||
674 | #[test] | ||
675 | fn unresolved_module_diagnostics() { | ||
676 | let db = TestDB::with_files( | ||
677 | r" | ||
678 | //- /lib.rs | ||
679 | mod foo; | ||
680 | mod bar; | ||
681 | mod baz {} | ||
682 | //- /foo.rs | ||
683 | ", | ||
684 | ); | ||
685 | let krate = db.test_crate(); | ||
686 | |||
687 | let crate_def_map = db.crate_def_map(krate); | ||
688 | |||
689 | expect![[r#" | ||
690 | [ | ||
691 | UnresolvedModule { | ||
692 | module: Idx::<ModuleData>(0), | ||
693 | declaration: InFile { | ||
694 | file_id: HirFileId( | ||
695 | FileId( | ||
696 | FileId( | ||
697 | 0, | ||
698 | ), | ||
699 | ), | ||
700 | ), | ||
701 | value: FileAstId::<syntax::ast::generated::nodes::Module>(1), | ||
702 | }, | ||
703 | candidate: "bar.rs", | ||
704 | }, | ||
705 | ] | ||
706 | "#]] | ||
707 | .assert_debug_eq(&crate_def_map.diagnostics); | ||
708 | } | ||
709 | |||
710 | #[test] | ||
711 | fn module_resolution_decl_inside_module_in_non_crate_root_2() { | ||
712 | check( | ||
713 | r#" | ||
714 | //- /main.rs | ||
715 | #[path="module/m2.rs"] | ||
716 | mod module; | ||
717 | |||
718 | //- /module/m2.rs | ||
719 | pub mod submod; | ||
720 | |||
721 | //- /module/submod.rs | ||
722 | pub struct Baz; | ||
723 | "#, | ||
724 | expect![[r#" | ||
725 | crate | ||
726 | module: t | ||
727 | |||
728 | crate::module | ||
729 | submod: t | ||
730 | |||
731 | crate::module::submod | ||
732 | Baz: t v | ||
733 | "#]], | ||
734 | ); | ||
735 | } | ||
736 | |||
737 | #[test] | ||
738 | fn nested_out_of_line_module() { | ||
739 | check( | ||
740 | r#" | ||
741 | //- /lib.rs | ||
742 | mod a { | ||
743 | mod b { | ||
744 | mod c; | ||
745 | } | ||
746 | } | ||
747 | |||
748 | //- /a/b/c.rs | ||
749 | struct X; | ||
750 | "#, | ||
751 | expect![[r#" | ||
752 | crate | ||
753 | a: t | ||
754 | |||
755 | crate::a | ||
756 | b: t | ||
757 | |||
758 | crate::a::b | ||
759 | c: t | ||
760 | |||
761 | crate::a::b::c | ||
762 | X: t v | ||
763 | "#]], | ||
764 | ); | ||
765 | } | ||
766 | |||
767 | #[test] | ||
768 | fn nested_out_of_line_module_with_path() { | ||
769 | check( | ||
770 | r#" | ||
771 | //- /lib.rs | ||
772 | mod a { | ||
773 | #[path = "d/e"] | ||
774 | mod b { | ||
775 | mod c; | ||
776 | } | ||
777 | } | ||
778 | |||
779 | //- /a/d/e/c.rs | ||
780 | struct X; | ||
781 | "#, | ||
782 | expect![[r#" | ||
783 | crate | ||
784 | a: t | ||
785 | |||
786 | crate::a | ||
787 | b: t | ||
788 | |||
789 | crate::a::b | ||
790 | c: t | ||
791 | |||
792 | crate::a::b::c | ||
793 | X: t v | ||
794 | "#]], | ||
795 | ); | ||
796 | } | ||