]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/completions/trait_impl.rs
Remove extra whitespace
[rust.git] / crates / ide_completion / src / completions / trait_impl.rs
1 //! Completion for associated items in a trait implementation.
2 //!
3 //! This module adds the completion items related to implementing associated
4 //! items within a `impl Trait for Struct` block. The current context node
5 //! must be within either a `FN`, `TYPE_ALIAS`, or `CONST` node
6 //! and an direct child of an `IMPL`.
7 //!
8 //! # Examples
9 //!
10 //! Considering the following trait `impl`:
11 //!
12 //! ```ignore
13 //! trait SomeTrait {
14 //!     fn foo();
15 //! }
16 //!
17 //! impl SomeTrait for () {
18 //!     fn f$0
19 //! }
20 //! ```
21 //!
22 //! may result in the completion of the following method:
23 //!
24 //! ```ignore
25 //! # trait SomeTrait {
26 //! #    fn foo();
27 //! # }
28 //!
29 //! impl SomeTrait for () {
30 //!     fn foo() {}$0
31 //! }
32 //! ```
33
34 use hir::{self, HasAttrs, HasSource};
35 use ide_db::{path_transform::PathTransform, traits::get_missing_assoc_items, SymbolKind};
36 use syntax::{
37     ast::{self, edit},
38     display::function_declaration,
39     AstNode, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, T,
40 };
41 use text_edit::TextEdit;
42
43 use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions};
44
45 #[derive(Debug, PartialEq, Eq)]
46 enum ImplCompletionKind {
47     All,
48     Fn,
49     TypeAlias,
50     Const,
51 }
52
53 pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) {
54     if let Some((kind, trigger, impl_def)) = completion_match(ctx.token.clone()) {
55         get_missing_assoc_items(&ctx.sema, &impl_def).into_iter().for_each(|item| match item {
56             hir::AssocItem::Function(fn_item)
57                 if kind == ImplCompletionKind::All || kind == ImplCompletionKind::Fn =>
58             {
59                 if let Some(impl_def) = ctx.sema.to_def(&impl_def) {
60                     add_function_impl(&trigger, acc, ctx, fn_item, impl_def)
61                 }
62             }
63             hir::AssocItem::TypeAlias(type_item)
64                 if kind == ImplCompletionKind::All || kind == ImplCompletionKind::TypeAlias =>
65             {
66                 add_type_alias_impl(&trigger, acc, ctx, type_item)
67             }
68             hir::AssocItem::Const(const_item)
69                 if kind == ImplCompletionKind::All || kind == ImplCompletionKind::Const =>
70             {
71                 if let Some(impl_def) = ctx.sema.to_def(&impl_def) {
72                     add_const_impl(&trigger, acc, ctx, const_item, impl_def)
73                 }
74             }
75             _ => {}
76         });
77     }
78 }
79
80 fn completion_match(mut token: SyntaxToken) -> Option<(ImplCompletionKind, SyntaxNode, ast::Impl)> {
81     // For keyword without name like `impl .. { fn $0 }`, the current position is inside
82     // the whitespace token, which is outside `FN` syntax node.
83     // We need to follow the previous token in this case.
84     if token.kind() == SyntaxKind::WHITESPACE {
85         token = token.prev_token()?;
86     }
87
88     let parent_kind = token.parent().map_or(SyntaxKind::EOF, |it| it.kind());
89     let impl_item_offset = match token.kind() {
90         // `impl .. { const $0 }`
91         // ERROR      0
92         //   CONST_KW <- *
93         T![const] => 0,
94         // `impl .. { fn/type $0 }`
95         // FN/TYPE_ALIAS  0
96         //   FN_KW        <- *
97         T![fn] | T![type] => 0,
98         // `impl .. { fn/type/const foo$0 }`
99         // FN/TYPE_ALIAS/CONST  1
100         //  NAME                0
101         //    IDENT             <- *
102         SyntaxKind::IDENT if parent_kind == SyntaxKind::NAME => 1,
103         // `impl .. { foo$0 }`
104         // MACRO_CALL       3
105         //  PATH            2
106         //    PATH_SEGMENT  1
107         //      NAME_REF    0
108         //        IDENT     <- *
109         SyntaxKind::IDENT if parent_kind == SyntaxKind::NAME_REF => 3,
110         _ => return None,
111     };
112
113     let impl_item = token.ancestors().nth(impl_item_offset)?;
114     // Must directly belong to an impl block.
115     // IMPL
116     //   ASSOC_ITEM_LIST
117     //     <item>
118     let impl_def = ast::Impl::cast(impl_item.parent()?.parent()?)?;
119     let kind = match impl_item.kind() {
120         // `impl ... { const $0 fn/type/const }`
121         _ if token.kind() == T![const] => ImplCompletionKind::Const,
122         SyntaxKind::CONST | SyntaxKind::ERROR => ImplCompletionKind::Const,
123         SyntaxKind::TYPE_ALIAS => ImplCompletionKind::TypeAlias,
124         SyntaxKind::FN => ImplCompletionKind::Fn,
125         SyntaxKind::MACRO_CALL => ImplCompletionKind::All,
126         _ => return None,
127     };
128     Some((kind, impl_item, impl_def))
129 }
130
131 fn add_function_impl(
132     fn_def_node: &SyntaxNode,
133     acc: &mut Completions,
134     ctx: &CompletionContext,
135     func: hir::Function,
136     impl_def: hir::Impl,
137 ) {
138     let fn_name = func.name(ctx.db).to_string();
139
140     let label = if func.assoc_fn_params(ctx.db).is_empty() {
141         format!("fn {}()", fn_name)
142     } else {
143         format!("fn {}(..)", fn_name)
144     };
145
146     let mut item = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label);
147     item.lookup_by(fn_name).set_documentation(func.docs(ctx.db));
148
149     let completion_kind = if func.self_param(ctx.db).is_some() {
150         CompletionItemKind::Method
151     } else {
152         CompletionItemKind::SymbolKind(SymbolKind::Function)
153     };
154     let range = replacement_range(ctx, fn_def_node);
155
156     if let Some(source) = func.source(ctx.db) {
157         let assoc_item = ast::AssocItem::Fn(source.value);
158         if let Some(transformed_item) = get_transformed_assoc_item(ctx, assoc_item, impl_def) {
159             let transformed_fn = match transformed_item {
160                 ast::AssocItem::Fn(func) => func,
161                 _ => unreachable!(),
162             };
163
164             let function_decl = function_declaration(&transformed_fn);
165             match ctx.config.snippet_cap {
166                 Some(cap) => {
167                     let snippet = format!("{} {{\n    $0\n}}", function_decl);
168                     item.snippet_edit(cap, TextEdit::replace(range, snippet));
169                 }
170                 None => {
171                     let header = format!("{} {{", function_decl);
172                     item.text_edit(TextEdit::replace(range, header));
173                 }
174             };
175             item.kind(completion_kind);
176             item.add_to(acc);
177         }
178     }
179 }
180
181 /// Transform a relevant associated item to inline generics from the impl, remove attrs and docs, etc.
182 fn get_transformed_assoc_item(
183     ctx: &CompletionContext,
184     assoc_item: ast::AssocItem,
185     impl_def: hir::Impl,
186 ) -> Option<ast::AssocItem> {
187     let assoc_item = assoc_item.clone_for_update();
188     let trait_ = impl_def.trait_(ctx.db)?;
189     let source_scope = &ctx.sema.scope_for_def(trait_);
190     let target_scope = &ctx.sema.scope(impl_def.source(ctx.db)?.syntax().value);
191     let transform = PathTransform {
192         subst: (trait_, impl_def.source(ctx.db)?.value),
193         source_scope,
194         target_scope,
195     };
196
197     transform.apply(assoc_item.clone());
198     Some(match assoc_item {
199         ast::AssocItem::Fn(func) => ast::AssocItem::Fn(edit::remove_attrs_and_docs(&func)),
200         _ => assoc_item,
201     })
202 }
203
204 fn add_type_alias_impl(
205     type_def_node: &SyntaxNode,
206     acc: &mut Completions,
207     ctx: &CompletionContext,
208     type_alias: hir::TypeAlias,
209 ) {
210     let alias_name = type_alias.name(ctx.db).to_string();
211
212     let snippet = format!("type {} = ", alias_name);
213
214     let range = replacement_range(ctx, type_def_node);
215     let mut item = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone());
216     item.text_edit(TextEdit::replace(range, snippet))
217         .lookup_by(alias_name)
218         .kind(SymbolKind::TypeAlias)
219         .set_documentation(type_alias.docs(ctx.db));
220     item.add_to(acc);
221 }
222
223 fn add_const_impl(
224     const_def_node: &SyntaxNode,
225     acc: &mut Completions,
226     ctx: &CompletionContext,
227     const_: hir::Const,
228     impl_def: hir::Impl,
229 ) {
230     let const_name = const_.name(ctx.db).map(|n| n.to_string());
231
232     if let Some(const_name) = const_name {
233         if let Some(source) = const_.source(ctx.db) {
234             let assoc_item = ast::AssocItem::Const(source.value);
235             if let Some(transformed_item) = get_transformed_assoc_item(ctx, assoc_item, impl_def) {
236                 let transformed_const = match transformed_item {
237                     ast::AssocItem::Const(const_) => const_,
238                     _ => unreachable!(),
239                 };
240
241                 let snippet = make_const_compl_syntax(&transformed_const);
242
243                 let range = replacement_range(ctx, const_def_node);
244                 let mut item =
245                     CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone());
246                 item.text_edit(TextEdit::replace(range, snippet))
247                     .lookup_by(const_name)
248                     .kind(SymbolKind::Const)
249                     .set_documentation(const_.docs(ctx.db));
250                 item.add_to(acc);
251             }
252         }
253     }
254 }
255
256 fn make_const_compl_syntax(const_: &ast::Const) -> String {
257     let const_ = edit::remove_attrs_and_docs(const_);
258
259     let const_start = const_.syntax().text_range().start();
260     let const_end = const_.syntax().text_range().end();
261
262     let start =
263         const_.syntax().first_child_or_token().map_or(const_start, |f| f.text_range().start());
264
265     let end = const_
266         .syntax()
267         .children_with_tokens()
268         .find(|s| s.kind() == T![;] || s.kind() == T![=])
269         .map_or(const_end, |f| f.text_range().start());
270
271     let len = end - start;
272     let range = TextRange::new(0.into(), len);
273
274     let syntax = const_.syntax().text().slice(range).to_string();
275
276     format!("{} = ", syntax.trim_end())
277 }
278
279 fn replacement_range(ctx: &CompletionContext, item: &SyntaxNode) -> TextRange {
280     let first_child = item
281         .children_with_tokens()
282         .find(|child| {
283             !matches!(child.kind(), SyntaxKind::COMMENT | SyntaxKind::WHITESPACE | SyntaxKind::ATTR)
284         })
285         .unwrap_or_else(|| SyntaxElement::Node(item.clone()));
286
287     TextRange::new(first_child.text_range().start(), ctx.source_range().end())
288 }
289
290 #[cfg(test)]
291 mod tests {
292     use expect_test::{expect, Expect};
293
294     use crate::{
295         tests::{check_edit, filtered_completion_list},
296         CompletionKind,
297     };
298
299     fn check(ra_fixture: &str, expect: Expect) {
300         let actual = filtered_completion_list(ra_fixture, CompletionKind::Magic);
301         expect.assert_eq(&actual)
302     }
303
304     #[test]
305     fn name_ref_function_type_const() {
306         check(
307             r#"
308 trait Test {
309     type TestType;
310     const TEST_CONST: u16;
311     fn test();
312 }
313 struct T;
314
315 impl Test for T {
316     t$0
317 }
318 "#,
319             expect![["
320 ta type TestType = \n\
321 ct const TEST_CONST: u16 = \n\
322 fn fn test()
323 "]],
324         );
325     }
326
327     #[test]
328     fn no_completion_inside_fn() {
329         check(
330             r"
331 trait Test { fn test(); fn test2(); }
332 struct T;
333
334 impl Test for T {
335     fn test() {
336         t$0
337     }
338 }
339 ",
340             expect![[""]],
341         );
342
343         check(
344             r"
345 trait Test { fn test(); fn test2(); }
346 struct T;
347
348 impl Test for T {
349     fn test() {
350         fn t$0
351     }
352 }
353 ",
354             expect![[""]],
355         );
356
357         check(
358             r"
359 trait Test { fn test(); fn test2(); }
360 struct T;
361
362 impl Test for T {
363     fn test() {
364         fn $0
365     }
366 }
367 ",
368             expect![[""]],
369         );
370
371         // https://github.com/rust-analyzer/rust-analyzer/pull/5976#issuecomment-692332191
372         check(
373             r"
374 trait Test { fn test(); fn test2(); }
375 struct T;
376
377 impl Test for T {
378     fn test() {
379         foo.$0
380     }
381 }
382 ",
383             expect![[""]],
384         );
385
386         check(
387             r"
388 trait Test { fn test(_: i32); fn test2(); }
389 struct T;
390
391 impl Test for T {
392     fn test(t$0)
393 }
394 ",
395             expect![[""]],
396         );
397
398         check(
399             r"
400 trait Test { fn test(_: fn()); fn test2(); }
401 struct T;
402
403 impl Test for T {
404     fn test(f: fn $0)
405 }
406 ",
407             expect![[""]],
408         );
409     }
410
411     #[test]
412     fn no_completion_inside_const() {
413         check(
414             r"
415 trait Test { const TEST: fn(); const TEST2: u32; type Test; fn test(); }
416 struct T;
417
418 impl Test for T {
419     const TEST: fn $0
420 }
421 ",
422             expect![[""]],
423         );
424
425         check(
426             r"
427 trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
428 struct T;
429
430 impl Test for T {
431     const TEST: T$0
432 }
433 ",
434             expect![[""]],
435         );
436
437         check(
438             r"
439 trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
440 struct T;
441
442 impl Test for T {
443     const TEST: u32 = f$0
444 }
445 ",
446             expect![[""]],
447         );
448
449         check(
450             r"
451 trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
452 struct T;
453
454 impl Test for T {
455     const TEST: u32 = {
456         t$0
457     };
458 }
459 ",
460             expect![[""]],
461         );
462
463         check(
464             r"
465 trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
466 struct T;
467
468 impl Test for T {
469     const TEST: u32 = {
470         fn $0
471     };
472 }
473 ",
474             expect![[""]],
475         );
476
477         check(
478             r"
479 trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
480 struct T;
481
482 impl Test for T {
483     const TEST: u32 = {
484         fn t$0
485     };
486 }
487 ",
488             expect![[""]],
489         );
490     }
491
492     #[test]
493     fn no_completion_inside_type() {
494         check(
495             r"
496 trait Test { type Test; type Test2; fn test(); }
497 struct T;
498
499 impl Test for T {
500     type Test = T$0;
501 }
502 ",
503             expect![[""]],
504         );
505
506         check(
507             r"
508 trait Test { type Test; type Test2; fn test(); }
509 struct T;
510
511 impl Test for T {
512     type Test = fn $0;
513 }
514 ",
515             expect![[""]],
516         );
517     }
518
519     #[test]
520     fn name_ref_single_function() {
521         check_edit(
522             "test",
523             r#"
524 trait Test {
525     fn test();
526 }
527 struct T;
528
529 impl Test for T {
530     t$0
531 }
532 "#,
533             r#"
534 trait Test {
535     fn test();
536 }
537 struct T;
538
539 impl Test for T {
540     fn test() {
541     $0
542 }
543 }
544 "#,
545         );
546     }
547
548     #[test]
549     fn single_function() {
550         check_edit(
551             "test",
552             r#"
553 trait Test {
554     fn test();
555 }
556 struct T;
557
558 impl Test for T {
559     fn t$0
560 }
561 "#,
562             r#"
563 trait Test {
564     fn test();
565 }
566 struct T;
567
568 impl Test for T {
569     fn test() {
570     $0
571 }
572 }
573 "#,
574         );
575     }
576
577     #[test]
578     fn hide_implemented_fn() {
579         check(
580             r#"
581 trait Test {
582     fn foo();
583     fn foo_bar();
584 }
585 struct T;
586
587 impl Test for T {
588     fn foo() {}
589     fn f$0
590 }
591 "#,
592             expect![[r#"
593                 fn fn foo_bar()
594             "#]],
595         );
596     }
597
598     #[test]
599     fn generic_fn() {
600         check_edit(
601             "foo",
602             r#"
603 trait Test {
604     fn foo<T>();
605 }
606 struct T;
607
608 impl Test for T {
609     fn f$0
610 }
611 "#,
612             r#"
613 trait Test {
614     fn foo<T>();
615 }
616 struct T;
617
618 impl Test for T {
619     fn foo<T>() {
620     $0
621 }
622 }
623 "#,
624         );
625         check_edit(
626             "foo",
627             r#"
628 trait Test {
629     fn foo<T>() where T: Into<String>;
630 }
631 struct T;
632
633 impl Test for T {
634     fn f$0
635 }
636 "#,
637             r#"
638 trait Test {
639     fn foo<T>() where T: Into<String>;
640 }
641 struct T;
642
643 impl Test for T {
644     fn foo<T>()
645 where T: Into<String> {
646     $0
647 }
648 }
649 "#,
650         );
651     }
652
653     #[test]
654     fn associated_type() {
655         check_edit(
656             "SomeType",
657             r#"
658 trait Test {
659     type SomeType;
660 }
661
662 impl Test for () {
663     type S$0
664 }
665 "#,
666             "
667 trait Test {
668     type SomeType;
669 }
670
671 impl Test for () {
672     type SomeType = \n\
673 }
674 ",
675         );
676     }
677
678     #[test]
679     fn associated_const() {
680         check_edit(
681             "SOME_CONST",
682             r#"
683 trait Test {
684     const SOME_CONST: u16;
685 }
686
687 impl Test for () {
688     const S$0
689 }
690 "#,
691             "
692 trait Test {
693     const SOME_CONST: u16;
694 }
695
696 impl Test for () {
697     const SOME_CONST: u16 = \n\
698 }
699 ",
700         );
701
702         check_edit(
703             "SOME_CONST",
704             r#"
705 trait Test {
706     const SOME_CONST: u16 = 92;
707 }
708
709 impl Test for () {
710     const S$0
711 }
712 "#,
713             "
714 trait Test {
715     const SOME_CONST: u16 = 92;
716 }
717
718 impl Test for () {
719     const SOME_CONST: u16 = \n\
720 }
721 ",
722         );
723     }
724
725     #[test]
726     fn complete_without_name() {
727         let test = |completion: &str, hint: &str, completed: &str, next_sibling: &str| {
728             check_edit(
729                 completion,
730                 &format!(
731                     r#"
732 trait Test {{
733     type Foo;
734     const CONST: u16;
735     fn bar();
736 }}
737 struct T;
738
739 impl Test for T {{
740     {}
741     {}
742 }}
743 "#,
744                     hint, next_sibling
745                 ),
746                 &format!(
747                     r#"
748 trait Test {{
749     type Foo;
750     const CONST: u16;
751     fn bar();
752 }}
753 struct T;
754
755 impl Test for T {{
756     {}
757     {}
758 }}
759 "#,
760                     completed, next_sibling
761                 ),
762             )
763         };
764
765         // Enumerate some possible next siblings.
766         for next_sibling in &[
767             "",
768             "fn other_fn() {}", // `const $0 fn` -> `const fn`
769             "type OtherType = i32;",
770             "const OTHER_CONST: i32 = 0;",
771             "async fn other_fn() {}",
772             "unsafe fn other_fn() {}",
773             "default fn other_fn() {}",
774             "default type OtherType = i32;",
775             "default const OTHER_CONST: i32 = 0;",
776         ] {
777             test("bar", "fn $0", "fn bar() {\n    $0\n}", next_sibling);
778             test("Foo", "type $0", "type Foo = ", next_sibling);
779             test("CONST", "const $0", "const CONST: u16 = ", next_sibling);
780         }
781     }
782
783     #[test]
784     fn snippet_does_not_overwrite_comment_or_attr() {
785         let test = |completion: &str, hint: &str, completed: &str| {
786             check_edit(
787                 completion,
788                 &format!(
789                     r#"
790 trait Foo {{
791     type Type;
792     fn function();
793     const CONST: i32 = 0;
794 }}
795 struct T;
796
797 impl Foo for T {{
798     // Comment
799     #[bar]
800     {}
801 }}
802 "#,
803                     hint
804                 ),
805                 &format!(
806                     r#"
807 trait Foo {{
808     type Type;
809     fn function();
810     const CONST: i32 = 0;
811 }}
812 struct T;
813
814 impl Foo for T {{
815     // Comment
816     #[bar]
817     {}
818 }}
819 "#,
820                     completed
821                 ),
822             )
823         };
824         test("function", "fn f$0", "fn function() {\n    $0\n}");
825         test("Type", "type T$0", "type Type = ");
826         test("CONST", "const C$0", "const CONST: i32 = ");
827     }
828
829     #[test]
830     fn generics_are_inlined_in_return_type() {
831         check_edit(
832             "function",
833             r#"
834 trait Foo<T> {
835     fn function() -> T;
836 }
837 struct Bar;
838
839 impl Foo<u32> for Bar {
840     fn f$0
841 }
842 "#,
843             r#"
844 trait Foo<T> {
845     fn function() -> T;
846 }
847 struct Bar;
848
849 impl Foo<u32> for Bar {
850     fn function() -> u32 {
851     $0
852 }
853 }
854 "#,
855         )
856     }
857
858     #[test]
859     fn generics_are_inlined_in_parameter() {
860         check_edit(
861             "function",
862             r#"
863 trait Foo<T> {
864     fn function(bar: T);
865 }
866 struct Bar;
867
868 impl Foo<u32> for Bar {
869     fn f$0
870 }
871 "#,
872             r#"
873 trait Foo<T> {
874     fn function(bar: T);
875 }
876 struct Bar;
877
878 impl Foo<u32> for Bar {
879     fn function(bar: u32) {
880     $0
881 }
882 }
883 "#,
884         )
885     }
886
887     #[test]
888     fn generics_are_inlined_when_part_of_other_types() {
889         check_edit(
890             "function",
891             r#"
892 trait Foo<T> {
893     fn function(bar: Vec<T>);
894 }
895 struct Bar;
896
897 impl Foo<u32> for Bar {
898     fn f$0
899 }
900 "#,
901             r#"
902 trait Foo<T> {
903     fn function(bar: Vec<T>);
904 }
905 struct Bar;
906
907 impl Foo<u32> for Bar {
908     fn function(bar: Vec<u32>) {
909     $0
910 }
911 }
912 "#,
913         )
914     }
915
916     #[test]
917     fn generics_are_inlined_complex() {
918         check_edit(
919             "function",
920             r#"
921 trait Foo<T, U, V> {
922     fn function(bar: Vec<T>, baz: U) -> Arc<Vec<V>>;
923 }
924 struct Bar;
925
926 impl Foo<u32, Vec<usize>, u8> for Bar {
927     fn f$0
928 }
929 "#,
930             r#"
931 trait Foo<T, U, V> {
932     fn function(bar: Vec<T>, baz: U) -> Arc<Vec<V>>;
933 }
934 struct Bar;
935
936 impl Foo<u32, Vec<usize>, u8> for Bar {
937     fn function(bar: Vec<u32>, baz: Vec<usize>) -> Arc<Vec<u8>> {
938     $0
939 }
940 }
941 "#,
942         )
943     }
944
945     #[test]
946     fn generics_are_inlined_in_associated_const() {
947         check_edit(
948             "BAR",
949             r#"
950 trait Foo<T> {
951     const BAR: T;
952 }
953 struct Bar;
954
955 impl Foo<u32> for Bar {
956     const B$0;
957 }
958 "#,
959             r#"
960 trait Foo<T> {
961     const BAR: T;
962 }
963 struct Bar;
964
965 impl Foo<u32> for Bar {
966     const BAR: u32 = ;
967 }
968 "#,
969         )
970     }
971
972     #[test]
973     fn generics_are_inlined_in_where_clause() {
974         check_edit(
975             "function",
976             r#"
977 trait SomeTrait<T> {}
978
979 trait Foo<T> {
980     fn function()
981     where Self: SomeTrait<T>;
982 }
983 struct Bar;
984
985 impl Foo<u32> for Bar {
986     fn f$0
987 }
988 "#,
989             r#"
990 trait SomeTrait<T> {}
991
992 trait Foo<T> {
993     fn function()
994     where Self: SomeTrait<T>;
995 }
996 struct Bar;
997
998 impl Foo<u32> for Bar {
999     fn function()
1000 where Self: SomeTrait<u32> {
1001     $0
1002 }
1003 }
1004 "#,
1005         )
1006     }
1007 }