aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/nameres/tests.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/nameres/tests.rs')
-rw-r--r--crates/ra_hir_def/src/nameres/tests.rs844
1 files changed, 408 insertions, 436 deletions
diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs
index 503099fb7..02dca80c2 100644
--- a/crates/ra_hir_def/src/nameres/tests.rs
+++ b/crates/ra_hir_def/src/nameres/tests.rs
@@ -6,558 +6,531 @@ mod primitives;
6 6
7use std::sync::Arc; 7use std::sync::Arc;
8 8
9use insta::assert_snapshot; 9use expect::{expect, Expect};
10use ra_db::{fixture::WithFixture, SourceDatabase}; 10use ra_db::{fixture::WithFixture, SourceDatabase};
11use test_utils::mark; 11use test_utils::mark;
12 12
13use crate::{db::DefDatabase, nameres::*, test_db::TestDB}; 13use crate::{db::DefDatabase, nameres::*, test_db::TestDB};
14 14
15fn def_map(ra_fixture: &str) -> String {
16 compute_crate_def_map(ra_fixture).dump()
17}
18
19fn compute_crate_def_map(fixture: &str) -> Arc<CrateDefMap> { 15fn compute_crate_def_map(fixture: &str) -> Arc<CrateDefMap> {
20 let db = TestDB::with_files(fixture); 16 let db = TestDB::with_files(fixture);
21 let krate = db.crate_graph().iter().next().unwrap(); 17 let krate = db.crate_graph().iter().next().unwrap();
22 db.crate_def_map(krate) 18 db.crate_def_map(krate)
23} 19}
24 20
21fn check(ra_fixture: &str, expect: Expect) {
22 let db = TestDB::with_files(ra_fixture);
23 let krate = db.crate_graph().iter().next().unwrap();
24 let actual = db.crate_def_map(krate).dump() + "\n";
25 expect.assert_eq(&actual);
26}
27
25#[test] 28#[test]
26fn crate_def_map_smoke_test() { 29fn crate_def_map_smoke_test() {
27 let map = def_map( 30 check(
28 r" 31 r#"
29 //- /lib.rs 32//- /lib.rs
30 mod foo; 33mod foo;
31 struct S; 34struct S;
32 use crate::foo::bar::E; 35use crate::foo::bar::E;
33 use self::E::V; 36use self::E::V;
34
35 //- /foo/mod.rs
36 pub mod bar;
37 fn f() {}
38
39 //- /foo/bar.rs
40 pub struct Baz;
41
42 union U {
43 to_be: bool,
44 not_to_be: u8,
45 }
46 37
47 enum E { V } 38//- /foo/mod.rs
39pub mod bar;
40fn f() {}
48 41
49 extern { 42//- /foo/bar.rs
50 static EXT: u8; 43pub struct Baz;
51 fn ext(); 44
52 } 45union U { to_be: bool, not_to_be: u8 }
53 ", 46enum E { V }
47
48extern {
49 static EXT: u8;
50 fn ext();
51}
52"#,
53 expect![[r#"
54 crate
55 E: t
56 S: t v
57 V: t v
58 foo: t
59
60 crate::foo
61 bar: t
62 f: v
63
64 crate::foo::bar
65 Baz: t v
66 E: t
67 EXT: v
68 U: t
69 ext: v
70 "#]],
54 ); 71 );
55 assert_snapshot!(map, @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 ⋮U: t
71 ⋮ext: v
72 "###)
73} 72}
74 73
75#[test] 74#[test]
76fn crate_def_map_super_super() { 75fn crate_def_map_super_super() {
77 let map = def_map( 76 check(
78 " 77 r#"
79 //- /lib.rs 78mod a {
80 mod a { 79 const A: usize = 0;
81 const A: usize = 0; 80 mod b {
82 81 const B: usize = 0;
83 mod b { 82 mod c {
84 const B: usize = 0; 83 use super::super::*;
85
86 mod c {
87 use super::super::*;
88 }
89 }
90 } 84 }
91 ", 85 }
86}
87"#,
88 expect![[r#"
89 crate
90 a: t
91
92 crate::a
93 A: v
94 b: t
95
96 crate::a::b
97 B: v
98 c: t
99
100 crate::a::b::c
101 A: v
102 b: t
103 "#]],
92 ); 104 );
93 assert_snapshot!(map, @r###"
94 ⋮crate
95 ⋮a: t
96
97 ⋮crate::a
98 ⋮A: v
99 ⋮b: t
100
101 ⋮crate::a::b
102 ⋮B: v
103 ⋮c: t
104
105 ⋮crate::a::b::c
106 ⋮A: v
107 ⋮b: t
108 "###)
109} 105}
110 106
111#[test] 107#[test]
112fn crate_def_map_fn_mod_same_name() { 108fn crate_def_map_fn_mod_same_name() {
113 let map = def_map( 109 check(
114 " 110 r#"
115 //- /lib.rs 111mod m {
116 mod m { 112 pub mod z {}
117 pub mod z {} 113 pub fn z() {}
118 pub fn z() {} 114}
119 } 115"#,
120 ", 116 expect![[r#"
117 crate
118 m: t
119
120 crate::m
121 z: t v
122
123 crate::m::z
124 "#]],
121 ); 125 );
122 assert_snapshot!(map, @r###"
123 ⋮crate
124 ⋮m: t
125
126 ⋮crate::m
127 ⋮z: t v
128
129 ⋮crate::m::z
130 "###)
131} 126}
132 127
133#[test] 128#[test]
134fn bogus_paths() { 129fn bogus_paths() {
135 mark::check!(bogus_paths); 130 mark::check!(bogus_paths);
136 let map = def_map( 131 check(
137 " 132 r#"
138 //- /lib.rs 133//- /lib.rs
139 mod foo; 134mod foo;
140 struct S; 135struct S;
141 use self; 136use self;
142 137
143 //- /foo/mod.rs 138//- /foo/mod.rs
144 use super; 139use super;
145 use crate; 140use crate;
146 141"#,
147 ", 142 expect![[r#"
143 crate
144 S: t v
145 foo: t
146
147 crate::foo
148 "#]],
148 ); 149 );
149 assert_snapshot!(map, @r###"
150 ⋮crate
151 ⋮S: t v
152 ⋮foo: t
153
154 ⋮crate::foo
155 "###
156 )
157} 150}
158 151
159#[test] 152#[test]
160fn use_as() { 153fn use_as() {
161 let map = def_map( 154 check(
162 " 155 r#"
163 //- /lib.rs 156//- /lib.rs
164 mod foo; 157mod foo;
165 158use crate::foo::Baz as Foo;
166 use crate::foo::Baz as Foo;
167 159
168 //- /foo/mod.rs 160//- /foo/mod.rs
169 pub struct Baz; 161pub struct Baz;
170 ", 162"#,
171 ); 163 expect![[r#"
172 assert_snapshot!(map, 164 crate
173 @r###" 165 Foo: t v
174 ⋮crate 166 foo: t
175 ⋮Foo: t v 167
176 ⋮foo: t 168 crate::foo
177 169 Baz: t v
178 ⋮crate::foo 170 "#]],
179 ⋮Baz: t v
180 "###
181 ); 171 );
182} 172}
183 173
184#[test] 174#[test]
185fn use_trees() { 175fn use_trees() {
186 let map = def_map( 176 check(
187 " 177 r#"
188 //- /lib.rs 178//- /lib.rs
189 mod foo; 179mod foo;
190 180use crate::foo::bar::{Baz, Quux};
191 use crate::foo::bar::{Baz, Quux};
192 181
193 //- /foo/mod.rs 182//- /foo/mod.rs
194 pub mod bar; 183pub mod bar;
195 184
196 //- /foo/bar.rs 185//- /foo/bar.rs
197 pub struct Baz; 186pub struct Baz;
198 pub enum Quux {}; 187pub enum Quux {};
199 ", 188"#,
189 expect![[r#"
190 crate
191 Baz: t v
192 Quux: t
193 foo: t
194
195 crate::foo
196 bar: t
197
198 crate::foo::bar
199 Baz: t v
200 Quux: t
201 "#]],
200 ); 202 );
201 assert_snapshot!(map, @r###"
202 ⋮crate
203 ⋮Baz: t v
204 ⋮Quux: t
205 ⋮foo: t
206
207 ⋮crate::foo
208 ⋮bar: t
209
210 ⋮crate::foo::bar
211 ⋮Baz: t v
212 ⋮Quux: t
213 "###);
214} 203}
215 204
216#[test] 205#[test]
217fn re_exports() { 206fn re_exports() {
218 let map = def_map( 207 check(
219 " 208 r#"
220 //- /lib.rs 209//- /lib.rs
221 mod foo; 210mod foo;
222 211use self::foo::Baz;
223 use self::foo::Baz;
224
225 //- /foo/mod.rs
226 pub mod bar;
227 212
228 pub use self::bar::Baz; 213//- /foo/mod.rs
214pub mod bar;
215pub use self::bar::Baz;
229 216
230 //- /foo/bar.rs 217//- /foo/bar.rs
231 pub struct Baz; 218pub struct Baz;
232 ", 219"#,
220 expect![[r#"
221 crate
222 Baz: t v
223 foo: t
224
225 crate::foo
226 Baz: t v
227 bar: t
228
229 crate::foo::bar
230 Baz: t v
231 "#]],
233 ); 232 );
234 assert_snapshot!(map, @r###"
235 ⋮crate
236 ⋮Baz: t v
237 ⋮foo: t
238
239 ⋮crate::foo
240 ⋮Baz: t v
241 ⋮bar: t
242
243 ⋮crate::foo::bar
244 ⋮Baz: t v
245 "###);
246} 233}
247 234
248#[test] 235#[test]
249fn std_prelude() { 236fn std_prelude() {
250 mark::check!(std_prelude); 237 mark::check!(std_prelude);
251 let map = def_map( 238 check(
252 " 239 r#"
253 //- /main.rs crate:main deps:test_crate 240//- /main.rs crate:main deps:test_crate
254 use Foo::*; 241use Foo::*;
255 242
256 //- /lib.rs crate:test_crate 243//- /lib.rs crate:test_crate
257 mod prelude; 244mod prelude;
258 #[prelude_import] 245#[prelude_import]
259 use prelude::*; 246use prelude::*;
260 247
261 //- /prelude.rs 248//- /prelude.rs
262 pub enum Foo { Bar, Baz }; 249pub enum Foo { Bar, Baz };
263 ", 250"#,
251 expect![[r#"
252 crate
253 Bar: t v
254 Baz: t v
255 "#]],
264 ); 256 );
265 assert_snapshot!(map, @r###"
266 ⋮crate
267 ⋮Bar: t v
268 ⋮Baz: t v
269 "###);
270} 257}
271 258
272#[test] 259#[test]
273fn can_import_enum_variant() { 260fn can_import_enum_variant() {
274 mark::check!(can_import_enum_variant); 261 mark::check!(can_import_enum_variant);
275 let map = def_map( 262 check(
276 " 263 r#"
277 //- /lib.rs 264enum E { V }
278 enum E { V } 265use self::E::V;
279 use self::E::V; 266"#,
280 ", 267 expect![[r#"
281 ); 268 crate
282 assert_snapshot!(map, @r###" 269 E: t
283 ⋮crate 270 V: t v
284 ⋮E: t 271 "#]],
285 ⋮V: t v
286 "###
287 ); 272 );
288} 273}
289 274
290#[test] 275#[test]
291fn edition_2015_imports() { 276fn edition_2015_imports() {
292 let map = def_map( 277 check(
293 " 278 r#"
294 //- /main.rs crate:main deps:other_crate edition:2015 279//- /main.rs crate:main deps:other_crate edition:2015
295 mod foo; 280mod foo;
296 mod bar; 281mod bar;
297 282
298 //- /bar.rs 283//- /bar.rs
299 struct Bar; 284struct Bar;
300
301 //- /foo.rs
302 use bar::Bar;
303 use other_crate::FromLib;
304
305 //- /lib.rs crate:other_crate edition:2018
306 struct FromLib;
307 ",
308 );
309 285
310 assert_snapshot!(map, @r###" 286//- /foo.rs
311 ⋮crate 287use bar::Bar;
312 ⋮bar: t 288use other_crate::FromLib;
313 ⋮foo: t 289
314 290//- /lib.rs crate:other_crate edition:2018
315 ⋮crate::bar 291struct FromLib;
316 ⋮Bar: t v 292"#,
317 293 expect![[r#"
318 ⋮crate::foo 294 crate
319 ⋮Bar: t v 295 bar: t
320 ⋮FromLib: t v 296 foo: t
321 "###); 297
298 crate::bar
299 Bar: t v
300
301 crate::foo
302 Bar: t v
303 FromLib: t v
304 "#]],
305 );
322} 306}
323 307
324#[test] 308#[test]
325fn item_map_using_self() { 309fn item_map_using_self() {
326 let map = def_map( 310 check(
327 " 311 r#"
328 //- /lib.rs 312//- /lib.rs
329 mod foo; 313mod foo;
330 use crate::foo::bar::Baz::{self}; 314use crate::foo::bar::Baz::{self};
331 //- /foo/mod.rs 315
332 pub mod bar; 316//- /foo/mod.rs
333 //- /foo/bar.rs 317pub mod bar;
334 pub struct Baz; 318
335 ", 319//- /foo/bar.rs
320pub struct Baz;
321"#,
322 expect![[r#"
323 crate
324 Baz: t v
325 foo: t
326
327 crate::foo
328 bar: t
329
330 crate::foo::bar
331 Baz: t v
332 "#]],
336 ); 333 );
337 assert_snapshot!(map, @r###"
338 ⋮crate
339 ⋮Baz: t v
340 ⋮foo: t
341
342 ⋮crate::foo
343 ⋮bar: t
344
345 ⋮crate::foo::bar
346 ⋮Baz: t v
347 "###);
348} 334}
349 335
350#[test] 336#[test]
351fn item_map_across_crates() { 337fn item_map_across_crates() {
352 let map = def_map( 338 check(
353 " 339 r#"
354 //- /main.rs crate:main deps:test_crate 340//- /main.rs crate:main deps:test_crate
355 use test_crate::Baz; 341use test_crate::Baz;
356
357 //- /lib.rs crate:test_crate
358 pub struct Baz;
359 ",
360 );
361 342
362 assert_snapshot!(map, @r###" 343//- /lib.rs crate:test_crate
363 ⋮crate 344pub struct Baz;
364 ⋮Baz: t v 345"#,
365 "###); 346 expect![[r#"
347 crate
348 Baz: t v
349 "#]],
350 );
366} 351}
367 352
368#[test] 353#[test]
369fn extern_crate_rename() { 354fn extern_crate_rename() {
370 let map = def_map( 355 check(
371 " 356 r#"
372 //- /main.rs crate:main deps:alloc 357//- /main.rs crate:main deps:alloc
373 extern crate alloc as alloc_crate; 358extern crate alloc as alloc_crate;
374 359mod alloc;
375 mod alloc; 360mod sync;
376 mod sync;
377 361
378 //- /sync.rs 362//- /sync.rs
379 use alloc_crate::Arc; 363use alloc_crate::Arc;
380 364
381 //- /lib.rs crate:alloc 365//- /lib.rs crate:alloc
382 struct Arc; 366struct Arc;
383 ", 367"#,
368 expect![[r#"
369 crate
370 alloc_crate: t
371 sync: t
372
373 crate::sync
374 Arc: t v
375 "#]],
384 ); 376 );
385
386 assert_snapshot!(map, @r###"
387 ⋮crate
388 ⋮alloc_crate: t
389 ⋮sync: t
390
391 ⋮crate::sync
392 ⋮Arc: t v
393 "###);
394} 377}
395 378
396#[test] 379#[test]
397fn extern_crate_rename_2015_edition() { 380fn extern_crate_rename_2015_edition() {
398 let map = def_map( 381 check(
399 " 382 r#"
400 //- /main.rs crate:main deps:alloc edition:2015 383//- /main.rs crate:main deps:alloc edition:2015
401 extern crate alloc as alloc_crate; 384extern crate alloc as alloc_crate;
402 385mod alloc;
403 mod alloc; 386mod sync;
404 mod sync;
405
406 //- /sync.rs
407 use alloc_crate::Arc;
408 387
409 //- /lib.rs crate:alloc 388//- /sync.rs
410 struct Arc; 389use alloc_crate::Arc;
411 ",
412 );
413 390
414 assert_snapshot!(map, 391//- /lib.rs crate:alloc
415 @r###" 392struct Arc;
416 ⋮crate 393"#,
417 ⋮alloc_crate: t 394 expect![[r#"
418 ⋮sync: t 395 crate
419 396 alloc_crate: t
420 ⋮crate::sync 397 sync: t
421 ⋮Arc: t v 398
422 "### 399 crate::sync
400 Arc: t v
401 "#]],
423 ); 402 );
424} 403}
425 404
426#[test] 405#[test]
427fn reexport_across_crates() { 406fn reexport_across_crates() {
428 let map = def_map( 407 check(
429 " 408 r#"
430 //- /main.rs crate:main deps:test_crate 409//- /main.rs crate:main deps:test_crate
431 use test_crate::Baz; 410use test_crate::Baz;
432
433 //- /lib.rs crate:test_crate
434 pub use foo::Baz;
435 411
436 mod foo; 412//- /lib.rs crate:test_crate
413pub use foo::Baz;
414mod foo;
437 415
438 //- /foo.rs 416//- /foo.rs
439 pub struct Baz; 417pub struct Baz;
440 ", 418"#,
419 expect![[r#"
420 crate
421 Baz: t v
422 "#]],
441 ); 423 );
442
443 assert_snapshot!(map, @r###"
444 ⋮crate
445 ⋮Baz: t v
446 "###);
447} 424}
448 425
449#[test] 426#[test]
450fn values_dont_shadow_extern_crates() { 427fn values_dont_shadow_extern_crates() {
451 let map = def_map( 428 check(
452 " 429 r#"
453 //- /main.rs crate:main deps:foo 430//- /main.rs crate:main deps:foo
454 fn foo() {} 431fn foo() {}
455 use foo::Bar; 432use foo::Bar;
456
457 //- /foo/lib.rs crate:foo
458 pub struct Bar;
459 ",
460 );
461 433
462 assert_snapshot!(map, @r###" 434//- /foo/lib.rs crate:foo
463 ⋮crate 435pub struct Bar;
464 ⋮Bar: t v 436"#,
465 ⋮foo: v 437 expect![[r#"
466 "###); 438 crate
439 Bar: t v
440 foo: v
441 "#]],
442 );
467} 443}
468 444
469#[test] 445#[test]
470fn std_prelude_takes_precedence_above_core_prelude() { 446fn std_prelude_takes_precedence_above_core_prelude() {
471 let map = def_map( 447 check(
472 r#" 448 r#"
473 //- /main.rs crate:main deps:core,std 449//- /main.rs crate:main deps:core,std
474 use {Foo, Bar}; 450use {Foo, Bar};
475 451
476 //- /std.rs crate:std deps:core 452//- /std.rs crate:std deps:core
477 #[prelude_import] 453#[prelude_import]
478 pub use self::prelude::*; 454pub use self::prelude::*;
479 mod prelude { 455mod prelude {
480 pub struct Foo; 456 pub struct Foo;
481 pub use core::prelude::Bar; 457 pub use core::prelude::Bar;
482 } 458}
483 459
484 //- /core.rs crate:core 460//- /core.rs crate:core
485 #[prelude_import] 461#[prelude_import]
486 pub use self::prelude::*; 462pub use self::prelude::*;
487 mod prelude { 463mod prelude {
488 pub struct Bar; 464 pub struct Bar;
489 } 465}
490 "#, 466"#,
467 expect![[r#"
468 crate
469 Bar: t v
470 Foo: t v
471 "#]],
491 ); 472 );
492
493 assert_snapshot!(map, @r###"
494 ⋮crate
495 ⋮Bar: t v
496 ⋮Foo: t v
497 "###);
498} 473}
499 474
500#[test] 475#[test]
501fn cfg_not_test() { 476fn cfg_not_test() {
502 let map = def_map( 477 check(
503 r#" 478 r#"
504 //- /main.rs crate:main deps:std 479//- /main.rs crate:main deps:std
505 use {Foo, Bar, Baz}; 480use {Foo, Bar, Baz};
506 481
507 //- /lib.rs crate:std 482//- /lib.rs crate:std
508 #[prelude_import] 483#[prelude_import]
509 pub use self::prelude::*; 484pub use self::prelude::*;
510 mod prelude { 485mod prelude {
511 #[cfg(test)] 486 #[cfg(test)]
512 pub struct Foo; 487 pub struct Foo;
513 #[cfg(not(test))] 488 #[cfg(not(test))]
514 pub struct Bar; 489 pub struct Bar;
515 #[cfg(all(not(any()), feature = "foo", feature = "bar", opt = "42"))] 490 #[cfg(all(not(any()), feature = "foo", feature = "bar", opt = "42"))]
516 pub struct Baz; 491 pub struct Baz;
517 } 492}
518 "#, 493"#,
494 expect![[r#"
495 crate
496 Bar: t v
497 Baz: _
498 Foo: _
499 "#]],
519 ); 500 );
520
521 assert_snapshot!(map, @r###"
522 ⋮crate
523 ⋮Bar: t v
524 ⋮Baz: _
525 ⋮Foo: _
526 "###);
527} 501}
528 502
529#[test] 503#[test]
530fn cfg_test() { 504fn cfg_test() {
531 let map = def_map( 505 check(
532 r#" 506 r#"
533 //- /main.rs crate:main deps:std 507//- /main.rs crate:main deps:std
534 use {Foo, Bar, Baz}; 508use {Foo, Bar, Baz};
535 509
536 //- /lib.rs crate:std cfg:test,feature=foo,feature=bar,opt=42 510//- /lib.rs crate:std cfg:test,feature=foo,feature=bar,opt=42
537 #[prelude_import] 511#[prelude_import]
538 pub use self::prelude::*; 512pub use self::prelude::*;
539 mod prelude { 513mod prelude {
540 #[cfg(test)] 514 #[cfg(test)]
541 pub struct Foo; 515 pub struct Foo;
542 #[cfg(not(test))] 516 #[cfg(not(test))]
543 pub struct Bar; 517 pub struct Bar;
544 #[cfg(all(not(any()), feature = "foo", feature = "bar", opt = "42"))] 518 #[cfg(all(not(any()), feature = "foo", feature = "bar", opt = "42"))]
545 pub struct Baz; 519 pub struct Baz;
546 } 520}
547 "#, 521"#,
522 expect![[r#"
523 crate
524 Bar: _
525 Baz: t v
526 Foo: t v
527 "#]],
548 ); 528 );
549
550 assert_snapshot!(map, @r###"
551 ⋮crate
552 ⋮Bar: _
553 ⋮Baz: t v
554 ⋮Foo: t v
555 "###);
556} 529}
557 530
558#[test] 531#[test]
559fn infer_multiple_namespace() { 532fn infer_multiple_namespace() {
560 let map = def_map( 533 check(
561 r#" 534 r#"
562//- /main.rs 535//- /main.rs
563mod a { 536mod a {
@@ -571,18 +544,17 @@ mod b {
571 pub const T: () = (); 544 pub const T: () = ();
572} 545}
573"#, 546"#,
547 expect![[r#"
548 crate
549 T: t v
550 a: t
551 b: t
552
553 crate::b
554 T: v
555
556 crate::a
557 T: t v
558 "#]],
574 ); 559 );
575
576 assert_snapshot!(map, @r###"
577 ⋮crate
578 ⋮T: t v
579 ⋮a: t
580 ⋮b: t
581
582 ⋮crate::b
583 ⋮T: v
584
585 ⋮crate::a
586 ⋮T: t v
587"###);
588} 560}