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