about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-12-11 17:51:02 +0000
committerGitHub <noreply@github.com>2021-12-11 17:51:02 +0000
commit36a7c0e00902172af7ea43704e58eedd32b308bc (patch)
tree27f4af9fc1c46071fb6af5bd66ffd2ec717e2ff6
parent0eb6039e4eee23ec6df7fdacde7a72e842af96e2 (diff)
parentd599f819e09b06f37482bf46f12e3231533de8d7 (diff)
downloadrust-36a7c0e00902172af7ea43704e58eedd32b308bc.tar.gz
rust-36a7c0e00902172af7ea43704e58eedd32b308bc.zip
Merge #10988
10988: Fix expected type calculation in struct literal if followed by comma r=flodiebold a=flodiebold



Co-authored-by: Florian Diebold <flodiebold@gmail.com>
-rw-r--r--crates/ide_completion/src/context.rs34
1 files changed, 29 insertions, 5 deletions
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs
index e8566b80b6f..d459e511614 100644
--- a/crates/ide_completion/src/context.rs
+++ b/crates/ide_completion/src/context.rs
@@ -561,11 +561,21 @@ impl<'a> CompletionContext<'a> {
                         })().unwrap_or((None, None))
                     },
                     ast::RecordExprField(it) => {
-                        cov_mark::hit!(expected_type_struct_field_with_leading_char);
-                        (
-                            it.expr().as_ref().and_then(|e| self.sema.type_of_expr(e)).map(TypeInfo::original),
-                            it.field_name().map(NameOrNameRef::NameRef),
-                        )
+                        if let Some(expr) = it.expr() {
+                            cov_mark::hit!(expected_type_struct_field_with_leading_char);
+                            (
+                                self.sema.type_of_expr(&expr).map(TypeInfo::original),
+                                it.field_name().map(NameOrNameRef::NameRef),
+                            )
+                        } else {
+                            cov_mark::hit!(expected_type_struct_field_followed_by_comma);
+                            let ty = self.sema.resolve_record_field(&it)
+                                .map(|(_, _, ty)| ty);
+                            (
+                                ty,
+                                it.field_name().map(NameOrNameRef::NameRef),
+                            )
+                        }
                     },
                     ast::MatchExpr(it) => {
                         cov_mark::hit!(expected_type_match_arm_without_leading_char);
@@ -1009,6 +1019,20 @@ fn foo() {
     }
 
     #[test]
+    fn expected_type_struct_field_followed_by_comma() {
+        cov_mark::check!(expected_type_struct_field_followed_by_comma);
+        check_expected_type_and_name(
+            r#"
+struct Foo { a: u32 }
+fn foo() {
+    Foo { a: $0, };
+}
+"#,
+            expect![[r#"ty: u32, name: a"#]],
+        )
+    }
+
+    #[test]
     fn expected_type_generic_struct_field() {
         check_expected_type_and_name(
             r#"