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