Skip to content

Commit e75651a

Browse files
authored
Merge pull request #21971 from ChayimFriedman2/fill-fields-coerce
fix: Check coercion, not unification, in "Fill struct fields", as the criteria to use an existing local as the field's value
2 parents 88662e1 + 3dcfde9 commit e75651a

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

crates/ide-diagnostics/src/handlers/missing_fields.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
120120
let field_expr = if let Some(local_candidate) = locals.get(&f.name(ctx.sema.db)) {
121121
cov_mark::hit!(field_shorthand);
122122
let candidate_ty = local_candidate.ty(ctx.sema.db);
123-
if ty.could_unify_with(ctx.sema.db, &candidate_ty) {
123+
if candidate_ty.could_coerce_to(ctx.sema.db, ty) {
124124
None
125125
} else {
126126
Some(generate_fill_expr(ty))
@@ -934,4 +934,30 @@ fn main() {
934934
"#,
935935
);
936936
}
937+
938+
#[test]
939+
fn coerce_existing_local() {
940+
check_fix(
941+
r#"
942+
struct A {
943+
v: f64,
944+
}
945+
946+
fn f() -> A {
947+
let v = loop {};
948+
A {$0}
949+
}
950+
"#,
951+
r#"
952+
struct A {
953+
v: f64,
954+
}
955+
956+
fn f() -> A {
957+
let v = loop {};
958+
A { v }
959+
}
960+
"#,
961+
);
962+
}
937963
}

0 commit comments

Comments
 (0)