Unnamed repository; edit this file 'description' to name the repository.
Fix `data_constructor` ignoring generics for struct
Tavo Annus 2024-05-25
parent a55e8bf · commit 0f68427
-rw-r--r--crates/hir/src/term_search.rs2
-rw-r--r--crates/hir/src/term_search/tactics.rs10
-rw-r--r--crates/ide-assists/src/handlers/term_search.rs12
3 files changed, 19 insertions, 5 deletions
diff --git a/crates/hir/src/term_search.rs b/crates/hir/src/term_search.rs
index 5c5ddae19e..7b70cdf459 100644
--- a/crates/hir/src/term_search.rs
+++ b/crates/hir/src/term_search.rs
@@ -329,7 +329,7 @@ pub fn term_search<DB: HirDatabase>(ctx: &TermSearchCtx<'_, DB>) -> Vec<Expr> {
while should_continue() {
lookup.new_round();
- solutions.extend(tactics::type_constructor(ctx, &defs, &mut lookup, should_continue));
+ solutions.extend(tactics::data_constructor(ctx, &defs, &mut lookup, should_continue));
solutions.extend(tactics::free_function(ctx, &defs, &mut lookup, should_continue));
solutions.extend(tactics::impl_method(ctx, &defs, &mut lookup, should_continue));
solutions.extend(tactics::struct_projection(ctx, &defs, &mut lookup, should_continue));
diff --git a/crates/hir/src/term_search/tactics.rs b/crates/hir/src/term_search/tactics.rs
index a26728272d..f95ff1dc0f 100644
--- a/crates/hir/src/term_search/tactics.rs
+++ b/crates/hir/src/term_search/tactics.rs
@@ -87,9 +87,9 @@ pub(super) fn trivial<'a, DB: HirDatabase>(
})
}
-/// # Type constructor tactic
+/// # Data constructor tactic
///
-/// Attempts different type constructors for enums and structs in scope
+/// Attempts different data constructors for enums and structs in scope
///
/// Updates lookup by new types reached and returns iterator that yields
/// elements that unify with `goal`.
@@ -99,7 +99,7 @@ pub(super) fn trivial<'a, DB: HirDatabase>(
/// * `defs` - Set of items in scope at term search target location
/// * `lookup` - Lookup table for types
/// * `should_continue` - Function that indicates when to stop iterating
-pub(super) fn type_constructor<'a, DB: HirDatabase>(
+pub(super) fn data_constructor<'a, DB: HirDatabase>(
ctx: &'a TermSearchCtx<'a, DB>,
defs: &'a FxHashSet<ScopeDef>,
lookup: &'a mut LookupTable,
@@ -308,7 +308,9 @@ pub(super) fn type_constructor<'a, DB: HirDatabase>(
// Early exit if some param cannot be filled from lookup
let param_exprs: Vec<Vec<Expr>> = fields
.into_iter()
- .map(|field| lookup.find(db, &field.ty(db)))
+ .map(|field| {
+ lookup.find(db, &field.ty_with_args(db, generics.iter().cloned()))
+ })
.collect::<Option<_>>()?;
// Note that we need special case for 0 param constructors because of multi cartesian
diff --git a/crates/ide-assists/src/handlers/term_search.rs b/crates/ide-assists/src/handlers/term_search.rs
index ffd1508ccb..94e0519cba 100644
--- a/crates/ide-assists/src/handlers/term_search.rs
+++ b/crates/ide-assists/src/handlers/term_search.rs
@@ -278,4 +278,16 @@ fn f() { let a = 1; let b = 0.0; let c: (i32, (i32, f64)) = todo$0!(); }"#,
r#"fn f() { let a = 1; let b = 0.0; let c: (i32, (i32, f64)) = (a, (a, b)); }"#,
)
}
+
+ #[test]
+ fn test_tuple_struct_with_generics() {
+ check_assist(
+ term_search,
+ r#"//- minicore: todo, unimplemented
+struct Foo<T>(T);
+fn f() { let a = 1; let b: Foo<i32> = todo$0!(); }"#,
+ r#"struct Foo<T>(T);
+fn f() { let a = 1; let b: Foo<i32> = Foo(a); }"#,
+ )
+ }
}