mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
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 <otavio@ossystems.com.br> ---- # Co-authored-by: Otavio Salvador <otavio@ossystems.com.br>
This commit is contained in:
commit
28f0171dbd
2 changed files with 32 additions and 2 deletions
|
@ -405,8 +405,15 @@ impl<'a> InferenceContext<'a> {
|
||||||
.subst(&a_ty.parameters)
|
.subst(&a_ty.parameters)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// FIXME:
|
TypeCtor::Adt(AdtId::UnionId(u)) => {
|
||||||
TypeCtor::Adt(AdtId::UnionId(_)) => None,
|
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,
|
||||||
},
|
},
|
||||||
_ => None,
|
_ => None,
|
||||||
|
|
|
@ -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]
|
#[test]
|
||||||
fn infer_refs() {
|
fn infer_refs() {
|
||||||
assert_snapshot!(
|
assert_snapshot!(
|
||||||
|
|
Loading…
Reference in a new issue