Skip to content

Commit 6d6bef0

Browse files
committed
Fix data_constructor ignoring generics for struct
1 parent 56d77b9 commit 6d6bef0

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/tools/rust-analyzer/crates/hir/src/term_search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ pub fn term_search<DB: HirDatabase>(ctx: &TermSearchCtx<'_, DB>) -> Vec<Expr> {
329329
while should_continue() {
330330
lookup.new_round();
331331

332-
solutions.extend(tactics::type_constructor(ctx, &defs, &mut lookup, should_continue));
332+
solutions.extend(tactics::data_constructor(ctx, &defs, &mut lookup, should_continue));
333333
solutions.extend(tactics::free_function(ctx, &defs, &mut lookup, should_continue));
334334
solutions.extend(tactics::impl_method(ctx, &defs, &mut lookup, should_continue));
335335
solutions.extend(tactics::struct_projection(ctx, &defs, &mut lookup, should_continue));

src/tools/rust-analyzer/crates/hir/src/term_search/tactics.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ pub(super) fn trivial<'a, DB: HirDatabase>(
8787
})
8888
}
8989

90-
/// # Type constructor tactic
90+
/// # Data constructor tactic
9191
///
92-
/// Attempts different type constructors for enums and structs in scope
92+
/// Attempts different data constructors for enums and structs in scope
9393
///
9494
/// Updates lookup by new types reached and returns iterator that yields
9595
/// elements that unify with `goal`.
@@ -99,7 +99,7 @@ pub(super) fn trivial<'a, DB: HirDatabase>(
9999
/// * `defs` - Set of items in scope at term search target location
100100
/// * `lookup` - Lookup table for types
101101
/// * `should_continue` - Function that indicates when to stop iterating
102-
pub(super) fn type_constructor<'a, DB: HirDatabase>(
102+
pub(super) fn data_constructor<'a, DB: HirDatabase>(
103103
ctx: &'a TermSearchCtx<'a, DB>,
104104
defs: &'a FxHashSet<ScopeDef>,
105105
lookup: &'a mut LookupTable,
@@ -308,7 +308,9 @@ pub(super) fn type_constructor<'a, DB: HirDatabase>(
308308
// Early exit if some param cannot be filled from lookup
309309
let param_exprs: Vec<Vec<Expr>> = fields
310310
.into_iter()
311-
.map(|field| lookup.find(db, &field.ty(db)))
311+
.map(|field| {
312+
lookup.find(db, &field.ty_with_args(db, generics.iter().cloned()))
313+
})
312314
.collect::<Option<_>>()?;
313315

314316
// Note that we need special case for 0 param constructors because of multi cartesian

src/tools/rust-analyzer/crates/ide-assists/src/handlers/term_search.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,16 @@ fn f() { let a = 1; let b = 0.0; let c: (i32, (i32, f64)) = todo$0!(); }"#,
278278
r#"fn f() { let a = 1; let b = 0.0; let c: (i32, (i32, f64)) = (a, (a, b)); }"#,
279279
)
280280
}
281+
282+
#[test]
283+
fn test_tuple_struct_with_generics() {
284+
check_assist(
285+
term_search,
286+
r#"//- minicore: todo, unimplemented
287+
struct Foo<T>(T);
288+
fn f() { let a = 1; let b: Foo<i32> = todo$0!(); }"#,
289+
r#"struct Foo<T>(T);
290+
fn f() { let a = 1; let b: Foo<i32> = Foo(a); }"#,
291+
)
292+
}
281293
}

0 commit comments

Comments
 (0)