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