1 //! Completes references after dot (fields and method calls).
2
3 use ide_db::FxHashSet;
4
5 use crate::{
6 context::{CompletionContext, DotAccess, DotAccessKind, ExprCtx, PathCompletionCtx, Qualified},
7 CompletionItem, CompletionItemKind, Completions,
8 };
9
10 /// Complete dot accesses, i.e. fields or methods.
complete_dot( acc: &mut Completions, ctx: &CompletionContext<'_>, dot_access: &DotAccess, )11 pub(crate) fn complete_dot(
12 acc: &mut Completions,
13 ctx: &CompletionContext<'_>,
14 dot_access: &DotAccess,
15 ) {
16 let receiver_ty = match dot_access {
17 DotAccess { receiver_ty: Some(receiver_ty), .. } => &receiver_ty.original,
18 _ => return,
19 };
20
21 // Suggest .await syntax for types that implement Future trait
22 if receiver_ty.impls_into_future(ctx.db) {
23 let mut item =
24 CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), "await");
25 item.detail("expr.await");
26 item.add_to(acc, ctx.db);
27 }
28
29 if let DotAccessKind::Method { .. } = dot_access.kind {
30 cov_mark::hit!(test_no_struct_field_completion_for_method_call);
31 } else {
32 complete_fields(
33 acc,
34 ctx,
35 receiver_ty,
36 |acc, field, ty| acc.add_field(ctx, dot_access, None, field, &ty),
37 |acc, field, ty| acc.add_tuple_field(ctx, None, field, &ty),
38 );
39 }
40 complete_methods(ctx, receiver_ty, |func| acc.add_method(ctx, dot_access, func, None, None));
41 }
42
complete_undotted_self( acc: &mut Completions, ctx: &CompletionContext<'_>, path_ctx: &PathCompletionCtx, expr_ctx: &ExprCtx, )43 pub(crate) fn complete_undotted_self(
44 acc: &mut Completions,
45 ctx: &CompletionContext<'_>,
46 path_ctx: &PathCompletionCtx,
47 expr_ctx: &ExprCtx,
48 ) {
49 if !ctx.config.enable_self_on_the_fly {
50 return;
51 }
52 if !path_ctx.is_trivial_path() {
53 return;
54 }
55 if !ctx.qualifier_ctx.none() {
56 return;
57 }
58 if !matches!(path_ctx.qualified, Qualified::No) {
59 return;
60 }
61 let self_param = match expr_ctx {
62 ExprCtx { self_param: Some(self_param), .. } => self_param,
63 _ => return,
64 };
65
66 let ty = self_param.ty(ctx.db);
67 complete_fields(
68 acc,
69 ctx,
70 &ty,
71 |acc, field, ty| {
72 acc.add_field(
73 ctx,
74 &DotAccess {
75 receiver: None,
76 receiver_ty: None,
77 kind: DotAccessKind::Field { receiver_is_ambiguous_float_literal: false },
78 },
79 Some(hir::known::SELF_PARAM),
80 field,
81 &ty,
82 )
83 },
84 |acc, field, ty| acc.add_tuple_field(ctx, Some(hir::known::SELF_PARAM), field, &ty),
85 );
86 complete_methods(ctx, &ty, |func| {
87 acc.add_method(
88 ctx,
89 &DotAccess {
90 receiver: None,
91 receiver_ty: None,
92 kind: DotAccessKind::Method { has_parens: false },
93 },
94 func,
95 Some(hir::known::SELF_PARAM),
96 None,
97 )
98 });
99 }
100
complete_fields( acc: &mut Completions, ctx: &CompletionContext<'_>, receiver: &hir::Type, mut named_field: impl FnMut(&mut Completions, hir::Field, hir::Type), mut tuple_index: impl FnMut(&mut Completions, usize, hir::Type), )101 fn complete_fields(
102 acc: &mut Completions,
103 ctx: &CompletionContext<'_>,
104 receiver: &hir::Type,
105 mut named_field: impl FnMut(&mut Completions, hir::Field, hir::Type),
106 mut tuple_index: impl FnMut(&mut Completions, usize, hir::Type),
107 ) {
108 let mut seen_names = FxHashSet::default();
109 for receiver in receiver.autoderef(ctx.db) {
110 for (field, ty) in receiver.fields(ctx.db) {
111 if seen_names.insert(field.name(ctx.db)) {
112 named_field(acc, field, ty);
113 }
114 }
115 for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() {
116 // Tuples are always the last type in a deref chain, so just check if the name is
117 // already seen without inserting into the hashset.
118 if !seen_names.contains(&hir::Name::new_tuple_field(i)) {
119 // Tuple fields are always public (tuple struct fields are handled above).
120 tuple_index(acc, i, ty);
121 }
122 }
123 }
124 }
125
complete_methods( ctx: &CompletionContext<'_>, receiver: &hir::Type, mut f: impl FnMut(hir::Function), )126 fn complete_methods(
127 ctx: &CompletionContext<'_>,
128 receiver: &hir::Type,
129 mut f: impl FnMut(hir::Function),
130 ) {
131 let mut seen_methods = FxHashSet::default();
132 receiver.iterate_method_candidates_with_traits(
133 ctx.db,
134 &ctx.scope,
135 &ctx.traits_in_scope(),
136 Some(ctx.module),
137 None,
138 |func| {
139 if func.self_param(ctx.db).is_some() && seen_methods.insert(func.name(ctx.db)) {
140 f(func);
141 }
142 None::<()>
143 },
144 );
145 }
146
147 #[cfg(test)]
148 mod tests {
149 use expect_test::{expect, Expect};
150
151 use crate::tests::{
152 check_edit, completion_list_no_kw, completion_list_no_kw_with_private_editable,
153 };
154
check(ra_fixture: &str, expect: Expect)155 fn check(ra_fixture: &str, expect: Expect) {
156 let actual = completion_list_no_kw(ra_fixture);
157 expect.assert_eq(&actual);
158 }
159
check_with_private_editable(ra_fixture: &str, expect: Expect)160 fn check_with_private_editable(ra_fixture: &str, expect: Expect) {
161 let actual = completion_list_no_kw_with_private_editable(ra_fixture);
162 expect.assert_eq(&actual);
163 }
164
165 #[test]
test_struct_field_and_method_completion()166 fn test_struct_field_and_method_completion() {
167 check(
168 r#"
169 struct S { foo: u32 }
170 impl S {
171 fn bar(&self) {}
172 }
173 fn foo(s: S) { s.$0 }
174 "#,
175 expect![[r#"
176 fd foo u32
177 me bar() fn(&self)
178 "#]],
179 );
180 }
181
182 #[test]
no_unstable_method_on_stable()183 fn no_unstable_method_on_stable() {
184 check(
185 r#"
186 //- /main.rs crate:main deps:std
187 fn foo(s: std::S) { s.$0 }
188 //- /std.rs crate:std
189 pub struct S;
190 impl S {
191 #[unstable]
192 pub fn bar(&self) {}
193 }
194 "#,
195 expect![""],
196 );
197 }
198
199 #[test]
unstable_method_on_nightly()200 fn unstable_method_on_nightly() {
201 check(
202 r#"
203 //- toolchain:nightly
204 //- /main.rs crate:main deps:std
205 fn foo(s: std::S) { s.$0 }
206 //- /std.rs crate:std
207 pub struct S;
208 impl S {
209 #[unstable]
210 pub fn bar(&self) {}
211 }
212 "#,
213 expect![[r#"
214 me bar() fn(&self)
215 "#]],
216 );
217 }
218
219 #[test]
test_struct_field_completion_self()220 fn test_struct_field_completion_self() {
221 check(
222 r#"
223 struct S { the_field: (u32,) }
224 impl S {
225 fn foo(self) { self.$0 }
226 }
227 "#,
228 expect![[r#"
229 fd the_field (u32,)
230 me foo() fn(self)
231 "#]],
232 )
233 }
234
235 #[test]
test_struct_field_completion_autoderef()236 fn test_struct_field_completion_autoderef() {
237 check(
238 r#"
239 struct A { the_field: (u32, i32) }
240 impl A {
241 fn foo(&self) { self.$0 }
242 }
243 "#,
244 expect![[r#"
245 fd the_field (u32, i32)
246 me foo() fn(&self)
247 "#]],
248 )
249 }
250
251 #[test]
test_no_struct_field_completion_for_method_call()252 fn test_no_struct_field_completion_for_method_call() {
253 cov_mark::check!(test_no_struct_field_completion_for_method_call);
254 check(
255 r#"
256 struct A { the_field: u32 }
257 fn foo(a: A) { a.$0() }
258 "#,
259 expect![[r#""#]],
260 );
261 }
262
263 #[test]
test_visibility_filtering()264 fn test_visibility_filtering() {
265 check(
266 r#"
267 //- /lib.rs crate:lib new_source_root:local
268 pub mod m {
269 pub struct A {
270 private_field: u32,
271 pub pub_field: u32,
272 pub(crate) crate_field: u32,
273 pub(super) super_field: u32,
274 }
275 }
276 //- /main.rs crate:main deps:lib new_source_root:local
277 fn foo(a: lib::m::A) { a.$0 }
278 "#,
279 expect![[r#"
280 fd pub_field u32
281 "#]],
282 );
283
284 check(
285 r#"
286 //- /lib.rs crate:lib new_source_root:library
287 pub mod m {
288 pub struct A {
289 private_field: u32,
290 pub pub_field: u32,
291 pub(crate) crate_field: u32,
292 pub(super) super_field: u32,
293 }
294 }
295 //- /main.rs crate:main deps:lib new_source_root:local
296 fn foo(a: lib::m::A) { a.$0 }
297 "#,
298 expect![[r#"
299 fd pub_field u32
300 "#]],
301 );
302
303 check(
304 r#"
305 //- /lib.rs crate:lib new_source_root:library
306 pub mod m {
307 pub struct A(
308 i32,
309 pub f64,
310 );
311 }
312 //- /main.rs crate:main deps:lib new_source_root:local
313 fn foo(a: lib::m::A) { a.$0 }
314 "#,
315 expect![[r#"
316 fd 1 f64
317 "#]],
318 );
319
320 check(
321 r#"
322 //- /lib.rs crate:lib new_source_root:local
323 pub struct A {}
324 mod m {
325 impl super::A {
326 fn private_method(&self) {}
327 pub(crate) fn crate_method(&self) {}
328 pub fn pub_method(&self) {}
329 }
330 }
331 //- /main.rs crate:main deps:lib new_source_root:local
332 fn foo(a: lib::A) { a.$0 }
333 "#,
334 expect![[r#"
335 me pub_method() fn(&self)
336 "#]],
337 );
338 check(
339 r#"
340 //- /lib.rs crate:lib new_source_root:library
341 pub struct A {}
342 mod m {
343 impl super::A {
344 fn private_method(&self) {}
345 pub(crate) fn crate_method(&self) {}
346 pub fn pub_method(&self) {}
347 }
348 }
349 //- /main.rs crate:main deps:lib new_source_root:local
350 fn foo(a: lib::A) { a.$0 }
351 "#,
352 expect![[r#"
353 me pub_method() fn(&self)
354 "#]],
355 );
356 }
357
358 #[test]
test_visibility_filtering_with_private_editable_enabled()359 fn test_visibility_filtering_with_private_editable_enabled() {
360 check_with_private_editable(
361 r#"
362 //- /lib.rs crate:lib new_source_root:local
363 pub mod m {
364 pub struct A {
365 private_field: u32,
366 pub pub_field: u32,
367 pub(crate) crate_field: u32,
368 pub(super) super_field: u32,
369 }
370 }
371 //- /main.rs crate:main deps:lib new_source_root:local
372 fn foo(a: lib::m::A) { a.$0 }
373 "#,
374 expect![[r#"
375 fd crate_field u32
376 fd private_field u32
377 fd pub_field u32
378 fd super_field u32
379 "#]],
380 );
381
382 check_with_private_editable(
383 r#"
384 //- /lib.rs crate:lib new_source_root:library
385 pub mod m {
386 pub struct A {
387 private_field: u32,
388 pub pub_field: u32,
389 pub(crate) crate_field: u32,
390 pub(super) super_field: u32,
391 }
392 }
393 //- /main.rs crate:main deps:lib new_source_root:local
394 fn foo(a: lib::m::A) { a.$0 }
395 "#,
396 expect![[r#"
397 fd pub_field u32
398 "#]],
399 );
400
401 check_with_private_editable(
402 r#"
403 //- /lib.rs crate:lib new_source_root:library
404 pub mod m {
405 pub struct A(
406 i32,
407 pub f64,
408 );
409 }
410 //- /main.rs crate:main deps:lib new_source_root:local
411 fn foo(a: lib::m::A) { a.$0 }
412 "#,
413 expect![[r#"
414 fd 1 f64
415 "#]],
416 );
417
418 check_with_private_editable(
419 r#"
420 //- /lib.rs crate:lib new_source_root:local
421 pub struct A {}
422 mod m {
423 impl super::A {
424 fn private_method(&self) {}
425 pub(crate) fn crate_method(&self) {}
426 pub fn pub_method(&self) {}
427 }
428 }
429 //- /main.rs crate:main deps:lib new_source_root:local
430 fn foo(a: lib::A) { a.$0 }
431 "#,
432 expect![[r#"
433 me crate_method() fn(&self)
434 me private_method() fn(&self)
435 me pub_method() fn(&self)
436 "#]],
437 );
438 check_with_private_editable(
439 r#"
440 //- /lib.rs crate:lib new_source_root:library
441 pub struct A {}
442 mod m {
443 impl super::A {
444 fn private_method(&self) {}
445 pub(crate) fn crate_method(&self) {}
446 pub fn pub_method(&self) {}
447 }
448 }
449 //- /main.rs crate:main deps:lib new_source_root:local
450 fn foo(a: lib::A) { a.$0 }
451 "#,
452 expect![[r#"
453 me pub_method() fn(&self)
454 "#]],
455 );
456 }
457
458 #[test]
test_local_impls()459 fn test_local_impls() {
460 check(
461 r#"
462 pub struct A {}
463 mod m {
464 impl super::A {
465 pub fn pub_module_method(&self) {}
466 }
467 fn f() {
468 impl super::A {
469 pub fn pub_foreign_local_method(&self) {}
470 }
471 }
472 }
473 fn foo(a: A) {
474 impl A {
475 fn local_method(&self) {}
476 }
477 a.$0
478 }
479 "#,
480 expect![[r#"
481 me local_method() fn(&self)
482 me pub_module_method() fn(&self)
483 "#]],
484 );
485 }
486
487 #[test]
test_doc_hidden_filtering()488 fn test_doc_hidden_filtering() {
489 check(
490 r#"
491 //- /lib.rs crate:lib deps:dep
492 fn foo(a: dep::A) { a.$0 }
493 //- /dep.rs crate:dep
494 pub struct A {
495 #[doc(hidden)]
496 pub hidden_field: u32,
497 pub pub_field: u32,
498 }
499
500 impl A {
501 pub fn pub_method(&self) {}
502
503 #[doc(hidden)]
504 pub fn hidden_method(&self) {}
505 }
506 "#,
507 expect![[r#"
508 fd pub_field u32
509 me pub_method() fn(&self)
510 "#]],
511 )
512 }
513
514 #[test]
test_union_field_completion()515 fn test_union_field_completion() {
516 check(
517 r#"
518 union U { field: u8, other: u16 }
519 fn foo(u: U) { u.$0 }
520 "#,
521 expect![[r#"
522 fd field u8
523 fd other u16
524 "#]],
525 );
526 }
527
528 #[test]
test_method_completion_only_fitting_impls()529 fn test_method_completion_only_fitting_impls() {
530 check(
531 r#"
532 struct A<T> {}
533 impl A<u32> {
534 fn the_method(&self) {}
535 }
536 impl A<i32> {
537 fn the_other_method(&self) {}
538 }
539 fn foo(a: A<u32>) { a.$0 }
540 "#,
541 expect![[r#"
542 me the_method() fn(&self)
543 "#]],
544 )
545 }
546
547 #[test]
test_trait_method_completion()548 fn test_trait_method_completion() {
549 check(
550 r#"
551 struct A {}
552 trait Trait { fn the_method(&self); }
553 impl Trait for A {}
554 fn foo(a: A) { a.$0 }
555 "#,
556 expect![[r#"
557 me the_method() (as Trait) fn(&self)
558 "#]],
559 );
560 check_edit(
561 "the_method",
562 r#"
563 struct A {}
564 trait Trait { fn the_method(&self); }
565 impl Trait for A {}
566 fn foo(a: A) { a.$0 }
567 "#,
568 r#"
569 struct A {}
570 trait Trait { fn the_method(&self); }
571 impl Trait for A {}
572 fn foo(a: A) { a.the_method()$0 }
573 "#,
574 );
575 }
576
577 #[test]
test_trait_method_completion_deduplicated()578 fn test_trait_method_completion_deduplicated() {
579 check(
580 r"
581 struct A {}
582 trait Trait { fn the_method(&self); }
583 impl<T> Trait for T {}
584 fn foo(a: &A) { a.$0 }
585 ",
586 expect![[r#"
587 me the_method() (as Trait) fn(&self)
588 "#]],
589 );
590 }
591
592 #[test]
completes_trait_method_from_other_module()593 fn completes_trait_method_from_other_module() {
594 check(
595 r"
596 struct A {}
597 mod m {
598 pub trait Trait { fn the_method(&self); }
599 }
600 use m::Trait;
601 impl Trait for A {}
602 fn foo(a: A) { a.$0 }
603 ",
604 expect![[r#"
605 me the_method() (as Trait) fn(&self)
606 "#]],
607 );
608 }
609
610 #[test]
test_no_non_self_method()611 fn test_no_non_self_method() {
612 check(
613 r#"
614 struct A {}
615 impl A {
616 fn the_method() {}
617 }
618 fn foo(a: A) {
619 a.$0
620 }
621 "#,
622 expect![[r#""#]],
623 );
624 }
625
626 #[test]
test_tuple_field_completion()627 fn test_tuple_field_completion() {
628 check(
629 r#"
630 fn foo() {
631 let b = (0, 3.14);
632 b.$0
633 }
634 "#,
635 expect![[r#"
636 fd 0 i32
637 fd 1 f64
638 "#]],
639 );
640 }
641
642 #[test]
test_tuple_struct_field_completion()643 fn test_tuple_struct_field_completion() {
644 check(
645 r#"
646 struct S(i32, f64);
647 fn foo() {
648 let b = S(0, 3.14);
649 b.$0
650 }
651 "#,
652 expect![[r#"
653 fd 0 i32
654 fd 1 f64
655 "#]],
656 );
657 }
658
659 #[test]
test_tuple_field_inference()660 fn test_tuple_field_inference() {
661 check(
662 r#"
663 pub struct S;
664 impl S { pub fn blah(&self) {} }
665
666 struct T(S);
667
668 impl T {
669 fn foo(&self) {
670 // FIXME: This doesn't work without the trailing `a` as `0.` is a float
671 self.0.a$0
672 }
673 }
674 "#,
675 expect![[r#"
676 me blah() fn(&self)
677 "#]],
678 );
679 }
680
681 #[test]
test_field_no_same_name()682 fn test_field_no_same_name() {
683 check(
684 r#"
685 //- minicore: deref
686 struct A { field: u8 }
687 struct B { field: u16, another: u32 }
688 impl core::ops::Deref for A {
689 type Target = B;
690 fn deref(&self) -> &Self::Target { loop {} }
691 }
692 fn test(a: A) {
693 a.$0
694 }
695 "#,
696 expect![[r#"
697 fd another u32
698 fd field u8
699 me deref() (use core::ops::Deref) fn(&self) -> &<Self as Deref>::Target
700 "#]],
701 );
702 }
703
704 #[test]
test_tuple_field_no_same_index()705 fn test_tuple_field_no_same_index() {
706 check(
707 r#"
708 //- minicore: deref
709 struct A(u8);
710 struct B(u16, u32);
711 impl core::ops::Deref for A {
712 type Target = B;
713 fn deref(&self) -> &Self::Target { loop {} }
714 }
715 fn test(a: A) {
716 a.$0
717 }
718 "#,
719 expect![[r#"
720 fd 0 u8
721 fd 1 u32
722 me deref() (use core::ops::Deref) fn(&self) -> &<Self as Deref>::Target
723 "#]],
724 );
725 }
726
727 #[test]
test_tuple_struct_deref_to_tuple_no_same_index()728 fn test_tuple_struct_deref_to_tuple_no_same_index() {
729 check(
730 r#"
731 //- minicore: deref
732 struct A(u8);
733 impl core::ops::Deref for A {
734 type Target = (u16, u32);
735 fn deref(&self) -> &Self::Target { loop {} }
736 }
737 fn test(a: A) {
738 a.$0
739 }
740 "#,
741 expect![[r#"
742 fd 0 u8
743 fd 1 u32
744 me deref() (use core::ops::Deref) fn(&self) -> &<Self as Deref>::Target
745 "#]],
746 );
747 }
748
749 #[test]
test_completion_works_in_consts()750 fn test_completion_works_in_consts() {
751 check(
752 r#"
753 struct A { the_field: u32 }
754 const X: u32 = {
755 A { the_field: 92 }.$0
756 };
757 "#,
758 expect![[r#"
759 fd the_field u32
760 "#]],
761 );
762 }
763
764 #[test]
works_in_simple_macro_1()765 fn works_in_simple_macro_1() {
766 check(
767 r#"
768 macro_rules! m { ($e:expr) => { $e } }
769 struct A { the_field: u32 }
770 fn foo(a: A) {
771 m!(a.x$0)
772 }
773 "#,
774 expect![[r#"
775 fd the_field u32
776 "#]],
777 );
778 }
779
780 #[test]
works_in_simple_macro_2()781 fn works_in_simple_macro_2() {
782 // this doesn't work yet because the macro doesn't expand without the token -- maybe it can be fixed with better recovery
783 check(
784 r#"
785 macro_rules! m { ($e:expr) => { $e } }
786 struct A { the_field: u32 }
787 fn foo(a: A) {
788 m!(a.$0)
789 }
790 "#,
791 expect![[r#"
792 fd the_field u32
793 "#]],
794 );
795 }
796
797 #[test]
works_in_simple_macro_recursive_1()798 fn works_in_simple_macro_recursive_1() {
799 check(
800 r#"
801 macro_rules! m { ($e:expr) => { $e } }
802 struct A { the_field: u32 }
803 fn foo(a: A) {
804 m!(m!(m!(a.x$0)))
805 }
806 "#,
807 expect![[r#"
808 fd the_field u32
809 "#]],
810 );
811 }
812
813 #[test]
macro_expansion_resilient()814 fn macro_expansion_resilient() {
815 check(
816 r#"
817 macro_rules! d {
818 () => {};
819 ($val:expr) => {
820 match $val { tmp => { tmp } }
821 };
822 // Trailing comma with single argument is ignored
823 ($val:expr,) => { $crate::d!($val) };
824 ($($val:expr),+ $(,)?) => {
825 ($($crate::d!($val)),+,)
826 };
827 }
828 struct A { the_field: u32 }
829 fn foo(a: A) {
830 d!(a.$0)
831 }
832 "#,
833 expect![[r#"
834 fd the_field u32
835 "#]],
836 );
837 }
838
839 #[test]
test_method_completion_issue_3547()840 fn test_method_completion_issue_3547() {
841 check(
842 r#"
843 struct HashSet<T> {}
844 impl<T> HashSet<T> {
845 pub fn the_method(&self) {}
846 }
847 fn foo() {
848 let s: HashSet<_>;
849 s.$0
850 }
851 "#,
852 expect![[r#"
853 me the_method() fn(&self)
854 "#]],
855 );
856 }
857
858 #[test]
completes_method_call_when_receiver_is_a_macro_call()859 fn completes_method_call_when_receiver_is_a_macro_call() {
860 check(
861 r#"
862 struct S;
863 impl S { fn foo(&self) {} }
864 macro_rules! make_s { () => { S }; }
865 fn main() { make_s!().f$0; }
866 "#,
867 expect![[r#"
868 me foo() fn(&self)
869 "#]],
870 )
871 }
872
873 #[test]
completes_after_macro_call_in_submodule()874 fn completes_after_macro_call_in_submodule() {
875 check(
876 r#"
877 macro_rules! empty {
878 () => {};
879 }
880
881 mod foo {
882 #[derive(Debug, Default)]
883 struct Template2 {}
884
885 impl Template2 {
886 fn private(&self) {}
887 }
888 fn baz() {
889 let goo: Template2 = Template2 {};
890 empty!();
891 goo.$0
892 }
893 }
894 "#,
895 expect![[r#"
896 me private() fn(&self)
897 "#]],
898 );
899 }
900
901 #[test]
issue_8931()902 fn issue_8931() {
903 check(
904 r#"
905 //- minicore: fn
906 struct S;
907
908 struct Foo;
909 impl Foo {
910 fn foo(&self) -> &[u8] { loop {} }
911 }
912
913 impl S {
914 fn indented(&mut self, f: impl FnOnce(&mut Self)) {
915 }
916
917 fn f(&mut self, v: Foo) {
918 self.indented(|this| v.$0)
919 }
920 }
921 "#,
922 expect![[r#"
923 me foo() fn(&self) -> &[u8]
924 "#]],
925 );
926 }
927
928 #[test]
completes_bare_fields_and_methods_in_methods()929 fn completes_bare_fields_and_methods_in_methods() {
930 check(
931 r#"
932 struct Foo { field: i32 }
933
934 impl Foo { fn foo(&self) { $0 } }"#,
935 expect![[r#"
936 fd self.field i32
937 lc self &Foo
938 sp Self
939 st Foo
940 bt u32
941 me self.foo() fn(&self)
942 "#]],
943 );
944 check(
945 r#"
946 struct Foo(i32);
947
948 impl Foo { fn foo(&mut self) { $0 } }"#,
949 expect![[r#"
950 fd self.0 i32
951 lc self &mut Foo
952 sp Self
953 st Foo
954 bt u32
955 me self.foo() fn(&mut self)
956 "#]],
957 );
958 }
959
960 #[test]
macro_completion_after_dot()961 fn macro_completion_after_dot() {
962 check(
963 r#"
964 macro_rules! m {
965 ($e:expr) => { $e };
966 }
967
968 struct Completable;
969
970 impl Completable {
971 fn method(&self) {}
972 }
973
974 fn f() {
975 let c = Completable;
976 m!(c.$0);
977 }
978 "#,
979 expect![[r#"
980 me method() fn(&self)
981 "#]],
982 );
983 }
984
985 #[test]
completes_method_call_when_receiver_type_has_errors_issue_10297()986 fn completes_method_call_when_receiver_type_has_errors_issue_10297() {
987 check(
988 r#"
989 //- minicore: iterator, sized
990 struct Vec<T>;
991 impl<T> IntoIterator for Vec<T> {
992 type Item = ();
993 type IntoIter = ();
994 fn into_iter(self);
995 }
996 fn main() {
997 let x: Vec<_>;
998 x.$0;
999 }
1000 "#,
1001 expect![[r#"
1002 me into_iter() (as IntoIterator) fn(self) -> <Self as IntoIterator>::IntoIter
1003 "#]],
1004 )
1005 }
1006
1007 #[test]
postfix_drop_completion()1008 fn postfix_drop_completion() {
1009 cov_mark::check!(postfix_drop_completion);
1010 check_edit(
1011 "drop",
1012 r#"
1013 //- minicore: drop
1014 struct Vec<T>(T);
1015 impl<T> Drop for Vec<T> {
1016 fn drop(&mut self) {}
1017 }
1018 fn main() {
1019 let x = Vec(0u32)
1020 x.$0;
1021 }
1022 "#,
1023 r"
1024 struct Vec<T>(T);
1025 impl<T> Drop for Vec<T> {
1026 fn drop(&mut self) {}
1027 }
1028 fn main() {
1029 let x = Vec(0u32)
1030 drop($0x);
1031 }
1032 ",
1033 )
1034 }
1035
1036 #[test]
issue_12484()1037 fn issue_12484() {
1038 check(
1039 r#"
1040 //- minicore: sized
1041 trait SizeUser {
1042 type Size;
1043 }
1044 trait Closure: SizeUser {}
1045 trait Encrypt: SizeUser {
1046 fn encrypt(self, _: impl Closure<Size = Self::Size>);
1047 }
1048 fn test(thing: impl Encrypt) {
1049 thing.$0;
1050 }
1051 "#,
1052 expect![[r#"
1053 me encrypt(…) (as Encrypt) fn(self, impl Closure<Size = <Self as SizeUser>::Size>)
1054 "#]],
1055 )
1056 }
1057
1058 #[test]
only_consider_same_type_once()1059 fn only_consider_same_type_once() {
1060 check(
1061 r#"
1062 //- minicore: deref
1063 struct A(u8);
1064 struct B(u16);
1065 impl core::ops::Deref for A {
1066 type Target = B;
1067 fn deref(&self) -> &Self::Target { loop {} }
1068 }
1069 impl core::ops::Deref for B {
1070 type Target = A;
1071 fn deref(&self) -> &Self::Target { loop {} }
1072 }
1073 fn test(a: A) {
1074 a.$0
1075 }
1076 "#,
1077 expect![[r#"
1078 fd 0 u8
1079 me deref() (use core::ops::Deref) fn(&self) -> &<Self as Deref>::Target
1080 "#]],
1081 );
1082 }
1083
1084 #[test]
no_inference_var_in_completion()1085 fn no_inference_var_in_completion() {
1086 check(
1087 r#"
1088 struct S<T>(T);
1089 fn test(s: S<Unknown>) {
1090 s.$0
1091 }
1092 "#,
1093 expect![[r#"
1094 fd 0 {unknown}
1095 "#]],
1096 );
1097 }
1098 }
1099