From 00f3b6c59ae3df9a7bfb1cd8b694d5f9b6a78be4 Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Wed, 13 May 2020 16:06:42 +0300 Subject: Correctly fill default type parameters --- crates/ra_assists/src/ast_transform.rs | 20 ++++++++- .../src/handlers/add_missing_impl_members.rs | 50 ++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/ast_transform.rs b/crates/ra_assists/src/ast_transform.rs index 9ac65ab39..d81218bc8 100644 --- a/crates/ra_assists/src/ast_transform.rs +++ b/crates/ra_assists/src/ast_transform.rs @@ -2,6 +2,7 @@ use rustc_hash::FxHashMap; use hir::{PathResolution, SemanticsScope}; +use hir_def::type_ref::TypeRef; use ra_ide_db::RootDatabase; use ra_syntax::{ algo::SyntaxRewriter, @@ -51,7 +52,24 @@ impl<'a> SubstituteTypeParams<'a> { .into_iter() // this is a trait impl, so we need to skip the first type parameter -- this is a bit hacky .skip(1) - .zip(substs.into_iter()) + // The actual list of trait type parameters may be longer than the one + // used in the `impl` block due to trailing default type parametrs. + // For that case we extend the `substs` with an empty iterator so we + // can still hit those trailing values and check if they actually have + // a default type. If they do, go for that type from `hir` to `ast` so + // the resulting change can be applied correctly. + .zip(substs.into_iter().map(Some).chain(std::iter::repeat(None))) + .filter_map(|(k, v)| match v { + Some(v) => Some((k, v)), + None => match k.default(source_scope.db)? { + TypeRef::Path(path) => Some(( + k, + ast::make::type_arg(&format!("{}", path.mod_path().as_ident()?)) + .type_ref()?, + )), + _ => None, + }, + }) .collect(); return SubstituteTypeParams { source_scope, diff --git a/crates/ra_assists/src/handlers/add_missing_impl_members.rs b/crates/ra_assists/src/handlers/add_missing_impl_members.rs index c1ce87914..22e1156d2 100644 --- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs @@ -615,6 +615,56 @@ trait Foo { struct S; impl Foo for S { <|>fn valid(some: u32) -> bool { false } +}"#, + ) + } + + #[test] + fn test_generic_single_default_parameter() { + check_assist( + add_missing_impl_members, + r#" +trait Foo { + fn bar(&self, other: &T); +} + +struct S; +impl Foo for S { <|> }"#, + r#" +trait Foo { + fn bar(&self, other: &T); +} + +struct S; +impl Foo for S { + <|>fn bar(&self, other: &Self) { + todo!() + } +}"#, + ) + } + + #[test] + fn test_generic_default_parameter_is_second() { + check_assist( + add_missing_impl_members, + r#" +trait Foo { + fn bar(&self, this: &T1, that: &T2); +} + +struct S; +impl Foo for S { <|> }"#, + r#" +trait Foo { + fn bar(&self, this: &T1, that: &T2); +} + +struct S; +impl Foo for S { + <|>fn bar(&self, this: &T, that: &Self) { + todo!() + } }"#, ) } -- cgit v1.2.3