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