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