]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/nameres/tests.rs
Merge #6018
[rust.git] / crates / hir_def / src / nameres / tests.rs
1 mod globs;
2 mod incremental;
3 mod macros;
4 mod mod_resolution;
5 mod diagnostics;
6 mod primitives;
7
8 use std::sync::Arc;
9
10 use base_db::{fixture::WithFixture, SourceDatabase};
11 use expect_test::{expect, Expect};
12 use test_utils::mark;
13
14 use crate::{db::DefDatabase, nameres::*, test_db::TestDB};
15
16 fn compute_crate_def_map(fixture: &str) -> Arc<CrateDefMap> {
17     let db = TestDB::with_files(fixture);
18     let krate = db.crate_graph().iter().next().unwrap();
19     db.crate_def_map(krate)
20 }
21
22 fn check(ra_fixture: &str, expect: Expect) {
23     let db = TestDB::with_files(ra_fixture);
24     let krate = db.crate_graph().iter().next().unwrap();
25     let actual = db.crate_def_map(krate).dump();
26     expect.assert_eq(&actual);
27 }
28
29 #[test]
30 fn crate_def_map_smoke_test() {
31     check(
32         r#"
33 //- /lib.rs
34 mod foo;
35 struct S;
36 use crate::foo::bar::E;
37 use self::E::V;
38
39 //- /foo/mod.rs
40 pub mod bar;
41 fn f() {}
42
43 //- /foo/bar.rs
44 pub struct Baz;
45
46 union U { to_be: bool, not_to_be: u8 }
47 enum E { V }
48
49 extern {
50     type Ext;
51     static EXT: u8;
52     fn ext();
53 }
54 "#,
55         expect![[r#"
56             crate
57             E: t
58             S: t v
59             V: t v
60             foo: t
61
62             crate::foo
63             bar: t
64             f: v
65
66             crate::foo::bar
67             Baz: t v
68             E: t
69             EXT: v
70             Ext: t
71             U: t
72             ext: v
73         "#]],
74     );
75 }
76
77 #[test]
78 fn crate_def_map_super_super() {
79     check(
80         r#"
81 mod a {
82     const A: usize = 0;
83     mod b {
84         const B: usize = 0;
85         mod c {
86             use super::super::*;
87         }
88     }
89 }
90 "#,
91         expect![[r#"
92             crate
93             a: t
94
95             crate::a
96             A: v
97             b: t
98
99             crate::a::b
100             B: v
101             c: t
102
103             crate::a::b::c
104             A: v
105             b: t
106         "#]],
107     );
108 }
109
110 #[test]
111 fn crate_def_map_fn_mod_same_name() {
112     check(
113         r#"
114 mod m {
115     pub mod z {}
116     pub fn z() {}
117 }
118 "#,
119         expect![[r#"
120             crate
121             m: t
122
123             crate::m
124             z: t v
125
126             crate::m::z
127         "#]],
128     );
129 }
130
131 #[test]
132 fn bogus_paths() {
133     mark::check!(bogus_paths);
134     check(
135         r#"
136 //- /lib.rs
137 mod foo;
138 struct S;
139 use self;
140
141 //- /foo/mod.rs
142 use super;
143 use crate;
144 "#,
145         expect![[r#"
146             crate
147             S: t v
148             foo: t
149
150             crate::foo
151         "#]],
152     );
153 }
154
155 #[test]
156 fn use_as() {
157     check(
158         r#"
159 //- /lib.rs
160 mod foo;
161 use crate::foo::Baz as Foo;
162
163 //- /foo/mod.rs
164 pub struct Baz;
165 "#,
166         expect![[r#"
167             crate
168             Foo: t v
169             foo: t
170
171             crate::foo
172             Baz: t v
173         "#]],
174     );
175 }
176
177 #[test]
178 fn use_trees() {
179     check(
180         r#"
181 //- /lib.rs
182 mod foo;
183 use crate::foo::bar::{Baz, Quux};
184
185 //- /foo/mod.rs
186 pub mod bar;
187
188 //- /foo/bar.rs
189 pub struct Baz;
190 pub enum Quux {};
191 "#,
192         expect![[r#"
193             crate
194             Baz: t v
195             Quux: t
196             foo: t
197
198             crate::foo
199             bar: t
200
201             crate::foo::bar
202             Baz: t v
203             Quux: t
204         "#]],
205     );
206 }
207
208 #[test]
209 fn re_exports() {
210     check(
211         r#"
212 //- /lib.rs
213 mod foo;
214 use self::foo::Baz;
215
216 //- /foo/mod.rs
217 pub mod bar;
218 pub use self::bar::Baz;
219
220 //- /foo/bar.rs
221 pub struct Baz;
222 "#,
223         expect![[r#"
224             crate
225             Baz: t v
226             foo: t
227
228             crate::foo
229             Baz: t v
230             bar: t
231
232             crate::foo::bar
233             Baz: t v
234         "#]],
235     );
236 }
237
238 #[test]
239 fn std_prelude() {
240     mark::check!(std_prelude);
241     check(
242         r#"
243 //- /main.rs crate:main deps:test_crate
244 use Foo::*;
245
246 //- /lib.rs crate:test_crate
247 mod prelude;
248 #[prelude_import]
249 use prelude::*;
250
251 //- /prelude.rs
252 pub enum Foo { Bar, Baz };
253 "#,
254         expect![[r#"
255             crate
256             Bar: t v
257             Baz: t v
258         "#]],
259     );
260 }
261
262 #[test]
263 fn can_import_enum_variant() {
264     mark::check!(can_import_enum_variant);
265     check(
266         r#"
267 enum E { V }
268 use self::E::V;
269 "#,
270         expect![[r#"
271             crate
272             E: t
273             V: t v
274         "#]],
275     );
276 }
277
278 #[test]
279 fn edition_2015_imports() {
280     check(
281         r#"
282 //- /main.rs crate:main deps:other_crate edition:2015
283 mod foo;
284 mod bar;
285
286 //- /bar.rs
287 struct Bar;
288
289 //- /foo.rs
290 use bar::Bar;
291 use other_crate::FromLib;
292
293 //- /lib.rs crate:other_crate edition:2018
294 struct FromLib;
295 "#,
296         expect![[r#"
297             crate
298             bar: t
299             foo: t
300
301             crate::bar
302             Bar: t v
303
304             crate::foo
305             Bar: t v
306             FromLib: t v
307         "#]],
308     );
309 }
310
311 #[test]
312 fn item_map_using_self() {
313     check(
314         r#"
315 //- /lib.rs
316 mod foo;
317 use crate::foo::bar::Baz::{self};
318
319 //- /foo/mod.rs
320 pub mod bar;
321
322 //- /foo/bar.rs
323 pub struct Baz;
324 "#,
325         expect![[r#"
326             crate
327             Baz: t v
328             foo: t
329
330             crate::foo
331             bar: t
332
333             crate::foo::bar
334             Baz: t v
335         "#]],
336     );
337 }
338
339 #[test]
340 fn item_map_across_crates() {
341     check(
342         r#"
343 //- /main.rs crate:main deps:test_crate
344 use test_crate::Baz;
345
346 //- /lib.rs crate:test_crate
347 pub struct Baz;
348 "#,
349         expect![[r#"
350             crate
351             Baz: t v
352         "#]],
353     );
354 }
355
356 #[test]
357 fn extern_crate_rename() {
358     check(
359         r#"
360 //- /main.rs crate:main deps:alloc
361 extern crate alloc as alloc_crate;
362 mod alloc;
363 mod sync;
364
365 //- /sync.rs
366 use alloc_crate::Arc;
367
368 //- /lib.rs crate:alloc
369 struct Arc;
370 "#,
371         expect![[r#"
372             crate
373             alloc_crate: t
374             sync: t
375
376             crate::sync
377             Arc: t v
378         "#]],
379     );
380 }
381
382 #[test]
383 fn extern_crate_rename_2015_edition() {
384     check(
385         r#"
386 //- /main.rs crate:main deps:alloc edition:2015
387 extern crate alloc as alloc_crate;
388 mod alloc;
389 mod sync;
390
391 //- /sync.rs
392 use alloc_crate::Arc;
393
394 //- /lib.rs crate:alloc
395 struct Arc;
396 "#,
397         expect![[r#"
398             crate
399             alloc_crate: t
400             sync: t
401
402             crate::sync
403             Arc: t v
404         "#]],
405     );
406 }
407
408 #[test]
409 fn reexport_across_crates() {
410     check(
411         r#"
412 //- /main.rs crate:main deps:test_crate
413 use test_crate::Baz;
414
415 //- /lib.rs crate:test_crate
416 pub use foo::Baz;
417 mod foo;
418
419 //- /foo.rs
420 pub struct Baz;
421 "#,
422         expect![[r#"
423             crate
424             Baz: t v
425         "#]],
426     );
427 }
428
429 #[test]
430 fn values_dont_shadow_extern_crates() {
431     check(
432         r#"
433 //- /main.rs crate:main deps:foo
434 fn foo() {}
435 use foo::Bar;
436
437 //- /foo/lib.rs crate:foo
438 pub struct Bar;
439 "#,
440         expect![[r#"
441             crate
442             Bar: t v
443             foo: v
444         "#]],
445     );
446 }
447
448 #[test]
449 fn std_prelude_takes_precedence_above_core_prelude() {
450     check(
451         r#"
452 //- /main.rs crate:main deps:core,std
453 use {Foo, Bar};
454
455 //- /std.rs crate:std deps:core
456 #[prelude_import]
457 pub use self::prelude::*;
458 mod prelude {
459     pub struct Foo;
460     pub use core::prelude::Bar;
461 }
462
463 //- /core.rs crate:core
464 #[prelude_import]
465 pub use self::prelude::*;
466 mod prelude {
467     pub struct Bar;
468 }
469 "#,
470         expect![[r#"
471             crate
472             Bar: t v
473             Foo: t v
474         "#]],
475     );
476 }
477
478 #[test]
479 fn cfg_not_test() {
480     check(
481         r#"
482 //- /main.rs crate:main deps:std
483 use {Foo, Bar, Baz};
484
485 //- /lib.rs crate:std
486 #[prelude_import]
487 pub use self::prelude::*;
488 mod prelude {
489     #[cfg(test)]
490     pub struct Foo;
491     #[cfg(not(test))]
492     pub struct Bar;
493     #[cfg(all(not(any()), feature = "foo", feature = "bar", opt = "42"))]
494     pub struct Baz;
495 }
496 "#,
497         expect![[r#"
498             crate
499             Bar: t v
500             Baz: _
501             Foo: _
502         "#]],
503     );
504 }
505
506 #[test]
507 fn cfg_test() {
508     check(
509         r#"
510 //- /main.rs crate:main deps:std
511 use {Foo, Bar, Baz};
512
513 //- /lib.rs crate:std cfg:test,feature=foo,feature=bar,opt=42
514 #[prelude_import]
515 pub use self::prelude::*;
516 mod prelude {
517     #[cfg(test)]
518     pub struct Foo;
519     #[cfg(not(test))]
520     pub struct Bar;
521     #[cfg(all(not(any()), feature = "foo", feature = "bar", opt = "42"))]
522     pub struct Baz;
523 }
524 "#,
525         expect![[r#"
526             crate
527             Bar: _
528             Baz: t v
529             Foo: t v
530         "#]],
531     );
532 }
533
534 #[test]
535 fn infer_multiple_namespace() {
536     check(
537         r#"
538 //- /main.rs
539 mod a {
540     pub type T = ();
541     pub use crate::b::*;
542 }
543
544 use crate::a::T;
545
546 mod b {
547     pub const T: () = ();
548 }
549 "#,
550         expect![[r#"
551             crate
552             T: t v
553             a: t
554             b: t
555
556             crate::b
557             T: v
558
559             crate::a
560             T: t v
561         "#]],
562     );
563 }
564
565 #[test]
566 fn underscore_import() {
567     check(
568         r#"
569 //- /main.rs
570 use tr::Tr as _;
571 use tr::Tr2 as _;
572
573 mod tr {
574     pub trait Tr {}
575     pub trait Tr2 {}
576 }
577     "#,
578         expect![[r#"
579             crate
580             _: t
581             _: t
582             tr: t
583
584             crate::tr
585             Tr: t
586             Tr2: t
587         "#]],
588     );
589 }
590
591 #[test]
592 fn underscore_reexport() {
593     check(
594         r#"
595 //- /main.rs
596 mod tr {
597     pub trait PubTr {}
598     pub trait PrivTr {}
599 }
600 mod reex {
601     use crate::tr::PrivTr as _;
602     pub use crate::tr::PubTr as _;
603 }
604 use crate::reex::*;
605     "#,
606         expect![[r#"
607             crate
608             _: t
609             reex: t
610             tr: t
611
612             crate::tr
613             PrivTr: t
614             PubTr: t
615
616             crate::reex
617             _: t
618             _: t
619         "#]],
620     );
621 }
622
623 #[test]
624 fn underscore_pub_crate_reexport() {
625     mark::check!(upgrade_underscore_visibility);
626     check(
627         r#"
628 //- /main.rs crate:main deps:lib
629 use lib::*;
630
631 //- /lib.rs crate:lib
632 use tr::Tr as _;
633 pub use tr::Tr as _;
634
635 mod tr {
636     pub trait Tr {}
637 }
638     "#,
639         expect![[r#"
640             crate
641             _: t
642         "#]],
643     );
644 }
645
646 #[test]
647 fn underscore_nontrait() {
648     check(
649         r#"
650 //- /main.rs
651 mod m {
652     pub struct Struct;
653     pub enum Enum {}
654     pub const CONST: () = ();
655 }
656 use crate::m::{Struct as _, Enum as _, CONST as _};
657     "#,
658         expect![[r#"
659             crate
660             m: t
661
662             crate::m
663             CONST: v
664             Enum: t
665             Struct: t v
666         "#]],
667     );
668 }
669
670 #[test]
671 fn underscore_name_conflict() {
672     check(
673         r#"
674 //- /main.rs
675 struct Tr;
676
677 use tr::Tr as _;
678
679 mod tr {
680     pub trait Tr {}
681 }
682     "#,
683         expect![[r#"
684             crate
685             _: t
686             Tr: t v
687             tr: t
688
689             crate::tr
690             Tr: t
691         "#]],
692     );
693 }