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 <otavio@ossystems.com.br>


----

#

Co-authored-by: Otavio Salvador <otavio@ossystems.com.br>
This commit is contained in:
bors[bot] 2020-07-12 09:45:37 +00:00 committed by GitHub
commit 28f0171dbd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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!(