1 use super::*;
2
3 #[test]
name_res_works_for_broken_modules()4 fn name_res_works_for_broken_modules() {
5 cov_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]
nested_module_resolution()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]
nested_module_resolution_2()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]
module_resolution_works_for_non_standard_filenames()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]
module_resolution_works_for_raw_modules()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 mod foo;
131 mod r#async;
132 pub struct Bar;
133
134 //- /async/foo.rs
135 pub struct Foo;
136
137 //- /async/async.rs
138 pub struct Baz;
139 "#,
140 expect![[r#"
141 crate
142 Bar: t v
143 r#async: t
144
145 crate::r#async
146 Bar: t v
147 foo: t
148 r#async: t
149
150 crate::r#async::foo
151 Foo: t v
152
153 crate::r#async::r#async
154 Baz: t v
155 "#]],
156 );
157 }
158
159 #[test]
module_resolution_works_for_inline_raw_modules()160 fn module_resolution_works_for_inline_raw_modules() {
161 check(
162 r#"
163 //- /lib.rs
164 mod r#async {
165 pub mod a;
166 pub mod r#async;
167 }
168 use self::r#async::a::Foo;
169 use self::r#async::r#async::Bar;
170
171 //- /async/a.rs
172 pub struct Foo;
173
174 //- /async/async.rs
175 pub struct Bar;
176 "#,
177 expect![[r#"
178 crate
179 Bar: t v
180 Foo: t v
181 r#async: t
182
183 crate::r#async
184 a: t
185 r#async: t
186
187 crate::r#async::a
188 Foo: t v
189
190 crate::r#async::r#async
191 Bar: t v
192 "#]],
193 );
194 }
195
196 #[test]
module_resolution_decl_path()197 fn module_resolution_decl_path() {
198 check(
199 r#"
200 //- /lib.rs
201 #[path = "bar/baz/foo.rs"]
202 mod foo;
203 use self::foo::Bar;
204
205 //- /bar/baz/foo.rs
206 pub struct Bar;
207 "#,
208 expect![[r#"
209 crate
210 Bar: t v
211 foo: t
212
213 crate::foo
214 Bar: t v
215 "#]],
216 );
217 }
218
219 #[test]
module_resolution_module_with_path_in_mod_rs()220 fn module_resolution_module_with_path_in_mod_rs() {
221 check(
222 r#"
223 //- /main.rs
224 mod foo;
225
226 //- /foo/mod.rs
227 #[path = "baz.rs"]
228 pub mod bar;
229 use self::bar::Baz;
230
231 //- /foo/baz.rs
232 pub struct Baz;
233 "#,
234 expect![[r#"
235 crate
236 foo: t
237
238 crate::foo
239 Baz: t v
240 bar: t
241
242 crate::foo::bar
243 Baz: t v
244 "#]],
245 );
246 }
247
248 #[test]
module_resolution_module_with_path_non_crate_root()249 fn module_resolution_module_with_path_non_crate_root() {
250 check(
251 r#"
252 //- /main.rs
253 mod foo;
254
255 //- /foo.rs
256 #[path = "baz.rs"]
257 pub mod bar;
258 use self::bar::Baz;
259
260 //- /baz.rs
261 pub struct Baz;
262 "#,
263 expect![[r#"
264 crate
265 foo: t
266
267 crate::foo
268 Baz: t v
269 bar: t
270
271 crate::foo::bar
272 Baz: t v
273 "#]],
274 );
275 }
276
277 #[test]
module_resolution_module_decl_path_super()278 fn module_resolution_module_decl_path_super() {
279 check(
280 r#"
281 //- /main.rs
282 #[path = "bar/baz/module.rs"]
283 mod foo;
284 pub struct Baz;
285
286 //- /bar/baz/module.rs
287 use super::Baz;
288 "#,
289 expect![[r#"
290 crate
291 Baz: t v
292 foo: t
293
294 crate::foo
295 Baz: t v
296 "#]],
297 );
298 }
299
300 #[test]
module_resolution_explicit_path_mod_rs()301 fn module_resolution_explicit_path_mod_rs() {
302 check(
303 r#"
304 //- /main.rs
305 #[path = "module/mod.rs"]
306 mod foo;
307
308 //- /module/mod.rs
309 pub struct Baz;
310 "#,
311 expect![[r#"
312 crate
313 foo: t
314
315 crate::foo
316 Baz: t v
317 "#]],
318 );
319 }
320
321 #[test]
module_resolution_relative_path()322 fn module_resolution_relative_path() {
323 check(
324 r#"
325 //- /main.rs
326 mod foo;
327
328 //- /foo.rs
329 #[path = "./sub.rs"]
330 pub mod foo_bar;
331
332 //- /sub.rs
333 pub struct Baz;
334 "#,
335 expect![[r#"
336 crate
337 foo: t
338
339 crate::foo
340 foo_bar: t
341
342 crate::foo::foo_bar
343 Baz: t v
344 "#]],
345 );
346 }
347
348 #[test]
module_resolution_relative_path_2()349 fn module_resolution_relative_path_2() {
350 check(
351 r#"
352 //- /main.rs
353 mod foo;
354
355 //- /foo/mod.rs
356 #[path="../sub.rs"]
357 pub mod foo_bar;
358
359 //- /sub.rs
360 pub struct Baz;
361 "#,
362 expect![[r#"
363 crate
364 foo: t
365
366 crate::foo
367 foo_bar: t
368
369 crate::foo::foo_bar
370 Baz: t v
371 "#]],
372 );
373 }
374
375 #[test]
module_resolution_relative_path_outside_root()376 fn module_resolution_relative_path_outside_root() {
377 check(
378 r#"
379 //- /a/b/c/d/e/main.rs crate:main
380 #[path="../../../../../outside.rs"]
381 mod foo;
382
383 //- /outside.rs
384 mod bar;
385
386 //- /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]
module_resolution_explicit_path_mod_rs_2()403 fn module_resolution_explicit_path_mod_rs_2() {
404 check(
405 r#"
406 //- /main.rs
407 #[path = "module/bar/mod.rs"]
408 mod foo;
409
410 //- /module/bar/mod.rs
411 pub struct Baz;
412 "#,
413 expect![[r#"
414 crate
415 foo: t
416
417 crate::foo
418 Baz: t v
419 "#]],
420 );
421 }
422
423 #[test]
module_resolution_explicit_path_mod_rs_with_win_separator()424 fn module_resolution_explicit_path_mod_rs_with_win_separator() {
425 check(
426 r#"
427 //- /main.rs
428 #[path = r"module\bar\mod.rs"]
429 mod foo;
430
431 //- /module/bar/mod.rs
432 pub struct Baz;
433 "#,
434 expect![[r#"
435 crate
436 foo: t
437
438 crate::foo
439 Baz: t v
440 "#]],
441 );
442 }
443
444 #[test]
module_resolution_decl_inside_inline_module_with_path_attribute()445 fn module_resolution_decl_inside_inline_module_with_path_attribute() {
446 check(
447 r#"
448 //- /main.rs
449 #[path = "models"]
450 mod foo { mod bar; }
451
452 //- /models/bar.rs
453 pub struct Baz;
454 "#,
455 expect![[r#"
456 crate
457 foo: t
458
459 crate::foo
460 bar: t
461
462 crate::foo::bar
463 Baz: t v
464 "#]],
465 );
466 }
467
468 #[test]
module_resolution_decl_inside_inline_module()469 fn module_resolution_decl_inside_inline_module() {
470 check(
471 r#"
472 //- /main.rs
473 mod foo { mod bar; }
474
475 //- /foo/bar.rs
476 pub struct Baz;
477 "#,
478 expect![[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
491 #[test]
module_resolution_decl_inside_inline_module_2_with_path_attribute()492 fn module_resolution_decl_inside_inline_module_2_with_path_attribute() {
493 check(
494 r#"
495 //- /main.rs
496 #[path = "models/db"]
497 mod foo { mod bar; }
498
499 //- /models/db/bar.rs
500 pub struct Baz;
501 "#,
502 expect![[r#"
503 crate
504 foo: t
505
506 crate::foo
507 bar: t
508
509 crate::foo::bar
510 Baz: t v
511 "#]],
512 );
513 }
514
515 #[test]
module_resolution_decl_inside_inline_module_3()516 fn module_resolution_decl_inside_inline_module_3() {
517 check(
518 r#"
519 //- /main.rs
520 #[path = "models/db"]
521 mod foo {
522 #[path = "users.rs"]
523 mod bar;
524 }
525
526 //- /models/db/users.rs
527 pub struct Baz;
528 "#,
529 expect![[r#"
530 crate
531 foo: t
532
533 crate::foo
534 bar: t
535
536 crate::foo::bar
537 Baz: t v
538 "#]],
539 );
540 }
541
542 #[test]
module_resolution_decl_inside_inline_module_empty_path()543 fn module_resolution_decl_inside_inline_module_empty_path() {
544 check(
545 r#"
546 //- /main.rs
547 #[path = ""]
548 mod foo {
549 #[path = "users.rs"]
550 mod bar;
551 }
552
553 //- /users.rs
554 pub struct Baz;
555 "#,
556 expect![[r#"
557 crate
558 foo: t
559
560 crate::foo
561 bar: t
562
563 crate::foo::bar
564 Baz: t v
565 "#]],
566 );
567 }
568
569 #[test]
module_resolution_decl_empty_path()570 fn module_resolution_decl_empty_path() {
571 check(
572 r#"
573 //- /main.rs
574 #[path = ""] // Should try to read `/` (a directory)
575 mod foo;
576
577 //- /foo.rs
578 pub struct Baz;
579 "#,
580 expect![[r#"
581 crate
582 foo: t
583
584 crate::foo
585 "#]],
586 );
587 }
588
589 #[test]
module_resolution_decl_inside_inline_module_relative_path()590 fn module_resolution_decl_inside_inline_module_relative_path() {
591 check(
592 r#"
593 //- /main.rs
594 #[path = "./models"]
595 mod foo { mod bar; }
596
597 //- /models/bar.rs
598 pub struct Baz;
599 "#,
600 expect![[r#"
601 crate
602 foo: t
603
604 crate::foo
605 bar: t
606
607 crate::foo::bar
608 Baz: t v
609 "#]],
610 );
611 }
612
613 #[test]
module_resolution_decl_inside_inline_module_in_crate_root()614 fn module_resolution_decl_inside_inline_module_in_crate_root() {
615 check(
616 r#"
617 //- /main.rs
618 mod foo {
619 #[path = "baz.rs"]
620 pub mod bar;
621 }
622 use self::foo::bar::Baz;
623
624 //- /foo/baz.rs
625 pub struct Baz;
626 "#,
627 expect![[r#"
628 crate
629 Baz: t v
630 foo: t
631
632 crate::foo
633 bar: t
634
635 crate::foo::bar
636 Baz: t v
637 "#]],
638 );
639 }
640
641 #[test]
module_resolution_decl_inside_inline_module_in_mod_rs()642 fn module_resolution_decl_inside_inline_module_in_mod_rs() {
643 check(
644 r#"
645 //- /main.rs
646 mod foo;
647
648 //- /foo/mod.rs
649 mod bar {
650 #[path = "qwe.rs"]
651 pub mod baz;
652 }
653 use self::bar::baz::Baz;
654
655 //- /foo/bar/qwe.rs
656 pub struct Baz;
657 "#,
658 expect![[r#"
659 crate
660 foo: t
661
662 crate::foo
663 Baz: t v
664 bar: t
665
666 crate::foo::bar
667 baz: t
668
669 crate::foo::bar::baz
670 Baz: t v
671 "#]],
672 );
673 }
674
675 #[test]
module_resolution_decl_inside_inline_module_in_non_crate_root()676 fn module_resolution_decl_inside_inline_module_in_non_crate_root() {
677 check(
678 r#"
679 //- /main.rs
680 mod foo;
681
682 //- /foo.rs
683 mod bar {
684 #[path = "qwe.rs"]
685 pub mod baz;
686 }
687 use self::bar::baz::Baz;
688
689 //- /foo/bar/qwe.rs
690 pub struct Baz;
691 "#,
692 expect![[r#"
693 crate
694 foo: t
695
696 crate::foo
697 Baz: t v
698 bar: t
699
700 crate::foo::bar
701 baz: t
702
703 crate::foo::bar::baz
704 Baz: t v
705 "#]],
706 );
707 }
708
709 #[test]
module_resolution_decl_inside_inline_module_in_non_crate_root_2()710 fn module_resolution_decl_inside_inline_module_in_non_crate_root_2() {
711 check(
712 r#"
713 //- /main.rs
714 mod foo;
715
716 //- /foo.rs
717 #[path = "bar"]
718 mod bar {
719 pub mod baz;
720 }
721 use self::bar::baz::Baz;
722
723 //- /bar/baz.rs
724 pub struct Baz;
725 "#,
726 expect![[r#"
727 crate
728 foo: t
729
730 crate::foo
731 Baz: t v
732 bar: t
733
734 crate::foo::bar
735 baz: t
736
737 crate::foo::bar::baz
738 Baz: t v
739 "#]],
740 );
741 }
742
743 #[test]
module_resolution_decl_inside_module_in_non_crate_root_2()744 fn module_resolution_decl_inside_module_in_non_crate_root_2() {
745 check(
746 r#"
747 //- /main.rs
748 #[path="module/m2.rs"]
749 mod module;
750
751 //- /module/m2.rs
752 pub mod submod;
753
754 //- /module/submod.rs
755 pub struct Baz;
756 "#,
757 expect![[r#"
758 crate
759 module: t
760
761 crate::module
762 submod: t
763
764 crate::module::submod
765 Baz: t v
766 "#]],
767 );
768 }
769
770 #[test]
nested_out_of_line_module()771 fn nested_out_of_line_module() {
772 check(
773 r#"
774 //- /lib.rs
775 mod a {
776 mod b {
777 mod c;
778 }
779 }
780
781 //- /a/b/c.rs
782 struct X;
783 "#,
784 expect![[r#"
785 crate
786 a: t
787
788 crate::a
789 b: t
790
791 crate::a::b
792 c: t
793
794 crate::a::b::c
795 X: t v
796 "#]],
797 );
798 }
799
800 #[test]
nested_out_of_line_module_with_path()801 fn nested_out_of_line_module_with_path() {
802 check(
803 r#"
804 //- /lib.rs
805 mod a {
806 #[path = "d/e"]
807 mod b {
808 mod c;
809 }
810 }
811
812 //- /a/d/e/c.rs
813 struct X;
814 "#,
815 expect![[r#"
816 crate
817 a: t
818
819 crate::a
820 b: t
821
822 crate::a::b
823 c: t
824
825 crate::a::b::c
826 X: t v
827 "#]],
828 );
829 }
830
831 #[test]
circular_mods()832 fn circular_mods() {
833 cov_mark::check!(circular_mods);
834 compute_crate_def_map(
835 r#"
836 //- /lib.rs
837 mod foo;
838 //- /foo.rs
839 #[path = "./foo.rs"]
840 mod foo;
841 "#,
842 );
843
844 compute_crate_def_map(
845 r#"
846 //- /lib.rs
847 mod foo;
848 //- /foo.rs
849 #[path = "./bar.rs"]
850 mod bar;
851 //- /bar.rs
852 #[path = "./foo.rs"]
853 mod foo;
854 "#,
855 );
856 }
857
858 #[test]
abs_path_ignores_local()859 fn abs_path_ignores_local() {
860 check(
861 r#"
862 //- /main.rs crate:main deps:core
863 pub use ::core::hash::Hash;
864 pub mod core {}
865
866 //- /lib.rs crate:core
867 pub mod hash { pub trait Hash {} }
868 "#,
869 expect![[r#"
870 crate
871 Hash: t
872 core: t
873
874 crate::core
875 "#]],
876 );
877 }
878
879 #[test]
cfg_in_module_file()880 fn cfg_in_module_file() {
881 // Inner `#![cfg]` in a module file makes the whole module disappear.
882 check(
883 r#"
884 //- /main.rs
885 mod module;
886
887 //- /module.rs
888 #![cfg(NEVER)]
889
890 struct AlsoShouldNotAppear;
891 "#,
892 expect![[r#"
893 crate
894 "#]],
895 )
896 }
897