Skip to content

Commit 36a7c0e

Browse files
Merge #10988
10988: Fix expected type calculation in struct literal if followed by comma r=flodiebold a=flodiebold Co-authored-by: Florian Diebold <[email protected]>
2 parents 0eb6039 + d599f81 commit 36a7c0e

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

crates/ide_completion/src/context.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -561,11 +561,21 @@ impl<'a> CompletionContext<'a> {
561561
})().unwrap_or((None, None))
562562
},
563563
ast::RecordExprField(it) => {
564-
cov_mark::hit!(expected_type_struct_field_with_leading_char);
565-
(
566-
it.expr().as_ref().and_then(|e| self.sema.type_of_expr(e)).map(TypeInfo::original),
567-
it.field_name().map(NameOrNameRef::NameRef),
568-
)
564+
if let Some(expr) = it.expr() {
565+
cov_mark::hit!(expected_type_struct_field_with_leading_char);
566+
(
567+
self.sema.type_of_expr(&expr).map(TypeInfo::original),
568+
it.field_name().map(NameOrNameRef::NameRef),
569+
)
570+
} else {
571+
cov_mark::hit!(expected_type_struct_field_followed_by_comma);
572+
let ty = self.sema.resolve_record_field(&it)
573+
.map(|(_, _, ty)| ty);
574+
(
575+
ty,
576+
it.field_name().map(NameOrNameRef::NameRef),
577+
)
578+
}
569579
},
570580
ast::MatchExpr(it) => {
571581
cov_mark::hit!(expected_type_match_arm_without_leading_char);
@@ -1008,6 +1018,20 @@ fn foo() {
10081018
)
10091019
}
10101020

1021+
#[test]
1022+
fn expected_type_struct_field_followed_by_comma() {
1023+
cov_mark::check!(expected_type_struct_field_followed_by_comma);
1024+
check_expected_type_and_name(
1025+
r#"
1026+
struct Foo { a: u32 }
1027+
fn foo() {
1028+
Foo { a: $0, };
1029+
}
1030+
"#,
1031+
expect![[r#"ty: u32, name: a"#]],
1032+
)
1033+
}
1034+
10111035
#[test]
10121036
fn expected_type_generic_struct_field() {
10131037
check_expected_type_and_name(

0 commit comments

Comments
 (0)