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