diff --git a/Cargo.lock b/Cargo.lock index 3a34978b136..b28c6d98b83 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -936,6 +936,7 @@ dependencies = [ "ra_db", "ra_fmt", "ra_hir", + "ra_hir_def", "ra_ide_db", "ra_prof", "ra_syntax", diff --git a/crates/ra_assists/Cargo.toml b/crates/ra_assists/Cargo.toml index 3bcf58ba4b0..488ab7bc87f 100644 --- a/crates/ra_assists/Cargo.toml +++ b/crates/ra_assists/Cargo.toml @@ -21,4 +21,5 @@ ra_prof = { path = "../ra_prof" } ra_db = { path = "../ra_db" } ra_ide_db = { path = "../ra_ide_db" } hir = { path = "../ra_hir", package = "ra_hir" } +hir_def = { path = "../ra_hir_def", package = "ra_hir_def" } test_utils = { path = "../test_utils" } diff --git a/crates/ra_assists/src/ast_transform.rs b/crates/ra_assists/src/ast_transform.rs index 9ac65ab397f..d81218bc80e 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 c1ce87914ab..22e1156d2a1 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!() + } }"#, ) } diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index e8e3211fcd5..3c56f39c138 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -989,6 +989,11 @@ impl TypeParam { ty: InEnvironment { value: ty, environment }, } } + + pub fn default(self, db: &dyn HirDatabase) -> Option { + let params = db.generic_params(self.id.parent); + params.types[self.id.local_id].default.clone() + } } // FIXME: rename from `ImplDef` to `Impl` diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 12c5228f563..8981cbe3398 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -13,6 +13,10 @@ pub fn name_ref(text: &str) -> ast::NameRef { ast_from_text(&format!("fn f() {{ {}; }}", text)) } +pub fn type_arg(text: &str) -> ast::TypeArg { + ast_from_text(&format!("impl F<{}> for G {{}}", text)) +} + pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment { ast_from_text(&format!("use {};", name_ref)) }