]> git.lizzy.rs Git - rust.git/blob - crates/ide-completion/src/tests/special.rs
1aea5d89b4a0fcea42052eac207a16a2fb21a546
[rust.git] / crates / ide-completion / src / tests / special.rs
1 //! Tests that don't fit into a specific category.
2
3 use expect_test::{expect, Expect};
4
5 use crate::tests::{check_edit, completion_list_no_kw, completion_list_with_trigger_character};
6
7 fn check(ra_fixture: &str, expect: Expect) {
8     let actual = completion_list_no_kw(ra_fixture);
9     expect.assert_eq(&actual)
10 }
11
12 pub(crate) fn check_with_trigger_character(
13     ra_fixture: &str,
14     trigger_character: Option<char>,
15     expect: Expect,
16 ) {
17     let actual = completion_list_with_trigger_character(ra_fixture, trigger_character);
18     expect.assert_eq(&actual)
19 }
20
21 #[test]
22 fn completes_if_prefix_is_keyword() {
23     check_edit(
24         "wherewolf",
25         r#"
26 fn main() {
27     let wherewolf = 92;
28     drop(where$0)
29 }
30 "#,
31         r#"
32 fn main() {
33     let wherewolf = 92;
34     drop(wherewolf)
35 }
36 "#,
37     )
38 }
39
40 /// Regression test for issue #6091.
41 #[test]
42 fn correctly_completes_module_items_prefixed_with_underscore() {
43     check_edit(
44         "_alpha",
45         r#"
46 fn main() {
47     _$0
48 }
49 fn _alpha() {}
50 "#,
51         r#"
52 fn main() {
53     _alpha()$0
54 }
55 fn _alpha() {}
56 "#,
57     )
58 }
59
60 #[test]
61 fn completes_prelude() {
62     check(
63         r#"
64 //- /main.rs crate:main deps:std
65 fn foo() { let x: $0 }
66
67 //- /std/lib.rs crate:std
68 pub mod prelude {
69     pub mod rust_2018 {
70         pub struct Option;
71     }
72 }
73 "#,
74         expect![[r#"
75                 md std
76                 st Option
77                 bt u32
78             "#]],
79     );
80 }
81
82 #[test]
83 fn completes_prelude_macros() {
84     check(
85         r#"
86 //- /main.rs crate:main deps:std
87 fn f() {$0}
88
89 //- /std/lib.rs crate:std
90 pub mod prelude {
91     pub mod rust_2018 {
92         pub use crate::concat;
93     }
94 }
95
96 mod macros {
97     #[rustc_builtin_macro]
98     #[macro_export]
99     macro_rules! concat { }
100 }
101 "#,
102         expect![[r#"
103                 fn f()        fn()
104                 ma concat!(…) macro_rules! concat
105                 md std
106                 bt u32
107             "#]],
108     );
109 }
110
111 #[test]
112 fn completes_std_prelude_if_core_is_defined() {
113     check(
114         r#"
115 //- /main.rs crate:main deps:core,std
116 fn foo() { let x: $0 }
117
118 //- /core/lib.rs crate:core
119 pub mod prelude {
120     pub mod rust_2018 {
121         pub struct Option;
122     }
123 }
124
125 //- /std/lib.rs crate:std deps:core
126 pub mod prelude {
127     pub mod rust_2018 {
128         pub struct String;
129     }
130 }
131 "#,
132         expect![[r#"
133                 md core
134                 md std
135                 st String
136                 bt u32
137             "#]],
138     );
139 }
140
141 #[test]
142 fn respects_doc_hidden() {
143     check(
144         r#"
145 //- /lib.rs crate:lib deps:std
146 fn f() {
147     format_$0
148 }
149
150 //- /std.rs crate:std
151 #[doc(hidden)]
152 #[macro_export]
153 macro_rules! format_args_nl {
154     () => {}
155 }
156
157 pub mod prelude {
158     pub mod rust_2018 {}
159 }
160             "#,
161         expect![[r#"
162                 fn f() fn()
163                 md std
164                 bt u32
165             "#]],
166     );
167 }
168
169 #[test]
170 fn respects_doc_hidden_in_assoc_item_list() {
171     check(
172         r#"
173 //- /lib.rs crate:lib deps:std
174 struct S;
175 impl S {
176     format_$0
177 }
178
179 //- /std.rs crate:std
180 #[doc(hidden)]
181 #[macro_export]
182 macro_rules! format_args_nl {
183     () => {}
184 }
185
186 pub mod prelude {
187     pub mod rust_2018 {}
188 }
189             "#,
190         expect![[r#"
191                 md std
192             "#]],
193     );
194 }
195
196 #[test]
197 fn associated_item_visibility() {
198     check(
199         r#"
200 //- /lib.rs crate:lib new_source_root:library
201 pub struct S;
202
203 impl S {
204     pub fn public_method() { }
205     fn private_method() { }
206     pub type PublicType = u32;
207     type PrivateType = u32;
208     pub const PUBLIC_CONST: u32 = 1;
209     const PRIVATE_CONST: u32 = 1;
210 }
211
212 //- /main.rs crate:main deps:lib new_source_root:local
213 fn foo() { let _ = lib::S::$0 }
214 "#,
215         expect![[r#"
216                 ct PUBLIC_CONST    pub const PUBLIC_CONST: u32
217                 fn public_method() fn()
218                 ta PublicType      pub type PublicType = u32
219             "#]],
220     );
221 }
222
223 #[test]
224 fn completes_union_associated_method() {
225     check(
226         r#"
227 union U {};
228 impl U { fn m() { } }
229
230 fn foo() { let _ = U::$0 }
231 "#,
232         expect![[r#"
233                 fn m() fn()
234             "#]],
235     );
236 }
237
238 #[test]
239 fn completes_trait_associated_method_1() {
240     check(
241         r#"
242 trait Trait { fn m(); }
243
244 fn foo() { let _ = Trait::$0 }
245 "#,
246         expect![[r#"
247                 fn m() (as Trait) fn()
248             "#]],
249     );
250 }
251
252 #[test]
253 fn completes_trait_associated_method_2() {
254     check(
255         r#"
256 trait Trait { fn m(); }
257
258 struct S;
259 impl Trait for S {}
260
261 fn foo() { let _ = S::$0 }
262 "#,
263         expect![[r#"
264                 fn m() (as Trait) fn()
265             "#]],
266     );
267 }
268
269 #[test]
270 fn completes_trait_associated_method_3() {
271     check(
272         r#"
273 trait Trait { fn m(); }
274
275 struct S;
276 impl Trait for S {}
277
278 fn foo() { let _ = <S as Trait>::$0 }
279 "#,
280         expect![[r#"
281                 fn m() (as Trait) fn()
282             "#]],
283     );
284 }
285
286 #[test]
287 fn completes_ty_param_assoc_ty() {
288     check(
289         r#"
290 trait Super {
291     type Ty;
292     const CONST: u8;
293     fn func() {}
294     fn method(&self) {}
295 }
296
297 trait Sub: Super {
298     type SubTy;
299     const C2: ();
300     fn subfunc() {}
301     fn submethod(&self) {}
302 }
303
304 fn foo<T: Sub>() { T::$0 }
305 "#,
306         expect![[r#"
307                 ct C2 (as Sub)           const C2: ()
308                 ct CONST (as Super)      const CONST: u8
309                 fn func() (as Super)     fn()
310                 fn subfunc() (as Sub)    fn()
311                 ta SubTy (as Sub)        type SubTy
312                 ta Ty (as Super)         type Ty
313                 me method(…) (as Super)  fn(&self)
314                 me submethod(…) (as Sub) fn(&self)
315             "#]],
316     );
317 }
318
319 #[test]
320 fn completes_self_param_assoc_ty() {
321     check(
322         r#"
323 trait Super {
324     type Ty;
325     const CONST: u8 = 0;
326     fn func() {}
327     fn method(&self) {}
328 }
329
330 trait Sub: Super {
331     type SubTy;
332     const C2: () = ();
333     fn subfunc() {}
334     fn submethod(&self) {}
335 }
336
337 struct Wrap<T>(T);
338 impl<T> Super for Wrap<T> {}
339 impl<T> Sub for Wrap<T> {
340     fn subfunc() {
341         // Should be able to assume `Self: Sub + Super`
342         Self::$0
343     }
344 }
345 "#,
346         expect![[r#"
347                 ct C2 (as Sub)           const C2: ()
348                 ct CONST (as Super)      const CONST: u8
349                 fn func() (as Super)     fn()
350                 fn subfunc() (as Sub)    fn()
351                 ta SubTy (as Sub)        type SubTy
352                 ta Ty (as Super)         type Ty
353                 me method(…) (as Super)  fn(&self)
354                 me submethod(…) (as Sub) fn(&self)
355             "#]],
356     );
357 }
358
359 #[test]
360 fn completes_type_alias() {
361     check(
362         r#"
363 struct S;
364 impl S { fn foo() {} }
365 type T = S;
366 impl T { fn bar() {} }
367
368 fn main() { T::$0; }
369 "#,
370         expect![[r#"
371                 fn bar() fn()
372                 fn foo() fn()
373             "#]],
374     );
375 }
376
377 #[test]
378 fn completes_qualified_macros() {
379     check(
380         r#"
381 #[macro_export]
382 macro_rules! foo { () => {} }
383
384 fn main() { let _ = crate::$0 }
385 "#,
386         expect![[r#"
387                 fn main()  fn()
388                 ma foo!(…) macro_rules! foo
389             "#]],
390     );
391 }
392
393 #[test]
394 fn does_not_complete_non_fn_macros() {
395     check(
396         r#"
397 mod m {
398     #[rustc_builtin_macro]
399     pub macro Clone {}
400 }
401
402 fn f() {m::$0}
403 "#,
404         expect![[r#""#]],
405     );
406     check(
407         r#"
408 mod m {
409     #[rustc_builtin_macro]
410     pub macro bench {}
411 }
412
413 fn f() {m::$0}
414 "#,
415         expect![[r#""#]],
416     );
417 }
418
419 #[test]
420 fn completes_reexported_items_under_correct_name() {
421     check(
422         r#"
423 fn foo() { self::m::$0 }
424
425 mod m {
426     pub use super::p::wrong_fn as right_fn;
427     pub use super::p::WRONG_CONST as RIGHT_CONST;
428     pub use super::p::WrongType as RightType;
429 }
430 mod p {
431     pub fn wrong_fn() {}
432     pub const WRONG_CONST: u32 = 1;
433     pub struct WrongType {};
434 }
435 "#,
436         expect![[r#"
437                 ct RIGHT_CONST
438                 fn right_fn()  fn()
439                 st RightType
440             "#]],
441     );
442
443     check_edit(
444         "RightType",
445         r#"
446 fn foo() { self::m::$0 }
447
448 mod m {
449     pub use super::p::wrong_fn as right_fn;
450     pub use super::p::WRONG_CONST as RIGHT_CONST;
451     pub use super::p::WrongType as RightType;
452 }
453 mod p {
454     pub fn wrong_fn() {}
455     pub const WRONG_CONST: u32 = 1;
456     pub struct WrongType {};
457 }
458 "#,
459         r#"
460 fn foo() { self::m::RightType }
461
462 mod m {
463     pub use super::p::wrong_fn as right_fn;
464     pub use super::p::WRONG_CONST as RIGHT_CONST;
465     pub use super::p::WrongType as RightType;
466 }
467 mod p {
468     pub fn wrong_fn() {}
469     pub const WRONG_CONST: u32 = 1;
470     pub struct WrongType {};
471 }
472 "#,
473     );
474 }
475
476 #[test]
477 fn completes_in_simple_macro_call() {
478     check(
479         r#"
480 macro_rules! m { ($e:expr) => { $e } }
481 fn main() { m!(self::f$0); }
482 fn foo() {}
483 "#,
484         expect![[r#"
485                 fn foo()  fn()
486                 fn main() fn()
487             "#]],
488     );
489 }
490
491 #[test]
492 fn function_mod_share_name() {
493     check(
494         r#"
495 fn foo() { self::m::$0 }
496
497 mod m {
498     pub mod z {}
499     pub fn z() {}
500 }
501 "#,
502         expect![[r#"
503                 fn z() fn()
504                 md z
505             "#]],
506     );
507 }
508
509 #[test]
510 fn completes_hashmap_new() {
511     check(
512         r#"
513 struct RandomState;
514 struct HashMap<K, V, S = RandomState> {}
515
516 impl<K, V> HashMap<K, V, RandomState> {
517     pub fn new() -> HashMap<K, V, RandomState> { }
518 }
519 fn foo() {
520     HashMap::$0
521 }
522 "#,
523         expect![[r#"
524                 fn new() fn() -> HashMap<K, V, RandomState>
525             "#]],
526     );
527 }
528
529 #[test]
530 fn completes_variant_through_self() {
531     cov_mark::check!(completes_variant_through_self);
532     check(
533         r#"
534 enum Foo {
535     Bar,
536     Baz,
537 }
538
539 impl Foo {
540     fn foo(self) {
541         Self::$0
542     }
543 }
544 "#,
545         expect![[r#"
546                 ev Bar    Bar
547                 ev Baz    Baz
548                 me foo(…) fn(self)
549             "#]],
550     );
551 }
552
553 #[test]
554 fn completes_non_exhaustive_variant_within_the_defining_crate() {
555     check(
556         r#"
557 enum Foo {
558     #[non_exhaustive]
559     Bar,
560     Baz,
561 }
562
563 fn foo(self) {
564     Foo::$0
565 }
566 "#,
567         expect![[r#"
568                 ev Bar Bar
569                 ev Baz Baz
570             "#]],
571     );
572
573     check(
574         r#"
575 //- /main.rs crate:main deps:e
576 fn foo(self) {
577     e::Foo::$0
578 }
579
580 //- /e.rs crate:e
581 enum Foo {
582     #[non_exhaustive]
583     Bar,
584     Baz,
585 }
586 "#,
587         expect![[r#"
588                 ev Baz Baz
589             "#]],
590     );
591 }
592
593 #[test]
594 fn completes_primitive_assoc_const() {
595     cov_mark::check!(completes_primitive_assoc_const);
596     check(
597         r#"
598 //- /lib.rs crate:lib deps:core
599 fn f() {
600     u8::$0
601 }
602
603 //- /core.rs crate:core
604 #[lang = "u8"]
605 impl u8 {
606     pub const MAX: Self = 255;
607
608     pub fn func(self) {}
609 }
610 "#,
611         expect![[r#"
612                 ct MAX     pub const MAX: Self
613                 me func(…) fn(self)
614             "#]],
615     );
616 }
617
618 #[test]
619 fn completes_variant_through_alias() {
620     cov_mark::check!(completes_variant_through_alias);
621     check(
622         r#"
623 enum Foo {
624     Bar
625 }
626 type Foo2 = Foo;
627 fn main() {
628     Foo2::$0
629 }
630 "#,
631         expect![[r#"
632                 ev Bar Bar
633             "#]],
634     );
635 }
636
637 #[test]
638 fn respects_doc_hidden2() {
639     check(
640         r#"
641 //- /lib.rs crate:lib deps:dep
642 fn f() {
643     dep::$0
644 }
645
646 //- /dep.rs crate:dep
647 #[doc(hidden)]
648 #[macro_export]
649 macro_rules! m {
650     () => {}
651 }
652
653 #[doc(hidden)]
654 pub fn f() {}
655
656 #[doc(hidden)]
657 pub struct S;
658
659 #[doc(hidden)]
660 pub mod m {}
661             "#,
662         expect![[r#""#]],
663     )
664 }
665
666 #[test]
667 fn type_anchor_empty() {
668     check(
669         r#"
670 trait Foo {
671     fn foo() -> Self;
672 }
673 struct Bar;
674 impl Foo for Bar {
675     fn foo() -> {
676         Bar
677     }
678 }
679 fn bar() -> Bar {
680     <_>::$0
681 }
682 "#,
683         expect![[r#"
684                 fn foo() (as Foo) fn() -> Self
685             "#]],
686     );
687 }
688
689 #[test]
690 fn type_anchor_type() {
691     check(
692         r#"
693 trait Foo {
694     fn foo() -> Self;
695 }
696 struct Bar;
697 impl Bar {
698     fn bar() {}
699 }
700 impl Foo for Bar {
701     fn foo() -> {
702         Bar
703     }
704 }
705 fn bar() -> Bar {
706     <Bar>::$0
707 }
708 "#,
709         expect![[r#"
710             fn bar()          fn()
711             fn foo() (as Foo) fn() -> Self
712         "#]],
713     );
714 }
715
716 #[test]
717 fn type_anchor_type_trait() {
718     check(
719         r#"
720 trait Foo {
721     fn foo() -> Self;
722 }
723 struct Bar;
724 impl Bar {
725     fn bar() {}
726 }
727 impl Foo for Bar {
728     fn foo() -> {
729         Bar
730     }
731 }
732 fn bar() -> Bar {
733     <Bar as Foo>::$0
734 }
735 "#,
736         expect![[r#"
737             fn foo() (as Foo) fn() -> Self
738         "#]],
739     );
740 }
741
742 #[test]
743 fn completes_fn_in_pub_trait_generated_by_macro() {
744     check(
745         r#"
746 mod other_mod {
747     macro_rules! make_method {
748         ($name:ident) => {
749             fn $name(&self) {}
750         };
751     }
752
753     pub trait MyTrait {
754         make_method! { by_macro }
755         fn not_by_macro(&self) {}
756     }
757
758     pub struct Foo {}
759
760     impl MyTrait for Foo {}
761 }
762
763 fn main() {
764     use other_mod::{Foo, MyTrait};
765     let f = Foo {};
766     f.$0
767 }
768 "#,
769         expect![[r#"
770             me by_macro() (as MyTrait) fn(&self)
771             me not_by_macro() (as MyTrait) fn(&self)
772         "#]],
773     )
774 }
775
776 #[test]
777 fn completes_fn_in_pub_trait_generated_by_recursive_macro() {
778     check(
779         r#"
780 mod other_mod {
781     macro_rules! make_method {
782         ($name:ident) => {
783             fn $name(&self) {}
784         };
785     }
786
787     macro_rules! make_trait {
788         () => {
789             pub trait MyTrait {
790                 make_method! { by_macro }
791                 fn not_by_macro(&self) {}
792             }
793         }
794     }
795
796     make_trait!();
797
798     pub struct Foo {}
799
800     impl MyTrait for Foo {}
801 }
802
803 fn main() {
804     use other_mod::{Foo, MyTrait};
805     let f = Foo {};
806     f.$0
807 }
808 "#,
809         expect![[r#"
810             me by_macro() (as MyTrait) fn(&self)
811             me not_by_macro() (as MyTrait) fn(&self)
812         "#]],
813     )
814 }
815
816 #[test]
817 fn completes_const_in_pub_trait_generated_by_macro() {
818     check(
819         r#"
820 mod other_mod {
821     macro_rules! make_const {
822         ($name:ident) => {
823             const $name: u8 = 1;
824         };
825     }
826
827     pub trait MyTrait {
828         make_const! { by_macro }
829     }
830
831     pub struct Foo {}
832
833     impl MyTrait for Foo {}
834 }
835
836 fn main() {
837     use other_mod::{Foo, MyTrait};
838     let f = Foo {};
839     Foo::$0
840 }
841 "#,
842         expect![[r#"
843             ct by_macro (as MyTrait) pub const by_macro: u8
844         "#]],
845     )
846 }
847
848 #[test]
849 fn completes_locals_from_macros() {
850     check(
851         r#"
852
853 macro_rules! x {
854     ($x:ident, $expr:expr) => {
855         let $x = 0;
856         $expr
857     };
858 }
859 fn main() {
860     x! {
861         foobar, {
862             f$0
863         }
864     };
865 }
866 "#,
867         expect![[r#"
868             fn main() fn()
869             lc foobar i32
870             ma x!(…)  macro_rules! x
871             bt u32
872         "#]],
873     )
874 }
875
876 #[test]
877 fn regression_12644() {
878     check(
879         r#"
880 macro_rules! __rust_force_expr {
881     ($e:expr) => {
882         $e
883     };
884 }
885 macro_rules! vec {
886     ($elem:expr) => {
887         __rust_force_expr!($elem)
888     };
889 }
890
891 struct Struct;
892 impl Struct {
893     fn foo(self) {}
894 }
895
896 fn f() {
897     vec![Struct].$0;
898 }
899 "#,
900         expect![[r#"
901             me foo() fn(self)
902         "#]],
903     );
904 }
905
906 #[test]
907 fn completes_after_colon_with_trigger() {
908     check_with_trigger_character(
909         r#"
910 //- minicore: option
911 fn foo { ::$0 }
912 "#,
913         Some(':'),
914         expect![[r#"
915             md core
916         "#]],
917     );
918     check_with_trigger_character(
919         r#"
920 //- minicore: option
921 fn foo { /* test */::$0 }
922 "#,
923         Some(':'),
924         expect![[r#"
925             md core
926         "#]],
927     );
928
929     check_with_trigger_character(
930         r#"
931 fn foo { crate::$0 }
932 "#,
933         Some(':'),
934         expect![[r#"
935             fn foo() fn()
936         "#]],
937     );
938
939     check_with_trigger_character(
940         r#"
941 fn foo { crate:$0 }
942 "#,
943         Some(':'),
944         expect![""],
945     );
946 }
947
948 #[test]
949 fn completes_after_colon_without_trigger() {
950     check_with_trigger_character(
951         r#"
952 fn foo { crate::$0 }
953 "#,
954         None,
955         expect![[r#"
956             fn foo() fn()
957         "#]],
958     );
959
960     check_with_trigger_character(
961         r#"
962 fn foo { crate:$0 }
963 "#,
964         None,
965         expect![""],
966     );
967 }
968
969 #[test]
970 fn no_completions_in_invalid_path() {
971     check(
972         r#"
973 fn foo { crate:::$0 }
974 "#,
975         expect![""],
976     );
977     check(
978         r#"
979 fn foo { crate::::$0 }
980 "#,
981         expect![""],
982     );
983
984     check(
985         r#"
986 fn foo { crate:::::$0 }
987 "#,
988         expect![""],
989     );
990 }