-
Notifications
You must be signed in to change notification settings - Fork 380
/
RsStubOnlyResolveTest.kt
935 lines (785 loc) · 19.1 KB
/
RsStubOnlyResolveTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/
package org.rust.lang.core.resolve
import org.rust.CheckTestmarkHit
import org.rust.MockAdditionalCfgOptions
class RsStubOnlyResolveTest : RsResolveTestBase() {
fun `test child mod`() = stubOnlyResolve("""
//- main.rs
mod child;
fn main() {
child::foo();
//^ child.rs
}
//- child.rs
pub fn foo() {}
""")
fun `test nested child mod`() = stubOnlyResolve("""
//- main.rs
mod inner {
pub mod child;
}
fn main() {
inner::child::foo();
//^ inner/child.rs
}
//- inner/child.rs
pub fn foo() {}
""")
fun `test mod decl`() = stubOnlyResolve("""
//- main.rs
mod foo;
//^ foo.rs
fn main() {}
//- foo.rs
// Empty file
""")
fun `test mod decl 2`() = stubOnlyResolve("""
//- foo/mod.rs
use crate::bar::Bar;
//^ bar.rs
//- main.rs
mod bar;
mod foo;
fn main() {}
//- bar.rs
pub struct Bar {}
""")
fun `test mod decl path`() = stubOnlyResolve("""
//- main.rs
#[path = "bar/baz/foo.rs"]
mod foo;
//^ bar/baz/foo.rs
fn main() {}
//- bar/baz/foo.rs
fn quux() {}
""")
fun `test module path in mod rs`() = stubOnlyResolve("""
//- main.rs
mod foo;
//- foo/mod.rs
#[path="baz.rs"]
mod bar;
fn foo() {
self::bar::baz();
//^ foo/baz.rs
}
//- foo/baz.rs
pub fn baz() {}
""")
fun `test module path in non crate root`() = stubOnlyResolve("""
//- main.rs
mod foo;
//- foo.rs
#[path="baz.rs"]
mod bar;
fn foo() {
self::bar::baz();
//^ baz.rs
}
//- baz.rs
pub fn baz() {}
""")
fun `test module path to different directory 1`() = stubOnlyResolve("""
//- main.rs
#[path = "src2/foo.rs"]
mod foo;
type T = foo::bar::S;
//^ src2/bar/mod.rs
//- src2/foo.rs
pub mod bar;
//- src2/bar/mod.rs
pub struct S;
""")
fun `test module path to different directory 2`() = stubOnlyResolve("""
//- main.rs
fn main() {
inner::foo::bar::func();
} //^ inner/src2/bar.rs
mod inner {
#[path = "src2/foo.rs"]
pub mod foo;
}
//- inner/src2/foo.rs
pub mod bar;
//- inner/src2/bar.rs
pub fn func() {}
""")
fun `test invalid path`() = stubOnlyResolve("""
//- main.rs
use foo::bar;
//^ unresolved
fn main() {}
//- foo.rs
#[path] mod bar;
""")
fun `test mod decl path super`() = stubOnlyResolve("""
//- bar/baz/quux.rs
fn quux() {
super::main();
} //^ main.rs
//- main.rs
#[path = "bar/baz/quux.rs"]
mod foo;
fn main(){}
""")
fun `test mod relative`() = stubOnlyResolve("""
//- main.rs
mod sub;
fn main() {
sub::foobar::quux();
} //^ foo.rs
//- sub.rs
#[path="./foo.rs"]
pub mod foobar;
//- foo.rs
pub fn quux() {}
""")
fun `test mod relative 2`() = stubOnlyResolve("""
//- main.rs
mod sub;
fn main() {
sub::foobar::quux();
} //^ foo.rs
//- sub/mod.rs
#[path="../foo.rs"]
pub mod foobar;
//- foo.rs
pub fn quux() {}
""")
fun `test resolve explicit mod path mod rs`() = stubOnlyResolve("""
//- main.rs
#[path = "sub/mod.rs"]
mod foo;
fn quux() {}
//- sub/mod.rs
fn foo() {
crate::quux();
} //^ main.rs
""")
@MockAdditionalCfgOptions("intellij_rust")
fun `test resolve in cfg disabled mod`() = stubOnlyResolve("""
//- main.rs
#[cfg(intellij_rust)]
#[path = "cfg_enabled/mod.rs"]
mod foo;
#[cfg(not(intellij_rust))]
#[path = "cfg_disabled/mod.rs"]
mod foo;
fn quux() {}
//- cfg_enabled/mod.rs
fn bar() {
crate::quux();
}
//- cfg_disabled/mod.rs
fn baz() {
crate::quux();
} //^ main.rs
""")
fun `test resolve explicit mod path mod rs 2`() = stubOnlyResolve("""
//- main.rs
#[path = "sub/bar/mod.rs"]
mod foo;
fn quux() {}
//- sub/bar/mod.rs
fn foo() {
crate::quux();
} //^ main.rs
""")
fun `test resolve explicit mod path mod rs windows path separator`() = stubOnlyResolve("""
//- main.rs
#[path = "sub\\bar\\mod.rs"]
mod foo;
fn quux() {}
//- sub/bar/mod.rs
fn foo() {
crate::quux();
} //^ main.rs
""")
fun `test module path`() = stubOnlyResolve("""
//- main.rs
#[path="aaa"]
mod foo {
mod bar;
}
fn quux() {}
//- aaa/bar.rs
fn foo() {
crate::quux();
} //^ main.rs
""")
fun `test module path 2`() = stubOnlyResolve("""
//- main.rs
#[path="aaa/bbb"]
mod foo {
mod bar;
}
fn quux() {}
//- aaa/bbb/bar.rs
fn foo() {
crate::quux();
} //^ main.rs
""")
fun `test module path 3`() = stubOnlyResolve("""
//- main.rs
#[path="aaa/bbb"]
mod foo {
#[path="ccc.rs"]
mod bar;
}
fn quux() {}
//- aaa/bbb/ccc.rs
fn foo() {
crate::quux();
} //^ main.rs
""")
fun `test empty module path`() = stubOnlyResolve("""
//- main.rs
#[path=""]
mod foo {
#[path="bbb.rs"]
mod bar;
}
fn quux() {}
//- bbb.rs
fn foo() {
crate::quux();
} //^ main.rs
""")
fun `test relative module path`() = stubOnlyResolve("""
//- main.rs
#[path="./aaa"]
mod foo {
mod bar;
}
fn quux() {}
//- aaa/bar.rs
fn foo() {
crate::quux();
} //^ main.rs
""")
fun `test path inside inline module in crate root`() = stubOnlyResolve("""
//- main.rs
mod foo {
#[path="baz.rs"]
pub mod bar;
}
fn main() {
self::foo::bar::foo();
//^ foo/baz.rs
}
//- foo/baz.rs
pub fn foo() {}
""")
fun `test path inside inline module in mod rs`() = stubOnlyResolve("""
//- main.rs
mod foo;
//- foo/mod.rs
mod bar {
#[path="qwe.rs"]
pub mod baz;
}
fn foo() {
self::bar::baz::baz();
//^ foo/bar/qwe.rs
}
//- foo/bar/qwe.rs
pub fn baz() {}
""")
fun `test path inside inline module in non crate root`() = stubOnlyResolve("""
//- main.rs
mod foo;
//- foo.rs
mod bar {
#[path="qwe.rs"]
pub mod baz;
}
fn foo() {
self::bar::baz::baz();
//^ foo/bar/qwe.rs
}
//- foo/bar/qwe.rs
pub fn baz() {}
""")
fun `test inline module path in non crate root`() = stubOnlyResolve("""
//- main.rs
mod foo;
//- foo.rs
#[path="bar"]
pub mod bar {
pub mod baz;
}
fn foo() {
self::bar::baz::baz();
//^ bar/baz.rs
}
//- bar/baz.rs
pub fn baz() {}
""")
fun `test inline module path in non crate root 2`() = stubOnlyResolve("""
//- main.rs
mod foo;
//- foo.rs
pub mod bar {
#[path="baz2"]
pub mod baz {
pub mod quux;
}
}
fn foo() {
self::bar::baz::quux::eggs();
//^ foo/bar/baz2/quux.rs
}
//- foo/bar/baz2/quux.rs
pub fn eggs() {}
""")
fun `test use from child`() = stubOnlyResolve("""
//- main.rs
use child::{foo};
mod child;
fn main() {
foo();
} //^ child.rs
//- child.rs
pub fn foo() {}
""")
fun `test use global path`() {
stubOnlyResolve("""
//- foo.rs
fn main() {
crate::bar::hello();
} //^ bar.rs
//- lib.rs
mod foo;
pub mod bar;
//- bar.rs
pub fn hello() {}
""")
}
fun `test mod decl wrong path`() = stubOnlyResolve("""
//- main.rs
#[path = "foo/bar/baz/rs"]
mod foo;
//^ unresolved
fn main() {}
""")
fun `test mod decl cycle`() = stubOnlyResolve("""
//- foo.rs
use quux;
//^ unresolved
#[path="bar.rs"]
mod bar;
//- baz.rs
#[path="foo.rs"]
mod foo;
//- bar.rs
#[path="baz.rs"]
mod baz;
""")
fun `test function type`() = stubOnlyResolve("""
//- main.rs
mod foo;
struct S { field: u32, }
fn main() { foo::id(S { field: 92 }).field }
//^ main.rs
//- foo.rs
use super::S;
pub fn id(x: S) -> S { x }
""")
fun `test tuple struct`() = stubOnlyResolve("""
//- main.rs
mod foo;
use foo::S;
fn f(s: S) { s.0.bar() }
//^ foo.rs
//- foo.rs
struct S(Bar);
struct Bar;
impl Bar { pub fn bar(self) {} }
""")
fun `test method call`() = stubOnlyResolve("""
//- main.rs
mod aux;
use aux::S;
fn main() {
let s: S = S;
s.foo();
//^ aux.rs
}
//- aux.rs
pub struct S;
impl S {
pub fn foo(&self) { }
}
""")
fun `test method call on enum`() = stubOnlyResolve("""
//- main.rs
mod aux;
use aux::S;
fn main() {
let s: S = S::X;
s.foo();
//^ aux.rs
}
//- aux.rs
enum S { X }
impl S {
pub fn foo(&self) { }
}
""")
fun `test resolve macro`() = checkByCode("""
macro_rules! foo_bar { () => () }
//X
mod b {
fn main() {
foo_bar!();
//^
}
}
""")
fun `test resolve macro multi file`() = stubOnlyResolve("""
//- b.rs
foo_bar!();
//^ main.rs
//- main.rs
macro_rules! foo_bar { () => () }
mod b;
fn main() {}
""")
fun `test resolve macro multi file 2`() = stubOnlyResolve("""
//- b.rs
macro_rules! foo_bar { () => () }
//- main.rs
#[macro_use]
mod b;
foo_bar!();
//^ b.rs
fn main() {}
""")
fun `test resolve macro multi file 3`() = stubOnlyResolve("""
//- b.rs
macro_rules! foo_bar { () => () }
foo_bar!();
//^ b.rs
//- main.rs
#[macro_use]
mod b;
""")
fun `test resolve macro multi file 4`() = stubOnlyResolve("""
//- b.rs
#![macro_use]
macro_rules! foo_bar { () => () }
//- lib.rs
mod b;
mod c {
foo_bar!();
//^ b.rs
}
""")
fun `test resolve crate keyword in path to crate root mod`() = stubOnlyResolve("""
//- main.rs
mod foo;
mod bar {
pub struct Foo;
}
//- foo.rs
use crate::bar::Foo;
//^ main.rs
""")
fun `test raw identifier`() = stubOnlyResolve("""
//- main.rs
mod r#match;
use r#match::bar;
fn main() {
bar();
//^ match.rs
}
//- match.rs
pub fn bar() { println!("Bar"); }
""")
fun `test module structure without mod rs 1`() = stubOnlyResolve("""
//- main.rs
mod foo;
//- foo.rs
mod bar;
//^ foo/bar.rs
//- foo/bar.rs
pub struct Bar;
""")
@CheckTestmarkHit(NameResolutionTestmarks.ModRsFile::class)
fun `test module structure without mod rs 2`() = stubOnlyResolve("""
//- main.rs
mod foo;
//- foo/mod.rs
mod bar;
//^ foo/bar.rs
//- foo/bar.rs
pub struct Bar;
//- foo/foo/bar.rs
pub struct Baz;
""")
@CheckTestmarkHit(NameResolutionTestmarks.CrateRootModule::class)
fun `test module structure without mod rs 3`() = stubOnlyResolve("""
//- main.rs
mod foo;
//^ foo.rs
//- foo.rs
pub struct Foo;
//- main/foo.rs
pub struct Bar;
""")
fun `test scoped resolve inside stubbed function body`() = stubOnlyResolve("""
//- main.rs
mod foo;
fn main() {
foo::S.baz();
} //^ foo.rs
//- foo.rs
pub struct S;
fn foobar() {
if true {
impl S {
pub fn baz(&self) {}
}
} else {
struct S;
}
}
""")
fun `test trait impl for const generic 1`() = stubOnlyResolve("""
//- main.rs
#![feature(const_generics)]
mod bar;
use bar::T;
fn main() {
let s = [0];
s.foo()
//^ bar.rs
}
//- bar.rs
pub trait T {
fn foo(&self);
}
impl T for [i32; 1] {
fn foo(&self) {}
}
""")
fun `test trait impl for const generic 2`() = stubOnlyResolve("""
//- main.rs
#![feature(const_generics)]
mod bar;
use bar::T;
fn main() {
let s = [0, 1];
s.foo()
//^ unresolved
}
//- bar.rs
pub trait T {
fn foo(&self);
}
impl T for [i32; 1] {
fn foo(&self) {}
}
""")
fun `test trait impl for const generic 3`() = stubOnlyResolve("""
//- main.rs
#![feature(const_generics)]
mod bar;
use bar::T;
fn main() {
let s = [0];
s.foo()
//^ bar.rs
}
//- bar.rs
pub trait T {
fn foo(&self);
}
impl <const N: usize> T for [i32; N] {
fn foo(&self) {}
}
""")
fun `test trait impl const generic 4`() = stubOnlyResolve("""
//- main.rs
#![feature(const_generics)]
mod bar;
use bar::T;
fn main() {
let s = bar::S::<0>;
s.foo()
//^ bar.rs
}
//- bar.rs
pub struct S<const N: usize>;
pub trait T {
fn foo(&self);
}
impl T for S<{ 0 }> {
fn foo(&self) {}
}
""")
fun `test trait impl const generic 5`() = stubOnlyResolve("""
//- main.rs
#![feature(const_generics)]
mod bar;
use bar::T;
fn main() {
let s = bar::S::<1>;
s.foo()
//^ unresolved
}
//- bar.rs
pub struct S<const N: usize>;
pub trait T {
fn foo(&self);
}
impl T for S<{ 0 }> {
fn foo(&self) {}
}
""")
fun `test trait impl const generic 6`() = stubOnlyResolve("""
//- main.rs
#![feature(const_generics)]
mod bar;
use bar::T;
fn main() {
let s = bar::S::<0>;
s.foo()
//^ bar.rs
}
//- bar.rs
pub struct S<const N: usize>;
pub trait T {
fn foo(&self);
}
impl <const N: usize> T for S<{ N }> {
fn foo(&self) {}
}
""")
fun `test item reexported from 'pub(crate)' mod in dependency crate`() = stubOnlyResolve("""
//- main.rs
use test_package::*;
use foo::*;
fn main() {
func();
} //^ main.rs
mod foo {
pub fn func() {}
} //X
//- lib.rs
mod foo {
pub(crate) fn func() {}
}
pub use foo::*;
""")
fun `test resolve in detached file`() = stubOnlyResolve("""
//- detached.rs
macro foo() {}
//X
fn main() {
foo!();
} //^ detached.rs
""")
fun `test resolve in detached file (to inline mod)`() = stubOnlyResolve("""
//- detached.rs
mod inner {
pub fn func() {}
} //X
use inner::func;
fn main() {
func();
} //^ detached.rs
""")
fun `test resolve in detached file (in inline mod)`() = stubOnlyResolve("""
//- detached.rs
mod inner {
fn func() {}
//X
fn main() {
func();
} //^ detached.rs
}
""")
fun `test resolve in detached file (from inline mod)`() = stubOnlyResolve("""
//- detached.rs
fn func() {}
//X
mod inner {
fn main() {
super::func();
} //^ detached.rs
}
""")
fun `test resolve in detached file (in inner mod)`() = stubOnlyResolve("""
//- detached.rs
mod inner;
//- detached/inner.rs
fn func() {}
//X
fn main() {
func();
} //^ detached/inner.rs
""")
fun `test resolve in detached file ($crate)`() = stubOnlyResolve("""
//- detached.rs
mod inner {
pub fn func() {}
} //X
macro_rules! as_is { ($($ t:tt)*) => {$($ t)*}; }
macro_rules! gen {
() => {
as_is! {
use $ crate::inner::func;
}
}
}
gen!();
fn main() {
func();
} //^ detached.rs
""")
fun `test resolve in detached file (in import)`() = stubOnlyResolve("""
//- detached.rs
pub use foo::func;
//^ detached.rs
pub mod foo {
pub fn func() {}
}
""")
fun `test method is not resolved to independent crate 1`() = stubOnlyResolve("""
//- lib.rs
pub struct Foo;
fn foo() {
Foo.bar();
} //^ unresolved
//- main.rs
use test_package::*;
impl Foo { // Incoherent impl
fn bar(&self) {}
}
""")
fun `test method is not resolved to independent crate 2`() = stubOnlyResolve("""
//- bar.rs
pub struct Foo;
pub trait Bar {
fn bar(&self);
}
//- lib.rs
mod bar;
pub use bar::*;
impl Bar for Foo {
fn bar(&self) {}
}
fn foo() {
Foo.bar();
} //^ lib.rs
//- main.rs
use test_package::*;
impl Bar for Foo { // Incoherent impl
fn bar(&self) {}
}
""")
}