Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infer: Add type inference support for Union types #5326

Merged
merged 1 commit into from Jul 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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