Skip to content

Commit

Permalink
Merge #5326
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bors[bot] and otavio committed Jul 12, 2020
2 parents 1a9d772 + 9d114b9 commit 28f0171
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
11 changes: 9 additions & 2 deletions crates/ra_hir_ty/src/infer/expr.rs
Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions crates/ra_hir_ty/src/tests/simple.rs
Expand Up @@ -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!(
Expand Down

0 comments on commit 28f0171

Please sign in to comment.