Skip to content

Commit f1bfa8f

Browse files
bors[bot]viorina
andcommitted
Merge #1525
1525: Complete fields in enum variants r=matklad a=viorina Co-authored-by: Ekaterina Babshukova <[email protected]>
2 parents 8bb81d7 + 2a1e11b commit f1bfa8f

File tree

5 files changed

+116
-16
lines changed

5 files changed

+116
-16
lines changed

crates/ra_hir/src/adt.rs

+7
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ pub enum VariantDef {
185185
impl_froms!(VariantDef: Struct, EnumVariant);
186186

187187
impl VariantDef {
188+
pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
189+
match self {
190+
VariantDef::Struct(it) => it.fields(db),
191+
VariantDef::EnumVariant(it) => it.fields(db),
192+
}
193+
}
194+
188195
pub(crate) fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
189196
match self {
190197
VariantDef::Struct(it) => it.field(db, name),

crates/ra_hir/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use crate::{
5555
};
5656

5757
pub use self::{
58-
adt::AdtDef,
58+
adt::{AdtDef, VariantDef},
5959
either::Either,
6060
expr::ExprScopes,
6161
generics::{GenericParam, GenericParams, HasGenericParams},

crates/ra_hir/src/source_binder.rs

+5
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ impl SourceAnalyzer {
266266
self.infer.as_ref()?.field_resolution(expr_id)
267267
}
268268

269+
pub fn resolve_variant(&self, struct_lit: &ast::StructLit) -> Option<crate::VariantDef> {
270+
let expr_id = self.body_source_map.as_ref()?.node_expr(struct_lit.into())?;
271+
self.infer.as_ref()?.variant_resolution(expr_id)
272+
}
273+
269274
pub fn resolve_macro_call(
270275
&self,
271276
db: &impl HirDatabase,

crates/ra_hir/src/ty/infer.rs

+15
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ pub struct InferenceResult {
113113
method_resolutions: FxHashMap<ExprId, Function>,
114114
/// For each field access expr, records the field it resolves to.
115115
field_resolutions: FxHashMap<ExprId, StructField>,
116+
variant_resolutions: FxHashMap<ExprId, VariantDef>,
116117
/// For each associated item record what it resolves to
117118
assoc_resolutions: FxHashMap<ExprOrPatId, ImplItem>,
118119
diagnostics: Vec<InferenceDiagnostic>,
@@ -127,6 +128,9 @@ impl InferenceResult {
127128
pub fn field_resolution(&self, expr: ExprId) -> Option<StructField> {
128129
self.field_resolutions.get(&expr).copied()
129130
}
131+
pub fn variant_resolution(&self, expr: ExprId) -> Option<VariantDef> {
132+
self.variant_resolutions.get(&expr).copied()
133+
}
130134
pub fn assoc_resolutions_for_expr(&self, id: ExprId) -> Option<ImplItem> {
131135
self.assoc_resolutions.get(&id.into()).copied()
132136
}
@@ -170,6 +174,7 @@ struct InferenceContext<'a, D: HirDatabase> {
170174
obligations: Vec<Obligation>,
171175
method_resolutions: FxHashMap<ExprId, Function>,
172176
field_resolutions: FxHashMap<ExprId, StructField>,
177+
variant_resolutions: FxHashMap<ExprId, VariantDef>,
173178
assoc_resolutions: FxHashMap<ExprOrPatId, ImplItem>,
174179
type_of_expr: ArenaMap<ExprId, Ty>,
175180
type_of_pat: ArenaMap<PatId, Ty>,
@@ -183,6 +188,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
183188
InferenceContext {
184189
method_resolutions: FxHashMap::default(),
185190
field_resolutions: FxHashMap::default(),
191+
variant_resolutions: FxHashMap::default(),
186192
assoc_resolutions: FxHashMap::default(),
187193
type_of_expr: ArenaMap::default(),
188194
type_of_pat: ArenaMap::default(),
@@ -213,6 +219,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
213219
InferenceResult {
214220
method_resolutions: self.method_resolutions,
215221
field_resolutions: self.field_resolutions,
222+
variant_resolutions: self.variant_resolutions,
216223
assoc_resolutions: self.assoc_resolutions,
217224
type_of_expr: expr_types,
218225
type_of_pat: pat_types,
@@ -232,6 +239,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
232239
self.field_resolutions.insert(expr, field);
233240
}
234241

242+
fn write_variant_resolution(&mut self, expr: ExprId, variant: VariantDef) {
243+
self.variant_resolutions.insert(expr, variant);
244+
}
245+
235246
fn write_assoc_resolution(&mut self, id: ExprOrPatId, item: ImplItem) {
236247
self.assoc_resolutions.insert(id, item);
237248
}
@@ -1069,6 +1080,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
10691080
}
10701081
Expr::StructLit { path, fields, spread } => {
10711082
let (ty, def_id) = self.resolve_variant(path.as_ref());
1083+
if let Some(variant) = def_id {
1084+
self.write_variant_resolution(tgt_expr, variant);
1085+
}
1086+
10721087
let substs = ty.substs().unwrap_or_else(Substs::empty);
10731088
for (field_idx, field) in fields.iter().enumerate() {
10741089
let field_ty = def_id

crates/ra_ide_api/src/completion/complete_struct_literal.rs

+88-15
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
1-
use hir::AdtDef;
1+
use hir::{Substs, Ty};
22

33
use crate::completion::{CompletionContext, Completions};
44

55
/// Complete fields in fields literals.
66
pub(super) fn complete_struct_literal(acc: &mut Completions, ctx: &CompletionContext) {
7-
let ty = match ctx.struct_lit_syntax.and_then(|it| ctx.analyzer.type_of(ctx.db, it.into())) {
7+
let (ty, variant) = match ctx.struct_lit_syntax.and_then(|it| {
8+
Some((ctx.analyzer.type_of(ctx.db, it.into())?, ctx.analyzer.resolve_variant(it)?))
9+
}) {
810
Some(it) => it,
9-
None => return,
10-
};
11-
let (adt, substs) = match ty.as_adt() {
12-
Some(res) => res,
1311
_ => return,
1412
};
15-
match adt {
16-
AdtDef::Struct(s) => {
17-
for field in s.fields(ctx.db) {
18-
acc.add_field(ctx, field, substs);
19-
}
20-
}
2113

22-
// FIXME unions
23-
AdtDef::Union(_) => (),
24-
AdtDef::Enum(_) => (),
14+
let ty_substs = match ty {
15+
Ty::Apply(it) => it.parameters,
16+
_ => Substs::empty(),
2517
};
18+
19+
for field in variant.fields(ctx.db) {
20+
acc.add_field(ctx, field, &ty_substs);
21+
}
2622
}
2723

2824
#[cfg(test)]
@@ -57,4 +53,81 @@ mod tests {
5753
⋮]
5854
"###);
5955
}
56+
57+
#[test]
58+
fn test_struct_literal_enum_variant() {
59+
let completions = complete(
60+
r"
61+
enum E {
62+
A { a: u32 }
63+
}
64+
fn foo() {
65+
let _ = E::A { <|> }
66+
}
67+
",
68+
);
69+
assert_debug_snapshot_matches!(completions, @r###"
70+
⋮[
71+
⋮ CompletionItem {
72+
⋮ label: "a",
73+
⋮ source_range: [119; 119),
74+
⋮ delete: [119; 119),
75+
⋮ insert: "a",
76+
⋮ kind: Field,
77+
⋮ detail: "u32",
78+
⋮ },
79+
⋮]
80+
"###);
81+
}
82+
83+
#[test]
84+
fn test_struct_literal_two_structs() {
85+
let completions = complete(
86+
r"
87+
struct A { a: u32 }
88+
struct B { b: u32 }
89+
90+
fn foo() {
91+
let _: A = B { <|> }
92+
}
93+
",
94+
);
95+
assert_debug_snapshot_matches!(completions, @r###"
96+
⋮[
97+
⋮ CompletionItem {
98+
⋮ label: "b",
99+
⋮ source_range: [119; 119),
100+
⋮ delete: [119; 119),
101+
⋮ insert: "b",
102+
⋮ kind: Field,
103+
⋮ detail: "u32",
104+
⋮ },
105+
⋮]
106+
"###);
107+
}
108+
109+
#[test]
110+
fn test_struct_literal_generic_struct() {
111+
let completions = complete(
112+
r"
113+
struct A<T> { a: T }
114+
115+
fn foo() {
116+
let _: A<u32> = A { <|> }
117+
}
118+
",
119+
);
120+
assert_debug_snapshot_matches!(completions, @r###"
121+
⋮[
122+
⋮ CompletionItem {
123+
⋮ label: "a",
124+
⋮ source_range: [93; 93),
125+
⋮ delete: [93; 93),
126+
⋮ insert: "a",
127+
⋮ kind: Field,
128+
⋮ detail: "u32",
129+
⋮ },
130+
⋮]
131+
"###);
132+
}
60133
}

0 commit comments

Comments
 (0)