1use std::{collections::hash_map, ops::ControlFlow};
4
5use hir::{Complete, Function, HasContainer, ItemContainer, MethodCandidateCallback, Name};
6use ide_db::{FxHashMap, FxHashSet};
7use itertools::Either;
8use syntax::SmolStr;
9
10use crate::{
11 CompletionItem, CompletionItemKind, Completions,
12 context::{
13 CompletionContext, DotAccess, DotAccessExprCtx, DotAccessKind, PathCompletionCtx,
14 PathExprCtx, Qualified,
15 },
16};
17
18pub(crate) fn complete_dot(
20 acc: &mut Completions,
21 ctx: &CompletionContext<'_, '_>,
22 dot_access: &DotAccess<'_>,
23) {
24 let receiver_ty = match dot_access {
25 DotAccess { receiver_ty: Some(receiver_ty), .. } => &receiver_ty.original,
26 _ => return,
27 };
28
29 let has_parens = matches!(dot_access.kind, DotAccessKind::Method);
30 let traits_in_scope = ctx.traits_in_scope();
31
32 if let Some(future_output) = receiver_ty.into_future_output(ctx.db) {
34 let await_str = SmolStr::new_static("await");
35 let mut item = CompletionItem::new(
36 CompletionItemKind::Keyword,
37 ctx.source_range(),
38 await_str.clone(),
39 ctx.edition,
40 );
41 item.detail("expr.await");
42 item.add_to(acc, ctx.db);
43
44 if ctx.config.enable_auto_await {
45 let dot_access_kind = match &dot_access.kind {
47 DotAccessKind::Field { receiver_is_ambiguous_float_literal: _ } => {
48 DotAccessKind::Field { receiver_is_ambiguous_float_literal: false }
49 }
50 it @ DotAccessKind::Method => *it,
51 };
52 let dot_access = DotAccess {
53 receiver: dot_access.receiver.clone(),
54 receiver_ty: Some(hir::TypeInfo {
55 original: future_output.clone(),
56 adjusted: None,
57 }),
58 kind: dot_access_kind,
59 ctx: dot_access.ctx,
60 };
61 complete_fields(
62 acc,
63 ctx,
64 &future_output,
65 |acc, field, ty| {
66 acc.add_field(ctx, &dot_access, Some(await_str.clone()), field, &ty)
67 },
68 |acc, field, ty| acc.add_tuple_field(ctx, Some(await_str.clone()), field, &ty),
69 has_parens,
70 );
71 complete_methods(ctx, &future_output, &traits_in_scope, |func| {
72 acc.add_method(ctx, &dot_access, func, Some(await_str.clone()), None)
73 });
74 }
75 }
76
77 complete_fields(
78 acc,
79 ctx,
80 receiver_ty,
81 |acc, field, ty| acc.add_field(ctx, dot_access, None, field, &ty),
82 |acc, field, ty| acc.add_tuple_field(ctx, None, field, &ty),
83 has_parens,
84 );
85 complete_methods(ctx, receiver_ty, &traits_in_scope, |func| {
86 acc.add_method(ctx, dot_access, func, None, None)
87 });
88
89 if ctx.config.enable_auto_iter && !receiver_ty.strip_references().impls_iterator(ctx.db) {
90 let iter = receiver_ty
95 .autoderef(ctx.db)
96 .map(|ty| ty.strip_references().add_reference(ctx.db, hir::Mutability::Shared))
97 .find_map(|ty| ty.into_iterator_iter(ctx.db))
98 .map(|ty| (ty, SmolStr::new_static("iter()")));
99 let into_iter = || {
101 receiver_ty
102 .clone()
103 .into_iterator_iter(ctx.db)
104 .map(|ty| (ty, SmolStr::new_static("into_iter()")))
105 };
106 if let Some((iter, iter_sym)) = iter.or_else(into_iter) {
107 let dot_access_kind = match &dot_access.kind {
109 DotAccessKind::Field { receiver_is_ambiguous_float_literal: _ } => {
110 DotAccessKind::Field { receiver_is_ambiguous_float_literal: false }
111 }
112 it @ DotAccessKind::Method => *it,
113 };
114 let dot_access = DotAccess {
115 receiver: dot_access.receiver.clone(),
116 receiver_ty: Some(hir::TypeInfo { original: iter.clone(), adjusted: None }),
117 kind: dot_access_kind,
118 ctx: dot_access.ctx,
119 };
120 complete_methods(ctx, &iter, &traits_in_scope, |func| {
121 acc.add_method(ctx, &dot_access, func, Some(iter_sym.clone()), None)
122 });
123 }
124 }
125}
126
127pub(crate) fn complete_undotted_self(
128 acc: &mut Completions,
129 ctx: &CompletionContext<'_, '_>,
130 path_ctx: &PathCompletionCtx<'_>,
131 expr_ctx: &PathExprCtx<'_>,
132) {
133 if !ctx.config.enable_self_on_the_fly {
134 return;
135 }
136 if !path_ctx.is_trivial_path() {
137 return;
138 }
139 if !ctx.qualifier_ctx.none() {
140 return;
141 }
142 if !matches!(path_ctx.qualified, Qualified::No) {
143 return;
144 }
145 let self_param = match expr_ctx {
146 PathExprCtx { self_param: Some(self_param), .. } => self_param,
147 _ => return,
148 };
149
150 let (param_name, ty) = match self_param {
151 Either::Left(self_param) => ("self", &self_param.ty(ctx.db)),
152 Either::Right(this_param) => ("this", this_param.ty()),
153 };
154 complete_fields(
155 acc,
156 ctx,
157 ty,
158 |acc, field, ty| {
159 acc.add_field(
160 ctx,
161 &DotAccess {
162 receiver: None,
163 receiver_ty: None,
164 kind: DotAccessKind::Field { receiver_is_ambiguous_float_literal: false },
165 ctx: DotAccessExprCtx {
166 in_block_expr: expr_ctx.in_block_expr,
167 in_breakable: expr_ctx.in_breakable,
168 },
169 },
170 Some(SmolStr::new_static(param_name)),
171 field,
172 &ty,
173 )
174 },
175 |acc, field, ty| {
176 acc.add_tuple_field(ctx, Some(SmolStr::new_static(param_name)), field, &ty)
177 },
178 false,
179 );
180 complete_methods(ctx, ty, &ctx.traits_in_scope(), |func| {
181 acc.add_method(
182 ctx,
183 &DotAccess {
184 receiver: None,
185 receiver_ty: None,
186 kind: DotAccessKind::Field { receiver_is_ambiguous_float_literal: false },
187 ctx: DotAccessExprCtx {
188 in_block_expr: expr_ctx.in_block_expr,
189 in_breakable: expr_ctx.in_breakable,
190 },
191 },
192 func,
193 Some(SmolStr::new_static(param_name)),
194 None,
195 )
196 });
197}
198
199fn complete_fields(
200 acc: &mut Completions,
201 ctx: &CompletionContext<'_, '_>,
202 receiver: &hir::Type<'_>,
203 mut named_field: impl FnMut(&mut Completions, hir::Field, hir::Type<'_>),
204 mut tuple_index: impl FnMut(&mut Completions, usize, hir::Type<'_>),
205 has_parens: bool,
206) {
207 let mut seen_names = FxHashSet::default();
208 for receiver in receiver.autoderef(ctx.db) {
209 for (field, ty) in receiver.fields(ctx.db) {
210 if seen_names.insert(field.name(ctx.db))
211 && (!has_parens || ty.is_fn() || ty.is_closure())
212 {
213 named_field(acc, field, ty);
214 }
215 }
216 for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() {
217 if !seen_names.contains(&hir::Name::new_tuple_field(i))
220 && (!has_parens || ty.is_fn() || ty.is_closure())
221 {
222 tuple_index(acc, i, ty);
224 }
225 }
226 }
227}
228
229fn complete_methods(
230 ctx: &CompletionContext<'_, '_>,
231 receiver: &hir::Type<'_>,
232 traits_in_scope: &FxHashSet<hir::TraitId>,
233 f: impl FnMut(hir::Function),
234) {
235 struct Callback<'a, 'db, F> {
236 ctx: &'a CompletionContext<'a, 'db>,
237 f: F,
238 seen_methods: FxHashSet<Function>,
242 seen_inherent_methods: FxHashMap<Name, Function>,
245 }
246
247 impl<F> MethodCandidateCallback for Callback<'_, '_, F>
248 where
249 F: FnMut(hir::Function),
250 {
251 fn on_inherent_method(&mut self, func: hir::Function) -> ControlFlow<()> {
254 if func.self_param(self.ctx.db).is_some() && self.seen_methods.insert(func) {
255 let same_name = self.seen_inherent_methods.entry(func.name(self.ctx.db));
256 let do_complete = match &same_name {
257 hash_map::Entry::Vacant(_) => true,
258 hash_map::Entry::Occupied(same_func) => {
259 match self.ctx.is_visible(same_func.get()) {
260 crate::context::Visible::Yes => false,
261 crate::context::Visible::Editable => true,
262 crate::context::Visible::No => true,
263 }
264 }
265 };
266 if do_complete {
267 same_name.insert_entry(func);
268 (self.f)(func);
269 }
270 }
271 ControlFlow::Continue(())
272 }
273
274 fn on_trait_method(&mut self, func: hir::Function) -> ControlFlow<()> {
275 if let ItemContainer::Trait(trait_) = func.container(self.ctx.db)
278 && (self.ctx.exclude_traits.contains(&trait_)
279 || trait_.complete(self.ctx.db) == Complete::IgnoreMethods)
280 {
281 return ControlFlow::Continue(());
282 }
283
284 if func.self_param(self.ctx.db).is_some() && self.seen_methods.insert(func) {
285 (self.f)(func);
286 }
287
288 ControlFlow::Continue(())
289 }
290 }
291
292 receiver.iterate_method_candidates_split_inherent(
293 ctx.db,
294 &ctx.scope,
295 traits_in_scope,
296 None,
297 Callback {
298 ctx,
299 f,
300 seen_methods: FxHashSet::default(),
301 seen_inherent_methods: FxHashMap::default(),
302 },
303 );
304}
305
306#[cfg(test)]
307mod tests {
308 use expect_test::expect;
309
310 use crate::tests::{check_edit, check_no_kw, check_with_private_editable};
311
312 #[test]
313 fn test_struct_field_and_method_completion() {
314 check_no_kw(
315 r#"
316struct S { foo: u32 }
317impl S {
318 fn bar(&self) {}
319}
320fn foo(s: S) { s.$0 }
321"#,
322 expect![[r#"
323 fd foo u32
324 me bar() fn(&self)
325 "#]],
326 );
327 }
328
329 #[test]
330 fn no_unstable_method_on_stable() {
331 check_no_kw(
332 r#"
333//- /main.rs crate:main deps:std
334fn foo(s: std::S) { s.$0 }
335//- /std.rs crate:std
336pub struct S;
337impl S {
338 #[unstable]
339 pub fn bar(&self) {}
340}
341"#,
342 expect![""],
343 );
344 }
345
346 #[test]
347 fn unstable_method_on_nightly() {
348 check_no_kw(
349 r#"
350//- toolchain:nightly
351//- /main.rs crate:main deps:std
352fn foo(s: std::S) { s.$0 }
353//- /std.rs crate:std
354pub struct S;
355impl S {
356 #[unstable]
357 pub fn bar(&self) {}
358}
359"#,
360 expect![[r#"
361 me bar() fn(&self)
362 "#]],
363 );
364 }
365
366 #[test]
367 fn test_struct_field_completion_self() {
368 check_no_kw(
369 r#"
370struct S { the_field: (u32,) }
371impl S {
372 fn foo(self) { self.$0 }
373}
374"#,
375 expect![[r#"
376 fd the_field (u32,)
377 me foo() fn(self)
378 "#]],
379 )
380 }
381
382 #[test]
383 fn test_struct_field_completion_autoderef() {
384 check_no_kw(
385 r#"
386struct A { the_field: (u32, i32) }
387impl A {
388 fn foo(&self) { self.$0 }
389}
390"#,
391 expect![[r#"
392 fd the_field (u32, i32)
393 me foo() fn(&self)
394 "#]],
395 )
396 }
397
398 #[test]
399 fn test_no_struct_field_completion_for_method_call() {
400 check_no_kw(
401 r#"
402struct A { the_field: u32 }
403fn foo(a: A) { a.$0() }
404"#,
405 expect![[r#""#]],
406 );
407 }
408
409 #[test]
410 fn test_visibility_filtering() {
411 check_no_kw(
412 r#"
413//- /lib.rs crate:lib new_source_root:local
414pub mod m {
415 pub struct A {
416 private_field: u32,
417 pub pub_field: u32,
418 pub(crate) crate_field: u32,
419 pub(super) super_field: u32,
420 }
421}
422//- /main.rs crate:main deps:lib new_source_root:local
423fn foo(a: lib::m::A) { a.$0 }
424"#,
425 expect![[r#"
426 fd pub_field u32
427 "#]],
428 );
429
430 check_no_kw(
431 r#"
432//- /lib.rs crate:lib new_source_root:library
433pub mod m {
434 pub struct A {
435 private_field: u32,
436 pub pub_field: u32,
437 pub(crate) crate_field: u32,
438 pub(super) super_field: u32,
439 }
440}
441//- /main.rs crate:main deps:lib new_source_root:local
442fn foo(a: lib::m::A) { a.$0 }
443"#,
444 expect![[r#"
445 fd pub_field u32
446 "#]],
447 );
448
449 check_no_kw(
450 r#"
451//- /lib.rs crate:lib new_source_root:library
452pub mod m {
453 pub struct A(
454 i32,
455 pub f64,
456 );
457}
458//- /main.rs crate:main deps:lib new_source_root:local
459fn foo(a: lib::m::A) { a.$0 }
460"#,
461 expect![[r#"
462 fd 1 f64
463 "#]],
464 );
465
466 check_no_kw(
467 r#"
468//- /lib.rs crate:lib new_source_root:local
469pub struct A {}
470mod m {
471 impl super::A {
472 fn private_method(&self) {}
473 pub(crate) fn crate_method(&self) {}
474 pub fn pub_method(&self) {}
475 }
476}
477//- /main.rs crate:main deps:lib new_source_root:local
478fn foo(a: lib::A) { a.$0 }
479"#,
480 expect![[r#"
481 me pub_method() fn(&self)
482 "#]],
483 );
484 check_no_kw(
485 r#"
486//- /lib.rs crate:lib new_source_root:library
487pub struct A {}
488mod m {
489 impl super::A {
490 fn private_method(&self) {}
491 pub(crate) fn crate_method(&self) {}
492 pub fn pub_method(&self) {}
493 }
494}
495//- /main.rs crate:main deps:lib new_source_root:local
496fn foo(a: lib::A) { a.$0 }
497"#,
498 expect![[r#"
499 me pub_method() fn(&self)
500 "#]],
501 );
502 }
503
504 #[test]
505 fn test_visibility_filtering_with_private_editable_enabled() {
506 check_with_private_editable(
507 r#"
508//- /lib.rs crate:lib new_source_root:local
509pub mod m {
510 pub struct A {
511 private_field: u32,
512 pub pub_field: u32,
513 pub(crate) crate_field: u32,
514 pub(super) super_field: u32,
515 }
516}
517//- /main.rs crate:main deps:lib new_source_root:local
518fn foo(a: lib::m::A) { a.$0 }
519"#,
520 expect![[r#"
521 fd crate_field u32
522 fd private_field u32
523 fd pub_field u32
524 fd super_field u32
525 "#]],
526 );
527
528 check_with_private_editable(
529 r#"
530//- /lib.rs crate:lib new_source_root:library
531pub mod m {
532 pub struct A {
533 private_field: u32,
534 pub pub_field: u32,
535 pub(crate) crate_field: u32,
536 pub(super) super_field: u32,
537 }
538}
539//- /main.rs crate:main deps:lib new_source_root:local
540fn foo(a: lib::m::A) { a.$0 }
541"#,
542 expect![[r#"
543 fd pub_field u32
544 "#]],
545 );
546
547 check_with_private_editable(
548 r#"
549//- /lib.rs crate:lib new_source_root:library
550pub mod m {
551 pub struct A(
552 i32,
553 pub f64,
554 );
555}
556//- /main.rs crate:main deps:lib new_source_root:local
557fn foo(a: lib::m::A) { a.$0 }
558"#,
559 expect![[r#"
560 fd 1 f64
561 "#]],
562 );
563
564 check_with_private_editable(
565 r#"
566//- /lib.rs crate:lib new_source_root:local
567pub struct A {}
568mod m {
569 impl super::A {
570 fn private_method(&self) {}
571 pub(crate) fn crate_method(&self) {}
572 pub fn pub_method(&self) {}
573 }
574}
575//- /main.rs crate:main deps:lib new_source_root:local
576fn foo(a: lib::A) { a.$0 }
577"#,
578 expect![[r#"
579 me crate_method() fn(&self)
580 me private_method() fn(&self)
581 me pub_method() fn(&self)
582 "#]],
583 );
584 check_with_private_editable(
585 r#"
586//- /lib.rs crate:lib new_source_root:library
587pub struct A {}
588mod m {
589 impl super::A {
590 fn private_method(&self) {}
591 pub(crate) fn crate_method(&self) {}
592 pub fn pub_method(&self) {}
593 }
594}
595//- /main.rs crate:main deps:lib new_source_root:local
596fn foo(a: lib::A) { a.$0 }
597"#,
598 expect![[r#"
599 me pub_method() fn(&self)
600 "#]],
601 );
602 }
603
604 #[test]
605 fn test_local_impls() {
606 check_no_kw(
607 r#"
608pub struct A {}
609mod m {
610 impl super::A {
611 pub fn pub_module_method(&self) {}
612 }
613 fn f() {
614 impl super::A {
615 pub fn pub_foreign_local_method(&self) {}
616 }
617 }
618}
619fn foo(a: A) {
620 impl A {
621 fn local_method(&self) {}
622 }
623 a.$0
624}
625"#,
626 expect![[r#"
627 me pub_module_method() fn(&self)
628 "#]],
629 );
630 }
631
632 #[test]
633 fn test_doc_hidden_filtering() {
634 check_no_kw(
635 r#"
636//- /lib.rs crate:lib deps:dep
637fn foo(a: dep::A) { a.$0 }
638//- /dep.rs crate:dep
639pub struct A {
640 #[doc(hidden)]
641 pub hidden_field: u32,
642 pub pub_field: u32,
643}
644
645impl A {
646 pub fn pub_method(&self) {}
647
648 #[doc(hidden)]
649 pub fn hidden_method(&self) {}
650}
651 "#,
652 expect![[r#"
653 fd pub_field u32
654 me pub_method() fn(&self)
655 "#]],
656 )
657 }
658
659 #[test]
660 fn test_union_field_completion() {
661 check_no_kw(
662 r#"
663union U { field: u8, other: u16 }
664fn foo(u: U) { u.$0 }
665"#,
666 expect![[r#"
667 fd field u8
668 fd other u16
669 "#]],
670 );
671 }
672
673 #[test]
674 fn test_method_completion_only_fitting_impls() {
675 check_no_kw(
676 r#"
677struct A<T>(T);
678impl A<u32> {
679 fn the_method(&self) {}
680}
681impl A<i32> {
682 fn the_other_method(&self) {}
683}
684fn foo(a: A<u32>) { a.$0 }
685"#,
686 expect![[r#"
687 fd 0 u32
688 me the_method() fn(&self)
689 "#]],
690 )
691 }
692
693 #[test]
694 fn test_trait_method_completion() {
695 check_no_kw(
696 r#"
697struct A {}
698trait Trait { fn the_method(&self); }
699impl Trait for A {}
700fn foo(a: A) { a.$0 }
701"#,
702 expect![[r#"
703 me the_method() (as Trait) fn(&self)
704 "#]],
705 );
706 check_edit(
707 "the_method",
708 r#"
709struct A {}
710trait Trait { fn the_method(&self); }
711impl Trait for A {}
712fn foo(a: A) { a.$0 }
713"#,
714 r#"
715struct A {}
716trait Trait { fn the_method(&self); }
717impl Trait for A {}
718fn foo(a: A) { a.the_method();$0 }
719"#,
720 );
721 }
722
723 #[test]
724 fn test_trait_method_completion_deduplicated() {
725 check_no_kw(
726 r"
727struct A {}
728trait Trait { fn the_method(&self); }
729impl<T> Trait for T {}
730fn foo(a: &A) { a.$0 }
731",
732 expect![[r#"
733 me the_method() (as Trait) fn(&self)
734 "#]],
735 );
736 }
737
738 #[test]
739 fn completes_trait_method_from_other_module() {
740 check_no_kw(
741 r"
742struct A {}
743mod m {
744 pub trait Trait { fn the_method(&self); }
745}
746use m::Trait;
747impl Trait for A {}
748fn foo(a: A) { a.$0 }
749",
750 expect![[r#"
751 me the_method() (as Trait) fn(&self)
752 "#]],
753 );
754 }
755
756 #[test]
757 fn test_no_non_self_method() {
758 check_no_kw(
759 r#"
760struct A {}
761impl A {
762 fn the_method() {}
763}
764fn foo(a: A) {
765 a.$0
766}
767"#,
768 expect![[r#""#]],
769 );
770 }
771
772 #[test]
773 fn test_tuple_field_completion() {
774 check_no_kw(
775 r#"
776fn foo() {
777 let b = (0, 3.14);
778 b.$0
779}
780"#,
781 expect![[r#"
782 fd 0 i32
783 fd 1 f64
784 "#]],
785 );
786 }
787
788 #[test]
789 fn test_tuple_struct_field_completion() {
790 check_no_kw(
791 r#"
792struct S(i32, f64);
793fn foo() {
794 let b = S(0, 3.14);
795 b.$0
796}
797"#,
798 expect![[r#"
799 fd 0 i32
800 fd 1 f64
801 "#]],
802 );
803 }
804
805 #[test]
806 fn test_tuple_field_inference() {
807 check_no_kw(
808 r#"
809pub struct S;
810impl S { pub fn blah(&self) {} }
811
812struct T(S);
813
814impl T {
815 fn foo(&self) {
816 self.0.$0
817 }
818}
819"#,
820 expect![[r#"
821 me blah() fn(&self)
822 "#]],
823 );
824 }
825
826 #[test]
827 fn test_field_no_same_name() {
828 check_no_kw(
829 r#"
830//- minicore: deref
831struct A { field: u8 }
832struct B { field: u16, another: u32 }
833impl core::ops::Deref for A {
834 type Target = B;
835 fn deref(&self) -> &Self::Target { loop {} }
836}
837fn test(a: A) {
838 a.$0
839}
840"#,
841 expect![[r#"
842 fd another u32
843 fd field u8
844 me deref() (use core::ops::Deref) fn(&self) -> &<Self as Deref>::Target
845 "#]],
846 );
847 }
848
849 #[test]
850 fn test_tuple_field_no_same_index() {
851 check_no_kw(
852 r#"
853//- minicore: deref
854struct A(u8);
855struct B(u16, u32);
856impl core::ops::Deref for A {
857 type Target = B;
858 fn deref(&self) -> &Self::Target { loop {} }
859}
860fn test(a: A) {
861 a.$0
862}
863"#,
864 expect![[r#"
865 fd 0 u8
866 fd 1 u32
867 me deref() (use core::ops::Deref) fn(&self) -> &<Self as Deref>::Target
868 "#]],
869 );
870 }
871
872 #[test]
873 fn test_tuple_struct_deref_to_tuple_no_same_index() {
874 check_no_kw(
875 r#"
876//- minicore: deref
877struct A(u8);
878impl core::ops::Deref for A {
879 type Target = (u16, u32);
880 fn deref(&self) -> &Self::Target { loop {} }
881}
882fn test(a: A) {
883 a.$0
884}
885"#,
886 expect![[r#"
887 fd 0 u8
888 fd 1 u32
889 me deref() (use core::ops::Deref) fn(&self) -> &<Self as Deref>::Target
890 "#]],
891 );
892 }
893
894 #[test]
895 fn test_inherent_method_no_same_name() {
896 check_no_kw(
897 r#"
898//- minicore: deref
899struct A {}
900struct B {}
901impl core::ops::Deref for A {
902 type Target = B;
903 fn deref(&self) -> &Self::Target { loop {} }
904}
905trait Foo { fn foo(&self) -> u32 {} }
906impl Foo for A {}
907impl Foo for B {}
908impl A { fn foo(&self) -> u8 {} }
909impl B { fn foo(&self) -> u16 {} }
910fn test(a: A) {
911 a.$0
912}
913"#,
914 expect![[r#"
915 me deref() (use core::ops::Deref) fn(&self) -> &<Self as Deref>::Target
916 me foo() fn(&self) -> u8
917 me foo() (as Foo) fn(&self) -> u32
918 "#]],
919 );
920
921 check_no_kw(
922 r#"
923//- minicore: deref
924//- /dep.rs crate:dep
925pub struct A {}
926pub struct B {}
927pub struct C {}
928pub struct D {}
929pub struct E {}
930pub struct F {}
931impl core::ops::Deref for A {
932 type Target = B;
933 fn deref(&self) -> &Self::Target { loop {} }
934}
935impl core::ops::Deref for B {
936 type Target = C;
937 fn deref(&self) -> &Self::Target { loop {} }
938}
939impl core::ops::Deref for C {
940 type Target = D;
941 fn deref(&self) -> &Self::Target { loop {} }
942}
943impl core::ops::Deref for D {
944 type Target = E;
945 fn deref(&self) -> &Self::Target { loop {} }
946}
947impl core::ops::Deref for E {
948 type Target = F;
949 fn deref(&self) -> &Self::Target { loop {} }
950}
951pub trait Foo { fn foo(&self) -> u32 {} }
952impl Foo for A {}
953impl Foo for B {}
954impl A { fn foo(&self) -> u8 {} }
955impl B { pub fn foo(&self) -> u16 {} }
956impl C { fn foo(&self) -> i8 {} }
957impl D { fn foo(&self) -> i16 {} }
958impl E { pub fn foo(&self) -> i32 {} }
959impl F { pub fn foo(&self) -> f32 {} }
960//- /main.rs crate:main deps:dep
961use dep::*;
962fn test(a: A) {
963 a.$0
964}
965"#,
966 expect![[r#"
967 me deref() (use core::ops::Deref) fn(&self) -> &<Self as Deref>::Target
968 me foo() fn(&self) -> u16
969 me foo() (as Foo) fn(&self) -> u32
970 "#]],
971 );
972 }
973
974 #[test]
975 fn test_completion_works_in_consts() {
976 check_no_kw(
977 r#"
978struct A { the_field: u32 }
979const X: u32 = {
980 A { the_field: 92 }.$0
981};
982"#,
983 expect![[r#"
984 fd the_field u32
985 "#]],
986 );
987 }
988
989 #[test]
990 fn works_in_simple_macro_1() {
991 check_no_kw(
992 r#"
993macro_rules! m { ($e:expr) => { $e } }
994struct A { the_field: u32 }
995fn foo(a: A) {
996 m!(a.x$0)
997}
998"#,
999 expect![[r#"
1000 fd the_field u32
1001 "#]],
1002 );
1003 }
1004
1005 #[test]
1006 fn works_in_simple_macro_2() {
1007 check_no_kw(
1009 r#"
1010macro_rules! m { ($e:expr) => { $e } }
1011struct A { the_field: u32 }
1012fn foo(a: A) {
1013 m!(a.$0)
1014}
1015"#,
1016 expect![[r#"
1017 fd the_field u32
1018 "#]],
1019 );
1020 }
1021
1022 #[test]
1023 fn works_in_simple_macro_recursive_1() {
1024 check_no_kw(
1025 r#"
1026macro_rules! m { ($e:expr) => { $e } }
1027struct A { the_field: u32 }
1028fn foo(a: A) {
1029 m!(m!(m!(a.x$0)))
1030}
1031"#,
1032 expect![[r#"
1033 fd the_field u32
1034 "#]],
1035 );
1036 }
1037
1038 #[test]
1039 fn macro_expansion_resilient() {
1040 check_no_kw(
1041 r#"
1042macro_rules! d {
1043 () => {};
1044 ($val:expr) => {
1045 match $val { tmp => { tmp } }
1046 };
1047 // Trailing comma with single argument is ignored
1048 ($val:expr,) => { $crate::d!($val) };
1049 ($($val:expr),+ $(,)?) => {
1050 ($($crate::d!($val)),+,)
1051 };
1052}
1053struct A { the_field: u32 }
1054fn foo(a: A) {
1055 d!(a.$0)
1056}
1057"#,
1058 expect![[r#"
1059 fd the_field u32
1060 "#]],
1061 );
1062 }
1063
1064 #[test]
1065 fn test_method_completion_issue_3547() {
1066 check_no_kw(
1067 r#"
1068struct HashSet<T> {}
1069impl<T> HashSet<T> {
1070 pub fn the_method(&self) {}
1071}
1072fn foo() {
1073 let s: HashSet<_>;
1074 s.$0
1075}
1076"#,
1077 expect![[r#"
1078 me the_method() fn(&self)
1079 "#]],
1080 );
1081 }
1082
1083 #[test]
1084 fn completes_method_call_when_receiver_is_a_macro_call() {
1085 check_no_kw(
1086 r#"
1087struct S;
1088impl S { fn foo(&self) {} }
1089macro_rules! make_s { () => { S }; }
1090fn main() { make_s!().f$0; }
1091"#,
1092 expect![[r#"
1093 me foo() fn(&self)
1094 "#]],
1095 )
1096 }
1097
1098 #[test]
1099 fn completes_after_macro_call_in_submodule() {
1100 check_no_kw(
1101 r#"
1102macro_rules! empty {
1103 () => {};
1104}
1105
1106mod foo {
1107 #[derive(Debug, Default)]
1108 struct Template2 {}
1109
1110 impl Template2 {
1111 fn private(&self) {}
1112 }
1113 fn baz() {
1114 let goo: Template2 = Template2 {};
1115 empty!();
1116 goo.$0
1117 }
1118}
1119 "#,
1120 expect![[r#"
1121 me private() fn(&self)
1122 "#]],
1123 );
1124 }
1125
1126 #[test]
1127 fn issue_8931() {
1128 check_no_kw(
1129 r#"
1130//- minicore: fn
1131struct S;
1132
1133struct Foo;
1134impl Foo {
1135 fn foo(&self) -> &[u8] { loop {} }
1136}
1137
1138impl S {
1139 fn indented(&mut self, f: impl FnOnce(&mut Self)) {
1140 }
1141
1142 fn f(&mut self, v: Foo) {
1143 self.indented(|this| v.$0)
1144 }
1145}
1146 "#,
1147 expect![[r#"
1148 me foo() fn(&self) -> &[u8]
1149 "#]],
1150 );
1151 }
1152
1153 #[test]
1154 fn completes_bare_fields_and_methods_in_methods() {
1155 check_no_kw(
1156 r#"
1157struct Foo { field: i32 }
1158
1159impl Foo { fn foo(&self) { $0 } }"#,
1160 expect![[r#"
1161 fd self.field i32
1162 me self.foo() fn(&self)
1163 lc self &Foo
1164 sp Self Foo
1165 st Foo Foo
1166 bt u32 u32
1167 "#]],
1168 );
1169 check_no_kw(
1170 r#"
1171struct Foo(i32);
1172
1173impl Foo { fn foo(&mut self) { $0 } }"#,
1174 expect![[r#"
1175 fd self.0 i32
1176 me self.foo() fn(&mut self)
1177 lc self &mut Foo
1178 sp Self Foo
1179 st Foo Foo
1180 bt u32 u32
1181 "#]],
1182 );
1183 }
1184
1185 #[test]
1186 fn completes_bare_fields_and_methods_in_this_closure() {
1187 check_no_kw(
1188 r#"
1189//- minicore: fn
1190struct Foo { field: i32 }
1191
1192impl Foo { fn foo(&mut self) { let _: fn(&mut Self) = |this| { $0 } } }"#,
1193 expect![[r#"
1194 fd this.field i32
1195 me this.foo() fn(&mut self)
1196 lc self &mut Foo
1197 lc this &mut Foo
1198 md core::
1199 sp Self Foo
1200 st Foo Foo
1201 tt Fn
1202 tt FnMut
1203 tt FnOnce
1204 bt u32 u32
1205 "#]],
1206 );
1207 }
1208
1209 #[test]
1210 fn completes_bare_fields_and_methods_in_other_closure() {
1211 check_no_kw(
1212 r#"
1213//- minicore: fn
1214struct Foo { field: i32 }
1215
1216impl Foo { fn foo(&self) { let _: fn(&Self) = |foo| { $0 } } }"#,
1217 expect![[r#"
1218 fd self.field i32
1219 me self.foo() fn(&self)
1220 lc foo &Foo
1221 lc self &Foo
1222 md core::
1223 sp Self Foo
1224 st Foo Foo
1225 tt Fn
1226 tt FnMut
1227 tt FnOnce
1228 bt u32 u32
1229 "#]],
1230 );
1231
1232 check_no_kw(
1233 r#"
1234//- minicore: fn
1235struct Foo { field: i32 }
1236
1237impl Foo { fn foo(&self) { let _: fn(&Self) = || { $0 } } }"#,
1238 expect![[r#"
1239 fd self.field i32
1240 me self.foo() fn(&self)
1241 lc self &Foo
1242 md core::
1243 sp Self Foo
1244 st Foo Foo
1245 tt Fn
1246 tt FnMut
1247 tt FnOnce
1248 bt u32 u32
1249 "#]],
1250 );
1251
1252 check_no_kw(
1253 r#"
1254//- minicore: fn
1255struct Foo { field: i32 }
1256
1257impl Foo { fn foo(&self) { let _: fn(&Self, &Self) = |foo, other| { $0 } } }"#,
1258 expect![[r#"
1259 fd self.field i32
1260 me self.foo() fn(&self)
1261 lc foo &Foo
1262 lc other &Foo
1263 lc self &Foo
1264 md core::
1265 sp Self Foo
1266 st Foo Foo
1267 tt Fn
1268 tt FnMut
1269 tt FnOnce
1270 bt u32 u32
1271 "#]],
1272 );
1273 }
1274
1275 #[test]
1276 fn macro_completion_after_dot() {
1277 check_no_kw(
1278 r#"
1279macro_rules! m {
1280 ($e:expr) => { $e };
1281}
1282
1283struct Completable;
1284
1285impl Completable {
1286 fn method(&self) {}
1287}
1288
1289fn f() {
1290 let c = Completable;
1291 m!(c.$0);
1292}
1293 "#,
1294 expect![[r#"
1295 me method() fn(&self)
1296 "#]],
1297 );
1298 }
1299
1300 #[test]
1301 fn completes_method_call_when_receiver_type_has_errors_issue_10297() {
1302 check_no_kw(
1303 r#"
1304//- minicore: iterator, sized
1305struct Vec<T>;
1306impl<T> IntoIterator for Vec<T> {
1307 type Item = ();
1308 type IntoIter = ();
1309 fn into_iter(self);
1310}
1311fn main() {
1312 let x: Vec<_>;
1313 x.$0;
1314}
1315"#,
1316 expect![[r#"
1317 me into_iter() (as IntoIterator) fn(self) -> <Self as IntoIterator>::IntoIter
1318 "#]],
1319 )
1320 }
1321
1322 #[test]
1323 fn postfix_drop_completion() {
1324 cov_mark::check!(postfix_drop_completion);
1325 check_edit(
1326 "drop",
1327 r#"
1328//- minicore: drop
1329struct Vec<T>(T);
1330impl<T> Drop for Vec<T> {
1331 fn drop(&mut self) {}
1332}
1333fn main() {
1334 let x = Vec(0u32)
1335 x.$0;
1336}
1337"#,
1338 r"
1339struct Vec<T>(T);
1340impl<T> Drop for Vec<T> {
1341 fn drop(&mut self) {}
1342}
1343fn main() {
1344 let x = Vec(0u32)
1345 drop($0x);
1346}
1347",
1348 )
1349 }
1350
1351 #[test]
1352 fn issue_12484() {
1353 check_no_kw(
1354 r#"
1355//- minicore: sized
1356trait SizeUser {
1357 type Size;
1358}
1359trait Closure: SizeUser {}
1360trait Encrypt: SizeUser {
1361 fn encrypt(self, _: impl Closure<Size = Self::Size>);
1362}
1363fn test(thing: impl Encrypt) {
1364 thing.$0;
1365}
1366 "#,
1367 expect![[r#"
1368 me encrypt(…) (as Encrypt) fn(self, impl Closure<Size = <Self as SizeUser>::Size>)
1369 "#]],
1370 )
1371 }
1372
1373 #[test]
1374 fn only_consider_same_type_once() {
1375 check_no_kw(
1376 r#"
1377//- minicore: deref
1378struct A(u8);
1379struct B(u16);
1380impl core::ops::Deref for A {
1381 type Target = B;
1382 fn deref(&self) -> &Self::Target { loop {} }
1383}
1384impl core::ops::Deref for B {
1385 type Target = A;
1386 fn deref(&self) -> &Self::Target { loop {} }
1387}
1388fn test(a: A) {
1389 a.$0
1390}
1391"#,
1392 expect![[r#"
1393 fd 0 u8
1394 me deref() (use core::ops::Deref) fn(&self) -> &<Self as Deref>::Target
1395 "#]],
1396 );
1397 }
1398
1399 #[test]
1400 fn no_inference_var_in_completion() {
1401 check_no_kw(
1402 r#"
1403struct S<T>(T);
1404fn test(s: S<Unknown>) {
1405 s.$0
1406}
1407"#,
1408 expect![[r#"
1409 fd 0 {unknown}
1410 "#]],
1411 );
1412 }
1413
1414 #[test]
1415 fn assoc_impl_1() {
1416 check_no_kw(
1417 r#"
1418//- minicore: deref
1419fn main() {
1420 let foo: Foo<&u8> = Foo::new(&42_u8);
1421 foo.$0
1422}
1423
1424trait Bar {
1425 fn bar(&self);
1426}
1427
1428impl Bar for u8 {
1429 fn bar(&self) {}
1430}
1431
1432struct Foo<F> {
1433 foo: F,
1434}
1435
1436impl<F> Foo<F> {
1437 fn new(foo: F) -> Foo<F> {
1438 Foo { foo }
1439 }
1440}
1441
1442impl<F: core::ops::Deref<Target = impl Bar>> Foo<F> {
1443 fn foobar(&self) {
1444 self.foo.deref().bar()
1445 }
1446}
1447"#,
1448 expect![[r#"
1449 fd foo &u8
1450 me foobar() fn(&self)
1451 "#]],
1452 );
1453 }
1454
1455 #[test]
1456 fn assoc_impl_2() {
1457 check_no_kw(
1458 r#"
1459//- minicore: deref
1460fn main() {
1461 let foo: Foo<&u8> = Foo::new(&42_u8);
1462 foo.$0
1463}
1464
1465trait Bar {
1466 fn bar(&self);
1467}
1468
1469struct Foo<F> {
1470 foo: F,
1471}
1472
1473impl<F> Foo<F> {
1474 fn new(foo: F) -> Foo<F> {
1475 Foo { foo }
1476 }
1477}
1478
1479impl<B: Bar, F: core::ops::Deref<Target = B>> Foo<F> {
1480 fn foobar(&self) {
1481 self.foo.deref().bar()
1482 }
1483}
1484"#,
1485 expect![[r#"
1486 fd foo &u8
1487 "#]],
1488 );
1489 }
1490
1491 #[test]
1492 fn test_struct_function_field_completion() {
1493 check_no_kw(
1494 r#"
1495struct S { va_field: u32, fn_field: fn() }
1496fn foo() { S { va_field: 0, fn_field: || {} }.fi$0() }
1497"#,
1498 expect![[r#"
1499 fd fn_field fn()
1500 "#]],
1501 );
1502
1503 check_edit(
1504 "fn_field",
1505 r#"
1506struct S { va_field: u32, fn_field: fn() }
1507fn foo() { S { va_field: 0, fn_field: || {} }.fi$0() }
1508"#,
1509 r#"
1510struct S { va_field: u32, fn_field: fn() }
1511fn foo() { (S { va_field: 0, fn_field: || {} }.fn_field)() }
1512"#,
1513 );
1514 }
1515
1516 #[test]
1517 fn test_tuple_function_field_completion() {
1518 check_no_kw(
1519 r#"
1520struct B(u32, fn())
1521fn foo() {
1522 let b = B(0, || {});
1523 b.$0()
1524}
1525"#,
1526 expect![[r#"
1527 fd 1 fn()
1528 "#]],
1529 );
1530
1531 check_edit(
1532 "1",
1533 r#"
1534struct B(u32, fn())
1535fn foo() {
1536 let b = B(0, || {});
1537 b.$0()
1538}
1539"#,
1540 r#"
1541struct B(u32, fn())
1542fn foo() {
1543 let b = B(0, || {});
1544 (b.1)()
1545}
1546"#,
1547 )
1548 }
1549
1550 #[test]
1551 fn test_fn_field_dot_access_method_has_parens_false() {
1552 check_no_kw(
1553 r#"
1554struct Foo { baz: fn() }
1555impl Foo {
1556 fn bar<T>(self, t: T) -> T { t }
1557}
1558
1559fn baz() {
1560 let foo = Foo{ baz: || {} };
1561 foo.ba$0;
1562}
1563"#,
1564 expect![[r#"
1565 fd baz fn()
1566 me bar(…) fn(self, T) -> T
1567 "#]],
1568 );
1569
1570 check_edit(
1571 "baz",
1572 r#"
1573struct Foo { baz: fn() }
1574impl Foo {
1575 fn bar<T>(self, t: T) -> T { t }
1576}
1577
1578fn baz() {
1579 let foo = Foo{ baz: || {} };
1580 foo.ba$0;
1581}
1582"#,
1583 r#"
1584struct Foo { baz: fn() }
1585impl Foo {
1586 fn bar<T>(self, t: T) -> T { t }
1587}
1588
1589fn baz() {
1590 let foo = Foo{ baz: || {} };
1591 (foo.baz)();
1592}
1593"#,
1594 );
1595
1596 check_edit(
1597 "bar",
1598 r#"
1599struct Foo { baz: fn() }
1600impl Foo {
1601 fn bar<T>(self, t: T) -> T { t }
1602}
1603
1604fn baz() {
1605 let foo = Foo{ baz: || {} };
1606 foo.ba$0;
1607}
1608"#,
1609 r#"
1610struct Foo { baz: fn() }
1611impl Foo {
1612 fn bar<T>(self, t: T) -> T { t }
1613}
1614
1615fn baz() {
1616 let foo = Foo{ baz: || {} };
1617 foo.bar(${1:t})$0;
1618}
1619"#,
1620 );
1621 }
1622
1623 #[test]
1624 fn skip_iter() {
1625 check_no_kw(
1626 r#"
1627 //- minicore: iterator, clone, builtin_impls
1628 fn foo() {
1629 [].$0
1630 }
1631 "#,
1632 expect![[r#"
1633 me clone() (as Clone) fn(&self) -> Self
1634 me fmt(…) (use core::fmt::Debug) fn(&self, &mut Formatter<'_>) -> Result<(), Error>
1635 me into_iter() (as IntoIterator) fn(self) -> <Self as IntoIterator>::IntoIter
1636 "#]],
1637 );
1638 check_no_kw(
1639 r#"
1640//- minicore: iterator
1641struct MyIntoIter;
1642impl IntoIterator for MyIntoIter {
1643 type Item = ();
1644 type IntoIter = MyIterator;
1645 fn into_iter(self) -> Self::IntoIter {
1646 MyIterator
1647 }
1648}
1649
1650struct MyIterator;
1651impl Iterator for MyIterator {
1652 type Item = ();
1653 fn next(&mut self) -> Self::Item {}
1654}
1655
1656fn foo() {
1657 MyIntoIter.$0
1658}
1659"#,
1660 expect![[r#"
1661 me into_iter() (as IntoIterator) fn(self) -> <Self as IntoIterator>::IntoIter
1662 me into_iter().by_ref() (as Iterator) fn(&mut self) -> &mut Self
1663 me into_iter().into_iter() (as IntoIterator) fn(self) -> <Self as IntoIterator>::IntoIter
1664 me into_iter().next() (as Iterator) fn(&mut self) -> Option<<Self as Iterator>::Item>
1665 me into_iter().nth(…) (as Iterator) fn(&mut self, usize) -> Option<<Self as Iterator>::Item>
1666 "#]],
1667 );
1668 check_no_kw(
1669 r#"
1670//- minicore: iterator, deref
1671struct Foo;
1672impl Foo { fn iter(&self) -> Iter { Iter } }
1673impl IntoIterator for &Foo {
1674 type Item = ();
1675 type IntoIter = Iter;
1676 fn into_iter(self) -> Self::IntoIter { Iter }
1677}
1678struct Ref;
1679impl core::ops::Deref for Ref {
1680 type Target = Foo;
1681 fn deref(&self) -> &Self::Target { &Foo }
1682}
1683struct Iter;
1684impl Iterator for Iter {
1685 type Item = ();
1686 fn next(&mut self) -> Option<Self::Item> { None }
1687}
1688fn foo() {
1689 Ref.$0
1690}
1691"#,
1692 expect![[r#"
1693 me deref() (use core::ops::Deref) fn(&self) -> &<Self as Deref>::Target
1694 me into_iter() (as IntoIterator) fn(self) -> <Self as IntoIterator>::IntoIter
1695 me iter() fn(&self) -> Iter
1696 me iter().by_ref() (as Iterator) fn(&mut self) -> &mut Self
1697 me iter().into_iter() (as IntoIterator) fn(self) -> <Self as IntoIterator>::IntoIter
1698 me iter().next() (as Iterator) fn(&mut self) -> Option<<Self as Iterator>::Item>
1699 me iter().nth(…) (as Iterator) fn(&mut self, usize) -> Option<<Self as Iterator>::Item>
1700 "#]],
1701 );
1702 }
1703
1704 #[test]
1705 fn skip_await() {
1706 check_no_kw(
1707 r#"
1708//- minicore: future
1709struct Foo;
1710impl Foo {
1711 fn foo(self) {}
1712}
1713
1714async fn foo() -> Foo { Foo }
1715
1716async fn bar() {
1717 foo().$0
1718}
1719"#,
1720 expect![[r#"
1721 me await.foo() fn(self)
1722 me into_future() (use core::future::IntoFuture) fn(self) -> <Self as IntoFuture>::IntoFuture
1723"#]],
1724 );
1725 check_edit(
1726 "foo",
1727 r#"
1728//- minicore: future
1729struct Foo;
1730impl Foo {
1731 fn foo(self) {}
1732}
1733
1734async fn foo() -> Foo { Foo }
1735
1736async fn bar() {
1737 foo().$0
1738}
1739"#,
1740 r#"
1741struct Foo;
1742impl Foo {
1743 fn foo(self) {}
1744}
1745
1746async fn foo() -> Foo { Foo }
1747
1748async fn bar() {
1749 foo().await.foo();$0
1750}
1751"#,
1752 );
1753 }
1754
1755 #[test]
1756 fn receiver_without_deref_impl_completion() {
1757 check_no_kw(
1758 r#"
1759//- minicore: receiver
1760#![feature(arbitrary_self_types)]
1761
1762use core::ops::Receiver;
1763
1764struct Foo;
1765
1766impl Foo {
1767 fn foo(self: Bar) {}
1768}
1769
1770struct Bar;
1771
1772impl Receiver for Bar {
1773 type Target = Foo;
1774}
1775
1776fn main() {
1777 let bar = Bar;
1778 bar.$0
1779}
1780"#,
1781 expect![[r#"
1782 me foo() fn(self: Bar)
1783 "#]],
1784 );
1785 }
1786
1787 #[test]
1788 fn no_iter_suggestion_on_iterator() {
1789 check_no_kw(
1790 r#"
1791//- minicore: iterator
1792struct MyIter;
1793impl Iterator for MyIter {
1794 type Item = ();
1795 fn next(&mut self) -> Option<Self::Item> { None }
1796}
1797
1798fn main() {
1799 MyIter.$0
1800}
1801"#,
1802 expect![[r#"
1803 me by_ref() (as Iterator) fn(&mut self) -> &mut Self
1804 me into_iter() (as IntoIterator) fn(self) -> <Self as IntoIterator>::IntoIter
1805 me next() (as Iterator) fn(&mut self) -> Option<<Self as Iterator>::Item>
1806 me nth(…) (as Iterator) fn(&mut self, usize) -> Option<<Self as Iterator>::Item>
1807 "#]],
1808 );
1809 }
1810}