1 mod globs;
2 mod incremental;
3 mod macros;
4 mod mod_resolution;
5 mod primitives;
6
7 use base_db::{fixture::WithFixture, SourceDatabase};
8 use expect_test::{expect, Expect};
9 use triomphe::Arc;
10
11 use crate::{db::DefDatabase, test_db::TestDB};
12
13 use super::DefMap;
14
compute_crate_def_map(ra_fixture: &str) -> Arc<DefMap>15 fn compute_crate_def_map(ra_fixture: &str) -> Arc<DefMap> {
16 let db = TestDB::with_files(ra_fixture);
17 let krate = db.crate_graph().iter().next().unwrap();
18 db.crate_def_map(krate)
19 }
20
render_crate_def_map(ra_fixture: &str) -> String21 fn render_crate_def_map(ra_fixture: &str) -> String {
22 let db = TestDB::with_files(ra_fixture);
23 let krate = db.crate_graph().iter().next().unwrap();
24 db.crate_def_map(krate).dump(&db)
25 }
26
check(ra_fixture: &str, expect: Expect)27 fn check(ra_fixture: &str, expect: Expect) {
28 let actual = render_crate_def_map(ra_fixture);
29 expect.assert_eq(&actual);
30 }
31
32 #[test]
crate_def_map_smoke_test()33 fn crate_def_map_smoke_test() {
34 check(
35 r#"
36 //- /lib.rs
37 mod foo;
38 struct S;
39 use crate::foo::bar::E;
40 use self::E::V;
41
42 //- /foo/mod.rs
43 pub mod bar;
44 fn f() {}
45
46 //- /foo/bar.rs
47 pub struct Baz;
48
49 union U { to_be: bool, not_to_be: u8 }
50 enum E { V }
51
52 extern {
53 type Ext;
54 static EXT: u8;
55 fn ext();
56 }
57 "#,
58 expect![[r#"
59 crate
60 E: _
61 S: t v
62 V: _
63 foo: t
64
65 crate::foo
66 bar: t
67 f: v
68
69 crate::foo::bar
70 Baz: t v
71 E: t
72 EXT: v
73 Ext: t
74 U: t
75 ext: v
76 "#]],
77 );
78 }
79
80 #[test]
crate_def_map_super_super()81 fn crate_def_map_super_super() {
82 check(
83 r#"
84 mod a {
85 const A: usize = 0;
86 mod b {
87 const B: usize = 0;
88 mod c {
89 use super::super::*;
90 }
91 }
92 }
93 "#,
94 expect![[r#"
95 crate
96 a: t
97
98 crate::a
99 A: v
100 b: t
101
102 crate::a::b
103 B: v
104 c: t
105
106 crate::a::b::c
107 A: v
108 b: t
109 "#]],
110 );
111 }
112
113 #[test]
crate_def_map_fn_mod_same_name()114 fn crate_def_map_fn_mod_same_name() {
115 check(
116 r#"
117 mod m {
118 pub mod z {}
119 pub fn z() {}
120 }
121 "#,
122 expect![[r#"
123 crate
124 m: t
125
126 crate::m
127 z: t v
128
129 crate::m::z
130 "#]],
131 );
132 }
133
134 #[test]
bogus_paths()135 fn bogus_paths() {
136 cov_mark::check!(bogus_paths);
137 check(
138 r#"
139 //- /lib.rs
140 mod foo;
141 struct S;
142 use self;
143
144 //- /foo/mod.rs
145 use super;
146 use crate;
147 "#,
148 expect![[r#"
149 crate
150 S: t v
151 foo: t
152
153 crate::foo
154 "#]],
155 );
156 }
157
158 #[test]
use_as()159 fn use_as() {
160 check(
161 r#"
162 //- /lib.rs
163 mod foo;
164 use crate::foo::Baz as Foo;
165
166 //- /foo/mod.rs
167 pub struct Baz;
168 "#,
169 expect![[r#"
170 crate
171 Foo: t v
172 foo: t
173
174 crate::foo
175 Baz: t v
176 "#]],
177 );
178 }
179
180 #[test]
use_trees()181 fn use_trees() {
182 check(
183 r#"
184 //- /lib.rs
185 mod foo;
186 use crate::foo::bar::{Baz, Quux};
187
188 //- /foo/mod.rs
189 pub mod bar;
190
191 //- /foo/bar.rs
192 pub struct Baz;
193 pub enum Quux {};
194 "#,
195 expect![[r#"
196 crate
197 Baz: t v
198 Quux: t
199 foo: t
200
201 crate::foo
202 bar: t
203
204 crate::foo::bar
205 Baz: t v
206 Quux: t
207 "#]],
208 );
209 }
210
211 #[test]
re_exports()212 fn re_exports() {
213 check(
214 r#"
215 //- /lib.rs
216 mod foo;
217 use self::foo::Baz;
218
219 //- /foo/mod.rs
220 pub mod bar;
221 pub use self::bar::Baz;
222
223 //- /foo/bar.rs
224 pub struct Baz;
225 "#,
226 expect![[r#"
227 crate
228 Baz: t v
229 foo: t
230
231 crate::foo
232 Baz: t v
233 bar: t
234
235 crate::foo::bar
236 Baz: t v
237 "#]],
238 );
239 }
240
241 #[test]
std_prelude()242 fn std_prelude() {
243 cov_mark::check!(std_prelude);
244 check(
245 r#"
246 //- /main.rs crate:main deps:test_crate
247 #[prelude_import]
248 use ::test_crate::prelude::*;
249
250 use Foo::*;
251
252 //- /lib.rs crate:test_crate
253 pub mod prelude;
254
255 //- /prelude.rs
256 pub enum Foo { Bar, Baz }
257 "#,
258 expect![[r#"
259 crate
260 Bar: t v
261 Baz: t v
262 "#]],
263 );
264 }
265
266 #[test]
can_import_enum_variant()267 fn can_import_enum_variant() {
268 cov_mark::check!(can_import_enum_variant);
269 check(
270 r#"
271 enum E { V }
272 use self::E::V;
273 "#,
274 expect![[r#"
275 crate
276 E: t
277 V: t v
278 "#]],
279 );
280 }
281
282 #[test]
edition_2015_imports()283 fn edition_2015_imports() {
284 check(
285 r#"
286 //- /main.rs crate:main deps:other_crate edition:2015
287 mod foo;
288 mod bar;
289
290 //- /bar.rs
291 struct Bar;
292
293 //- /foo.rs
294 use bar::Bar;
295 use other_crate::FromLib;
296
297 //- /lib.rs crate:other_crate edition:2018
298 pub struct FromLib;
299 "#,
300 expect![[r#"
301 crate
302 bar: t
303 foo: t
304
305 crate::bar
306 Bar: t v
307
308 crate::foo
309 Bar: _
310 FromLib: t v
311 "#]],
312 );
313 }
314
315 #[test]
item_map_using_self()316 fn item_map_using_self() {
317 check(
318 r#"
319 //- /lib.rs
320 mod foo;
321 use crate::foo::bar::Baz::{self};
322
323 //- /foo/mod.rs
324 pub mod bar;
325
326 //- /foo/bar.rs
327 pub struct Baz;
328 "#,
329 expect![[r#"
330 crate
331 Baz: t
332 foo: t
333
334 crate::foo
335 bar: t
336
337 crate::foo::bar
338 Baz: t v
339 "#]],
340 );
341 }
342
343 #[test]
item_map_across_crates()344 fn item_map_across_crates() {
345 check(
346 r#"
347 //- /main.rs crate:main deps:test_crate
348 use test_crate::Baz;
349
350 //- /lib.rs crate:test_crate
351 pub struct Baz;
352 "#,
353 expect![[r#"
354 crate
355 Baz: t v
356 "#]],
357 );
358 }
359
360 #[test]
extern_crate_rename()361 fn extern_crate_rename() {
362 check(
363 r#"
364 //- /main.rs crate:main deps:alloc
365 extern crate alloc as alloc_crate;
366 mod alloc;
367 mod sync;
368
369 //- /sync.rs
370 use alloc_crate::Arc;
371
372 //- /lib.rs crate:alloc
373 pub struct Arc;
374 "#,
375 expect![[r#"
376 crate
377 alloc: t
378 alloc_crate: t
379 sync: t
380
381 crate::alloc
382
383 crate::sync
384 Arc: t v
385 "#]],
386 );
387 }
388
389 #[test]
extern_crate_rename_2015_edition()390 fn extern_crate_rename_2015_edition() {
391 check(
392 r#"
393 //- /main.rs crate:main deps:alloc edition:2015
394 extern crate alloc as alloc_crate;
395 mod alloc;
396 mod sync;
397
398 //- /sync.rs
399 use alloc_crate::Arc;
400
401 //- /lib.rs crate:alloc
402 pub struct Arc;
403 "#,
404 expect![[r#"
405 crate
406 alloc: t
407 alloc_crate: t
408 sync: t
409
410 crate::alloc
411
412 crate::sync
413 Arc: t v
414 "#]],
415 );
416 }
417
418 #[test]
macro_use_extern_crate_self()419 fn macro_use_extern_crate_self() {
420 cov_mark::check!(ignore_macro_use_extern_crate_self);
421 check(
422 r#"
423 //- /main.rs crate:main
424 #[macro_use]
425 extern crate self as bla;
426 "#,
427 expect![[r#"
428 crate
429 bla: t
430 "#]],
431 );
432 }
433
434 #[test]
reexport_across_crates()435 fn reexport_across_crates() {
436 check(
437 r#"
438 //- /main.rs crate:main deps:test_crate
439 use test_crate::Baz;
440
441 //- /lib.rs crate:test_crate
442 pub use foo::Baz;
443 mod foo;
444
445 //- /foo.rs
446 pub struct Baz;
447 "#,
448 expect![[r#"
449 crate
450 Baz: t v
451 "#]],
452 );
453 }
454
455 #[test]
values_dont_shadow_extern_crates()456 fn values_dont_shadow_extern_crates() {
457 check(
458 r#"
459 //- /main.rs crate:main deps:foo
460 fn foo() {}
461 use foo::Bar;
462
463 //- /foo/lib.rs crate:foo
464 pub struct Bar;
465 "#,
466 expect![[r#"
467 crate
468 Bar: t v
469 foo: v
470 "#]],
471 );
472 }
473
474 #[test]
no_std_prelude()475 fn no_std_prelude() {
476 check(
477 r#"
478 //- /main.rs edition:2018 crate:main deps:core,std
479 #![cfg_attr(not(never), no_std)]
480 use Rust;
481
482 //- /core.rs crate:core
483 pub mod prelude {
484 pub mod rust_2018 {
485 pub struct Rust;
486 }
487 }
488 //- /std.rs crate:std deps:core
489 pub mod prelude {
490 pub mod rust_2018 {
491 }
492 }
493 "#,
494 expect![[r#"
495 crate
496 Rust: t v
497 "#]],
498 );
499 }
500
501 #[test]
edition_specific_preludes()502 fn edition_specific_preludes() {
503 // We can't test the 2015 prelude here since you can't reexport its contents with 2015's
504 // absolute paths.
505
506 check(
507 r#"
508 //- /main.rs edition:2018 crate:main deps:std
509 use Rust2018;
510
511 //- /std.rs crate:std
512 pub mod prelude {
513 pub mod rust_2018 {
514 pub struct Rust2018;
515 }
516 }
517 "#,
518 expect![[r#"
519 crate
520 Rust2018: t v
521 "#]],
522 );
523 check(
524 r#"
525 //- /main.rs edition:2021 crate:main deps:std
526 use Rust2021;
527
528 //- /std.rs crate:std
529 pub mod prelude {
530 pub mod rust_2021 {
531 pub struct Rust2021;
532 }
533 }
534 "#,
535 expect![[r#"
536 crate
537 Rust2021: t v
538 "#]],
539 );
540 }
541
542 #[test]
std_prelude_takes_precedence_above_core_prelude()543 fn std_prelude_takes_precedence_above_core_prelude() {
544 check(
545 r#"
546 //- /main.rs edition:2018 crate:main deps:core,std
547 use {Foo, Bar};
548
549 //- /std.rs crate:std deps:core
550 pub mod prelude {
551 pub mod rust_2018 {
552 pub struct Foo;
553 pub use core::prelude::rust_2018::Bar;
554 }
555 }
556
557 //- /core.rs crate:core
558 pub mod prelude {
559 pub mod rust_2018 {
560 pub struct Bar;
561 }
562 }
563 "#,
564 expect![[r#"
565 crate
566 Bar: t v
567 Foo: t v
568 "#]],
569 );
570 }
571
572 #[test]
cfg_not_test()573 fn cfg_not_test() {
574 check(
575 r#"
576 //- /main.rs edition:2018 crate:main deps:std
577 use {Foo, Bar, Baz};
578
579 //- /lib.rs crate:std
580 pub mod prelude {
581 pub mod rust_2018 {
582 #[cfg(test)]
583 pub struct Foo;
584 #[cfg(not(test))]
585 pub struct Bar;
586 #[cfg(all(not(any()), feature = "foo", feature = "bar", opt = "42"))]
587 pub struct Baz;
588 }
589 }
590 "#,
591 expect![[r#"
592 crate
593 Bar: t v
594 Baz: _
595 Foo: _
596 "#]],
597 );
598 }
599
600 #[test]
cfg_test()601 fn cfg_test() {
602 check(
603 r#"
604 //- /main.rs edition:2018 crate:main deps:std
605 use {Foo, Bar, Baz};
606
607 //- /lib.rs crate:std cfg:test,feature=foo,feature=bar,opt=42
608 pub mod prelude {
609 pub mod rust_2018 {
610 #[cfg(test)]
611 pub struct Foo;
612 #[cfg(not(test))]
613 pub struct Bar;
614 #[cfg(all(not(any()), feature = "foo", feature = "bar", opt = "42"))]
615 pub struct Baz;
616 }
617 }
618 "#,
619 expect![[r#"
620 crate
621 Bar: _
622 Baz: t v
623 Foo: t v
624 "#]],
625 );
626 }
627
628 #[test]
infer_multiple_namespace()629 fn infer_multiple_namespace() {
630 check(
631 r#"
632 //- /main.rs
633 mod a {
634 pub type T = ();
635 pub use crate::b::*;
636 }
637
638 use crate::a::T;
639
640 mod b {
641 pub const T: () = ();
642 }
643 "#,
644 expect![[r#"
645 crate
646 T: t v
647 a: t
648 b: t
649
650 crate::a
651 T: t v
652
653 crate::b
654 T: v
655 "#]],
656 );
657 }
658
659 #[test]
underscore_import()660 fn underscore_import() {
661 check(
662 r#"
663 //- /main.rs
664 use tr::Tr as _;
665 use tr::Tr2 as _;
666
667 mod tr {
668 pub trait Tr {}
669 pub trait Tr2 {}
670 }
671 "#,
672 expect![[r#"
673 crate
674 _: t
675 _: t
676 tr: t
677
678 crate::tr
679 Tr: t
680 Tr2: t
681 "#]],
682 );
683 }
684
685 #[test]
underscore_reexport()686 fn underscore_reexport() {
687 check(
688 r#"
689 //- /main.rs
690 mod tr {
691 pub trait PubTr {}
692 pub trait PrivTr {}
693 }
694 mod reex {
695 use crate::tr::PrivTr as _;
696 pub use crate::tr::PubTr as _;
697 }
698 use crate::reex::*;
699 "#,
700 expect![[r#"
701 crate
702 _: t
703 reex: t
704 tr: t
705
706 crate::reex
707 _: t
708 _: t
709
710 crate::tr
711 PrivTr: t
712 PubTr: t
713 "#]],
714 );
715 }
716
717 #[test]
underscore_pub_crate_reexport()718 fn underscore_pub_crate_reexport() {
719 cov_mark::check!(upgrade_underscore_visibility);
720 check(
721 r#"
722 //- /main.rs crate:main deps:lib
723 use lib::*;
724
725 //- /lib.rs crate:lib
726 use tr::Tr as _;
727 pub use tr::Tr as _;
728
729 mod tr {
730 pub trait Tr {}
731 }
732 "#,
733 expect![[r#"
734 crate
735 _: t
736 "#]],
737 );
738 }
739
740 #[test]
underscore_nontrait()741 fn underscore_nontrait() {
742 check(
743 r#"
744 //- /main.rs
745 mod m {
746 pub struct Struct;
747 pub enum Enum {}
748 pub const CONST: () = ();
749 }
750 use crate::m::{Struct as _, Enum as _, CONST as _};
751 "#,
752 expect![[r#"
753 crate
754 m: t
755
756 crate::m
757 CONST: v
758 Enum: t
759 Struct: t v
760 "#]],
761 );
762 }
763
764 #[test]
underscore_name_conflict()765 fn underscore_name_conflict() {
766 check(
767 r#"
768 //- /main.rs
769 struct Tr;
770
771 use tr::Tr as _;
772
773 mod tr {
774 pub trait Tr {}
775 }
776 "#,
777 expect![[r#"
778 crate
779 _: t
780 Tr: t v
781 tr: t
782
783 crate::tr
784 Tr: t
785 "#]],
786 );
787 }
788
789 #[test]
cfg_the_entire_crate()790 fn cfg_the_entire_crate() {
791 check(
792 r#"
793 //- /main.rs
794 #![cfg(never)]
795
796 pub struct S;
797 pub enum E {}
798 pub fn f() {}
799 "#,
800 expect![[r#"
801 crate
802 "#]],
803 );
804 }
805
806 #[test]
use_crate_as()807 fn use_crate_as() {
808 check(
809 r#"
810 use crate as foo;
811
812 use foo::bar as baz;
813
814 fn bar() {}
815 "#,
816 expect![[r#"
817 crate
818 bar: v
819 baz: v
820 foo: t
821 "#]],
822 );
823 }
824
825 #[test]
self_imports_only_types()826 fn self_imports_only_types() {
827 check(
828 r#"
829 //- /main.rs
830 mod m {
831 pub macro S() {}
832 pub struct S;
833 }
834
835 use self::m::S::{self};
836 "#,
837 expect![[r#"
838 crate
839 S: t
840 m: t
841
842 crate::m
843 S: t v m
844 "#]],
845 );
846 }
847
848 #[test]
import_from_extern_crate_only_imports_public_items()849 fn import_from_extern_crate_only_imports_public_items() {
850 check(
851 r#"
852 //- /lib.rs crate:lib deps:settings,macros
853 use macros::settings;
854 use settings::Settings;
855 //- /settings.rs crate:settings
856 pub struct Settings;
857 //- /macros.rs crate:macros
858 mod settings {}
859 pub const settings: () = ();
860 "#,
861 expect![[r#"
862 crate
863 Settings: t v
864 settings: v
865 "#]],
866 )
867 }
868
869 #[test]
non_prelude_deps()870 fn non_prelude_deps() {
871 check(
872 r#"
873 //- /lib.rs crate:lib deps:dep extern-prelude:
874 use dep::Struct;
875 //- /dep.rs crate:dep
876 pub struct Struct;
877 "#,
878 expect![[r#"
879 crate
880 Struct: _
881 "#]],
882 );
883 check(
884 r#"
885 //- /lib.rs crate:lib deps:dep extern-prelude:
886 extern crate dep;
887 use dep::Struct;
888 //- /dep.rs crate:dep
889 pub struct Struct;
890 "#,
891 expect![[r#"
892 crate
893 Struct: t v
894 dep: t
895 "#]],
896 );
897 }
898
899 #[test]
braced_supers_in_use_tree()900 fn braced_supers_in_use_tree() {
901 cov_mark::check!(concat_super_mod_paths);
902 check(
903 r#"
904 mod some_module {
905 pub fn unknown_func() {}
906 }
907
908 mod other_module {
909 mod some_submodule {
910 use { super::{ super::unknown_func, }, };
911 }
912 }
913
914 use some_module::unknown_func;
915 "#,
916 expect![[r#"
917 crate
918 other_module: t
919 some_module: t
920 unknown_func: v
921
922 crate::other_module
923 some_submodule: t
924
925 crate::other_module::some_submodule
926 unknown_func: v
927
928 crate::some_module
929 unknown_func: v
930 "#]],
931 )
932 }
933