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/Cargo.toml | 1 + crates/ra_assists/src/ast_transform.rs | 20 ++++++++- .../src/handlers/add_missing_impl_members.rs | 50 ++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/Cargo.toml b/crates/ra_assists/Cargo.toml index 3bcf58ba4..488ab7bc8 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 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 From a55ad203888b5e43cf6cbf015d562d7bd1abe0bb Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Thu, 14 May 2020 09:56:20 +0300 Subject: Use generic_defaults and display_source_code --- crates/ra_assists/Cargo.toml | 1 - crates/ra_assists/src/ast_transform.rs | 21 ++++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/Cargo.toml b/crates/ra_assists/Cargo.toml index 488ab7bc8..3bcf58ba4 100644 --- a/crates/ra_assists/Cargo.toml +++ b/crates/ra_assists/Cargo.toml @@ -21,5 +21,4 @@ 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 d81218bc8..4001ca73c 100644 --- a/crates/ra_assists/src/ast_transform.rs +++ b/crates/ra_assists/src/ast_transform.rs @@ -1,8 +1,7 @@ //! `AstTransformer`s are functions that replace nodes in an AST and can be easily combined. use rustc_hash::FxHashMap; -use hir::{PathResolution, SemanticsScope}; -use hir_def::type_ref::TypeRef; +use hir::{HirDisplay, PathResolution, SemanticsScope}; use ra_ide_db::RootDatabase; use ra_syntax::{ algo::SyntaxRewriter, @@ -61,14 +60,18 @@ impl<'a> SubstituteTypeParams<'a> { .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(( + None => { + let default = k.default(source_scope.db)?; + Some(( k, - ast::make::type_arg(&format!("{}", path.mod_path().as_ident()?)) - .type_ref()?, - )), - _ => None, - }, + ast::make::type_arg( + &default + .display_source_code(source_scope.db, source_scope.module()?.into()) + .ok()?, + ) + .type_ref()?, + )) + } }) .collect(); return SubstituteTypeParams { -- cgit v1.2.3 From 7e9396c7ebd8b6b245086c2292e100200c8adb19 Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Thu, 14 May 2020 10:14:04 +0300 Subject: Change type_arg to type_ref func --- crates/ra_assists/src/ast_transform.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/ast_transform.rs b/crates/ra_assists/src/ast_transform.rs index 4001ca73c..18f3ec683 100644 --- a/crates/ra_assists/src/ast_transform.rs +++ b/crates/ra_assists/src/ast_transform.rs @@ -64,12 +64,11 @@ impl<'a> SubstituteTypeParams<'a> { let default = k.default(source_scope.db)?; Some(( k, - ast::make::type_arg( + ast::make::type_ref( &default .display_source_code(source_scope.db, source_scope.module()?.into()) .ok()?, ) - .type_ref()?, )) } }) -- cgit v1.2.3 From 2dfbec149f6de3fe8e64680521aa6076928b24f9 Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Thu, 14 May 2020 10:31:34 +0300 Subject: Fix formatting --- crates/ra_assists/src/ast_transform.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/ast_transform.rs b/crates/ra_assists/src/ast_transform.rs index 18f3ec683..3079a02a2 100644 --- a/crates/ra_assists/src/ast_transform.rs +++ b/crates/ra_assists/src/ast_transform.rs @@ -68,7 +68,7 @@ impl<'a> SubstituteTypeParams<'a> { &default .display_source_code(source_scope.db, source_scope.module()?.into()) .ok()?, - ) + ), )) } }) -- cgit v1.2.3