diff options
author | Igor Aleksanov <[email protected]> | 2020-08-14 05:34:07 +0100 |
---|---|---|
committer | Igor Aleksanov <[email protected]> | 2020-08-14 05:34:07 +0100 |
commit | c26c911ec1e6c2ad1dcb7d155a6a1d528839ad1a (patch) | |
tree | 7cff36c38234be0afb65273146d8247083a5cfeb /crates/assists/src/handlers/add_missing_impl_members.rs | |
parent | 3c018bf84de5c693b5ee1c6bec0fed3b201c2060 (diff) | |
parent | f1f73649a686dc6e6449afc35e0fa6fed00e225d (diff) |
Merge branch 'master' into add-disable-diagnostics
Diffstat (limited to 'crates/assists/src/handlers/add_missing_impl_members.rs')
-rw-r--r-- | crates/assists/src/handlers/add_missing_impl_members.rs | 711 |
1 files changed, 711 insertions, 0 deletions
diff --git a/crates/assists/src/handlers/add_missing_impl_members.rs b/crates/assists/src/handlers/add_missing_impl_members.rs new file mode 100644 index 000000000..81b61ebf8 --- /dev/null +++ b/crates/assists/src/handlers/add_missing_impl_members.rs | |||
@@ -0,0 +1,711 @@ | |||
1 | use hir::HasSource; | ||
2 | use syntax::{ | ||
3 | ast::{ | ||
4 | self, | ||
5 | edit::{self, AstNodeEdit, IndentLevel}, | ||
6 | make, AstNode, NameOwner, | ||
7 | }, | ||
8 | SmolStr, | ||
9 | }; | ||
10 | |||
11 | use crate::{ | ||
12 | assist_context::{AssistContext, Assists}, | ||
13 | ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}, | ||
14 | utils::{get_missing_assoc_items, render_snippet, resolve_target_trait, Cursor}, | ||
15 | AssistId, AssistKind, | ||
16 | }; | ||
17 | |||
18 | #[derive(PartialEq)] | ||
19 | enum AddMissingImplMembersMode { | ||
20 | DefaultMethodsOnly, | ||
21 | NoDefaultMethods, | ||
22 | } | ||
23 | |||
24 | // Assist: add_impl_missing_members | ||
25 | // | ||
26 | // Adds scaffold for required impl members. | ||
27 | // | ||
28 | // ``` | ||
29 | // trait Trait<T> { | ||
30 | // Type X; | ||
31 | // fn foo(&self) -> T; | ||
32 | // fn bar(&self) {} | ||
33 | // } | ||
34 | // | ||
35 | // impl Trait<u32> for () {<|> | ||
36 | // | ||
37 | // } | ||
38 | // ``` | ||
39 | // -> | ||
40 | // ``` | ||
41 | // trait Trait<T> { | ||
42 | // Type X; | ||
43 | // fn foo(&self) -> T; | ||
44 | // fn bar(&self) {} | ||
45 | // } | ||
46 | // | ||
47 | // impl Trait<u32> for () { | ||
48 | // fn foo(&self) -> u32 { | ||
49 | // ${0:todo!()} | ||
50 | // } | ||
51 | // | ||
52 | // } | ||
53 | // ``` | ||
54 | pub(crate) fn add_missing_impl_members(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | ||
55 | add_missing_impl_members_inner( | ||
56 | acc, | ||
57 | ctx, | ||
58 | AddMissingImplMembersMode::NoDefaultMethods, | ||
59 | "add_impl_missing_members", | ||
60 | "Implement missing members", | ||
61 | ) | ||
62 | } | ||
63 | |||
64 | // Assist: add_impl_default_members | ||
65 | // | ||
66 | // Adds scaffold for overriding default impl members. | ||
67 | // | ||
68 | // ``` | ||
69 | // trait Trait { | ||
70 | // Type X; | ||
71 | // fn foo(&self); | ||
72 | // fn bar(&self) {} | ||
73 | // } | ||
74 | // | ||
75 | // impl Trait for () { | ||
76 | // Type X = (); | ||
77 | // fn foo(&self) {}<|> | ||
78 | // | ||
79 | // } | ||
80 | // ``` | ||
81 | // -> | ||
82 | // ``` | ||
83 | // trait Trait { | ||
84 | // Type X; | ||
85 | // fn foo(&self); | ||
86 | // fn bar(&self) {} | ||
87 | // } | ||
88 | // | ||
89 | // impl Trait for () { | ||
90 | // Type X = (); | ||
91 | // fn foo(&self) {} | ||
92 | // $0fn bar(&self) {} | ||
93 | // | ||
94 | // } | ||
95 | // ``` | ||
96 | pub(crate) fn add_missing_default_members(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | ||
97 | add_missing_impl_members_inner( | ||
98 | acc, | ||
99 | ctx, | ||
100 | AddMissingImplMembersMode::DefaultMethodsOnly, | ||
101 | "add_impl_default_members", | ||
102 | "Implement default members", | ||
103 | ) | ||
104 | } | ||
105 | |||
106 | fn add_missing_impl_members_inner( | ||
107 | acc: &mut Assists, | ||
108 | ctx: &AssistContext, | ||
109 | mode: AddMissingImplMembersMode, | ||
110 | assist_id: &'static str, | ||
111 | label: &'static str, | ||
112 | ) -> Option<()> { | ||
113 | let _p = profile::span("add_missing_impl_members_inner"); | ||
114 | let impl_def = ctx.find_node_at_offset::<ast::Impl>()?; | ||
115 | let impl_item_list = impl_def.assoc_item_list()?; | ||
116 | |||
117 | let trait_ = resolve_target_trait(&ctx.sema, &impl_def)?; | ||
118 | |||
119 | let def_name = |item: &ast::AssocItem| -> Option<SmolStr> { | ||
120 | match item { | ||
121 | ast::AssocItem::Fn(def) => def.name(), | ||
122 | ast::AssocItem::TypeAlias(def) => def.name(), | ||
123 | ast::AssocItem::Const(def) => def.name(), | ||
124 | ast::AssocItem::MacroCall(_) => None, | ||
125 | } | ||
126 | .map(|it| it.text().clone()) | ||
127 | }; | ||
128 | |||
129 | let missing_items = get_missing_assoc_items(&ctx.sema, &impl_def) | ||
130 | .iter() | ||
131 | .map(|i| match i { | ||
132 | hir::AssocItem::Function(i) => ast::AssocItem::Fn(i.source(ctx.db()).value), | ||
133 | hir::AssocItem::TypeAlias(i) => ast::AssocItem::TypeAlias(i.source(ctx.db()).value), | ||
134 | hir::AssocItem::Const(i) => ast::AssocItem::Const(i.source(ctx.db()).value), | ||
135 | }) | ||
136 | .filter(|t| def_name(&t).is_some()) | ||
137 | .filter(|t| match t { | ||
138 | ast::AssocItem::Fn(def) => match mode { | ||
139 | AddMissingImplMembersMode::DefaultMethodsOnly => def.body().is_some(), | ||
140 | AddMissingImplMembersMode::NoDefaultMethods => def.body().is_none(), | ||
141 | }, | ||
142 | _ => mode == AddMissingImplMembersMode::NoDefaultMethods, | ||
143 | }) | ||
144 | .collect::<Vec<_>>(); | ||
145 | |||
146 | if missing_items.is_empty() { | ||
147 | return None; | ||
148 | } | ||
149 | |||
150 | let target = impl_def.syntax().text_range(); | ||
151 | acc.add(AssistId(assist_id, AssistKind::QuickFix), label, target, |builder| { | ||
152 | let n_existing_items = impl_item_list.assoc_items().count(); | ||
153 | let source_scope = ctx.sema.scope_for_def(trait_); | ||
154 | let target_scope = ctx.sema.scope(impl_item_list.syntax()); | ||
155 | let ast_transform = QualifyPaths::new(&target_scope, &source_scope) | ||
156 | .or(SubstituteTypeParams::for_trait_impl(&source_scope, trait_, impl_def)); | ||
157 | let items = missing_items | ||
158 | .into_iter() | ||
159 | .map(|it| ast_transform::apply(&*ast_transform, it)) | ||
160 | .map(|it| match it { | ||
161 | ast::AssocItem::Fn(def) => ast::AssocItem::Fn(add_body(def)), | ||
162 | ast::AssocItem::TypeAlias(def) => ast::AssocItem::TypeAlias(def.remove_bounds()), | ||
163 | _ => it, | ||
164 | }) | ||
165 | .map(|it| edit::remove_attrs_and_docs(&it)); | ||
166 | let new_impl_item_list = impl_item_list.append_items(items); | ||
167 | let first_new_item = new_impl_item_list.assoc_items().nth(n_existing_items).unwrap(); | ||
168 | |||
169 | let original_range = impl_item_list.syntax().text_range(); | ||
170 | match ctx.config.snippet_cap { | ||
171 | None => builder.replace(original_range, new_impl_item_list.to_string()), | ||
172 | Some(cap) => { | ||
173 | let mut cursor = Cursor::Before(first_new_item.syntax()); | ||
174 | let placeholder; | ||
175 | if let ast::AssocItem::Fn(func) = &first_new_item { | ||
176 | if let Some(m) = func.syntax().descendants().find_map(ast::MacroCall::cast) { | ||
177 | if m.syntax().text() == "todo!()" { | ||
178 | placeholder = m; | ||
179 | cursor = Cursor::Replace(placeholder.syntax()); | ||
180 | } | ||
181 | } | ||
182 | } | ||
183 | builder.replace_snippet( | ||
184 | cap, | ||
185 | original_range, | ||
186 | render_snippet(cap, new_impl_item_list.syntax(), cursor), | ||
187 | ) | ||
188 | } | ||
189 | }; | ||
190 | }) | ||
191 | } | ||
192 | |||
193 | fn add_body(fn_def: ast::Fn) -> ast::Fn { | ||
194 | if fn_def.body().is_some() { | ||
195 | return fn_def; | ||
196 | } | ||
197 | let body = make::block_expr(None, Some(make::expr_todo())).indent(IndentLevel(1)); | ||
198 | fn_def.with_body(body) | ||
199 | } | ||
200 | |||
201 | #[cfg(test)] | ||
202 | mod tests { | ||
203 | use crate::tests::{check_assist, check_assist_not_applicable}; | ||
204 | |||
205 | use super::*; | ||
206 | |||
207 | #[test] | ||
208 | fn test_add_missing_impl_members() { | ||
209 | check_assist( | ||
210 | add_missing_impl_members, | ||
211 | r#" | ||
212 | trait Foo { | ||
213 | type Output; | ||
214 | |||
215 | const CONST: usize = 42; | ||
216 | |||
217 | fn foo(&self); | ||
218 | fn bar(&self); | ||
219 | fn baz(&self); | ||
220 | } | ||
221 | |||
222 | struct S; | ||
223 | |||
224 | impl Foo for S { | ||
225 | fn bar(&self) {} | ||
226 | <|> | ||
227 | }"#, | ||
228 | r#" | ||
229 | trait Foo { | ||
230 | type Output; | ||
231 | |||
232 | const CONST: usize = 42; | ||
233 | |||
234 | fn foo(&self); | ||
235 | fn bar(&self); | ||
236 | fn baz(&self); | ||
237 | } | ||
238 | |||
239 | struct S; | ||
240 | |||
241 | impl Foo for S { | ||
242 | fn bar(&self) {} | ||
243 | $0type Output; | ||
244 | const CONST: usize = 42; | ||
245 | fn foo(&self) { | ||
246 | todo!() | ||
247 | } | ||
248 | fn baz(&self) { | ||
249 | todo!() | ||
250 | } | ||
251 | |||
252 | }"#, | ||
253 | ); | ||
254 | } | ||
255 | |||
256 | #[test] | ||
257 | fn test_copied_overriden_members() { | ||
258 | check_assist( | ||
259 | add_missing_impl_members, | ||
260 | r#" | ||
261 | trait Foo { | ||
262 | fn foo(&self); | ||
263 | fn bar(&self) -> bool { true } | ||
264 | fn baz(&self) -> u32 { 42 } | ||
265 | } | ||
266 | |||
267 | struct S; | ||
268 | |||
269 | impl Foo for S { | ||
270 | fn bar(&self) {} | ||
271 | <|> | ||
272 | }"#, | ||
273 | r#" | ||
274 | trait Foo { | ||
275 | fn foo(&self); | ||
276 | fn bar(&self) -> bool { true } | ||
277 | fn baz(&self) -> u32 { 42 } | ||
278 | } | ||
279 | |||
280 | struct S; | ||
281 | |||
282 | impl Foo for S { | ||
283 | fn bar(&self) {} | ||
284 | fn foo(&self) { | ||
285 | ${0:todo!()} | ||
286 | } | ||
287 | |||
288 | }"#, | ||
289 | ); | ||
290 | } | ||
291 | |||
292 | #[test] | ||
293 | fn test_empty_impl_def() { | ||
294 | check_assist( | ||
295 | add_missing_impl_members, | ||
296 | r#" | ||
297 | trait Foo { fn foo(&self); } | ||
298 | struct S; | ||
299 | impl Foo for S { <|> }"#, | ||
300 | r#" | ||
301 | trait Foo { fn foo(&self); } | ||
302 | struct S; | ||
303 | impl Foo for S { | ||
304 | fn foo(&self) { | ||
305 | ${0:todo!()} | ||
306 | } | ||
307 | }"#, | ||
308 | ); | ||
309 | } | ||
310 | |||
311 | #[test] | ||
312 | fn fill_in_type_params_1() { | ||
313 | check_assist( | ||
314 | add_missing_impl_members, | ||
315 | r#" | ||
316 | trait Foo<T> { fn foo(&self, t: T) -> &T; } | ||
317 | struct S; | ||
318 | impl Foo<u32> for S { <|> }"#, | ||
319 | r#" | ||
320 | trait Foo<T> { fn foo(&self, t: T) -> &T; } | ||
321 | struct S; | ||
322 | impl Foo<u32> for S { | ||
323 | fn foo(&self, t: u32) -> &u32 { | ||
324 | ${0:todo!()} | ||
325 | } | ||
326 | }"#, | ||
327 | ); | ||
328 | } | ||
329 | |||
330 | #[test] | ||
331 | fn fill_in_type_params_2() { | ||
332 | check_assist( | ||
333 | add_missing_impl_members, | ||
334 | r#" | ||
335 | trait Foo<T> { fn foo(&self, t: T) -> &T; } | ||
336 | struct S; | ||
337 | impl<U> Foo<U> for S { <|> }"#, | ||
338 | r#" | ||
339 | trait Foo<T> { fn foo(&self, t: T) -> &T; } | ||
340 | struct S; | ||
341 | impl<U> Foo<U> for S { | ||
342 | fn foo(&self, t: U) -> &U { | ||
343 | ${0:todo!()} | ||
344 | } | ||
345 | }"#, | ||
346 | ); | ||
347 | } | ||
348 | |||
349 | #[test] | ||
350 | fn test_cursor_after_empty_impl_def() { | ||
351 | check_assist( | ||
352 | add_missing_impl_members, | ||
353 | r#" | ||
354 | trait Foo { fn foo(&self); } | ||
355 | struct S; | ||
356 | impl Foo for S {}<|>"#, | ||
357 | r#" | ||
358 | trait Foo { fn foo(&self); } | ||
359 | struct S; | ||
360 | impl Foo for S { | ||
361 | fn foo(&self) { | ||
362 | ${0:todo!()} | ||
363 | } | ||
364 | }"#, | ||
365 | ) | ||
366 | } | ||
367 | |||
368 | #[test] | ||
369 | fn test_qualify_path_1() { | ||
370 | check_assist( | ||
371 | add_missing_impl_members, | ||
372 | r#" | ||
373 | mod foo { | ||
374 | pub struct Bar; | ||
375 | trait Foo { fn foo(&self, bar: Bar); } | ||
376 | } | ||
377 | struct S; | ||
378 | impl foo::Foo for S { <|> }"#, | ||
379 | r#" | ||
380 | mod foo { | ||
381 | pub struct Bar; | ||
382 | trait Foo { fn foo(&self, bar: Bar); } | ||
383 | } | ||
384 | struct S; | ||
385 | impl foo::Foo for S { | ||
386 | fn foo(&self, bar: foo::Bar) { | ||
387 | ${0:todo!()} | ||
388 | } | ||
389 | }"#, | ||
390 | ); | ||
391 | } | ||
392 | |||
393 | #[test] | ||
394 | fn test_qualify_path_generic() { | ||
395 | check_assist( | ||
396 | add_missing_impl_members, | ||
397 | r#" | ||
398 | mod foo { | ||
399 | pub struct Bar<T>; | ||
400 | trait Foo { fn foo(&self, bar: Bar<u32>); } | ||
401 | } | ||
402 | struct S; | ||
403 | impl foo::Foo for S { <|> }"#, | ||
404 | r#" | ||
405 | mod foo { | ||
406 | pub struct Bar<T>; | ||
407 | trait Foo { fn foo(&self, bar: Bar<u32>); } | ||
408 | } | ||
409 | struct S; | ||
410 | impl foo::Foo for S { | ||
411 | fn foo(&self, bar: foo::Bar<u32>) { | ||
412 | ${0:todo!()} | ||
413 | } | ||
414 | }"#, | ||
415 | ); | ||
416 | } | ||
417 | |||
418 | #[test] | ||
419 | fn test_qualify_path_and_substitute_param() { | ||
420 | check_assist( | ||
421 | add_missing_impl_members, | ||
422 | r#" | ||
423 | mod foo { | ||
424 | pub struct Bar<T>; | ||
425 | trait Foo<T> { fn foo(&self, bar: Bar<T>); } | ||
426 | } | ||
427 | struct S; | ||
428 | impl foo::Foo<u32> for S { <|> }"#, | ||
429 | r#" | ||
430 | mod foo { | ||
431 | pub struct Bar<T>; | ||
432 | trait Foo<T> { fn foo(&self, bar: Bar<T>); } | ||
433 | } | ||
434 | struct S; | ||
435 | impl foo::Foo<u32> for S { | ||
436 | fn foo(&self, bar: foo::Bar<u32>) { | ||
437 | ${0:todo!()} | ||
438 | } | ||
439 | }"#, | ||
440 | ); | ||
441 | } | ||
442 | |||
443 | #[test] | ||
444 | fn test_substitute_param_no_qualify() { | ||
445 | // when substituting params, the substituted param should not be qualified! | ||
446 | check_assist( | ||
447 | add_missing_impl_members, | ||
448 | r#" | ||
449 | mod foo { | ||
450 | trait Foo<T> { fn foo(&self, bar: T); } | ||
451 | pub struct Param; | ||
452 | } | ||
453 | struct Param; | ||
454 | struct S; | ||
455 | impl foo::Foo<Param> for S { <|> }"#, | ||
456 | r#" | ||
457 | mod foo { | ||
458 | trait Foo<T> { fn foo(&self, bar: T); } | ||
459 | pub struct Param; | ||
460 | } | ||
461 | struct Param; | ||
462 | struct S; | ||
463 | impl foo::Foo<Param> for S { | ||
464 | fn foo(&self, bar: Param) { | ||
465 | ${0:todo!()} | ||
466 | } | ||
467 | }"#, | ||
468 | ); | ||
469 | } | ||
470 | |||
471 | #[test] | ||
472 | fn test_qualify_path_associated_item() { | ||
473 | check_assist( | ||
474 | add_missing_impl_members, | ||
475 | r#" | ||
476 | mod foo { | ||
477 | pub struct Bar<T>; | ||
478 | impl Bar<T> { type Assoc = u32; } | ||
479 | trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); } | ||
480 | } | ||
481 | struct S; | ||
482 | impl foo::Foo for S { <|> }"#, | ||
483 | r#" | ||
484 | mod foo { | ||
485 | pub struct Bar<T>; | ||
486 | impl Bar<T> { type Assoc = u32; } | ||
487 | trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); } | ||
488 | } | ||
489 | struct S; | ||
490 | impl foo::Foo for S { | ||
491 | fn foo(&self, bar: foo::Bar<u32>::Assoc) { | ||
492 | ${0:todo!()} | ||
493 | } | ||
494 | }"#, | ||
495 | ); | ||
496 | } | ||
497 | |||
498 | #[test] | ||
499 | fn test_qualify_path_nested() { | ||
500 | check_assist( | ||
501 | add_missing_impl_members, | ||
502 | r#" | ||
503 | mod foo { | ||
504 | pub struct Bar<T>; | ||
505 | pub struct Baz; | ||
506 | trait Foo { fn foo(&self, bar: Bar<Baz>); } | ||
507 | } | ||
508 | struct S; | ||
509 | impl foo::Foo for S { <|> }"#, | ||
510 | r#" | ||
511 | mod foo { | ||
512 | pub struct Bar<T>; | ||
513 | pub struct Baz; | ||
514 | trait Foo { fn foo(&self, bar: Bar<Baz>); } | ||
515 | } | ||
516 | struct S; | ||
517 | impl foo::Foo for S { | ||
518 | fn foo(&self, bar: foo::Bar<foo::Baz>) { | ||
519 | ${0:todo!()} | ||
520 | } | ||
521 | }"#, | ||
522 | ); | ||
523 | } | ||
524 | |||
525 | #[test] | ||
526 | fn test_qualify_path_fn_trait_notation() { | ||
527 | check_assist( | ||
528 | add_missing_impl_members, | ||
529 | r#" | ||
530 | mod foo { | ||
531 | pub trait Fn<Args> { type Output; } | ||
532 | trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); } | ||
533 | } | ||
534 | struct S; | ||
535 | impl foo::Foo for S { <|> }"#, | ||
536 | r#" | ||
537 | mod foo { | ||
538 | pub trait Fn<Args> { type Output; } | ||
539 | trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); } | ||
540 | } | ||
541 | struct S; | ||
542 | impl foo::Foo for S { | ||
543 | fn foo(&self, bar: dyn Fn(u32) -> i32) { | ||
544 | ${0:todo!()} | ||
545 | } | ||
546 | }"#, | ||
547 | ); | ||
548 | } | ||
549 | |||
550 | #[test] | ||
551 | fn test_empty_trait() { | ||
552 | check_assist_not_applicable( | ||
553 | add_missing_impl_members, | ||
554 | r#" | ||
555 | trait Foo; | ||
556 | struct S; | ||
557 | impl Foo for S { <|> }"#, | ||
558 | ) | ||
559 | } | ||
560 | |||
561 | #[test] | ||
562 | fn test_ignore_unnamed_trait_members_and_default_methods() { | ||
563 | check_assist_not_applicable( | ||
564 | add_missing_impl_members, | ||
565 | r#" | ||
566 | trait Foo { | ||
567 | fn (arg: u32); | ||
568 | fn valid(some: u32) -> bool { false } | ||
569 | } | ||
570 | struct S; | ||
571 | impl Foo for S { <|> }"#, | ||
572 | ) | ||
573 | } | ||
574 | |||
575 | #[test] | ||
576 | fn test_with_docstring_and_attrs() { | ||
577 | check_assist( | ||
578 | add_missing_impl_members, | ||
579 | r#" | ||
580 | #[doc(alias = "test alias")] | ||
581 | trait Foo { | ||
582 | /// doc string | ||
583 | type Output; | ||
584 | |||
585 | #[must_use] | ||
586 | fn foo(&self); | ||
587 | } | ||
588 | struct S; | ||
589 | impl Foo for S {}<|>"#, | ||
590 | r#" | ||
591 | #[doc(alias = "test alias")] | ||
592 | trait Foo { | ||
593 | /// doc string | ||
594 | type Output; | ||
595 | |||
596 | #[must_use] | ||
597 | fn foo(&self); | ||
598 | } | ||
599 | struct S; | ||
600 | impl Foo for S { | ||
601 | $0type Output; | ||
602 | fn foo(&self) { | ||
603 | todo!() | ||
604 | } | ||
605 | }"#, | ||
606 | ) | ||
607 | } | ||
608 | |||
609 | #[test] | ||
610 | fn test_default_methods() { | ||
611 | check_assist( | ||
612 | add_missing_default_members, | ||
613 | r#" | ||
614 | trait Foo { | ||
615 | type Output; | ||
616 | |||
617 | const CONST: usize = 42; | ||
618 | |||
619 | fn valid(some: u32) -> bool { false } | ||
620 | fn foo(some: u32) -> bool; | ||
621 | } | ||
622 | struct S; | ||
623 | impl Foo for S { <|> }"#, | ||
624 | r#" | ||
625 | trait Foo { | ||
626 | type Output; | ||
627 | |||
628 | const CONST: usize = 42; | ||
629 | |||
630 | fn valid(some: u32) -> bool { false } | ||
631 | fn foo(some: u32) -> bool; | ||
632 | } | ||
633 | struct S; | ||
634 | impl Foo for S { | ||
635 | $0fn valid(some: u32) -> bool { false } | ||
636 | }"#, | ||
637 | ) | ||
638 | } | ||
639 | |||
640 | #[test] | ||
641 | fn test_generic_single_default_parameter() { | ||
642 | check_assist( | ||
643 | add_missing_impl_members, | ||
644 | r#" | ||
645 | trait Foo<T = Self> { | ||
646 | fn bar(&self, other: &T); | ||
647 | } | ||
648 | |||
649 | struct S; | ||
650 | impl Foo for S { <|> }"#, | ||
651 | r#" | ||
652 | trait Foo<T = Self> { | ||
653 | fn bar(&self, other: &T); | ||
654 | } | ||
655 | |||
656 | struct S; | ||
657 | impl Foo for S { | ||
658 | fn bar(&self, other: &Self) { | ||
659 | ${0:todo!()} | ||
660 | } | ||
661 | }"#, | ||
662 | ) | ||
663 | } | ||
664 | |||
665 | #[test] | ||
666 | fn test_generic_default_parameter_is_second() { | ||
667 | check_assist( | ||
668 | add_missing_impl_members, | ||
669 | r#" | ||
670 | trait Foo<T1, T2 = Self> { | ||
671 | fn bar(&self, this: &T1, that: &T2); | ||
672 | } | ||
673 | |||
674 | struct S<T>; | ||
675 | impl Foo<T> for S<T> { <|> }"#, | ||
676 | r#" | ||
677 | trait Foo<T1, T2 = Self> { | ||
678 | fn bar(&self, this: &T1, that: &T2); | ||
679 | } | ||
680 | |||
681 | struct S<T>; | ||
682 | impl Foo<T> for S<T> { | ||
683 | fn bar(&self, this: &T, that: &Self) { | ||
684 | ${0:todo!()} | ||
685 | } | ||
686 | }"#, | ||
687 | ) | ||
688 | } | ||
689 | |||
690 | #[test] | ||
691 | fn test_assoc_type_bounds_are_removed() { | ||
692 | check_assist( | ||
693 | add_missing_impl_members, | ||
694 | r#" | ||
695 | trait Tr { | ||
696 | type Ty: Copy + 'static; | ||
697 | } | ||
698 | |||
699 | impl Tr for ()<|> { | ||
700 | }"#, | ||
701 | r#" | ||
702 | trait Tr { | ||
703 | type Ty: Copy + 'static; | ||
704 | } | ||
705 | |||
706 | impl Tr for () { | ||
707 | $0type Ty; | ||
708 | }"#, | ||
709 | ) | ||
710 | } | ||
711 | } | ||