Skip to content

Commit 28f0171

Browse files
bors[bot]otavio
andauthored
Merge #5326
5326: infer: Add type inference support for Union types r=flodiebold a=otavio This adds the type inference to Union types and add a small test case for it, ensuring it keeps working in future. Fixes: #5277 Signed-off-by: Otavio Salvador <[email protected]> ---- # Co-authored-by: Otavio Salvador <[email protected]>
2 parents 1a9d772 + 9d114b9 commit 28f0171

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

crates/ra_hir_ty/src/infer/expr.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,15 @@ impl<'a> InferenceContext<'a> {
405405
.subst(&a_ty.parameters)
406406
})
407407
}
408-
// FIXME:
409-
TypeCtor::Adt(AdtId::UnionId(_)) => None,
408+
TypeCtor::Adt(AdtId::UnionId(u)) => {
409+
self.db.union_data(u).variant_data.field(name).map(|local_id| {
410+
let field = FieldId { parent: u.into(), local_id };
411+
self.write_field_resolution(tgt_expr, field);
412+
self.db.field_types(u.into())[field.local_id]
413+
.clone()
414+
.subst(&a_ty.parameters)
415+
})
416+
}
410417
_ => None,
411418
},
412419
_ => None,

crates/ra_hir_ty/src/tests/simple.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,29 @@ fn test() {
324324
);
325325
}
326326

327+
#[test]
328+
fn infer_union() {
329+
assert_snapshot!(
330+
infer(r#"
331+
union MyUnion {
332+
foo: u32,
333+
bar: f32,
334+
}
335+
336+
unsafe fn baz(u: MyUnion) {
337+
let inner = u.foo;
338+
}
339+
"#),
340+
@r###"
341+
61..62 'u': MyUnion
342+
73..99 '{ ...foo; }': ()
343+
83..88 'inner': u32
344+
91..92 'u': MyUnion
345+
91..96 'u.foo': u32
346+
"###
347+
);
348+
}
349+
327350
#[test]
328351
fn infer_refs() {
329352
assert_snapshot!(

0 commit comments

Comments
 (0)