infer: Add type inference support for Union types

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 <otavio@ossystems.com.br>
This commit is contained in:
Otavio Salvador 2020-07-11 19:28:07 -03:00
parent 02efda91d0
commit 9d114b9707
2 changed files with 32 additions and 2 deletions

View file

@ -405,8 +405,15 @@ impl<'a> InferenceContext<'a> {
.subst(&a_ty.parameters)
})
}
// FIXME:
TypeCtor::Adt(AdtId::UnionId(_)) => None,
TypeCtor::Adt(AdtId::UnionId(u)) => {
self.db.union_data(u).variant_data.field(name).map(|local_id| {
let field = FieldId { parent: u.into(), local_id };
self.write_field_resolution(tgt_expr, field);
self.db.field_types(u.into())[field.local_id]
.clone()
.subst(&a_ty.parameters)
})
}
_ => None,
},
_ => None,

View file

@ -324,6 +324,29 @@ fn test() {
);
}
#[test]
fn infer_union() {
assert_snapshot!(
infer(r#"
union MyUnion {
foo: u32,
bar: f32,
}
unsafe fn baz(u: MyUnion) {
let inner = u.foo;
}
"#),
@r###"
61..62 'u': MyUnion
73..99 '{ ...foo; }': ()
83..88 'inner': u32
91..92 'u': MyUnion
91..96 'u.foo': u32
"###
);
}
#[test]
fn infer_refs() {
assert_snapshot!(